CENG400 Assignment 2-Sol-V1
CENG400 Assignment 2-Sol-V1
Problem 1:
Add comments and compile the following program (ignore the last line)
Address Label Instruction Comments HEX
0x0040000C add $t0, $zero, Set t0 to 0 0x00004020
$zero
loop: beq $a0, $zero, end If a0 equals 0, jump to 0x10800003
end
addi $t0, $t0, 1 Increment t0 0x21080001
addi $a0, $a0, -1 Decrement a0 0x2084ffff
j loop Jump to loop 0x08100004
end: nop Ends program 0x00000000
Problem 2:
Write a MIPS program that counts, in $v0, the number of occurrences of 01 in the value stored
inregister $a0.
Example:
If the value stored in $a0 is 0x057A0152 then $v0 should be set to 8 because
0x057A0152 = 0000 0101 0111 1010 0000 0001 0101 0010
Solution:
# counter $v0
j loop
j loop
end: j end
Problem 3:
Consider the following MIPS program
Solution:
addi $s0, $zero, 0x10010000 # store A address into s0
addi $s1, $zero, 0x10010100 # store B address into s1
addi $t0, $zero, 0 # i=0
addi $t1, $zero, 0 # j=0
add $t2, $zero, $s0 # t2=A used for first iteration
add $t3, $zero, $s1 # t3=B
loop: sw $t0, 0($t2) # A[i] = i
sw $t1, 0($t3) # B[j] = j
addi $t0, $t0, 1 # i=i+1
addi $t1, $t1, -1 # j=j-1, no subi
# shift should be done next so that we don't overwrite in the last iteration
sll $t2, $t0, 2 # t2 = i*4
sll $t3, $t1, 2 # t3 = j*4
add $t2, $s0, $t2 # t2 = A+i*4
add $t3, $s1, $t3 # t3 = B+j*4
bne $t2, $t3, loop # stopping criteria
nop