0

I need to subtract two numbers that the user will input. It is my first time writing in assembly that's why the code might not be at all great but I tried to make it as legible as possible.

Here is my code:

.data                                                                                       # Program data are placed below the .data directive
num1: .word 0                                                                               # First integer variable, initialized to 0
num2: .word 0                                                                               # Second integer variable, initialized to 0
subs: .word 0                                                                               # Variable for storing the sum of the two integers
str:        .asciiz "Name: Name Surname\nA.M: Somenumber\n"                             # Storing string in variable str
str1:       .asciiz "Enter first integer: "                                                 #
str2:       .asciiz "Enter second integer: "                                                #
finalStr:   .asciiz "The result of the substraction is "                                    #

.text                                                                                       # Program is placed under the .text directive

main:                                                                                       # Standard label in QtSpim for the main program. It should be always used

li $v0, 4     
la $a0, str                                                                                 # Store string   
syscall                                                                                     # Use this MIPS command to execute a system call

li $v0, 4     
la $a0, str1                                                                                # Store string   
syscall                                                                                     # Use this MIPS command to execute a system call


li $v0,5                                                                                    # Read integer
syscall                                                                                     # Invoke the operating system.

li $v0,1                                                                                    # Print integer
lw $a0,num1                                                                                 # Load the integer
syscall                                                                                     # Invoke the operating system.



li $v0, 4     
la $a0, str2                                                                                # Store string   
syscall                                                                                     # Use this MIPS command to execute a system call


li $v0,5                                                                                    # Read integer
syscall                                                                                     # Invoke the operating system.

li $v0,1                                                                                    # Print integer
lw $a1,num2                                                                                 # Load the integer
syscall                                                                                     # Invoke the operating system.

sub $s0, $a1, $a0                                                                           # Substraction 

sw $s0, subs                                                                                # Store the difference in memory (in variable subs)

la $a0, finalStr                                                                            # To print a string, first its address should be stored to register $a0
li $v0, 4                                                                                   # System call value for print_string.
syscall                                                                                     # Use this MIPS command to execute a system call

move $a0, $s0                                                                               # To print an integer, it should be first stored to register $a0
li $v0, 1                                                                                   # System call value for print_int

li $v0,10
syscall

My problem is that I'm not really sure if the input field of code is correct since in console right after I Input it shows a zero and the second input a huge ridiculous number. Also the subtraction doesn't get shown at all.

Here is an example of the console in the last run:

Name: Name Surname
A.M: Somenumber
Enter first integer: 5
0Enter second integer: 10
268501072The result of the substraction is 

Any ideas of where the problem might be?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
NickCh
  • 5
  • 1
  • 7

1 Answers1

1

You didn't store the numbers entered in the variables. Also, the print_int syscall expects number in $a0 so when you try to print the second number from $a1 it won't work.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Jester
  • 56,577
  • 4
  • 81
  • 125