以mips读取和打印整数

时间:2013-11-02 23:22:19

标签: integer mips

我的程序假设读取一个整数并将其打印回用户,但无论输入什么,每次只打印268501230。任何帮助将不胜感激。

.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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:4)

这是我编写程序以获取整数输入并将其打印出来的方法

.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