CDA3101-L10-ptrs-arrys-MIPSprogs

CDA3101-L10-ptrs-arrys-MIPSprogs - CDA 3101 Fall 2011...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CDA 3101 Fall 2011 Introduction to Computer Organization Pointers & Arrays MIPS Programs 23 September 2011 Overview Pointers (addresses) and values Argument passing Storage lifetime and scope Pointer arithmetic Pointers and arrays Pointers in MIPS 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 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) 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) 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 = &y illegal!!!!! array name is not a variable y: x: p: z[0] z[1] 1 2 2 2 3 4 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 1. x = *p; /* x gets 100 */ lw $s0, 0($a0) # dereferencing p 1.*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 Code Static Heap Stack Arrays, Pointers, and Functions...
View Full Document

Page1 / 33

CDA3101-L10-ptrs-arrys-MIPSprogs - CDA 3101 Fall 2011...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online