在MIPS汇编语言中,读取和打印一个整数。

11

我的程序应该读取一个整数并将其打印回用户,但每次它只打印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个回答

21

这是我如何编写一个程序来获取整数输入并将其打印出来

.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

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

那不是syscall 5的工作方式。整数返回在 $v0 中,所以代码应该是这样的:
li $v0,5
syscall
move $t0,$v0

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

这里也使用了错误的寄存器。应该将要打印的整数放入$a0,而非$t0

这是系统调用列表以及它们使用的寄存器


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接