assembly language chapter 2 mips –arithmetic instructions –control instructions translating...

41
Assembly Language Chapter 2 • MIPS – arithmetic instructions – control instructions • Translating Instructions to binary • MIPS – memory instructions • Advanced data structures

Post on 20-Dec-2015

231 views

Category:

Documents


0 download

TRANSCRIPT

Assembly LanguageChapter 2

• MIPS – arithmetic instructions– control instructions

• Translating Instructions to binary

• MIPS– memory instructions

• Advanced data structures

Learning a new ISALearn the syntax, semantics of:

• _______________ operations

• _______________ operations

• _______________ operations

MIPS Registers – 32 registersName Reg Number Usage Preserved

across call?

$zero 0 The constant 0 Yes

$v0-$v1 2-3 Function results No

$a0-$a3 4-7 Arguments No

$t0-$t7 8-15 Temporaries No

$s0-$s8 16-23 Saved Yes

$t8-$t9 24-25 More temporaries No

$gp 28 Global pointer Yes

$sp 29 Stack pointer Yes

$fp 30 Frame pointer Yes

$ra 31 Return address Yes

Page 140, Figure 3.13

Saved vs Temporary regs

• Saved:– Register __________ value after function call– Easier to program

• Temporary– Register __________ value after function call– More efficient

Design Principle 1Simplicity Favors Regularity

Arithmetic “R-Format”

• Two input registers

• One output registerregister #5

Operation rs rt rd shamt funct # meaning

add $2,$3,$5 3 5 2 0 32 # $2 <- $3 + $5

sub $2,$3,$5 3 5 2 0 34 # $2 <- $3 - $5

addu $2,$3,$5 3 5 2 0 33 # $2 <- $3 + $5

slt $2, $3, $5 3 5 2 0 42 # if ($3 < $5) $2 <- 1#else $2 <- 0

Arithmetic “I-format”

• One input register• One hard-coded constant• One output register

Operation rs rt rd shamt funct # comment

addi $2, $3, 8 3 2 8 # $2 <- $3 + 8

andi $2, $3, 10 3 2 10 # $2 <- $3 & 10

slti $2, $3, 7 3 2 7 # if ($3 < 7) $2 <- 1 #else $2 <- 0

Design Principle 2:Small is faster

• Variables must be loaded into registers before use

• How many registers should ISA provide?• More:

• Fewer:

• How many bits necessary for each operand?• 32 regs:_____ 64 regs______ N regs:

Conditional & Unconditional Branches

• goto loop• if (i < 100) goto loop

Operation rs rt rd shamt funct # comment

beq 3 2 distance to loop # if ($3 == $2) goto loop

bne 3 2 distance to loop # if ($3 != $2) goto loop

jr 3 | | 8 # goto $3

j loop # goto loop

MIPS Example 1if (x == y) equal = 1;else equal = 0;

assume x is in $s0,y is in $s1, andequal is in $v0.

MIPS Example 2

sum=0;for(i=0; i < 100; i++)

sum += i;

translated into more detailed C (with gotos)sum = 0;i = 0;

loop: if ( ) goto ______;sum += i; ;goto ______;

end:

MIPS Example 2 In Assembly

Translating into machine code

assembly inst op rs rt rd shamt func comment

add $s0, $0, $0 # i = 0

slti $t0, $s0,100 # t0 <= (i < 100 ?)

beq $t0, $0, end # if (i >= 100) goto end

la $t0, A # t0 <= r0 + A

lw $t2, 0($t1) # ti <= M[A+i] = A[i]

addi $s0, $s0, 1 # i++

jump loop # goto loop

Why memory operations?Problem:

• _____________ (collection of regs) must be small for ______________

• Register number is _______________into instruction

• Register number not always known at compile-time (__________________)

Why memory operations?Solution:

• Build a ________________________ called ___________ that will store memory that does not fit in ___________.

• Allow registers to hold the ________________________________ that data resides.

• Many _________________________ are supported by different ISA’s.

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 _____________ _________________________________

Memory Setup in C/Java

• int X;

• What does this do? What does the memory look like?

