I am having trouble converting C code to MIPS code Hello, I have been trying to
ID: 3561882 • Letter: I
Question
I am having trouble converting C code to MIPS code
Hello,
I have been trying to convert this C code into MIPs assembly code but I got many errors I don't know what the reason is.
int membership;
int howMany;
int totalPrice;
int discount;
printf("Please enter 0 if you have a membership, 1 or non-zero integer otherwise: ");
//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: ");
//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) ", 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 ", discount);
else
printf ("There is no discount ");
Explanation / Answer
.data
print1: .asciiz "Please enter 0 if you have a membership, 1 or non-zero integer otherwise: "
print2: .asciiz "Please enter your number of units to be purchased:"
print3: .asciiz "You have a "
print4: .asciiz " percent discount "
print5: .asciiz "There is no discount "
#variable declarations
membership: .word 0
howmany: .word 0
totalprice: .word 0
discount: .word 0
.text
main :
li $v0,4
la $a0,print1
syscall
li $v0,5
syscall
la $t1,membership
sw $v0,0($t1)
li $v0,4
la $a0,print2
syscall
li $v0,5
syscall
la $t1,howmany
sw $v0,0($t1)
add $v1,$v0,$v0 #2*howmany
add $v1,$v1,$v1 #4*howmany
add $v1,$v0,$v1 #5*howmany
la $t1,totalprice
sw $v1,0($t1)
la $t1,membership
lw $v0,0($t1)
li $t3,0
bne $v0,$0,Nomember
li $t2,150
bgt $v1,$t2,discount30
blt $v1,$0,printdiscount
li $t3,20
b printdiscount
discount30 :
li $t3,30
b printdiscount
discount15 :
li $t3,15
b printdiscount
Nomember:
li $t2,200
bgt $v1,$t2,discount15
printdiscount:
ble $t3,$0,nodiscount
li $v0,4
la $a0,print3
syscall
li $v0,1
add $a0,$0,$t3
syscall
li $v0,4
la $a0,print4
syscall
b end
nodiscount:
li $v0,4
la $a0,print5
syscall
end: b end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.