减去两个输入数字

Subtract two input numbers

我需要减去用户输入的两个数字。这是我第一次用汇编编写代码,这就是为什么代码可能一点也不好,但我尽量让它尽可能清晰。

这是我的代码:

.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

我的问题是我不太确定代码的输入字段是否正确,因为在我输入后的控制台中它显示为零,而第二个输入是一个巨大的荒谬数字。减法也根本不显示。

这里是最后运行的控制台示例:

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

知道问题出在哪里吗?

您没有将输入的数字存储在变量中。此外,print_int 系统调用需要 $a0 中的数字,因此当您尝试从 $a1 打印第二个数字时,它不会工作。