MIPS中的简单添加

时间:2013-02-13 16:04:59

标签: assembly mips

我目前正在参加MIPS汇编课程,我们使用的这本书已经绝版,所以我依靠互联网寻求帮助,以便我理解。这个程序有三个整数。其中两个添加/ sub / mult / div,第三个是运算符。这是代码。

    .text
    .globl __start
__start:

    # Prompt for first int and accept first int
    la $a0,firstint
    li $v0,4
    syscall

    li $v0,5
    move $s0, $v0
    syscall

    # Prompt for second int and accept second int
    la $a0,firstint
    li $v0,4
    syscall

    li $v0,5
    move $s1, $v0
    syscall

    # Prompt for operation
    la $a0,operation
    li $v0,4
    syscall

    li $v0,5
    move $s2, $v0
    syscall

    beq $s2,0,__add0

    li $v0,10
    syscall

__add0:
    la $a0,added
    li $v0,4
    syscall

    add $a0, $s0, $s1
    li $a0,1
    syscall


    .data
firstint:   .asciiz "Enter the first integer: "
secondint:  .asciiz "Enter the second integer: "
operation:  .asciiz "Enter operation (add=0, subtract=1, multiply=2, divide=3): "
added:      .asciiz "The added number is: "

我的理解是,如果$ s2中的值等于0,beq将跳转到add0但是它似乎没有发生。输入操作类型后输出停止。示例输出:

Enter the first integer: 10
Enter the first integer: 5
Enter operation (add=0, subtract=1, multiply=2, divide=3): 0

-- program is finished running --

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

你必须在移动前进行系统调用:

li $v0,5
syscall
move $s2, $v0