0% found this document useful (0 votes)
35 views3 pages

Sheet 5

Uploaded by

Maryem Raef
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views3 pages

Sheet 5

Uploaded by

Maryem Raef
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SHEET 5

1. Give the minimal sequence of MIPS instructions to perform the following


instruction.
ble $t5, $t3, L #if t5≤t3 go to L

sgt $s0, $t5, $t3 #if t5 > t3, s0=1


beq $s0, $zero, L

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?

sll $t0, $t0, 2


slt $t2, $t0, $zero

$t0= (0010 0000 0000 0000 0001 0100 0000 1110)


after shift:
$t0= (1000 0000 0000 0000 0101 0000 0011 1000)
because the most significant bit of $t0 is 1, $t0 contains a
negative number.
$t0 < 0
$t2 = 1
4. What is the MIPS assembly code to load this 32-bit constant into register s6?
0000 0001 1100 0101 0000 1010 0110 0011
We divide the 32-bit constant into 16-most significant bit and 16-
least significant bit.
16-most significant= 0000 0001 1100 0101 = (453) dec
16-least significant= 0000 1010 0110 0011 = (2659) dec
lui $s6, 453
ori $s6, $s6, 2659

5. Consider the following MIPS assembly code:


j L02
L01: add $t2, $t2, $t1
L02: blt $t2, $t3, L01

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?

Initially, $t1 = 2, $t2 = 1, and $t3 = 4


Code execution goes like this:
j L02
blt $t2, $t3, L01 # $t2 = 1 and $t3 = 4, so we branch to L01
add $t2, $t2, $t1 # $t2 += $t1, so $t2 = 3
blt $t2, $t3, L01 # $t2 = 3 and $t3 = 4, so we branch to L01
add $t2, $t2, $t1 # $t2 += $t1, so $t2 = 5
blt $t2, $t3, L01 # $t2 = 5, so we do not branch and execution of
this code stops
So, at the end $t2 = 5.

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}

addi $s1, $zero, 100 #i=100


loop: 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 $t4, 0($t2) #t4=a[i]
lw $t5, 0($t3) #t5=b[i]
ble $t5, $t4, end
sw $t5, 0($t2) #a[i]= b[i]
sll $t6, $s1, 3 #t6=8*i
add $t6, $t6, $s1 #t6=9*i
addi $t6, $t6, -7 #t6= 5*i-7
sw $t6, 0($t3) #b[i]= 5*i-7
addi $s1, $s1,-1 #i =i-1
j loop
end:

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:

You might also like