Program Memory MIPS memory operations Getting Variable Addresses Advanced structures.

Post on 14-Dec-2015

225 views 0 download

Tags:

Transcript of Program Memory MIPS memory operations Getting Variable Addresses Advanced structures.

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

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

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”

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)

Memory Addressing Modes

Mode Example Meaning UseDisplacement Lw R4, 100(R1) $4 <- M[$1 + 100] Arrays w/constant

offset

Register Indirect Lw R4, (R1) $4 <- M[$1] You have pointer

Indexed Lw R4, (R1+R2) $4 <- M[$1 + $2] $1 has base,$2 has offset

Direct or absolute Lw R4, (1001) $4 <- M[1001] Globals, static data?

Memory indirect Lw R4, @(R1) $4 <- M[M[$1]] Double-pointer

Autoincrement Lw R1, (R2) + $4 <- M[$2], $2 = $2 + d

Stepping through array in loop

Autodecrement Lw R1, (R2) - $4 <- M[$2], $2 = $2 - d

Stepping through array in loop

Scaled Lw 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

Address depends on variable type

• Local variables on the stack

• Global variables declared and allocated in data segment

• Heap variables (malloc or new)

Declaring, Allocating & InitializingGlobal Variables

C:int GlobalA = 3;

int main(int argc, char *argv[]){ }

Java:public class MyClass{ public static int GlobalA = 3;};

MIPS:.dataGlobalA: .word 0x03;

.text

main:

Declaring & InitializingLocal 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 regsaddi $t0, $0, 5sw $t0, 0 ($sp)

Declaring & InitializingHeap 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 regsaddi $a0, $0, 4jal mallocsw $v0, 0($sp)addi $t0, $0, 5sw $t0, 0($v0)

Java:int main(int argc, char *argv[]){ int LocalA[] = new int[1] LocalA[0] = 5;}

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

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

MIPS Example 3A = B + A;la $t0, A // $t0 = &A (address of A)lw $t1, 0($t0) // $t1 = Alw $t2, 16($sp) // $t2 = Badd $t1, $t1, $t2 // $t1= A + Bsw $t1, 0($t0) // A = $t1B = 2 * A;la $t0, A # $t0 = &A (address of A)lw $t1, 0($t0) # $t1 = Asll $t1, $t1, 1 # $t1 = 2 * Asw $t1, 16($sp) # B = 2*A

Assumptions: A is a global varB is a local var at 16+$sp

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

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};

};

Declaring & Initializing Global Arrays in MIPS

.dataGlobalA: .word 0x03;GlobalB:.word 0x20040002 0x20080001 0x200b0001 0x8b502a .word 0x15400003 0x01084020 0x20840000 0x800fffa .word 0x10010200 0x00000000

.text

main:

Declaring & InitializingLocal Arrays

int main(int argc, char *argv[]){ int LocalA = 5; int LocalB[] = {1,2,3};

}

add $sp, $sp, -(24 + x + 4 + 12) # where x is space for preserved regsaddi $t0, $0, 5sw $t0, 12($sp)addi $t0, $0, 1sw $t0, 0($sp)addi $t0, $0, 2sw $t1, 4($sp)addi $t0, $0, 3sw $t1, 8($sp) // and so forth

Not possible in Java!!!!!In Java, arrays are references to Array objects.

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; }};

Declaring & InitializingHeap Arrays in MIPS

add $sp, $sp, -(24 + x + 8) # where x is space for preserved regsaddi $a0, $0, 4jal mallocsw $v0, 4($sp) // store the reference into the stackaddi $t0, $0, 5sw $t0, 0($v0) // initialize first elements as 5 (*LocalA = 5)addi $a0, $0, 12jal mallocsw $v0, 0($sp) // store the reference into the stackaddi $t0, $0, 1sw $t0, 0($v0) // LocalB[0] = 1addi $t0, $0, 2sw $t0, 4($v0) // LocalB[1] = 2addi $t0, $0, 3sw $t0, 8($v0) // LocalB[2] = 3

MIPS Example 5Translate from C codeint A[100]; // ints are 4 bytes in Java/Cchar B[100]; // chars are 1 byte in Cvoid main() {char c = B[50]; A[1] = A[5] + 7;}

Assumptions: A&B are global,

c is in the stack,6 bytes from $sp

MIPS Example 5Translate int A[100]; // ints are 4 bytes in C/Javachar B[100]; // chars are 1 byte in C

char c = B[50]; A[1] = A[5] + 7;

