2015-10-17 105 views
0

我需要减去用户输入的两个数字。这是我第一次在程序集中编写,这就是为什么代码可能不是很好,但我尽可能使它更清晰易读。减去两个输入数字

这里是我的代码:

.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 

我的问题是,我真的不知道,如果代码输入字段是因为在控制台正确I输入后马上显示为零,第二输入巨大的荒谬数字。此外,减法并没有显示出来。

这里是在最后一次运行控制台的一个例子:

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

的是哪里的问题可能是任何想法?

回答

1

您没有存储输入到变量中的数字。另外,系统调用print_int期望$a0中的编号,因此当您尝试从$a 1打印第二个编号时,它将不起作用。

+0

我看到了,谢谢:) – NickCh