2013-11-02 146 views
2

我的程序假設讀取一個整數並將其打印回給用戶,但每次只打印輸入2685。任何幫助,將不勝感激。以mips讀取並打印整數

.data 
prompt2: .asciiz "Please enter value: " 
array1: .space 40 
array2: .space 40 
buffer: .space 4 
.text 

main: 

#Prints the prompt2 string 
li $v0, 4 
la $a0, prompt2 
syscall 

#reads one integer from user and saves in t0 
li $v0, 5 
la $t0, buffer 
syscall 

li $v0, 1  
li $t0, 5  # $integer to print 
syscall   

exitProgram: li $v0, 10 # system call to 
    syscall   # terminate program 

回答

5
#reads one integer from user and saves in t0 
li $v0, 5 
la $t0, buffer 
syscall 

這不是如何系統調用的5部作品。整數返回在$v0,所以代碼應該是這樣的:

li $v0,5 
syscall 
move $t0,$v0 

li $v0, 1  
li $t0, 5  # $integer to print 
syscall 

你在這裏使用了錯誤的寄存器爲好。要打印的整數應該寫入$a0,而不是$t0

這是a list of syscalls and the registers they use

3

這是我怎麼會寫一個程序來得到一個整數輸入並打印出來

.data 

    text: .asciiz "Enter a number: " 

.text 

main: 
    # Printing out the text 
    li $v0, 4 
    la $a0, text 
    syscall 

    # Getting user input 
    li $v0, 5 
    syscall 

    # Moving the integer input to another register 
    move $t0, $v0 

    # Printing out the number 
    li $v0, 1 
    move $a0, $t0 
    syscall 

    # End Program 
    li $v0, 10 
    syscall