Professional Documents
Culture Documents
Translate C Into MIPS Assembly Translate C Into MIPS Assembly
Translate C Into MIPS Assembly Translate C Into MIPS Assembly
5JJ70
‘Implementatie van rekenprocessen’
Henk Corporaal
December 2009
Welcome
So far we covered almost all of C,
and a large part of C++
We also introduced the MIPS processor and its
instructions
1. Cycle 1
Fetch instruction to which PC points
New PC = PC+4
2. Cycle 2
Decode instruction
Fetch $s2 from register file
3. Cycle 3
Execution: Add 8 to $s2
4. Cycle 4
Fetch data from address (8+$s2)
5. C l 5
Cycle
Write data back to register file location $s1
@HC Computation 5JJ70 pg 4
Pipelined MIPS
Control flow
Jump
Conditional Branch
Call & Return
C code: A = B + C + D;
E = F - A;
MIPS code:
d add $t0, $s1, $s2
add $s0, $t0, $s3
sub $s4,
, $s5,
, $s0
CPU Memory
register file
IO
@HC Computation 5JJ70 pg 8
Register allocation
Compiler tries to keep as many variables in registers as
possible: graph coloring
) stack
Example:
Program:
g Live Ranges
a b c d
a define
a :=
(def)
c :=
b :=
:= b
d :=
:= a
:= c
a consume := d
((cons))
Inf
Inference
n G Graph
ph
a
Coloring:
a = red b c
b = green
c = blue
d = green d
Graph
p needs 3 colors => program
p g needs 3 registers
g
12
16
20
24
Question:
If words are aligned, what are then the least 2
significant
i ifi t bit
bits of
f a word
d address?
dd ?
MIPS code:
d lw $t0, 32($s3)
add $t0, $s2, $t0
sw $t0,, 32($s3)
( )
Instruction
I t ti F Format:
t
op rs rt rd shamt funct
Question:
Can you guess what the field names stand for?
35 18 9 32
op rs rt 16 bit number
MIPS conditional
diti l branch
b h instructions:
i st ti s:
Example: if (i==j) h = i + j;
Example:
Example
F
Formats:
t
R op rs rt rd shamt funct
I op rs rt 16 bit address
J op 26 bit address
MIPS Instructions:
addi $29
$29, $29
$29, 4
slti $8, $18, 10
andi $29, $29, 6
ori $29,, $29,, 4
1010101010101010 0000000000000000
0000000000000000 1010101010101010
ori
1010101010101010 1010101010101010
F
Formats:
I op rs rt 16 bit address
J op 26 bit address
Questions:
Above addresses are not 32 bits !!
How do we extend them to 32 bits?
H
How do
d we hhandle
dl this
hi with
i h lload
d and
d store instructions?
i i
I op rs rt 16 bit address
Jump
p instructions just
j use high
g order (4)
( ) bits of PC
32-bit Jump address = PC[31..28} + Instr[25..0] + [00]
Address boundaries of 256 MB
@HC Computation 5JJ70 pg 24
Overview of MIPS
simple instructions, all 32 bits wide
very structured,
structured no unnecessary baggage
only three instruction formats
R op rs rt rd shamt funct
I op rs rt 16 bit address
J op 26 bit address
Arithmetic subtract sub $s1, $s2, $s3 $s1 = $s2 - $s3 Three operands; data in registers
add immediate addi $s1, $s2, 100 $s1 = $s2 + 100 Used to add constants
load word lw $s1, 100($s2) $s1 = Memory[$s2 + 100] Word from memory to register
store word sw $s1, 100($s2) Memory[$s2 + 100] = $s1 Word from register to memory
Data transfer load byte lb $s1
$s1, 100($s2) $s1 = Memory[$s2 + 100] Byte from memory to register
store byte sb $s1, 100($s2) Memory[$s2 + 100] = $s1 Byte from register to memory
load upper immediate lui $s1, 100 $s1 = 100 * 2
16 Loads constant in upper 16 bits
branch on equal beq $s1, $s2, 25 if ($s1 == $s2) go to Equal test; PC-relative branch
PC + 4 + 100
branch on not equal bne $s1, $s2, 25 if ($s1 != $s2) go to Not equal test; PC-relative
PC + 4 + 100
Conditional
b
branch
h sett on less
l than
th slt $s1,
$s1 $s2,
$s2 $s3 if ($s2 < $s3) $s1 = 1;
1 C
Compare lless th
than; ffor b
beq, b
bne
else $s1 =0
set less than slti $s1, $s2, 100 if ($s2 < 100) $s1 = 1; Compare less than constant
immediate else $s1 = 0
lwc1
Load and save fp words
swc1
@HC Computation 5JJ70 pg 27
Floating point instructions examples
add.s $f2, $f3, $f4
2 . R e g is te r a d d r e ss in g
op rs rt rd . .. fu n c t R e g is te rs
R e g is te r
3 . B a s e a d d r e s s in g
op rs rt A d dress M emory
R e g iste r + B y te H a lfw o r d W o rd
4 . P C -re la tive a d d re ss in g
op rs rt A d dress M emory
PC + W o rd
5 . P se u d o d ir e ct a d d r e s s in g
op A d d re ss M emory
PC W o rd
# calculate address of
Loop: muli $t1,$s3,4 # save[i]
add $t1,$t1,$s6
lw $t0,0($t1)
bne $t0
$t0,$s5,Exit
$s5 Exit sll $t1,$s3,2
add $s3,$s3,$s4
j Loop
Exit: Faster alternative
l i
switch (k) {
case 0: f=i+j; …. break;
case 1: ...............;
case 2: ...............;
;
case 3: ...............;
}
Assembler Code (see book CD for real MIPS code): jump table
sub $sp, $sp, 12 # adjust the stack to make room for 3 words
sw $t1
$t1, 8($
8($sp)
) # copy th
the argument
t i
in register
i t $t1 t
to stack
t k
sw $t0, 4($sp) # copy the argument in register $t0 to stack
sw $s0, 0($sp # copy the argument in register $s0 to stack
The special
p instruction jal jjumps
p to the subroutine (function),
( ) and
at the same time stores the return address in register $ra
The return address in $ra is the current value of the program
counter + 4.
jal my_function # store PC+4 in $a, jump to routine my_function
j $ra # return from function call
add $sp, $sp, 12 # adjust the stack pointer, freeing memory.
@HC Computation 5JJ70 pg 35
Using a Stack
subi $sp,$sp,8
mpty
sw $s0,4($sp)
, ( p)
em
sw $s1,0($sp)
$sp
filled
lw $s0,4($sp)
high address lw $s1,0($sp)
addi $sp,$sp,8
Assembler code:
Factorial: n! = n* (n-1)!
0! = 1
low address
$a0 = 0 Caller:
$ra = ...
100 addi $a0,$zero,2
$a0 = 1
$ra = ... 104 jal fact
$a0 = 2
$ra = 108
108 ....
$sp
filled
high address
Pointer version: