Mips:计算两个输入的总和

时间:2012-11-27 03:08:17

标签: assembly mips

看起来很简单,但我认为我的程序无法编译,因为我正在覆盖$ v0寄存器?提前致谢

更新:没关系得到它,当我打印系统来打印总和时,我的订单错了... 修复以防任何人需要参考。

.data
prompt1: .asciiz "\n\n Enter the first integer please:"
prompt2: .asciiz "Enter the second integer please:"
result: .asciiz "The result is:"

.text

main:

    #t0-to hold first integer
    #t1-to hold second integer
    #t2- used to hold the sum of t$1 and t$2

        #first number

    li $v0, 4 #syscall to print string
        la $a0, prompt1  #address of string to print
        syscall

        li $v0, 5 #syscall to read an integer
        syscall
        move $t0, $v0  #move the number to read into $t0

    #second number
    li $v0, 4
    la $a0, prompt2
    syscall

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

        add $t2, $t1, $t0 #compute the sum

    #print out sum of $t2
    li $v0, 4       # load syscall print int into $v0
    move $a0, $t2   #move the number to print into $a0
    li, $v0,1
    la, $a0, result
    syscall


Done:

    li $v0, 10    #syscall to exit
        syscall

3 个答案:

答案 0 :(得分:5)

.data
prompt1: .asciiz "\n\n Enter the first integer please:"
prompt2: .asciiz "Enter the second integer please:"
result: .asciiz "The result is:"

              .text
main:
    #t0-to hold first integer
    #t1-to hold second integer
    #t2- used to hold the sum of t$1 and t$2
        #first number
        li $v0, 4 #syscall to print string
        la $a0, prompt1  #address of string to print
        syscall
#
        li $v0, 5 #syscall to read an integer
        syscall
        move $t0, $v0  #move the number to read into $t0
    #second number
    li $v0, 4
    la $a0, prompt2
    syscall
#
    li $v0,5        
    syscall
    move $t1,$v0
#
    #print out sum of $t2
    li $v0, 4
    la $a0, result
    syscall
#
    add $a0, $t1, $t0 #compute the sum
    li $v0, 1
    syscall
#
    li $v0, 10
    syscall

答案 1 :(得分:0)

实际上你在这部分搞砸了:

#print out sum of $t2 li $v0, 4 # load syscall print int into $v0 move $a0, $t2 #move the number to print into $a0 li, $v0,1 la, $a0, result syscall

让我们一行一行:
li $v0, 4 #You are ready to indicate that you want to print string
la $a0, result #you have loaded the address of string now
syscall # Now you will see: "The result is:

现在让我们打印数字
li $v0, 1
move $a0, $t2
syscall

脚注:每当您指示您将使用系统调用时,您应该首先完成该命令。 例如,如果要打印某些内容,则必须完成该集的系统调用,然后继续打印整数。

例如: 让我们说我们想要打印这样的东西。 数量:1

MIPS代码将是:
.data prompt: .asciiz "Number : " .text main: #Now lets read a number li $v0, 5 syscall # -> note syscall is done for each instruction #Lets complete printing first li $v0, 4 la $a0, prompt syscall

#我们现在有$ t0的数字    #if我们这样做    li $v0, 1 la $a0, prompt syscall
   #ask问答自己。什么将被打印。    #the我们输入的数字或提示的地址    #你需要在这里修改什么?

答案 2 :(得分:0)

.text
main:
    #print the msg1
    li $v0 4
    la $a0 msg1
    syscall 

    #read the num 1
    li $v0 5
    syscall

    sw $v0 a1

    #print the msg2
    li $v0 4
    la $a0 msg2
    syscall 

    #read the Num 2
    li $v0 5
    syscall

    sw $v0 a2

    #print the msg3
    li $v0 4
    la $a0 msg3
    syscall 

    lw $t0 a1
    lw $t1 a2


    li $v0 1
    add $a0 $t0 $t1
    syscall  
.data
    msg1: .asciiz "Enter the first number :"
    msg2: .asciiz "Enter the second number :"
    a1 : .word 0
    a2 : .word 0
    msg3: .asciiz  "The sum is = "