如何在MIPS中添加两个Integer变量

时间:2017-09-04 03:00:15

标签: variables mips add

我尝试提示用户输入两个数字,将它们存储在变量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

1 个答案:

答案 0 :(得分:0)

这是您应该如何为所需的输出编写代码:

    .data
    msg1: .asciiz "Enter the first number: "
    msg2: .asciiz "\nEnter the second number: "
    result: .asciiz "\nThe result of addition is: "

    .text
    li $v0,4
    la $a0,msg1
    syscall

    li $v0,5
    syscall
    move $t1,$v0

    li $v0,4
    la $a0,msg2
    syscall

    li $v0,5
    syscall
    move $t2,$v0

    Add $t3,$t1,$t2

    li $v0,4
    la $a0,msg3
    syscall

    li $v0,1
    move $a0,$t3
    syscall

    li $v0,10
    syscall