Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following program tries to copy words from the address in register $a0 to th

ID: 3882920 • Letter: T

Question

The following program tries to copy words from the address in register $a0 to the address in register $a1, counting the number of words copied in register $v0. The program stops copying when it finds a word equal to 0. You do not have to preserve the contents of registers $v1, $a0, and $a1. This terminating word should be copied but not counted. addi $v0. $zero, 0 # Initialize count loop: lw $v1, 0 ($a0) # Read next word from source sw $v1, 0 ($a1) # Write to destination addi $a0, $a0, 4 # Advance pointer to next source # addi $a1, $a1, 4 #Advance pointer to next destination beq $v1, $zero, loop # Loop if word copied ! = zero There are multiple bugs in this MIPS program: fix them and turn in a bug-free version.

Explanation / Answer

Ans::

The multiple bugs to be fixed are the following::

1.Bug-1 : Count '$v0' is initialized to 0., not to –1 to avoiding counting the 0 word.

2.Bug-2 : count '$v0' isn't incremented.

3.Bug-3 : Loops if the worrd copied is equals to 0 rathr than not-equals.

=> The Bug free version is as below:-

For clear understand I put line numbers.

First one is $vo init to -1 in line 1.

1. addi $v0, $zero, -1 # Initializing to avoid countng 0 word.

2. loop:: lw, $v1, 0($a0) # Reading the next word frm the source.

3. addi $v0, $v0, 1 # Incrementing the count-word copied.

4. sw $v1, 0($a1) # Writing it to the destintion.

5. addi $a0, $a0, 4 # Advnce-pointer to the next-source.

6. addi $a1, $a1, 4 # Advnce-pointer to the next-destination.

7. bne $v1, $zero, loop # Looping if the word copied not equals ( != ) to 0.

// Thank you //