I'm trying to load this file but QtSpim stops responding when doing so
# Description : This program reads two user inserted integers and asks the
# user which is their greatest common divisor.
# If the user answers correctly the program congratulates the user, if
# not, the user is asked to try again.
.text
.globl _start
_start:
la $a0, str1 # loads str1 address to $a0
li $v0, 4 # loads $v0 with the contents of $a0 to print it
syscall # prints str1
li $v0, 5 # reads the first user inserted integer from the console and
# stores it to $v0
add $a1, $v0, $zero # stores the first integer from $v0 to $a1
syscall # executes the previous commands
la $a0, str2 # loads str2 address to $a0
li $v1, 4 # loads $v1 with the contents of $a0 to print it
syscall # prints str2
li $v1, 5 # reads the second user inserted integer from the console
# and stores it to $v1
add $a2, $v1, $zero # stores the second integer from $v1 to $a2
syscall # executes the previous commands
la $a0, str3 # loads str3 address to $a0
li $v0, 4 # loads $v0 with the contents of $a0 to print it
syscall # prints str3
jal gcd # calls the method named gcd
syscall
gcd:
# y = a % b;
rem $a3, $a1, $a2 # stores the remainder of the division of the
# first integer with the second to $a3
# while (y != 0) {
# a = b;
# b = y;
# y = a % b;
# }
again:
# y = 0; break;
beq $a3, $zero goto exit # if the contents of $a3 equal zero,
# go to exit
syscall
# a = b;
add $a1, $a2, $zero # stores the contents of the 2nd integer to
# the register of the 1st
syscall
# b = y;
add $a2, $a3, $zero # stores the contentnts of y to the register
# of the 2nd integer
syscall
# y = a % b;
rem $a3, $a1, $a2 # stores the remainder of the division of
# the first integer with the second to $a3
j again # jumps to again to do the next iteration of the
# loop
syscall
exit:
jal printQuestion # calls the method named printQuestion
syscall
printQuestion:
loop:
li $v0, 5 # reads the user's answer from the console and
# stores it to $v0
add $s0, $v0, $zero # stores the user's answer from $v0 to $s0
syscall #executes the previous commands
# s = b;
beq $s0, $a2 goto end # if the contents of $s0 are equal with
# the contents of $a2
la $a0, str5 # loads str5 address to $a0
li $v1, 4 # loads $v1 with the contents of $a0 to print it
syscall # prints str5
la $a0, str6 # loads str6 address to $a0
li $v1, 4 # loads $v1 with the contents of $a0 to print it
syscall # prints str6
j loop # jumps to loop
end:
la $a0, str4 # loads str4 address to $a0
li $v1, 4 # loads $v0 with the contents of $a0 to print it
syscall # prints str4
.data
str1: .asciiz "Please insert the first integer : \n"
str2: .asciiz "Please insert the second integer : \n"
str3: .asciiz "Which is the GCD of the first and second integer? \n"
str4: .asciiz "Congratulations! \n"
str5: .asciiz "Wrong answer. Please try again. \n"
str6: .asciiz "Which is the GCD? \n"
However, when a different file (that I copy pasted to see if it works) does load and I try to run it, I'll get these errors: Exception occurred at PC=0x00000000
Bad address in text read: 0x00000000
Attempt to execute non-instruction at 0x80000180
Is it my code's fault or is something else wrong?
Edit: I'm using the Simple Machine setting.
Spim expects the user code to start at label main, so without it, it will error when running the code.
So you need to change the _start to main in the label and .global.
The beq $a3, $zero goto exit used in two places in invalid as the beq command expects a comma after the $zeroo, and the label to go to - not 'goto label'
You also have a number of syscall statements, but don't set the values v0 value - you are assuming its still the same, or forgot to do it ? Also in some places it looks like you are seeting v1 when you probally meant to set v0 for the syscall.
For example:
la $a0, str1 # loads str1 address to $a0
li $v0, 4 # loads $v0 with the contents of $a0 to print it
syscall # prints str1
Is setting v0 to 4, getting ready for the print string system, which expects the string it will print in a0 (which you set up)
The next lines:
li $v0, 5 # reads the first user inserted integer from the console and
# stores it to $v0
add $a1, $v0, $zero # stores the first integer from $v0 to $a1
syscall # executes the previous commands
Setting v0 to 5 is getting ready for the reading systecall - not sure what add a1, v0 is supposed to be doing, but after the syscall,, v0 will hold the read value. that now needs be stored somewhere.
Next lines:
la $a0, str2 # loads str2 address to $a0
li $v1, 4 # loads $v1 with the contents of $a0 to print it
syscall # prints str2
You want to print str2 similar to how you printed atr1, so a0 is set to the address str2 (which toy did), v0 needs to be 4 (Not v1)
There are repeated instances of that throught the code, as well as places where you do a syscall without setting v0 at all.
User contributions licensed under CC BY-SA 3.0