Sheet 5
Sheet 5
2. Give the minimal sequence of MIPS instructions to perform the following branch
instruction:
MBLE $rs, $rt, label branch to label if M[R[$rs]] <= rt
The instruction is similar to normal branch instructions BEQ and BNE except that it compares
a value in memory to a register instead of comparing two registers.
lw $t0, 0($rs)
Sgt $s0, $t0, $rt
beq $s0, $zero, label
.
.
Label:
3. Suppose that $t0 contains 2000140E hex. What is the value of $t2 after executing
the following instruction sequence?
Assume that, immediately before executing the above code, the contents of $t1=2 and the
contents of $t2=1, and $t3=4. What is the value of $t2 after the above assembly code
finishes?
6. For the following C statements, write the corresponding MIPS assembly code. Do
not use Mul instruction. COMMENT YOUR CODE. Assume that variable i is
stored in registers $s1. Arrays a and b are arrays of words with base addresses in
$t7 and $t8, respectively.
for (i=100; b[i]>a[i]; i--)
{a[i]=b[i]
b[i]= 9*i-7}
Similar: Assume that variable i is stored in register $s1. Arrays a, b, and c are arrays of words
with base addresses in $t7, $t8 and $t0, respectively. For the following C statements, write the
corresponding MIPS assembly code:
for (i=0; b[i]>a[i]; i++)
c[i] = b[i] – a[i]
Arrays a, b, and c are arravs of words of length 50 with base addresses in $t7,
$t8 and t0 respectively. Write the corresponding MIPS assembly code that calculates each
element in array c as the sum of the corresponding elements in arrays a and b.
the problem can be written in high level language as:
for(i=0; i<50; i++)
c[i]=a[i]+b[i]
addi $s1, $zero, 50 #i=50
loop: beq $s1, $0, end #if i=0 go to end
sll $t1, $s1, 2 #t1=4*i
add $t2, $t7, $t1 #t2= address of a[i]
add $t3, $t8, $t1 #t3= address of b[i]
lw $t2, 0($t2) #t2=a[i]
lw $t3, 0($t3) #t3=b[i]
add $t4, $t2, $t3 #t4=a[i]+b[i]
add $t5, $t0, $t1 #t5= address of c[i]
sw $t4, 0($t5) #c[i]=a[i]+b[i]
addi $s1, $s1,-1 #i =i-1
j loop
end: