We have not studied branch instructions yet, but they are actually quite easy to
ID: 3674535 • Letter: W
Question
We have not studied branch instructions yet, but they are actually quite easy to understand. In a branch instruction, a test is made To a certain condition, normally comparing two registers or perhaps determining the value of a single register. Consider the instruction "branch if less than zero:" An example is bgez $t1, next. What this branch instruction instructs the computer to do Is to evaluate the contents of the register $t1. If the contents are greater than or equal to 0 (greater than equal to 0), then the computer executes the instruction labeled "next" immediately after the branch. That instruction may be anywhere in the program, but if the contents of $t1 are greater than equal to 0, the computer immediately finds the instruction labeled "next" and executes it, then continues on from that point, executing instructions sequentially. If the contents of $t1 are less than zero, the computer simply executes the instruction following the branch instruction. Let's use that simple branch instruction to do the following: Load each of the two words declared in the data statement shown to the right roam, into registers, and then determine if either is greater than equal to 0. If either number is greater than equal to 0, print it out using syscall 1. If a number is negative, do not print it out. End the program with a syscall 10. Note: you can declare words as either decimal or hexadecimal. SPIM understands both number systems. If only one number is printed out, which number is it?Explanation / Answer
The code will be as follows:
text
.globl main
main:
li $v0, 4
la $a0, prompt
syscall
li $t0, 0 #count for the loop to get two integers
getnum:
li $v1, 5 #read integer
syscall
addi $t0, $t0, 1 #increment the counter
ble $t0, 2, getnum
printnum:
bgez $v0,syscall1
bgez $v1,syscall1 #load address of num to print
li $v0, 10
syscall
.data
data1: .word 0x63f2e3
data2: .word 0x9100ab72
prompt:
.asciiz "Enter 2 numbers:"
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.