【问题标题】:How to add two Integer variables in MIPS如何在 MIPS 中添加两个整数变量
【发布时间】: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、动态内存分配等)。

标签: variables mips add


【解决方案1】:

这就是你应该为你想要的输出编写代码的方式:

    .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

【讨论】:

    猜你喜欢
    • 2021-03-31
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多