我正在尝试将此 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");
我总是收到&#34;尝试在0x00400138执行非指令&#34;
我的代码有什么问题
#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
答案 0 :(得分:1)
最可能的情况是,您最终会转到Exit
标签,并且那里没有代码可以实际执行任何操作。
因此它会尝试执行那里的 ,这不太可能结束。
你需要做一些能阻止CPU在你的代码末端运行的东西永远不会落地。
这可能是syscall
终止你的程序或返回操作系统,或者它可能像无限循环一样简单:
Exit: j Exit