CDA 3101 Fall 2013 Introduction to Computer Organization Pointers & Arrays MIPS Programs 16 September 2013.

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

The University of Adelaide, School of Computer Science
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
COMP3221 lec18-pointers.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 18 : Pointers & Arrays in C/ Assembly
Lecture 8 Sept 23 Completion of Ch 2 translating procedure into MIPS role of compilers, assemblers, linking, loading etc. pitfalls, conclusions Chapter.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
Wannabe Lecturer Alexandre Joly inst.eecs.berkeley.edu/~cs61c-te
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
Lecture 8: MIPS Instruction Set
CS61C L11 Linker © UC Regents 1 CS61C - Machine Structures Lecture 11 - Starting a Program October 4, 2000 David Patterson
8.
Compiling, Assembling, Loading, Linking (CALL) I (1) Fall 2005 Lecture 09: Program Translation.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 09: Compiling-Assembling-Linking Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer.
1 Starting a Program The 4 stages that take a C++ program (or any high-level programming language) and execute it in internal memory are: Compiler - C++
Lec 9Systems Architecture1 Systems Architecture Lecture 9: Assemblers, Linkers, and Loaders Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some.
Instructor: Justin Hsia 7/08/2013Summer Lecture #81 CS 61C: Great Ideas in Computer Architecture Running a Program.
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
CS61C L18 Running a Program I (1) Garcia, Spring 2007 © UCB Gaze-controlled UI!  Researchers at Stanford have designed a system that allows a user to.
inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 19 – Running a Program II (Compiling, Assembling, Linking, Loading) Researchers.
Cs 61C L8 Proc II., Arrays and Pointers in C 1 Patterson Fall 00 ©UCB CS61C - Machine Structures Lecture 8 - Procedure Conventions part II, plus, Arrays.
CS 61C L13 CALL (1) A Carle, Summer 2006 © UCB inst.eecs.berkeley.edu/~cs61c/su06 CS61C : Machine Structures Lecture #13: CALL Andy Carle.
CS61C L18 Running a Program I (1) Garcia, Fall 2006 © UCB Security hole finder  Google’s just-released new /* Code Search */, which allows you to specify.
Cs 61C L6 Instruction, Proc II.1 Patterson Spring 99 ©UCB CS61C Instruction Representation and Functions, Part II Lecture 6 February 5, 1999 Dave Patterson.
CS 61C L14Introduction to MIPS: Instruction Representation II (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS 61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
MIPS Assembler Programming
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
RISC Concepts, MIPS ISA and the Mini–MIPS project
CS 61C L14Introduction to MIPS: Instruction Representation II (1) Garcia, Spring 2004 © UCB Roy Wang inst.eecs.berkeley.edu/~cs61c-tf inst.eecs.berkeley.edu/~cs61c.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 19 – Running a Program II (Compiling, Assembling, Linking, Loading) I’m.
ELEC2041 lec18-pointers.1 Saeid Nooshabadi COMP3221/9221 Microprocessors and Interfacing Lectures 7 : Pointers & Arrays in C/ Assembly
Cs 61C L10 Start.1 Patterson Spring 99 ©UCB CS61C Starting a Program Lecture 10 February 19, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson) www-inst.eecs.berkeley.edu/~cs61c/schedule.html.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Computer Architecture and Design – ECEN 350 Part 4 [Some slides adapted from M. Irwin, D. Paterson and others]
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
CS 61C L3.2.2 Floating Point 2 (1) K. Meinz, Summer 2004 © UCB CS61C : Machine Structures Lecture Floating Point II & Linking Kurt Meinz.
Lecture 4: MIPS Instruction Set
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
8.
CENG 311 Starting a Program. Review (1/2) °IEEE 754 Floating Point Standard: Kahan pack as much in as could get away with +/- infinity, Not-a-Number (Nan),
The Assembly Process Computer Organization and Assembly Language: Module 10.
CS 61C: Great Ideas in Computer Architecture Running a Program - CALL (Compiling, Assembling, Linking, and Loading) 1 Instructors: Nick Weaver & Vladimir.
CS 61C: Great Ideas in Computer Architecture CALL continued ( Linking and Loading) 1 Instructors: Nicholas Weaver & Vladimir Stojanovic
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
CS 110 Computer Architecture Lecture 7: Running a Program - CALL (Compiling, Assembling, Linking, and Loading) Instructor: Sören Schwertfeger
Lecture 3 Translation.
Rocky K. C. Chang Version 0.1, 25 September 2017
Computer Architecture & Operations I
Lecture 6: Assembly Programs
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
There is one handout today at the front and back of the room!
CDA 3101 Spring 2016 Introduction to Computer Organization
Green Subscription Based PC Announced
MIPS Instructions.
The University of Adelaide, School of Computer Science
Computer Organization and Design Assembly & Compilation
Program and memory layout
Lecture 6: Assembly Programs
April 2006 Saeid Nooshabadi
10/6: Lecture Topics C Brainteaser More on Procedure Call
Program Assembly.
Presentation transcript:

CDA 3101 Fall 2013 Introduction to Computer Organization Pointers & Arrays MIPS Programs 16 September 2013

Overview Pointers (addresses) and values Argument passing Storage lifetime and scope Pointer arithmetic Pointers and arrays Pointers in MIPS

Review: Pointers Pointer: a variable that contains the address of another variable –HLL version of machine language memory address Why use Pointers? –Sometimes only way to express computation –Often more compact and efficient code Why not? –Huge source of bugs in real software, perhaps the largest single source 1) Dangling reference (premature free) 2) Memory leaks (tardy free): can't have long-running jobs without periodic restart of them

Review: C Pointer Operators Suppose c has value 100, it is located in memory at address 0x Unary operator & gives address: p = &c; gives address of c to p ; –p “points to” c (p == 0x ) (Referencing) Unary operator * gives value that pointer points to –if p = &c => * p == 100 (Dereferencing a pointer) Deferencing  data transfer in assembler –... =... *p... ;  load (get value from location pointed to by p) –*p =... ;  store (put value into location pointed to by p)

Review: Pointer Arithmetic int x = 1, y = 2;/* x and y are integer variables */ int z[10];/* an array of 10 ints, z points to start */ int *p;/* p is a pointer to an int */ x = 21;/* assigns x the new value 21 */ z[0] = 2; z[1] = 3/* assigns 2 to the first, 3 to the next */ p = &z[0];/* p refers to the first element of z */ p = z; /* same thing; p[ i ] == z[ i ]*/ p = p+1;/* now it points to the next element, z[1] */ p++;/* now it points to the one after that, z[2] */ *p = 4;/* assigns 4 to there, z[2] == 4*/ p = 3;/* bad idea! Absolute address!!! */ p = &x;/* p points to x, *p == 21 */ z = &yillegal!!!!! array name is not a variable y: x: p: z[0] z[1]

Review: Assembly Code c is int, has value 100, in memory at address 0x , p in $a0, x in $s0 1.p = &c; /* p gets 0x */ lui $a0,0x1000 # p = 0x x = *p; /* x gets 100 */ lw $s0, 0($a0) # dereferencing p 3.*p = 200; /* c gets 200 */ addi $t0,$0,200 sw $t0, 0($a0) # dereferencing p

Argument Passing Options 2 choices –“Call by Value”: pass a copy of the item to the function/procedure –“Call by Reference”: pass a pointer to the item to the function/procedure Single word variables passed by value Passing an array? e.g., a[100] –Pascal (call by value) copies 100 words of a[] onto the stack –C (call by reference) passes a pointer (1 word) to the array a[] in a register

Lifetime of Storage and Scope Automatic (stack allocated) –Typical local variables of a function –Created upon call, released upon return –Scope is the function Heap allocated –Created upon malloc, released upon free –Referenced via pointers External / static –Exist for entire program CodeStatic Heap Stack

Arrays, Pointers, and Functions 4 versions of array function that adds two arrays and puts sum in a third array (sumarray) 1.Third array is passed to function 2.Using a local array (on stack) for result and passing a pointer to it 3.Third array is allocated on heap 4.Third array is declared static Purpose of example is to show interaction of C statements, pointers, and memory allocation

Version 1 int x[100], y[100], z[100]; sumarray(x, y, z); C calling convention means: sumarray(&x[0], &y[0], &z[0]); Really passing pointers to arrays addi $a0,$gp,0 # x[0] starts at $gp addi $a1,$gp,400 # y[0] above x[100] addi $a2,$gp,800 # z[0] above y[100] jal sumarray

Version 1: Compiled Code void sumarray(int a[], int b[], int c[]) { int i; for(i = 0; i < 100; i = i + 1) c[i] = a[i] + b[i]; } addi$t0,$a0,400 # beyond end of a[] Loop:beq$a0,$t0,Exit lw$t1, 0($a0) # $t1=a[i] lw$t2, 0($a1) # $t2=b[i] add$t1,$t1,$t2 # $t1=a[i] + b[i] sw$t1, 0($a2) # c[i]=a[i] + b[i] addi$a0,$a0,4 # $a0++ addi$a1,$a1,4 # $a1++ addi$a2,$a2,4 # $a2++ jLoop Exit:jr $ra