Assumptions: A & B are globalc is in the stack,6 bytes from $sp

Use LoadByte, not LoadWord, because char (in C) is 1 byte

la $s1, Blb $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

MIPS Example 6Translate int A[100];int i; …x = A[i];

Assumptions: &(A[0]) is in $s0,x is in $t0i is in $s1

MIPS Example 6Translate int A[100];int i; …x = A[i];

lw $t0, $s1 ($s0)

Assumptions: &(A[0]) is in $s0,x is in $t0i is in $s1

Memory Addressing Modes lw $t0, $s1 ($s0) # $t0 = A[i]

Mode Example Meaning UseDisplacement Lw R4, 100(R1) $4 <- M[$1 + 100] Arrays w/constant

offset

Register Indirect Lw R4, (R1) $4 <- M[$1] You have pointer

Indexed Lw R4, (R1+R2) $4 <- M[$1 + $2] $1 has base,$2 has offset

Direct or absolute Lw R4, (1001) $4 <- M[1001] Globals, static data?

Memory indirect Lw R4, @(R1) $4 <- M[M[$1]] Double-pointer

Autoincrement Lw R1, (R2) + $4 <- M[$2], $2 = $2 + d

Stepping through array in loop

Autodecrement Lw R1, (R2) - $4 <- M[$2], $2 = $2 - d

Stepping through array in loop

Scaled Lw R4, 100(R2)[R3] $4 <- M[100+$2+ ($3*d)]

Indexing arrays of large elements

Memory Addressing Modes lw $t0, ($s1 + $s0) # $t0 = A[i]

Mode Example Meaning UseDisplacement Lw R4, 100(R1) $4 <- M[$1 + 100] Arrays w/constant

offset

Register Indirect Lw R4, (R1) $4 <- M[$1] You have pointer

Indexed Lw R4, (R1+R2) $4 <- M[$1 + $2] $1 has base,$2 has offset

Direct or absolute Lw R4, (1001) $4 <- M[1001] Globals, static data?

Memory indirect Lw R4, @(R1) $4 <- M[M[$1]] Double-pointer

Autoincrement Lw R1, (R2) + $4 <- M[$2], $2 = $2 + d

Stepping through array in loop

Autodecrement Lw R1, (R2) - $4 <- M[$2], $2 = $2 - d

Stepping through array in loop

Scaled Lw R4, 100(R2)[R3] $4 <- M[100+$2+ ($3*d)]

Indexing arrays of large elements

Memory Addressing Modes lw $t0, 0 ($s0)[$s1] # $t0 = A[i]

Mode Example Meaning UseDisplacement Lw R4, 100(R1) $4 <- M[$1 + 100] Arrays w/constant

offset

Register Indirect Lw R4, (R1) $4 <- M[$1] You have pointer

Indexed Lw R4, (R1+R2) $4 <- M[$1 + $2] $1 has base,$2 has offset

Direct or absolute Lw R4, (1001) $4 <- M[1001] Globals, static data?

Memory indirect Lw R4, @(R1) $4 <- M[M[$1]] Double-pointer

Autoincrement Lw R1, (R2) + $4 <- M[$2], $2 = $2 + d

Stepping through array in loop

Autodecrement Lw R1, (R2) - $4 <- M[$2], $2 = $2 - d

Stepping through array in loop

Scaled Lw R4, 100(R2)[R3] $4 <- M[100+$2+ ($3*d)]

Indexing arrays of large elements

MIPS Example 6Translate 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 $t0i is in $s1

MIPS Example 6Translate int A[100];int i; …x = A[i];

sll $t1, $s1, 2 # $t1 = i<<2 or i * 4add $t1, $s0, $t1 # $t1 = (&A[0] +i*4) or &(A[i])

Assumptions: &(A[0]) is in $s0,x is in $t0i is in $s1

MIPS Example 6Translate int A[100];int i; …x = A[i];

sll $t1, $s1, 2 # $t1 = i<<2 or i * 4add $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 $t0i is in $s1

Objects

• Member variables are a constant offset from the beginning of the object

• Member functions have hidden “this” pointer as first argument

Linked Listclass 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”

Assembly code for LinkFor all methods, assume “this” pointer is in $a0, first argument is in $a1Place the return value in $v0.

SetDataGetData

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

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

Linked Listclass 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();

};

C++ syntaxJava syntax

Code forvoid 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

Lab 2

• No pseudoinstructions in first part (no la)

• Dynamically allocate links– System call– Look in manual for the proper inputs/outputs