【发布时间】:2017-09-04 03:00:14
【问题描述】:
我正在尝试提示用户输入两个数字,将它们存储在变量 A 和 B 中,然后将变量相加并将总和保存到变量 S。我知道我可以通过将值直接保存到寄存器,但我需要知道如何使用变量本身来做到这一点。
这是我的代码
.data
prompt1: .asciiz "Enter an Integer A: /n"
prompt2: .asciiz "Enter another Integer B: /n"
response: .asciiz "The sum of A and B (A + B) is: "
A: .word
B: .word
S: .word
.text
#Prompt for input
li $v0, 4
la $a0, prompt1
syscall
#receive input
li $v0, 5
syscall
#Store input to A
sw $v0, A
syscall
#Prompt for second input
li $v0, 4
la $a0, prompt2
syscall
#receive input
li $v0, 5
syscall
#Store input to B
sw $v0, B
syscall
#Add A and B and store to register t0
add $s0, A, B
syscall
#Store total to variable S
move S, $t0
syscall
#Display response
li $v0, 4
la $a0, response
syscall
li $v0, 1
move $a0, S
syscall
【问题讨论】:
-
你不能。 ADD 指令将两个寄存器相加。另外我敢肯定你有很多不应该存在的系统调用
-
是的,
syscall不是您在每条指令后添加的内容。它是一条指令,具有特定目的(执行系统调用,例如控制台 I/O、动态内存分配等)。