Conditional and Unconditional
Operation
The instructions to make
decisions are called branch instructions. There are two kind of branch
instructions: conditional instructions and unconditional instructions. Compared
with high-level programming languages, the conditional branch is similar to an if statement,
while unconditional branch (or just branch) to goto statement.
Conditional
branch
|
branch on equal
|
beq $1,$2,100
|
if ($1 == $2) go to
PC+4+100
|
Equal test; PC
relative branch
|
branch on not equal
|
bne $1,$2,100
|
if ($1 != $2) go to
PC+4+100
|
Not equal test; PC
relative
|
|
set on less than
|
slt $1,$2,$3
|
if ($2 < $3) $1
= 1; else $1 = 0
|
Compare less than;
2`s complement
|
|
set less than
immediate
|
slti $1,$2,100
|
if ($2 < 100) $1
= 1; else $1 = 0
|
Compare <
constant; 2`s complement
|
|
set less than
unsigned
|
sltu $1,$2,$3
|
if ($2 < $3) $1
= 1; else $1 = 0
|
Compare less than;
natural number
|
|
set less than
immediate unsigned
|
sltiu $1,$2,100
|
if ($2 < 100) $1
= 1; else $1 = 0
|
Compare constant;
natural number
|
Example :
Compiling an if statement
into a conditional branch
The if statement of C
language
f
|
g
|
h
|
i
|
j
|
$s0
|
$s1
|
$s2
|
$s3
|
$s4
|
if (i==j) f = g + h; else f = g - h;
can be compiled to
bne
$s3, $s4, Else # go to Else if
i!= j
add
$s0, $s1, $s2 # f = g + h
j
Exit # go to Exit
Exit :
lw
$t0, 0($t1) # $t0 = A[i]
add
$s1, $s1, $t0 # $s1 = g = g +
A[i]
add
$s3, $s3, $s4 # i = i + j
bne
$s3, $s2, ForLoop # go to ForLoop
if i != h
Compiling the while loop
i
|
j
|
k
|
A[]
|
$s3
|
$s4
|
$s5
|
$s6
|
while (A[i] == k) i = i + j;
can be compiled to :
Loop:
add
$t1, $s3, $s3
add
$t1, $t1, $t1 # $t1 = 4 * i
add
$t1, $t1, $s6
lw
$t0, 0($t1) # $t0 = A[i]
bne
$t0, $s5, Exit # go to Exit if
A[i] != k
add
$s3, $s3, $s4 # i = i + j
j
Loop # go to Loop
Exit:
Unconditional jump
|
jump
|
j 10000
|
goto 10000
|
Jump to target address
|
jump register
|
j $31
|
goto $31
|
For switch, procedure return
|
|
jump and link
|
jal 10000
|
$31 = PC + 4;
go to 10000
|
For procedure call
|
By : Ku Man Yi B031210161
0 comments:
Post a Comment