Presentation is loading. Please wait.

Presentation is loading. Please wait.

Program Memory MIPS memory operations Getting Variable Addresses Advanced structures.

Similar presentations


Presentation on theme: "Program Memory MIPS memory operations Getting Variable Addresses Advanced structures."— Presentation transcript:

1 Program Memory MIPS memory operations Getting Variable Addresses Advanced structures

2 Registers vs Memory Registers are memory Everything residing in a register has a real home Variables live in memory and are brought into registers When that register is needed for something else, the item fast, temporary in memory (with an associated address) for computations is stored back to memory.

3 Memory Setup in C/Java int X; What does this do? What does the memory look like? X is located here. An int is 4 bytes, so it takes 4 locations &X &X + 4 “&X” means “address of X”

4 Operation# comment lw $2, 32($3)# $2 <- M[32 +$3] sw $2, 16($3)# M[16 +$3] <- $2 lb $2, 0($3)# $2 <- M[0 +$3] sb $2, 3($3)# M[3 +$3] <- $2 Load/Store Instructions Displacement addressing mode Register indirect is Displacement with 0 offset lw = load word (4 bytes), lb = load byte (1 byte)

5 Memory Addressing Modes ModeExampleMeaningUse DisplacementLw R4, 100(R1)$4 <- M[$ ]Arrays w/constant offset Register IndirectLw R4, (R1)$4 <- M[$1]You have pointer IndexedLw R4, (R1+R2)$4 <- M[$1 + $2]$1 has base,$2 has offset Direct or absoluteLw R4, (1001)$4 <- M[1001]Globals, static data? Memory indirectLw <- M[M[$1]]Double-pointer AutoincrementLw R1, (R2) +$4 <- M[$2], $2 = $2 + d Stepping through array in loop AutodecrementLw R1, (R2) -$4 <- M[$2], $2 = $2 - d Stepping through array in loop ScaledLw R4, 100(R2)[R3]$4 <- M[100+$2+ ($3*d)] Indexing arrays of large elements MIPS supports ONLY Displacement mode Register Indirect and Direct are supported implicitly by Displacement

6 Address depends on variable type Local variables on the stack Global variables declared and allocated in data segment Heap variables (malloc or new)

7 Declaring, Allocating & Initializing Global Variables C: int GlobalA = 3; int main(int argc, char *argv[]) { } Java: public class MyClass{ public static int GlobalA = 3; }; MIPS:.data GlobalA:.word 0x03;.text main:

8 Declaring & Initializing Local Variables in HLL C: int main(int argc, char *argv[]) { int LocalA = 5; } Java: public class MyClass { public static void main(String[] argv) { int LocalA = 5; } }; MIPS: add $sp, $sp, -(24 + x + 4)# where x is space for preserved regs addi $t0, $0, 5 sw $t0, 0 ($sp)

9 Declaring & Initializing Heap Variables in HLL C: int main(int argc, char *argv[]) { int *LocalA = (int *)malloc(4); *LocalA = 5; } add $sp, $sp, -(24 + x + 4)# where x is space for preserved regs addi $a0, $0, 4 jal malloc sw $v0, 0($sp) addi $t0, $0, 5 sw $t0, 0($v0) Java: int main(int argc, char *argv[]) { int LocalA[] = new int[1] LocalA[0] = 5; }

10 How do I get &A? Global (Static) Vars –la $s0, GlobalA Local (Stack) Vars –addi $s0, $sp, const Dynamic (Heap) Vars –“new” returns the address into $v0 –sw $v0, 16($sp) –HeapA, in stack, points to a location in the heap!!! int GlobalA; int main(int argc, char *argv[]) { int StackA; int *HeapA = new int; } Assembler changes this pseudoinstruction into the real thing

11 MIPS Memory Notes la $s0, variable is a pseudoinstruction – assembler replaces it (i.e. 0x ) –lui $s0, 0x1004 –ori $s0, $s0, 0x0378 Register spilling - not enough registers, must save a value in memory Alignment – Integers must have addresses that are evenly divisible by word size (in bytes) – All variables’ addresses are divisible by their size

12 MIPS Example 3 A = B + A; la $t0, A// $t0 = &A (address of A) lw $t1, 0($t0) // $t1 = A lw $t2, 16($sp) // $t2 = B add $t1, $t1, $t2 // $t1= A + B sw $t1, 0($t0) // A = $t1 B = 2 * A; la $t0, A# $t0 = &A (address of A) lw $t1, 0($t0) # $t1 = A sll $t1, $t1, 1 # $t1 = 2 * A sw $t1, 16($sp) # B = 2*A Assumptions: A is a global var B is a local var at 16+$sp

13 Memory Setup in C/Java C++: int *intarray = new int[10]; Java: int[] intarray = new int[10]; What does this do? What does the memory look like? Where is intarray[5] located? intarray + 20 Where is intarray[i] located? intarray + 4*i intarray intarray + 20 &(intarray[0]) + 20 intarray &(intarray[0])

14 Declaring & Initializing Global Arrays in HLL int GlobalA = 3; int GlobalB[] = {0x , 0x , 0x200b0001, 0x8b502a, 0x , 0x , 0x , 0x800fffa, 0x , 0x }; int main(int argc, char *argv[]) { } public class MyClass{ public static int GlobalA = 3; public static int GlobalB[] = {0x , 0x , 0x200b0001, 0x8b502a, 0x , 0x , 0x , 0x800fffa, 0x , 0x }; };

15 Declaring & Initializing Global Arrays in MIPS.data GlobalA:.word 0x03; GlobalB:.word 0x x x200b0001 0x8b502a.word 0x x x x800fffa.word 0x x text main:

16 Declaring & Initializing Local Arrays int main(int argc, char *argv[]) { int LocalA = 5; int LocalB[] = {1,2,3}; } add $sp, $sp, -(24 + x )# where x is space for preserved regs addi $t0, $0, 5 sw $t0, 12($sp) addi $t0, $0, 1 sw $t0, 0($sp) addi $t0, $0, 2 sw $t1, 4($sp) addi $t0, $0, 3 sw $t1, 8($sp) // and so forth Not possible in Java!!!!! In Java, arrays are references to Array objects.

17 Declaring & Initializing Heap Arrays in HLL int main(int argc, char *argv[]) { int *LocalA = (int *)malloc(4); *LocalA = 5; int *LocalB = (int *)malloc(12); LocalB[0] = 1; LocalB[1] = 2; LocalB[2] = 3; } public class MyClass{ public static void main(int argc, String argv) { int LocalA[] = new int[1]; LocalA[0] = 5; int LocalB[] = new int[3]; LocalB[0] = 1; LocalB[1] = 2; LocalB[2] = 3; } };

18 Declaring & Initializing Heap Arrays in MIPS add $sp, $sp, -(24 + x + 8)# where x is space for preserved regs addi $a0, $0, 4 jal malloc sw $v0, 4($sp) // store the reference into the stack addi $t0, $0, 5 sw $t0, 0($v0) // initialize first elements as 5 (*LocalA = 5) addi $a0, $0, 12 jal malloc sw $v0, 0($sp) // store the reference into the stack addi $t0, $0, 1 sw $t0, 0($v0) // LocalB[0] = 1 addi $t0, $0, 2 sw $t0, 4($v0) // LocalB[1] = 2 addi $t0, $0, 3 sw $t0, 8($v0) // LocalB[2] = 3

19 MIPS Example 5 Translate from C code int A[100]; // ints are 4 bytes in Java/C char B[100]; // chars are 1 byte in C void main() { char c = B[50]; A[1] = A[5] + 7; } Assumptions: A&B are global, c is in the stack, 6 bytes from $sp

20 MIPS Example 5 Translate int A[100]; // ints are 4 bytes in C/Java char B[100]; // chars are 1 byte in C char c = B[50]; A[1] = A[5] + 7; Assumptions: A & B are global c is in the stack, 6 bytes from $sp Use LoadByte, not LoadWord, because char (in C) is 1 byte la $s1, B lb $t1, 50($s1)# $t1 = B[50]; lw $t0, 5 ($s0); x = A[5];lw $t0, 20 ($s0)# $t0 = A[5]; addi $t0, $t0, 7# $t0 = A[5] + 7; sw $t0, 4 ($s0)# A[1] = A[5] + 7; sb $t1, 6($sp)# c = B[50]; la $s0, A

21 MIPS Example 6 Translate int A[100]; int i; … x = A[i]; Assumptions: &(A[0]) is in $s0, x is in $t0 i is in $s1

22 MIPS Example 6 Translate int A[100]; int i; … x = A[i]; lw $t0, $s1 ($s0) Assumptions: &(A[0]) is in $s0, x is in $t0 i is in $s1

23 Memory Addressing Modes lw $t0, $s1 ($s0) # $t0 = A[i] ModeExampleMeaningUse DisplacementLw R4, 100(R1)$4 <- M[$ ]Arrays w/constant offset Register IndirectLw R4, (R1)$4 <- M[$1]You have pointer IndexedLw R4, (R1+R2)$4 <- M[$1 + $2]$1 has base,$2 has offset Direct or absoluteLw R4, (1001)$4 <- M[1001]Globals, static data? Memory indirectLw <- M[M[$1]]Double-pointer AutoincrementLw R1, (R2) +$4 <- M[$2], $2 = $2 + d Stepping through array in loop AutodecrementLw R1, (R2) -$4 <- M[$2], $2 = $2 - d Stepping through array in loop ScaledLw R4, 100(R2)[R3]$4 <- M[100+$2+ ($3*d)] Indexing arrays of large elements

