用 MIPS 汇编语言读取和打印整数

问题描述 投票:0回答:2

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

integer mips mars-simulator spim
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

这不是系统调用 5 的工作原理。整数在

$v0
中返回,因此代码应该类似于:

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

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

您在这里也使用了错误的寄存器。要打印的整数应输入

$a0
,而不是
$t0

这里是系统调用及其使用的寄存器的列表

© www.soinside.com 2019 - 2024. All rights reserved.