Memory is an endless list of memory spots Each spot has a

“memory address”

X is located here.An int is 4 bytes, so it takes 4 locations

&X

&X + 4 “&X” means “address of X”

Operation rs rt rd shamt funct # comment

lw $2, 32($3) 3 # $2 <- M[32 +$3]

sw $2, 16($3) 3 # M[16 +$3] <- $2

lb $2, 0($3) 3 # $2 <- M[0 +$3]

sb $2, 3($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)

Possible Memory Addressing Modes (MIPS supports few)

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

Variable Types

• Local variables on __________________

• Global variables declared and allocated in ________________________

• Heap variables created with ________________________

Declaring, Allocating & Initializing

Global VariablesC: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, Allocating & Initializing

Local VariablesC: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, Allocating & Initializing

Heap Variables C:int main(int argc, char *argv[]){ int *LocalA = (int *)malloc(4); *LocalA = 5;}

…Real MIPS….add $sp, $sp, -(24 + x + 4)addi $a0, $0, 4jal mallocsw $v0, 4($sp)addi $t0, $0, 5sw $t0, 0($v0)

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

…….MIPS Simulator……add $sp, $sp, -(24 + x + 4)addi $a0, $0, 4addi $v0, $0, 9syscallsw $v0, 4($sp)addi $t0, $0, 5sw $t0, 0($v0)

How do I get &X (address of X)?

int GlobalA;

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

int *HeapA = new int;

}

• Global (Static) Vars

• Local (Stack) Vars

• Dynamic (Heap) Vars

MIPS Memory Notes

• la $s0, variable is a pseudoinstruction – assembler replaces it with – ____________– ____________

• Register ________ - not enough registers, must save a value in memory

• Alignment – Integers must have addresses that are evenly divisible by _________

MIPS Example 3A = B + A;

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?• Where is intarray[i] located?

Declaring, Allocating & Initializing

Local 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, 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.

Declaring, Allocating & Initializing

Global Arrays in HLLint 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, Allocating & 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, Allocating & 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 LocalB[] = new int[3]; LocalB[0] = 1; LocalB[1] = 2; LocalB[2] = 3; }};

Declaring, Allocating & Initializing

Heap Arrays in MIPS…..Real MIPS……add $sp, $sp, -(24 + x + 8)addi $a0, $0, 4jal mallocsw $v0, 4($sp)addi $t0, $0, 5sw $t0, 0($v0)addi $a0, $0, 12jal mallocsw $v0, 0($sp)addi $t0, $0, 1sw $t0, 0($v0)addi $t0, $0, 2sw $t1, 4($v0)addi $t0, $0, 3sw $t1, 8($v0)

…..Simulator MIPS……add $sp, $sp, -(24 + x + 8)addi $a0, $0, 4 # how much spaceaddi $v0, $0, 9syscallsw $v0, 4($sp) # address in $v0addi $t0, $0, 5sw $t0, 0($v0)addi $a0, $0, 12 # how much spaceaddi $v0, $0, 9syscallsw $v0, 0($sp) # address in $v0addi $t0, $0, 1sw $t0, 0($v0)addi $t0, $0, 2sw $t1, 4($v0)addi $t0, $0, 3sw $t1, 8($v0)

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 6Translate int A[100];int i; …x = A[i];

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

Objects

• Members variables are _______________ from the beginning of the object

• Member functions have hidden ________ __________________________

Linked Listclass Link { public: (since we don’t know how to do method calls yet)

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?

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

SetData GetData// this.data = d // return this.data

Which is the source?

Which is the destination?

Is d in memory or in a register?

Is this.data in a register or in memory?

Which is the source?

Which is the destination?

Is this.data in memory or a register? Is $v0 in a register or in memory?

Linked Lists

• The memory is not ________________ – it is scattered about.

• Allocated dynamically, so we must call ___________________________

• allocator gives you the address of the beginning of the allocated memory in ____

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;

Java Syntax//C++ Syntax

Voluntary Assignment

• Create a snippet of Java, C, or C++ code that you are unsure how to translate into mips.

• E-mail it to me before class Thursday

• I will choose from among them to do more examples