24 Memory Addressing Modes lw $t0, ($s1 + $s0) # $t0 = A[i] ModeExampleMeaningUse DisplacementLw R4, 100(R1)$4 <- M[$ ]Arrays w/constant offset Register IndirectLw R4, (R1)$4 <- M[$1]You have pointer IndexedLw R4, (R1+R2)$4 <- M[$1 + $2]$1 has base,$2 has offset Direct or absoluteLw R4, (1001)$4 <- M[1001]Globals, static data? Memory indirectLw <- M[M[$1]]Double-pointer AutoincrementLw R1, (R2) +$4 <- M[$2], $2 = $2 + d Stepping through array in loop AutodecrementLw R1, (R2) -$4 <- M[$2], $2 = $2 - d Stepping through array in loop ScaledLw R4, 100(R2)[R3]$4 <- M[100+$2+ ($3*d)] Indexing arrays of large elements

25 Memory Addressing Modes lw $t0, 0 ($s0)[$s1] # $t0 = A[i] ModeExampleMeaningUse DisplacementLw R4, 100(R1)$4 <- M[$ ]Arrays w/constant offset Register IndirectLw R4, (R1)$4 <- M[$1]You have pointer IndexedLw R4, (R1+R2)$4 <- M[$1 + $2]$1 has base,$2 has offset Direct or absoluteLw R4, (1001)$4 <- M[1001]Globals, static data? Memory indirectLw <- M[M[$1]]Double-pointer AutoincrementLw R1, (R2) +$4 <- M[$2], $2 = $2 + d Stepping through array in loop AutodecrementLw R1, (R2) -$4 <- M[$2], $2 = $2 - d Stepping through array in loop ScaledLw R4, 100(R2)[R3]$4 <- M[100+$2+ ($3*d)] Indexing arrays of large elements

26 MIPS Example 6 Translate int A[100]; int i; … x = A[i]; lw $t0, $s1 ($s0) sll $t1, $s1, 2# $t1 = i<<2 or i * 4 Assumptions: &(A[0]) is in $s0, x is in $t0 i is in $s1

27 MIPS Example 6 Translate int A[100]; int i; … x = A[i]; sll $t1, $s1, 2# $t1 = i<<2 or i * 4 add $t1, $s0, $t1# $t1 = (&A[0] +i*4) or &(A[i]) Assumptions: &(A[0]) is in $s0, x is in $t0 i is in $s1

28 MIPS Example 6 Translate int A[100]; int i; … x = A[i]; sll $t1, $s1, 2# $t1 = i<<2 or i * 4 add $t1, $s0, $t1# $t1 = (i*4+&A[0]) or &(A[i]) lw $t0, 0($t1)# $t0 = A[i]; Assumptions: &(A[0]) is in $s0, x is in $t0 i is in $s1

29 Objects Member variables are a constant offset from the beginning of the object Member functions have hidden “this” pointer as first argument

30 Linked List class Link { private: Link*next; void*data; public: Link(){next=NULL;data=NULL;} inline void SetData(void *d){data = d;} inline void *GetData(){return data;} inline void SetNext(Link *n){next = n;} inline Link *GetNext(){return next;} }; If “this” pointer (address of current Link) is stored in $a0, what is the memory location of the current object’s “data” variable? 4 + $a0 C++ syntax: Link * means “reference to a Link” In Java, all Objects must use references void * means “reference to an item of unknown type”

31 Assembly code for Link For all methods, assume “this” pointer is in $a0, first argument is in $a1 Place the return value in $v0. SetData GetData this->data = d // this.data = d return this->data // return this.data sw $a1, 4 ($a0) lw $v0, 4($a0) Which is the source? Which is the destination? d this.data Is d in a register or in memory? Is this.data in a register or in memory? reg $a1 memory Which is the source? Which is the destination? this.data $v0 Is this.data in memory or a register? Is $v0 in a register or in memory? memory reg $v0

32 Dynamically Allocated Structures Linked Lists The memory is not contiguous – it is scattered about. Allocated dynamically, so we must call malloc or new allocator gives you the address of the beginning of the allocated memory in $v0

33 Linked List class LinkedList { private: Link*head; Link*tail; public: LinkedList(); ~LinkedList(); void InsertHead(void *data); void InsertTail(void *data); void *RemoveHead(); }; class LinkedList { private: Linkhead; Linktail; public: LinkedList(); ~LinkedList(); void InsertHead(Object data); void InsertTail(Object data); Object RemoveHead(); }; C++ syntax Java syntax

34 Code for void InsertTail(void *data) “this” Linked List is in $a0, data is in $a1 Link link = new Link(); //Link *link = new Link(); link.data = data; //link->data = data; this.tail.next = link; //this->tail->next = link; this.tail = link; //this->tail = link; jal new # don’t worry about args Where does “new” place the address of the new Link? $v0 sw $a1, 4 ($v0) lw $t0, 4 ($a0) # $t0 = this.tail sw $v0, 0 ($t0) # this.tail.next = link sw $v0, 4 ($a0) # this.tail = link Java Syntax //C++ Syntax

35 Lab 2 No pseudoinstructions in first part (no la) Dynamically allocate links –System call –Look in manual for the proper inputs/outputs


Download ppt "Program Memory MIPS memory operations Getting Variable Addresses Advanced structures."

Similar presentations


Ads by Google