10

看起来很简单,但我认为我的程序无法编译,因为我覆盖了 $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
4

3 回答 3

6
.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
于 2015-04-04T16:54:47.610 回答
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 中的号码 #如果我们做
li $v0, 1 la $a0, prompt syscall
#ask question and answer you yourself. 将打印什么。#我们输入的号码或提示地址#这里需要修改什么?

于 2016-02-28T02:09:20.873 回答
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 = "
于 2016-04-18T17:57:50.640 回答