2015-10-17 99 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