Version 2 int *sumarray(int a[],int b[]) { int i, c[100]; for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c; } addi $t0,$a0,400 # beyond end of a[] addi $sp,$sp,-400 # space for c addi $t3,$sp,0 # ptr for c addi $v0,$t3,0 # $v0 = &c[0] Loop: beq $a0,$t0,Exit lw $t1, 0($a0) # $t1=a[i] lw $t2, 0($a1) # $t2=b[i] add $t1,$t1,$t2 # $t1=a[i] + b[i] sw $t1, 0($t3) # c[i]=a[i] + b[i] addi $a0,$a0,4 # $a0++ addi $a1,$a1,4 # $a1++ addi $t3,$t3,4 # $t3++ j Loop Exit: addi $sp,$sp, 400 # pop stack jr $ra c[100] $sp a[100] B[100]

Version 3 int * sumarray(int a[],int b[]) { int i; int *c; c = (int *) malloc(100); for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c; } CodeStatic Heap Stack c[100] Not reused unless freed –Can lead to memory leaks –Java, Scheme have garbage collectors to reclaim free space

Version 3: Compiled Code addi$t0,$a0,400 # beyond end of a[] addi $sp,$sp,-12 # space for regs sw$ra, 0($sp) # save $ra sw$a0, 4($sp) # save 1st arg. sw$a1, 8($sp) # save 2nd arg. addi$a0,$zero,400 jalmalloc addi $t3,$v0,0# ptr for c lw$a0, 4($sp) # restore 1st arg. lw$a1, 8($sp) # restore 2nd arg. Loop:beq $a0,$t0,Exit... (loop as before on prior slide ) jLoop Exit:lw $ra, 0($sp) # restore $ra addi $sp, $sp, 12 # pop stack jr $ra

Version 4 int * sumarray(int a[],int b[]) { int i; static int c[100]; for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c; } Compiler allocates once for function, space is reused –Will be changed next time sumarray invoked –Why describe? used in C libraries Code Static Heap Stack c[100]

New Topic - MIPS Programs Data types and addressing included in the ISA Compromise between application requirements and hardware implementation MIPS data types 32-bit word 16-bit half word 8-bit bytes Addressing Modes Data Register 16-bit signed constants Base addressing Instructions PC-relative (Pseudo) direct

Overview of Program Development C program: foo.c Compiler (cc) Assembly program: foo.s Assembler (as) Object(mach lang module): foo.o Linker (ld) Executable(mach lang pgm): a.out Loader Memory lib.o

Assembler Reads and use directives Replace pseudoinstructions –subu $sp,$sp,32addiu $sp, $sp, -32 –sd $a0, 32($sp) sw $a0, 32($sp) sw $a1, 36($sp) –mul $t7,$t6,$t5mult $t6,$t5 mflo $t7 –la $a0, 0xAABBCCDD lui $at, 0xAABB ori $a0, $at, 0xCCDD Produce machine language Create object file (*.o)

Assembler Directives Directions to assembler that don’t produce machine instructions. align n Align the next datum on a 2 n byte boundary.text Subsequent items put in user text segment.data Subsequent items put in user data segment.globl sym sym can be referenced from other files.asciiz str Store the string str in memory.word w1…wn Store the n 32-bit quantities in successive memory words. byte b1..bn Store n 8-bit values in successive bytes of memory. float f1..fn : Store n floating-point numbers in successive memory words

Absolute Addresses Which instructions need relocation editing? j/jalxxxxx Loads / stores to variables in static area lw/sw$gp$xaddress Conditional branches beq/bne$rs$rt address –PC-relative addressing preserved even if code moves Jump instructions Direct (absolute) references to data (e.g. la )

Producing Machine Language Simple case –Arithmetic, logical, shifts, etc. –All necessary info is within the instruction already. Conditional branches (beq, bne) –Once pseudoinstructions are replaced by real ones, we know by how many instructions for branch span –PC-relative, easy to handle Direct (absolute) addresses –Jumps (j and jal) –Direct (absolute) references to data –These can’t be determined yet, so we create two tables

Assembler Tables Symbol table –List of “items” in this file that may be used by other files. Labels: function calling Data: anything in the.data section; variables which may be accessed across files –First Pass: record label-address pairs –Second Pass: produce machine code –Can jump to a later label without first declaring it Relocation Table –List of “items” for which this file needs the address. –Any label jumped to: j or jal internal external (including lib files) –Any piece of data (e.g. la instruction)

Object File Format Object file header: size and position of the other pieces of the object file Text segment: the machine code Data segment: binary representation of the data in the source file Relocation information: identifies lines of code that need to be “handled” Symbol table: list of this file’s labels and data that can be referenced Debugging information

Linker (Link Editor) Combines object (.o) files into an executable file Enables separate (independent) compilation of files –Only recompile modified file (module) Windows NT source is >30 M lines of code! And Growing! Edits the “links” in jump and link instructions Process (input: object files produced by assembler) –Step 1: put together the text segments from each.o file –Step 2: put together the data segments from each.o file and concatenate this onto end of text segments –Step 3: resolve references. Go through Relocation Table and handle each entry (fill in all absolute addresses)

Resolving References Four types of references (addresses) –PC-relative (e.g. beq, bne ): never relocate –Absolute address ( j, jal ): always relocate –External reference ( jal ): always relocate –Data reference ( lui and ori ): always relocate Linker assumes first word of first text segment is at address 0x Linker knows: –Length of each text and data segment –Ordering of text and data segments Linker calculates: –Absolute address of each label to be jumped to (internal or external) and each piece of data being referenced

Loader Executable file is stored on disk. Loader’s job: load it into memory and start it running. In reality, loader is part of the operating system (OS) 1.Reads header to determine size of text and data segments 2.Creates new address space for program large enough to hold text and data segments, along with a stack segment 3.Copies instructions and data from executable file memory 4.Copies arguments passed to the program onto the stack 5.Initializes machine registers, $sp = 1st free stack location 6.Jumps to start-up routine that copies program’s arguments from stack to registers and sets the PC 7.If main routine returns, start-up routine terminates program with the exit system call

Example: C => Asm => Obj => Exe => Run #include int main (int argc, char *argv[]) { int i; int sum = 0; for (i = 0; i <= 100; i = i + 1) sum = sum + i * i; printf ("The sum from is %d\n", sum); }

Example: C => Asm => Obj => Exe => Run.text.align2.globlmain main: subu $sp,$sp,32 sw $ra, 20($sp) sd $a0, 32($sp) sw $0, 24($sp) sw $0, 28($sp) loop: lw $t6, 28($sp) mul $t7, $t6,$t6 lw $t8, 24($sp) addu $t9, $t8,$t7 sw $t9, 24($sp) addu $t0, $t6, 1 sw $t0, 28($sp) ble $t0,100, loop la $a0, str lw $a1, 24($sp) jal printf move $v0, $0 lw $ra, 20($sp) addiu $sp,$sp,32 jr $ra.data.align0 str:.asciiz"The sum from is %d\n"

Example: C => Asm => Obj => Exe => Run 00 addiu $29,$29, sw$31,20($29) 08 sw$4, 32($29) 0c sw$5, 36($29) 10 sw $0, 24($29) 14 sw $0, 28($29) 18 lw $14,28($29) 1c mult$14,$14 20 mflo$15 24 lw $24,24($29) 28 addu $25,$24,$15 2c sw $25,24($29) 30 addiu $8,$14, 1 34 sw$8,28($29) 38 slti$1,$8, 101 3c bne$1,$0, loop 40 lui$4, hi.str 44 ori$4,$4,lo.str 48 lw$5,24($29) 4c jalprintf 50 add$2, $0, $0 54 lw $31,20($29) 58 addiu $29,$29,32 5c jr $31 60 The Replace pseudoinstructions; assign addresses (start at 0x00)

Symbol and Relocation Tables Symbol Table –Label Address main:0x loop:0x str:0x printf:0x004003b0 Relocation Information –AddressInstr. TypeDependency –0x HI16str –0x LO16str –0x cjalprintf

Example: C => Asm => Obj => Exe => Run 00 addiu $29,$29, sw$31,20($29) 08 sw$4,32($29) 0c sw$5,36($29) 10 sw $0, 24($29) 14 sw $0, 28($29) 18 lw $14, 28($29) 1c multu $14, $14 20 mflo $15 24 lw $24, 24($29) 28 addu $25,$24,$15 2c sw $25, 24($29) 30 addiu $8,$14, 1 34 sw$8,28($29) 38 slti$1,$8, 101 3c bne$1,$0, lui$4, ori$4,$4, lw $5,24($29) 4c jal add$2, $0, $0 54 lw $31,20($29) 58 addiu $29,$29,32 5c jr $31 Edit addresses

Example: C => Asm => Obj => Exe => Run 0x x x x c x x x x c x x x x c x x x x c x x x x c x x x x c

MIPS Program - Summary Compiler converts a single HLL file into a single assembly language file. Assembler removes pseudos, converts what it can to machine language, and creates a checklist for the linker (relocation table). This changes each.s file into a.o file. Linker combines several.o files and resolves absolute addresses. Loader loads executable into memory and begins execution.