Как бы вы go решили эту проблему в псевдокоде языка высокого уровня? Один из способов -
while true
print("Enter a number: ")
n = read_a_number()
if n < 0
print("Error. Negative number")
else
# process the non-negative number (>= 0)
# detect when to break while loop
Как только вышеприведенное становится понятным, его довольно легко перевести в MIPS.
.data
prompt: .asciiz "Enter a number: "
error: .asciiz "Error. Negative number\n"
.text
while:
la $a0, prompt # print prompt to enter a number
li $v0, 4 # $v0 = 4 to print string with address in $a0
syscall
li $v0, 5 # to read an int. Read in $v0
syscall
bltz $v0, negative # is the input negative?
j non_negative # else, handle non_negative
negative:
la $a0, error # print error
li $v0, 4
syscall
j next # and goto 'next'
non_negative:
# process the non-negative number (>= 0)
j next # and goto 'next'
next:
# detect when to break while loop (or do that at the start of the loop)
j while # continue loop
after_while: # after the loop
li $v0, 10 # $v0=10 to exit
syscall