c - 我想知道,为什么执行后得到 "Attempt to execute non-instruction at 0x00400138"?

标签 c if-statement assembly mips

我正在尝试将这个 C 代码实现到 MIPS(汇编)中,但我在正确执行此操作时遇到了困难。

int membership;
int howMany;
int totalPrice;
int discount;
printf("Please enter 0 if you have a membership, 1 or non-zero integer otherwise:\n");
// read an integer from a user input and store it in membership
scanf("%d", &membership);
printf("Please enter your number of units to be purchased:\n");
// read an integer from a user input and store it in howMany
scanf("%d", &howMany);
totalPrice = howMany*5;
printf("Your total price is %d dollar(s)\n", totalPrice)
// compute its discount
if (membership == 0 && totalPrice > 150)
     discount = 30;
else if (membership == 0 && totalPrice <= 150 && totalPrice > 0)
     discount = 20;
else if (membership != 0 && totalPrice > 200)
     discount = 15;
else
     discount = 0;
// print out the discount
if (discount > 0)
    printf ("You have a %d percent discount\n", discount);  
else
    printf ("There is no discount\n");  

我总是收到“尝试在 0x00400138 处执行非指令”

我的代码有什么问题

#data declarations: declare variable names used in program, storage allocated in RAM
                .data       
message1:       .asciiz     "Please enter 0 if you have a membership, 1 or non-zero integer otherwise:\n" 
message2:       .asciiz     "Please enter your number of units to be purchased:\n"
message3_1:     .asciiz     "Your total price is "
message3_2:     .asciiz      " dollar(s)\n"
message4_1:     .asciiz     "You have a "
message4_2:     .asciiz     " percent discount\n"
message5:       .asciiz     "There is no discount\n"
testmessage1:   .asciiz     "\nI am Memberhsip\n"
testmessage2:   .asciiz     "\nI am somewhere\n"
testmessage3:   .asciiz     "\nLalala\n"
#program code is contained below under .text
                .text
                .globl    main    #define a global function main

# the program begins execution at main()
main:
            #
            #   Print "Please enter 0 if you have a membership, 1 or non-zero integer otherwise:\n"
            #
            la      $a0,message1
            li      $v0,4
            syscall
            #read int
            li      $v0,5
            syscall
            move    $t0,$v0


            #testing
            #move       $a0,$t0
            #li     $v0,1
            #syscall

            la      $a0,message2
            li      $v0,4
            syscall

            li      $v0,5
            syscall
            add     $t1,$zero,$v0



            #
            # $t0 -> membership
            # $t1 -> # of units
            # $t2 -> 5
            # $t3 -> totalPrice
            li      $t2,5

            mult    $t1,$t2
            mflo    $t3


            li      $v0,4
            la      $a0,message3_1
            syscall

            li      $v0,1
            move        $a0,$t3
            syscall

            li      $v0,4
            la      $a0,message3_2
            syscall
            #
            # Go to Membership if $t0 == 0
            #
            beq     $t0,$zero,Membership

            #
            # Go to Guest if  $t0 != 0
            #
            bne     $t0,$zero,Guest


            beq $t4,$zero,Else

            #move   $t4,$zero



            jr      $ra
Membership:
            la      $a0,testmessage1
            li      $v0,4
            syscall

            bgt     $t3,150,Membership_MoreThan_150

            ble     $t3,150,Membership_LessThanEql_150

            j       Exit

Membership_MoreThan_150:
            #la     $a0,testmessage3
            #li     $v0,4
            #syscall

            #discout = 30
            addi    $t4,$zero,30
            j       Result

Membership_LessThanEql_150:
            bgt     $t3,0,Membership_LessThanEql_150_2
            j       Exit
Membership_LessThanEql_150_2:
            addi    $t4,$zero,20
            j       Result
Guest:
            #la     $a0,testmessage2
            #li     $v0,4
            #syscall

            bgt     $t3,200,Guest_MoreThan_200

            j Exit
Guest_MoreThan_200:
            addi    $t4,$zero,15
            j       Result
Result:
            #
            #   First part of message3 "Your total price is "
            #
            la      $a0,message4_1
            li      $v0,4
            syscall

            #
            #   Getting the discount value, we get it from the above subs.
            #
            li      $v0,1
            move    $a0,$t4
            syscall

            #
            #   Second part of message3 " dollar(s)\n"
            #
            li      $v0,4
            la      $a0,message4_2
            syscall

            # jmp to Exit
            j       Exit

Else:
            li      $v0,4
            la      $a0,message5
            syscall
            j       Exit
Exit:
            #bgt        $t4,$zero,LastExit
LastExit:
            #li     $v0,4
            #la     $a0,message5
            #syscall

最佳答案

可能的情况是,您最终到达Exit标签,但那里没有代码可以实际执行任何操作。

因此,它将尝试执行那里的任何内容,这不太可能有好的结果。

您需要做一些事情来阻止 CPU 在代码末尾运行到永无止境。

这可能是一个终止程序或返回操作系统的系统调用,也可能是像无限循环一样简单的东西:

Exit: j Exit

关于c - 我想知道,为什么执行后得到 "Attempt to execute non-instruction at 0x00400138"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25802163/

相关文章:

python - 使用 Python 3 检查 JSON 键是否为空的条件语句

python - 为什么这个 python 逻辑语句的行为与我的预期行为相反?

获取 PID 及其所有子项的 CPU 使用率的 C 程序

c++ - 如何在 Linux 中读取/dev/ttyUSB0 设备?

c - 两个线程,首先添加第二个减法

c++ - 快速排除成员是否在集合中

javascript - 执行此操作的最快方法?三元运算符?转变?大批?

c - ARM 汇编程序 - 如何使用 CMP、BLT 和 BGT?

c - 我应该知道什么来优化程序?

c - 从 rsp 反汇编 sub 和不同的函数返回