Documente Academic
Documente Profesional
Documente Cultură
Architecture
MIPS
Section 2.6-2.7,2.8,2.9
Instructions for Making Decisions
beq reg1, reg2, L1
Go to the statement labeled L1 if the value in reg1 equals the value in
reg2
bne reg1, reg2, L1
Go to the statement labeled L1 if the value in reg1 does not equals the
value in reg2
j L1
Unconditional jump
jr $t0
“jump register”. Jump to the instruction specified in register $t0
Conditional Branches
2
Making Decisions
Example
if ( a != b) goto L1; // x,y,z,a,b mapped to $s0-$s4
x = y + z;
L1 : x = x – a;
Reminder
Registers variable in C code $s0 ... $s7 $16 ... 23
Registers temporary variable $t0 ... $t7 $8 ... 15
Register $zero always 0
3
if-then-else
Example:
if ( a==b) x = y + z;
else x = y – z ;
4
Example: Loop with array index
Loop: g = g + A [i];
i = i + j;
if (i != h)goto Loop
....
$s1, $s2, $s3, $s4 = g, h, i, j, array A base = $s5
5
Loops
Example :
while ( A[i] == k ) // i,j,k in $s3. $s4, $s5
i = i + j; // A is in $s6
6
Other decisions
Set R1 on R2 less than R3
slt R1, R2, R3
Compares two registers, R2 and R3
R1 = 1 if R2 < R3 else
R1 = 0 if R2 >= R3
Example
slt $t1, $s1, $s2
Branch less than
Example: if(A < B) goto LESS
slt $t1, $s1, $s2 #t1 = 1 if A < B
bne $t1, $0, LESS
7
Switch statement
switch(k) {
case 0 : f = i + j; break;
case 1 : f = g + h; break;
case 2 : f = g – h; break;
case 3 : f = i – j; break;
}
f-k in $s0-$s5 and $t2 contains 4 (maximum of var k)
The switch statement can be converted into a big chain of if-
then-else statements.
A more efficient method is to use a jump address table of
addresses of alternative instruction sequences and the jr
instruction. Assume the table base address in $t4
8
Switch cont.
slt $t3, $s5, $zero # is k < 0
bne $t3, $zero, Exit # if k < 0, goto Exit
slt $t3, $s5, $t2 # is k < 4, here $t2=4
beq $t3, $zero, Exit # if k >=4 goto Exit
sll $t1, $s5, 2 # $t1 = 4 * k
add $t1, $t1, $t4 # $t1 = addr. Of $t4[k]
lw $t0, 0($t1) # $t0 = $t4[k]
jr $t0 # jump to addr. In $t0
# $t4[0]=&L0, $t4[1]=&L1, …,
L0 : add $s0, $s3, $s4 # f = i + j
j Exit
L1 : add $s0, $s1, $s2 # f = g + h
j Exit
L2 : sub $s0, $s1, $s2 # f = g – h
j Exit
L3 : sub $s0, $s1, $s2 # f = i – j
Exit :
9
Addresses in Branches and Jumps
Instructions:
bne $t4,$t5,Label Next instruction is at Label if $t4 ≠ $t5
beq $t4,$t5,Label Next instruction is at Label if $t4 = $t5
j Label Next instruction is at Label
Formats:
I op rs rt 16 bit address
J op 26 bit address
10
PC-relative addressing
Program Counter
Adressable space
relative to PC
11
Example
LOOP: mult $9, $19, $10 # R9 = R19*R10
lw $8, 1000($9) # R8 = @(R9+1000)
bne $8, $21, EXIT
add $19, $19, $20 #i = i + j
j LOOP
EXIT: ......
Assume LOOP is placed at location 80000
op rs rt
80000 0 19 10 9 0 24
80004 35 9 8 1000
80008 5 8 21 8
80012 0 19 20 19 0 32
80016 2 80000
80020 ...
12
Example
LOOP: mult $9, $19, $10 # R9 = R19*R10
lw $8, 1000($9) # R8 = @(R9+1000)
bne $8, $21, EXIT
add $19, $19, $20 #i = i + j
j LOOP
EXIT: ...
Assume LOOP is placed at location 80000
op rs rt
80000 0 19 10 9 0 24
80004 35 9 8 1000
80008 5 8 21 2
2
80012 0 19 20 19 0 32
80016 2 20000
20000
80020 ...
13
MIPS Addressing Modes
14
15
Load Upper Immediate
Example: lui $t0, 255
Transfers the immediate field into the register’s top 16 bits and
fills the register’s lower 16 bits with zeros
R8[31:16] <-- IR[15:0] ; top 16 bits of R8 <-- bottom 16 bits of the IR
R8[15:0] <-- 0; bottom 16 bits of R8 are zeroed
17
How about larger constants?
We'd like to be able to load a 32 bit constant into a register
Must use two instructions, new "load upper immediate" instruction
lui $t0, 1010101010101010
filled with zeros
1010101010101010 0000000000000000
1010101010101010 0010101010101010
18
Procedure calls
Procedures or subroutines:
Needed for structured programming
Steps followed in executing a procedure call:
Place parameters in a place where the procedure (callee) can access
them
Transfer control to the procedure
Acquire the storage resources needed for the procedure
Perform desired task
Place results in a place where the calling program (caller) can
access them
Return control to the point of origin
Leaf Procedures
19
Resources Involved
Registers used for procedure calling:
$a0 - $a3 : four argument registers in which to pass parameters
$v0 - $v1 : two value registers in which to return values
$ra : one return address register to return to the point of origin
Transferring the control to the callee:
jal ProcedureAddress
jump-and-link to the procedure address
the return address (PC+4) is saved in $ra
Example: jal 20000
Returning the control to the caller:
jr $ra
instruction following jal is executed next
20
Memory
Stacks
Useful for stacked environments/subroutine call & return even if
operand stack not part of architecture
Stacks that Grow Up vs. Stacks that Grow Down:
High address inf. Big 0 Little
Low address
21
Calling conventions
int func(int g, int h, int i, int j)
{ int f;
f = ( g + h ) – ( i + j ) ;
return ( f );
} // g,h,i,j - $a0,$a1,$a2,$a3, f in $s0
func :
addi $sp, $sp, -12 #make room in stack for 3 words
sw $t1, 8($sp) #save the regs we want to use
sw $t0, 4($sp)
sw $s0, 0($sp)
add $t0, $a0, $a1 #$t0 = g + h
add $t1, $a2, $a3 #$t1 = i + j
sub $s0, $t0, $t1 #$s0 has the result
add $v0, $s0, $zero #return reg $v0 has f
22
Calling (cont.)
lw $s0, 0($sp) # restore $s0
lw $t0, 4($sp) # restore $t0
lw $t1, 8($sp) # restore $t1
addi $sp, $sp, 12 # restore sp
jr $ra
23
Nested Calls
Stacking of Subroutine Calls & Returns and Environments:
A: A
CALL B
B: A B
CALL C
C: D: A B C
RET
A B
RET
A
24
Compiling a String Copy Proc.
void strcpy ( char x[ ], y[ ])
{ int i=0;
while ( x[ i ] = y [ i ] != 0)
i ++ ;
} // x and y base addr. are in $a0 and $a1
strcpy :
addi $sp, $sp, -4 # reserve 1 word space in stack
sw $s0, 0($sp) # save $s0
add $s0, $zer0, $zer0 # i = 0
L1 : add $t1, $a1, $s0 # addr. of y[ i ] in $t1
lb $t2, 0($t1) # $t2 = y[ i ]
add $t3, $a0, $s0 # addr. Of x[ i ] in $t3
sb $t2, 0($t3) # x[ i ] = y [ i ]
beq $t2, $zero, L2 # if y [ i ] = 0 goto L2
addi $s0, $s0, 1 # i ++
j L1 # go to L1
L2 : lw $s0, 0($sp) # restore $s0
addi $sp, $sp, 4 # restore $sp
jr $ra # return
25
Thank You