如何使用汇编进行算术运算?

时间:2019-04-14 03:57:07

标签: assembly mips

我现在正在研究Assembly,并且对功能的使用有所了解。 基本上,我试图通过输入获得两个数字并进行一些操作,如add,or和xor。 我的问题是我没有调用函数就得到了它。但是,当我将所有代码放在一起时(Add,or和xor),程序仅执行第一个操作。

我不知道如何声明变量并使用它们。 这是我一直在努力的代码。

main:
    # Get 1 number from user, put into $t0
    li $v0, 5 #load syscall read_int into $v0
    syscall   #make the syscall
    move $t0, $v0 #move the number read into $t0 

    # Get 2 number from user, put into $t1
    li $v0, 5 #load syscall read_int into $v0
    syscall #make the syscall
    move $t1, $v0 #move the number read into $t1

    add $t2, $t0, $t1 #make the sum

    #Print $t2
    move $a0, $t2 #move the number to print into $a0
    li $v0, 1 #load syscall print_int into $v0
    syscall #make the syscall

    li $v0, 10 #syscall code 10 to exit
    syscall #make the syscall 

另一方面,尝试声明num1和num2变量时,我陷入了困境:

.data
    num1: .byte 8
    num2: .byte 8

.text
    main:

    add $v0, num1, num2

    move $a0, $t2 #move the number to print into $a0
    li $v0, 1 #load syscall print_int into $v0
    syscall #make the syscall

我当时正与C语言建立联系以尝试变得有意义,例如:

int main (...)
sum(num1, numm2);


sum (num1, num2){
    result = num1 + num2;
    return result;
}

以此类推...

0 个答案:

没有答案