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

The program at the right is a simple loop that loads words starting at the locat

ID: 3825514 • Letter: T

Question

The program at the right is a simple loop that loads words starting at the location indicated in the program and tests them. For certain values of data, a word is output. When the program encounters a certain condition in the data, it stops. Please answer the following questions about the program: What data is the program outputting? From what memory location is the first data word loaded? What causes the program to halt? What does the program do when a word is not output? How many data words are output?

Explanation / Answer

Decoding the program line by line:

main: la $t0,p

This line loads the address location of p into register t0

loop: lw $a0,($t0)

loop label is here. This line stores the value present at Address present in t0 register to a0 register

beqz $a0,done

This line checks if the value in a0 register is equal to a0 or not. If yes, it jumps to the label done

bgtz $a0,next

This line checks if the value in a0 register is greater than or equal to zero or not. If yes, it jumps to label next

li $v0,1

Storing 1 in v0 register tells the system to print the integer present in $a0 register

syscall

performs the operation according to value present in v0 register. Here prints integer present in $a0 register

li $a0,0x0a

loading hex value into a0 register

li $v0,11

loading 11 into v0 tells the system to print character present in a0 register

syscall

Here it prints newline (ASCII value = 10 = 0x0a)

next: addi $t0,$t0,4

next label is situated here. incrementing t0 to next address location (offset from p)

j loop

returns to loop label

done: li $v0,10

loading 10 in v0 register tells system to end the program when syscall is called.

syscall

terminates the program

What does this code do?

This code iterates through the elements of an array stored in a memory location. It prints all the negative numbers present in this array, ignores the positive numbers. This code stops running when it finds a zero in the array.

3.1

This program prints any negative numbers stored in the array.

3.2

Data is loaded from the base address of Array p

3.3

Whenever the program finds a 0 in the array, it halts the program by calling done

3.4

If there are more elements in the array, the program just points to the next location. If it reaches the end of array, the program halts.

3.5

All the negative numbers present in the array are output.