我需要减去用户输入的两个数字。这是我第一次在汇编中写道,为什么代码可能一点也不好,但我尽量让它尽可能清晰。
这是我的代码:
.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
关于问题可能在哪里的任何想法?
答案 0 :(得分:1)
您没有存储在变量中输入的数字。此外,print_int
系统调用期望$a0
中的号码,因此当您尝试从$a
1打印第二个号码时,它将无法正常工作。