2. The following program tries to copy words from the address inregister $a0 to
ID: 3609781 • Letter: 2
Question
2. The following program tries to copy words from the address inregister $a0 to the
address in register $a1, counting the number of words copied inregister $v0. The program
stops copying when it finds a word equal to 0. This terminatingword should be copied but
not counted. You do not have to preserve the contents ofregisters $v1, $a0, and $a1.
loop: lw $v1, 0($a0) # Read next wordfrom source
addi $v0, $v0, 1 # Increment countwords copied
sw $v1, 0($a1) # Write todestination
addi $a0, $a0, 1 # Increment pointerto next source
addi $a1, $a1, 1 # Increment pointerto next destination
bne $v1, $zero, loop # Loop if wordcopied is different from 0
There are multiple bugs in this MIPS program. Fix them and turnin a bug-free version. You
may want to use SPIM to check your program.
Explanation / Answer
main: la $a0, d1 #loadaddress of data
la $a1, d2 #loadaddress of where being put
li $v0, -1 #initialize counter at -1 so will finish not counting final move
loop: lw $v1,0($a0) # Read next word fromsource
addi $v0, $v0,1 # Increment count words copied
sw $v1,0($a1) # Write to destination
addi $a0, $a0,4 #Increment pointer to next source (add 4since a word is 4 bytes)
addi $a1, $a1,4 #Increment pointer to next destination (add 4 since a word is 4 bytes)
bne $v1,$zero, loop # Loop if word copied is different from0
.align 4
.data
d1: .word 8,4,2,0 #data to be moved
d2: .word 1,1,1,1 #data to be moved to
#when finished d2=d1 and v0 has count of how many words moved inthis case 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.