web-dev-qa-db-ja.com

Mips:2つの入力の合計を計算する

非常に単純に見えますが、$ 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
11
Daisy R.
.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
6
user4749734
.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 = "
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

脚注:syscallを使用することを指定するたびに、最初にそのコマンドを完了する必要があります。たとえば、何かを印刷する場合は、そのセットの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
#質問をして自分で答えてください。印刷されるもの。 #入力した番号またはプロンプトのアドレス#ここで何を変更する必要がありますか?

0
code_love