You are on page 1of 3

Jump instruction format can reach any place within boundary of 256 MB = (228)10

but the destination is (230)10, so it cannot be used.


Beq is immediate-type format, so max. immediate can be written is (217)10, but
here the immediate is (4000 0000)16-(2000 0000)16 = (2000 0000)16 = (229)10, so it
beq cannot be used here.

I-format as it will take a register name as well as the branch address.

loop: slt $t0,$zero,$t2


beq $t0,$zero,Exit
addi $t2,$t2,-1
j loop
Exit:

add $t0,$zero,$zero
Loop: slt $t2,$t0,$s0
beq $t2,$zero,Exit
add $t1,$zero,$zero
Loopa: slt $t3,$t1,$s1
beq $t3,$zero,Exita
sll $t4,$t1,4
add $t4,$t4,$s2
add $t5,$t0,$t1
sw $t5,0($t4)
addi $t1,$t1,1
j loopa
addi $t0,$t0,1
Exita: j loop
Exit:

There are 14 static code instructions.


Dynamic code instructions = 10*1*14 = 140

addi $t1, $0, $0 #initialize i=0


LOOP: lw $s1, 0($s0) #get MemArray[$s0]
add $s2, $s2, $s1 #result += MemArray[$s0]
addi $s0, $s0, 4 #$s0 += 4
addi $t1, $t1, 1 #i++
slti $t2, $t1, 100 #check if(i<100)then $t2=1
bne $t2, $s0, LOOP #branch if $t2 != $s0
Then the C code could be:
int i=0 , temp;
do { result+= MemArray[i];
i++;
if(i<100) temp=1;
else temp=0;
}
while(temp!= &MemArray[i])

First, it is noticed that the goal is to get the sum of the array elements, but the loop
above does not do this job, instead, it is an infinite loop as the address of
MemArray[i] cannot equal i at any iteration. Then, the branch should occur
when i<100 (i.e. when $t2 != 0).
Note that we want to reduce dynamic code size not the static one.
addi $t1, $0, $0 #initialize i=0
LOOP: lw $s1, 0($s0) #get MemArray[$s0]
add $s2, $s2, $s1 #result += MemArray[$s0]
addi $s0, $s0, 4 #$s0 += 4
addi $t1, $t1, 1 #i++
slti $t2, $t1, 100 #check if(i<100)then $t2=1
bne $t2, $zero, LOOP #branch if $t2 != zero

The destination is (20 01 49 24)16.


The destination is more than 256 MB, so single jump cannot be used.

It cannot be used as the difference is more than (217)10.

It can be used as the difference is (88,356)10 & it can represent (131072)10.

You might also like