Lec3

advertisement
Assembly Language
Chapter 2
• MIPS
– arithmetic instructions
– control instructions
• Translating Instructions to binary
• MIPS
– memory instructions
• Advanced data structures
Learning a new ISA
Learn the syntax, semantics of:
• _______________ operations
• _______________ operations
• _______________ operations
MIPS Registers – 32 registers
Name
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
Saved vs Temporary regs
• Saved:
– Register __________ value after function call
– Easier to program
• Temporary
– Register __________ value after function call
– More efficient
Design Principle 1
Simplicity Favors Regularity
Arithmetic “R-Format”
• Two input registers
• One output register
register #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
5
0
42
# if ($3 < $5) $2 <- 1
#else
$2 <- 0
slt $2, $3, $5 3
2
Arithmetic “I-format”
• One input register
• One hard-coded constant
• One output register
Operation
rs rt
rd shamt funct # comment
2
2
8
10
# $2 <- $3 + 8
# $2 <- $3 & 10
slti $2, $3, 7 3 2
7
# if ($3 < 7) $2 <- 1
#else
$2 <- 0
addi $2, $3, 8 3
andi $2, $3, 10 3
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
jr
j
3 2 distance to loop # if ($3 != $2) goto loop
|
| 8
# goto $3
3
# goto loop
loop
MIPS Example 1
if (x == y)
equal = 1;
else
equal = 0;
assume x is in $s0,
y is in $s1, and
equal 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? X is located here.
An int is 4 bytes, so
it takes 4 locations
&X
&X + 4
Memory is an endless
list of memory spots
“&X” means
“address of X”
Each spot has a
“memory address”
Load/Store Instructions
• Displacement addressing mode
• Register indirect is Displacement with 0 offset
• lw = load word (4 bytes), lb = load byte (1 byte)
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
Possible Memory Addressing
Modes (MIPS supports few)
Mode
Example
Meaning
Use
Displacement
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 Variables
C:
int GlobalA = 3;
int main(int argc, char *argv[])
{
}
MIPS:
.data
GlobalA:
.word 0x03
.text
main:
Java:
public class MyClass{
public static int GlobalA = 3;
};
Declaring, Allocating & Initializing
Local Variables
C:
int main(int argc, char *argv[])
{
int LocalA = 5;
}
MIPS:
add $sp, $sp, -(24 + x + 4)
addi $t0, $0, 5
sw $t0, 0 ($sp)
Java:
public class MyClass {
public static void main(String[] argv)
{
int LocalA = 5;
}
};
# where x is space for preserved regs
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, 4
jal malloc
sw $v0, 4($sp)
addi $t0, $0, 5
sw $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, 4
addi $v0, $0, 9
syscall
sw $v0, 4($sp)
addi $t0, $0, 5
sw $t0, 0($v0)
How do I get &X (address of X)?
• Global (Static) Vars
int GlobalA;
int main(int argc, char *argv[])
{
int StackA;
• Local (Stack) Vars
int *HeapA = new int;
• 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 3
A = B + A;
B = 2 * A;
Assumptions:
A is a global var
B 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};
Not possible in Java!!!!!
In Java, arrays are references to
Array objects.
}
add $sp, $sp, -(24 + x + 4 + 12)
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
# where x is space for preserved regs
Declaring, Allocating & Initializing
Global 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, Allocating & 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:
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, 4
jal malloc
sw $v0, 4($sp)
addi $t0, $0, 5
sw $t0, 0($v0)
addi $a0, $0, 12
jal malloc
sw $v0, 0($sp)
addi $t0, $0, 1
sw $t0, 0($v0)
addi $t0, $0, 2
sw $t1, 4($v0)
addi $t0, $0, 3
sw $t1, 8($v0)
…..Simulator MIPS……
add $sp, $sp, -(24 + x + 8)
addi $a0, $0, 4 # how much space
addi $v0, $0, 9
syscall
sw $v0, 4($sp) # address in $v0
addi $t0, $0, 5
sw $t0, 0($v0)
addi $a0, $0, 12 # how much space
addi $v0, $0, 9
syscall
sw $v0, 0($sp) # address in $v0
addi $t0, $0, 1
sw $t0, 0($v0)
addi $t0, $0, 2
sw $t1, 4($v0)
addi $t0, $0, 3
sw $t1, 8($v0)
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
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
Objects
• Members variables are _______________
from the beginning of the object
• Member functions have hidden ________
__________________________
Linked List
class 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 Link
For all methods, assume “this” pointer is in $a0, first argument is in $a1
Place the return value in $v0.
SetData
// this.data = d
Which is the
source?
Which is the
source?
Which is the
destination?
Which is the
destination?
Is d in
memory or in
a register?
Is this.data in
memory or a
register?
Is this.data in
a register or in
memory?
Is $v0 in a
register or in
memory?
GetData
// return this.data
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 List
class LinkedList {
class LinkedList {
private:
private:
Link *head;
Link head;
Link *tail;
Link tail;
public:
public:
LinkedList();
LinkedList();
~LinkedList();
~LinkedList();
void InsertHead(void *data);
void InsertHead(Object data);
void InsertTail(void *data);
void InsertTail(Object data);
void *RemoveHead();
Object RemoveHead();
};
};
C++ syntax
Java syntax
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;
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
Download