Asking user to input two numbers, store them in memory, doing addition and substraction and display both result

0

I'm new to MIPS, and I'm working on this question, my code wold assemble, but after I hit the run buttom and entered the first number, it would show a runtime exception: Runtime exception at 0x00400018: address out of range 0x00000000

I don't understand what's happening to my code, can anyone help me?

    .data
str1:   .asciiz "Enter number 1: " 

str2:   .asciiz "Enter number 2: " 

str3:   .asciiz "The sum of the two number is: "

str4:   .asciiz "The difference of the two number is: "

num1:   .word 4

num2:   .word 4

    .text
    .globl main
main:

la  $t0, str1

li  $v0, 4      #syscall code for print_str

la  $a0, ($t0)  #address of string to print

syscall         #print str1

la  $s0, num1

li  $v0, 5      #syscall code for read int 

syscall         #read int

sw  $s0, ($v0)  #store the enterd value in num1

    la  $t1, str2
    li  $v0, 4      #syscall for print_str
    la  $a0, ($t1)      #address of string to print
    syscall         #print str2
    la  $s1, num2
    li  $v0, 5      #syscall code for read int
    syscall         #read int
    sw  $s1, ($v0)  #store the entered value in num2

    add $s2, $s0, $s1   #add num1 and num2

    la  $t2, str3
    li  $v0, 4      #syscall for print_str
    la  $a0, ($t2)      #address of str3
    la  $a1, ($s2)      #address of the sum
    syscall         #print str3 and the sum

    sub $s3, $s0, $s1   #substract num1 and num2    ERROR COULD BE HERE

    la  $t3, str4
    li  $v0, 4      #syscall for print_str
    la  $a0, ($t3)      #address of str4
    la  $a1, ($s3)      #address of the difference
    syscall         #print str4 and the difference
    li  $v0, 10     #exit
    syscall

Error in C:\Users\Desktop\exercise2.s line 18: Runtime exception at 0x00400018: address out of range 0x00000000

Go: execution terminated with errors.

mips
asked on Stack Overflow May 28, 2019 by Subjectivist_Fallacy • edited May 28, 2019 by Michael

1 Answer

0

Many problems in your code.

la $a0, ($t0) is incorrect. The assembler should warn you. la is a two instructions macro that` loads an address from a label, while $t0 is a register (that already holds the value of str1).

It should be replaced by la $a0, str1 or (much better because it is a a unique instruction) mov $a0, $t1 as you already did this computation for $t1. You have the same problem for str2, str3 and str4. But there is no need to use a temporary register and you could directly do the computation in $a0.

There is another problem. According to documentation after syscall 5 "$v0 contains integer read". So the sw $s0, ($v0) after reading the ints are incorrect. What should be stored is $v0 and the store address is num1 and has already been loaded in $s0.

add $s2, $s1, $s0 is also incorrect, as $s0 and $s1 values are integer addresses, not integer values.

Last, you cannot print at once a string and an integer. It must be done in two syscalls : syscall 4 to print a string and syscall 1 to print the integer.

   .data
str1:   .asciiz "Enter number 1: " 
str2:   .asciiz "Enter number 2: " 
str3:   .asciiz "The sum of the two number is: "
str4:   .asciiz "The difference of the two number is: "
num1:   .word 4
num2:   .word 4

    .text
    .globl main
main:
    la  $a0, str1
    li  $v0, 4      #syscall code for print_str
    syscall         #print str1

    li  $v0, 5      #syscall code for read int
    syscall         #read int
    mov $s0, $v0    ## keep the value of int in a register
    la  $t1, num2
    sw  $v0, ($t1)  #store the entered value in num2


    li  $v0, 4      #syscall for print_str
    la  $a0, str2      #address of string to print
    syscall         #print str2

    li  $v0, 5      #syscall code for read int
    syscall         #read int
    mov $s1, $v0    ## keep the value of int in a register
    la  $t1, num2
    sw  $v0, ($t1)  #store the entered value in num2

    add $s2, $s0, $s1   #add num1 and num2

    la  $a0, str3
    li  $v0, 4      #syscall for print_str
    syscall         #print str3
    li  $v0, 1      #syscall for print integer
    mov $a0, $s2    #value of the sum
    syscall         #print sum

    sub $s3, $t0, $t1   #substract num1 and num2

    la  $a0, str4
    li  $v0, 4      #syscall for print_str
    syscall         #print str4
    li  $v0, 1      #syscall for print integer
    mov $a0, $s3    #value of the diff
    syscall         #print diff value

BTW, storing the integers is useless. I kept them because it is in the title of the question, but it you just need the sum and diff, the sw instructions and corresponding la can be suppressed.

answered on Stack Overflow May 28, 2019 by Alain Merigot

User contributions licensed under CC BY-SA 3.0