Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CDA 3101 Fall 2013 Introduction to Computer Organization Pointers & Arrays MIPS Programs 16 September 2013."— Presentation transcript:

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

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

3 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

4 Review: C Pointer Operators Suppose c has value 100, it is located in memory at address 0x10000000 Unary operator & gives address: p = &c; gives address of c to p ; –p “points to” c (p == 0x10000000) (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)

5 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] 1 2 2 2 3 4

6 Review: Assembly Code c is int, has value 100, in memory at address 0x10000000, p in $a0, x in $s0 1.p = &c; /* p gets 0x10000000*/ lui $a0,0x1000 # p = 0x10000000 2. 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

7 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

8 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

9 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

10 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

11 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

12 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]

13 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

14 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

15 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]

16 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

17 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

18 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)

19 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

20 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 )

21 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

22 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)

23 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

24 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)

25 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 0x00000000. 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

26 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

27 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 0.. 100 is %d\n", sum); }

28 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 0.. 100 is %d\n"

29 Example: C => Asm => Obj => Exe => Run 00 addiu $29,$29,-32 04 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)

30 Symbol and Relocation Tables Symbol Table –Label Address main:0x00000000 loop:0x00000018 str:0x10000430 printf:0x004003b0 Relocation Information –AddressInstr. TypeDependency –0x00000040HI16str –0x00000044LO16str –0x0000004cjalprintf

31 Example: C => Asm => Obj => Exe => Run 00 addiu $29,$29,-32 04 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, -9 40 lui$4, 4096 44 ori$4,$4,1072 48 lw $5,24($29) 4c jal1048812 50 add$2, $0, $0 54 lw $31,20($29) 58 addiu $29,$29,32 5c jr $31 Edit addresses

32 Example: C => Asm => Obj => Exe => Run 0x0040000000100111101111011111111111100000 0x0040000410101111101111110000000000010100 0x0040000810101111101001000000000000100000 0x0040000c10101111101001010000000000100100 0x0040001010101111101000000000000000011000 0x0040001410101111101000000000000000011100 0x0040001810001111101011100000000000011100 0x0040001c10001111101110000000000000011000 0x0040002000000001110011100000000000011001 0x0040002400100101110010000000000000000001 0x0040002800101001000000010000000001100101 0x0040002c10101111101010000000000000011100 0x0040003000000000000000000111100000010010 0x0040003400000011000011111100100000100001 0x0040003800010100001000001111111111110111 0x0040003c10101111101110010000000000011000 0x0040004000111100000001000001000000000000 0x0040004410001111101001010000000000011000 0x0040004800001100000100000000000011101100 0x0040004c00100100100001000000010000110000 0x0040005010001111101111110000000000010100 0x0040005400100111101111010000000000100000 0x0040005800000011111000000000000000001000 0x0040005c00000000000000000001000000100001

33 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.


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

Similar presentations


Ads by Google