简体   繁体   中英

MARS MIPS Which Branch to use to check if numbers are evenly divisible

.data
prompt: .asciiz "Enter your first number:"
prompt2: .asciiz "Enter your second number:"
prompt3: .asciiz "Your Number was evenly divisible by the second number!"

.text

main:
li $s0, 0   #Sets $s0 0
li $v0, 4   #Get user input
la $a0, prompt  #Displays "Enter your first number:"
syscall

li $v0, 5   #Gets users integer
syscall
move $t0, $v0   #Stores user input to $t0

li $v0 4    #Get user input
la $a0 prompt2  #Display prompt 2
syscall

li $v0 5    #Get int from user
syscall
move $t1, $v0

divu $t0, $t1  #Divides $t0/$t1
mfhi $t3
bnez $t3, exit



exit:
li $v0 10
syscall

The program I am trying to write is supposed to take two integer inputs from the user and divide them. If the number is evenly divisible by the second number then I will display "Prompt3:".

I have written most of it and it all seemed to work out, but I am having trouble with the conditional statement.

You are using the wrong pseudoinstruction ( div/3 ).

You should use div or divu then mfhi to obtain the remainder of the integer division and then test that remainder to see if it's zero or not. Eg:

divu $t0, $t1
mfhi $t3
bnez $t3, exit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM