Chapter 2
Chapter 2
C code:
f = (g + h) - (i + j);
Compiled MIPS code:
add t0, g, h # temp t0 = g + h
add t1, i, j # temp t1 = i + j
sub f, t0, t1 # f = t0 - t1
CreationRegister
of Online Operands
and Blended Course
C code:
f = (g + h) - (i + j);
f, … , j in $s0, … , $s4
C code:
g = h + A[8];
g in $s1, h in $s2, base address of A in $s3
C code:
A[12] = h + A[8];
h in $s2, base address of A in $s3
x
Range: –2 n – 1 to +2n – 1 – 1
Example
1111 1111 1111 1111 1111 1111 1111 11002
= –1×2 3 1 + 1×230 + … + 1×22 +0×21 +0×20
= –2,147,483,648 + 2,147,483,644 = –4 1 0
Using 32 bits
–2,147,483,648 to +2,147,483,647
2s-Complement Signed
Creation of Online and Integers (Cont’d)
Blended Course
x x 1111...1112
1
x 1 x
Example: negate +2
+2 = 0000 0000 …
00102
– 2 = 1111 1111 … 11012 + 1
Creation ofSign Extension
Online and Blended Course
op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits
6 bits
Instruction fields
op: operation code (opcode)
rs: first source register number
rt: second source register number
rd: destination register number
shamt: shift amount (00000 for now)
funct: function code (extends opcode)
CreationR-format Example
of Online and Blended Course
op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
0 17 18 8 0 32
000000100011001001000000001000002 = 0232402016
Hexadecimal
Creation of Online and Blended Course
Base 16
Compact representation of bit strings
4 bits per hex digit
0 0000 4 0100 8 1000 c 1100
1 0001 5 0101 9 1001 d 1101
2 0010 6 0110 a 1010 e 1110
3 0011 7 0111 b 1011 f 1111
op rs rt constant or address
6 bits 5 bits 5 bits 16 bits
op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits
6 bits
C code:
if (i==j) f = g+h;
else f = g-h;
f, g, … in $s0, $s1, …
Compiled MIPS
code: bne $s3, $s4, Else
add $s1, $s2
$s0, j
Else: Exit $s1, $s2
sub $s0,
Exit: … Assembler calculates addresses
Compiling
Creation Loop
of Online and Statements
Blended Course
C code:
while (save[i] == k) i += 1;
i in $s3, k in $s5, address of save in $s6
Compiled MIPS code:
Loop: sll $t1, $s3, 2
add $t1, $t1, $s6
lw $t0, 0($t1)
bne $t0, $s5, Exit
addi $s3, $s3, 1
j Loop
Exit: …
Basic Blocks
Creation of Online and Blended Course
Steps required
1. Place parameters in registers
2. Transfer control to procedure
3. Acquire storage for procedure
4. Perform procedure’s operations
5. Place result in register for caller
6. Return to place of call
Creation ofRegister Usage
Online and Blended Course
C code:
int leaf_example (int g, h, i, j)
{ int f;
f = + h) - (i + j);
(g return f;
}
Arguments g, … , j in $a0, … , $a3
Result in $v0
Leaf Procedure
Creation of OnlineExample (Cont’d)
and Blended Course
MIPS code:
leaf_example:
addi $sp, $sp, -4
Save $s0 on stack
sw $s0, 0($sp)
add $t0, $a0, $a1
add $t1, $a2, $a3 Procedure body
$s0, $t0, $t1
sub Result
add $v0, $s0, $zero
Restore $s0
lw $s0, 0($sp)
addi $sp, $sp, 4 Return
jr $ra
Non-Leaf
Creation Procedures
of Online and Blended Course
C code:
int fact (int n)
{
if (n < 1) return (1);
else return n * fact(n - 1);
}
Argument n in $a0
Result in $v0
Non-Leaf
CreationProcedure
of Online andExample (Cont’d)
Blended Course
MIPS code:
fact:
addi $sp, $sp, -8 # adjust stack for 2 items
sw $ra, 4($sp) # save return address
sw $a0, 0($sp) # save argument
slti $t0, $a0, 1 # test for n < 1
beq $t0, $zero, L1
addi $v0, $zero, 1 # if so, result is 1
addi $sp, $sp, 8 # pop 2 items from stack
jr $ra and return
#
L1: addi $a0, $a0, -1 # else decrement n
jal fact # recursive call
lw $a0, 0($sp) # restore original n
lw $ra, 4($sp) # and return address
addi $sp, $sp, 8 pop 2 items from stack
#
mul $v0, $a0, $v0 # multiply to get
Local
Creation Data and
of Online on the Stack
Blended Course
C code (naïve):
Null-terminated string
void strcpy (char x[], char y[])
{ int i;
i =0;
while ((x[i]=y[i])!='\0')
i += 1;
}
Addresses of x, y in $a0, $a1
i in $s0
String Copy
Creation Example
of Online (Cont’d)
and Blended Course
MIPS code:
strcpy:
addi $sp, $sp, -4 # adjust stack for 1 item
sw $s0, 0($sp) # save $s0
add $s0, $zero, $zero # i = 0
L1: add $t1, $s0, $a1 # addr of y[i] in $t1
lbu $t2, 0($t1) # $t2 = y[i]
add $t3, $s0, $a0 # addr of x[i] in $t3
sb $t2, 0($t3) # x[i] = y[i]
beq $t2, $zero, L2 # exit loop if y[i] == 0
addi $s0, $s0, 1 # i = i + 1
j L1 # next iteration of loop
L2: lw $s0, 0($sp) # restore saved $s0
addi $sp, $sp, 4 # pop 1 item from stack
jr $ra # and return
32-bit
Creation of OnlineConstants
and Blended Course
lui $s0, 61 0000 0000 0011 1101 0000 0000 0000 0000
ori $s0, $s0, 2304 0000 0000 0011 1101 0000 1001 0000 0000
CreationBranch
of OnlineAddressing
and Blended Course
op rs rt constant or address
PC-relative addressing
Target address = PC + offset × 4
PC already incremented by 4 by this time
Creation Jump Addressing
of Online and Blended Course
op address
6 bits 26 bits
mapped mapped
Compare
Creation and Branch
of Online in ARM
and Blended Course