Professional Documents
Culture Documents
Week 2
Assembly
Definitions
Register R-Format Instructions
Opcode 6 bits rs 5 bits rt 5 bits rd 5 bits shamt 5 bits funct 6 bits
I-Format Instructions
Opcode 6 bits rs 5 bits rt 5 bits Constant or Address 16 bits
Overflow
Assembly
Basic MIPS assembly Instructions
R-Format Instructions:
add $reg1, $reg2, $reg3 sub $reg1, $reg2, $reg3 Mul $reg1, $reg2, $reg3
I-Format Instructions:
addi $reg1, $reg2, Const sw $reg1, Const($reg2) lw $reg1, Const($reg2)
Assembly
Example 1 C to MIPS
Consider the following C code: a=4*d+2*(b+c); A) Translate the code to MIPS assembly given that a,b,c,d are stored in $s0-$s3 (Without using MUL instructions). B) What problems could arise from this implementation? C) The registers $s0-$s3 now contain the Addresses of a,b,c,d. Translate the code to MIPS assembler.
Assembly
Example 1 Solution a=4*d+2*(b+c);
A) Translate the code to MIPS given that a,b,c,d are stored in $s0-$s3 add $t0, $s1, $s2 # put b+c in $to sll $t0, $t0, 1 # Multiply b+c by 2 sll $t1, $s3, 2 # put 4*d in $t1 add $s0, $t1, $t0 # put 4*d + 2*(b+c) back in $s0 (The new value of a) B) What problems could arise from this implementation? If d or b+c are too large, using sll could cause an error! C) The registers $s0-$s3 now contain the Addresses of a,b,c,d. Translate the code to MIPS assembler. lw $t1, 0($s1) #load b to $t1 lw $t2, 0($s2) #load c to $t2 lw $t3, 0($s3) #load d to $t3 add $t0, $t1, $t2 # put b+c in $to sll $t0, $t0, 1 # Multiply b+c by 2 sll $t1, $t3, 2 # put 4*d in $t1 add $t0, $t1, $t0 # put 4*d + 2*(b+c) in $t0 sw $t0,0($s0) #Store the answer back in as address ($s0)
Assembly
Example 2 Assembly to C
Consider the following Assembly code:
LOOP: add $t0, $s0, $s1 add $t1, $s0, $s2
A( Assume that $s3 and $s4 contain the addresses of variables K,L. Translate the code to C. B( Consider the same code with the line: j LOOP at the end. Translate the code with this modification.
Assembly
Example 2 - Solution
A( Translate the code to C
Assume b,c,d stored in $s0-$s2
LOOP: add $t0, $s0, $s1 =>While (1) { add $t1, $s0, $s2 a=(b+c)+(b+d); add $t0, $t0, $t1 =>
addi $t0, $t0, -3 sll $t0, $t0, 3 lw $t2, 0($s3) Sub $t0, $t0, $t2 Sw $t0, 0($s4) => => a=a-3; a=8*a;
=>
=>
L=a-K;
}
j LOOP
Assembly
Example 3 C Branching and loops
Consider the following C code:
do
{
if (i==6) { i=i-2; } i--; }
while (i>0);
Assembly
Example 3 Solution
do { if (i==6) {
i=i-2;
} i --; } while (i>=0);
Initialize: addi $t2, $0, 6 lw $t0, 0($s0) Loop: bne $t0, $t2, Else addi $t0, $t0, -2 sw $t0, 0($s0) Else: addi $t0, $t0, -1 sw $t0, 0($s0) slti $t1, $t0, 0 beq $t1, $0, Loop Exit:
# put the Constant 6 in $t2 # put is value in $t0 # if i != 6, skip next two lines # i = i-2 # Store is new value in its proper address # Finished an iteration, decrease i by 1. # if i<0, $t1=1, else $t1=0 # if $t1=0, jump to beginning of loop # Loop broken, continue to other code lines
Assembly
Assembly
Example 5 Arrays and Offsets
Consider the following C code: a = b[10] c[5];
b[7] = a-c[3];
Where the addresses of b[0] and c[0] are stored in $s0, $s1 and the value of a is in $s2. Translate the code to assembly. lw $t0, 40($s0) #load b[10] to $t0
lw $t1, 20($s1)
sub $s2, $t0, $t1 lw $t0, 12($s1) sub $t0, $s2, $t0 sw $t0, 28($s0)
There are two sets of commands. One set for signed numbers and other are for unsigned:
Signed & Unsigned In MIPS, negative numbers are represented in 2s complement form
add $reg1, $reg2, $reg3 addi $reg1, $reg2, Const sub $reg1, $reg2, $reg3 Mul $reg1, $reg2, $reg3
addu $reg1, $reg2, $reg3 addiu $reg1, $reg2, Const subu $reg1, $reg2, $reg3 Mulu $reg1, $reg2, $reg3
Representing a result of an instruction performed on multiple numbers that requires more bits that are available is called Overflow For example: adding two positive signed numbers. MSB must be 0. If the MSB is 0 its an Overflow