1 / 35

Program Memory

Program Memory. MIPS memory operations Getting Variable Addresses Advanced structures. Registers vs Memory. Registers are memory Everything residing in a register has a real home Variables live in memory and are brought into registers

Download Presentation

Program Memory

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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” means “address of X” &X &X + 4

  4. Load/Store Instructions • Displacement addressing mode • Register indirect is Displacement with 0 offset • lw = load word (4 bytes), lb = load byte (1 byte)

  5. Register Indirect and Direct are supported implicitly by Displacement Memory Addressing Modes MIPS supports ONLY Displacement mode

  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 & InitializingGlobal 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 & InitializingLocal Variables in HLL Java: public class MyClass { public static void main(String[] argv) { int LocalA = 5; } }; C: int main(int argc, char *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 & InitializingHeap Variables in HLL C: int main(int argc, char *argv[]) { int *LocalA = (int *)malloc(4); *LocalA = 5; } Java: int main(int argc, char *argv[]) { int LocalA[] = new int[1] LocalA[0] = 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)

  10. 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!!! How do I get &A? Assembler changes this pseudoinstruction into the real thing int GlobalA; int main(int argc, char *argv[]) { int StackA; int *HeapA = new int; }

  11. MIPS Memory Notes • la $s0, variable is a pseudoinstruction – assembler replaces it (i.e. 0x10040378) • 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 Assumptions: A is a global var B is a local var at 16+$sp 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

  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 &(intarray[0]) intarray + 20 &(intarray[0]) + 20

  14. Declaring & InitializingGlobal Arrays in HLL int GlobalA = 3; int GlobalB[] = {0x20040002, 0x20080001, 0x200b0001, 0x8b502a, 0x15400003, 0x01084020, 0x20840000, 0x800fffa, 0x10010200, 0x00000000}; int main(int argc, char *argv[]) { } public class MyClass{ public static int GlobalA = 3; public static int GlobalB[] = {0x20040002, 0x20080001, 0x200b0001, 0x8b502a, 0x15400003, 0x01084020, 0x20840000, 0x800fffa, 0x10010200, 0x00000000}; };

  15. Declaring & Initializing Global Arrays in MIPS .data GlobalA: .word 0x03; GlobalB: .word 0x20040002 0x20080001 0x200b0001 0x8b502a .word 0x15400003 0x01084020 0x20840000 0x800fffa .word 0x10010200 0x00000000 .text main:

  16. Declaring & InitializingLocal Arrays int main(int argc, char *argv[]) { int LocalA = 5; int LocalB[] = {1,2,3}; } Not possible in Java!!!!! In Java, arrays are references to Array objects. add $sp, $sp, -(24 + x + 4 + 12) # 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

  17. Declaring & InitializingHeap 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 & InitializingHeap 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 Assumptions: A&B are global, c is in the stack, 6 bytes from $sp 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; }

  20. MIPS Example 5 Assumptions: A & B are global c is in the stack, 6 bytes from $sp 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; Use LoadByte, not LoadWord, because char (in C) is 1 byte la $s1, B lb $t1, 50($s1) # $t1 = B[50]; sb $t1, 6($sp) # c = B[50]; la $s0, A 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;

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

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

  23. Memory Addressing Modeslw $t0, $s1 ($s0) # $t0 = A[i]

  24. Memory Addressing Modeslw $t0, ($s1 + $s0) # $t0 = A[i]

  25. Memory Addressing Modeslw $t0, 0 ($s0)[$s1] # $t0 = A[i]

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

  27. MIPS Example 6 Assumptions: &(A[0]) is in $s0, x is in $t0 i is in $s1 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])

  28. MIPS Example 6 Assumptions: &(A[0]) is in $s0, x is in $t0 i is in $s1 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];

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

  30. C++ syntax: Link * means “reference to a Link” In Java, all Objects must use references Linked List void * means “reference to an item of unknown type” 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

  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. GetData SetData return this->data // return this.data Which is the source? Which is the destination? this->data = d // this.data = d d lw $v0, 4($a0) sw $a1, 4 ($a0) Which is the source? Which is the destination? this.data this.data $v0 Is d in a register or in memory? Is this.data in a register or in memory? reg $a1 Is this.data in memory or a register? Is $v0 in a register or in memory? memory memory reg $v0

  32. Dynamically Allocated StructuresLinked 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: Link head; Link tail; public: LinkedList(); ~LinkedList(); void InsertHead(Object data); void InsertTail(Object data); Object RemoveHead(); }; Java syntax C++ syntax

  34. Code forvoid InsertTail(void *data) “this” Linked List is in $a0, data is in $a1 jal new # don’t worry about args 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; 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 Where does “new” place the address of the new Link? $v0 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

More Related