assembly - MIPS 中的简单加法

标签 assembly mips

我目前正在上 MIPS 汇编类(class),我们使用的书已经绝版,所以我依靠互联网寻求帮助,以便我能理解。该程序接受三个整数。其中两个用于 add/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 --

有什么想法吗?

最佳答案

您必须在移动之前执行系统调用:

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

关于assembly - MIPS 中的简单加法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14857729/

相关文章:

memory - 如何在WinMobile6上启用ARMv6非对齐访问?

linux - 优化打印计数器的循环

c++ - Lambdas 作为环境的闭包。 RIP寄存器的关键作用

将 Mips 循环转换为 C

assembly - Big Endian 到 Little Endian 使用 MIPS 没有逻辑运算?

c# - 划分/移动 assembly 差异

c++ - gcc 内联 asm 跳转到带有交叉抛出异常的标签

assembly - MIPS 程序集返回调用分支语句

assembly - MIPS 存储字 (sw)

memory - MIPS 说明 : sw