MIPS计算器输出不正确

时间:2014-10-16 19:23:00

标签: math mips integer-arithmetic

我正在尝试实现一个MIPS计算器,它以3个整数作为输入并返回一个输出。目前,代码输出“int1,int2和int3的总和为1”,我不明白为什么。也许我还没有正确编写加法子程序?

        .data
welc:   .asciiz "Enter an integer:\n"
sum:    .asciiz "The sum of "
prod:   .asciiz "The product of "
divi:   .asciiz "The quotient of "
subt:   .asciiz "The difference of "
also:   .asciiz " and "
comma:  .asciiz ", "
rem:    .asciiz "remainder:"
is:     .asciiz " is "

int1:   .word 1 #space to hold first int
int2:   .word 1 #space to hold second int
int3:   .word 1 #space to hold third int
input:  .space 1 #space to hold raw input
out:    .word 1 #space to hold output
remain: .word 1 #space to hold remainder

        .text
        .globl main
main :  li $v0, 4       #syscall 4, print string
        la $a0, welc    #give argument: string "Enter an integer:"
        syscall

        li $v0, 5   #tell syscall we want to read int 1
        syscall
        la $s1, int1    #load int1 into $s1
        sw $v0, 0($s1)  #copy int from $v0 to int 1

        li $v0, 4   #syscall 4, print string
        la $a0, welc    #give argument: string "Enter an integer:"
        syscall

        li $v0, 5   #tell syscall we want to read int 2
        syscall
        la $s2, int2    #load int2 into $s2
        sw $v0, 0($s2)  #copy int from $v0 to int 2

        li $v0, 4   #syscall 4, print string
        la $a0, welc    #give argument: string "Enter an integer:"
        syscall

        li $v0, 5   #tell syscall we want to read int 3
        syscall
        la $s3, int3    #load int3 into $s3
        sw $v0, 0($s3)  #copy int from $v0 to int 3

        la $s0, out #load output to $s0

        j plus  #jump to calc

plus:   add $s0, $s1, $s2   #add int1 and int2 and put in out
        add $s0, $s0, $s3   #add out and int3

        li $v0, 4   #tell syscall to print string
        la,$a0, sum #print sum string
        syscall
        li $v0, 1   #tell syscall to print int
        la $s1, int1    #tell syscall to print int1
        lw $a0, 0($s1)  #load int 1 into $a0 and print
        syscall
        li $v0, 4   #tell syscall to print string
        la,$a0, comma   #tell syscall to print comma
        syscall
        li $v0, 1   #tell syscall to print int
        la $s2, int2    #tell syscall to print int2
        lw $a0, 0($s2)  #load int 2 into $a0 and print
        syscall
        li $v0, 4   #tell syscall to print string
        la,$a0, comma   #tell syscall to print comma
        syscall
        li $v0, 4   #tell syscall to print string
        la,$a0, also    #tell syscall to print comma
        syscall
        li $v0, 1   #tell syscall to print int
        la $s3, int3    #tell syscall to print int3
        lw $a0, 0($s3)  #load int 3 into $a0 and print
        syscall
        li $v0, 4   #tell syscall to print string
        la,$a0, is  #tell syscall to print is
        syscall
        li $v0, 1   #tell syscall to print int
        la $s0, out #tell syscall to print output
        lw $a0, 0($s0)
        syscall

        li $v0, 10  #exit code
        syscall

1 个答案:

答案 0 :(得分:0)

$s0$s1$s2,您有地址。因此,在$s0中,您拥有不想要的地址总和。此外,您还要输出out的值,这是您从未更改的值。

在添加数字之前,您必须使用lw加载其值。然后,如果要在out处打印值,则必须先将$s0存储在该地址。