1. Write a MIPS assembly language program that reads a customer\'s current and p
ID: 3883636 • Letter: 1
Question
1. Write a MIPS assembly language program that reads a customer's current and previous meter readings of electricity and a month to compute its electricity bill.
If a customer spent 0 or less (technically this should not happen, though) KWH (kilowatt-hours) that is computed by current meter reading - previous meter reading,
then the program should print out "There is no bill to pay. ".
If a customer spent less than or equals to 200 KWH in a month, then the payment should be 10 dollars.
If a customer spent more than 200 KWH in a month of May, June, July, August, or September, then the payment is computed by:
payment = 10 + (used KWH - 200) / 15;
If a customer spent more than 300 KWH in any other month, then the payment is compute by:
payment = 10 + (used KWH - 200) /20;
Then if the payment is greater than 0, it should print out the payment amount, along with its used KWH.
Explanation / Answer
CODE (Explanation in comments):
# NOTE: There is no error handling for months in this code and bill will be printed in integer only
# initialising variales
.data:
curr_reading: .space 4
prev_reading: .space 4
month: .space 4
payment: .space 4
usage: .space 4
prompt1: .asciiz "Enter Current month reading: "
prompt2: .asciiz "Enter Previous month reading: "
prompt3: .asciiz "Enter Current month(1-12): "
prompt4: .asciiz "There is no Bill to Pay "
prompt5: .asciiz "Amount to pay: "
prompt6: .asciiz "Month: "
newline: .asciiz " "
# code
.globl main
.text
main:
#prompt for current month reading
li $v0, 4
la $a0, prompt1
syscall
#read value and store
li $v0, 5
syscall
sw $v0, curr_reading
#prompt for previous month reading
li $v0, 4
la $a0, prompt2
syscall
#read value and store
li $v0, 5
syscall
sw $v0, prev_reading
#prompt for month
li $v0, 4
la $a0, prompt3
syscall
#read value and store
li $v0, 5
syscall
sw $v0, month
#calculate usage
lw $t1, curr_reading
lw $t2, prev_reading
sub $t3, $t1, $t2
sw $t3, usage
#check for no usage and jump to loop
ble $t3, 0, no_usage
#initialize payment to 10 if there is usage
li $t4, 10
sw $t4, payment
#calculate total payment if more than 200kWH is used
bgt $t3, 200, calculate_total
#else print payment
b print_payment
#print no bill message and exit
no_usage:
li $v0, 4
la $a0, prompt4
syscall
b exit
#calculat total based on month
calculate_total:
lw $t2, usage
lw $t3, month
sub $t2, $t2, 200
#divide by 15 if between may and september
blt $t3, 5, div_15
bgt $t3, 9, div_15
#else divide by 20
div $t2, $t2, 20
b final_payment
div_15:
div $t2,$t2,15
#calculate final payment
final_payment:
lw $t1, payment
add $t1, $t1, $t2
sw $t1, payment
#print messages and exit
print_payment:
li $v0, 4
la $a0, prompt5
syscall
li $v0, 1
lw $a0, payment
syscall
li $v0, 4
la $a0, newline
syscall
li $v0, 4
la $a0, prompt6
syscall
li $v0, 1
lw $a0, month
syscall
li $v0, 4
la $a0, newline
syscall
exit:
li $v0, 10
syscall
OUTPUT:
Enter Current month reading: 10
Enter Previous month reading: 10
Enter Current month(1-12): 3
There is no Bill to Pay
-- program is finished running --
Reset: reset completed.
Enter Current month reading: 600
Enter Previous month reading: 200
Enter Current month(1-12): 11
Amount to pay: 23
Month: 11
-- program is finished running --
Reset: reset completed.
Enter Current month reading: 900
Enter Previous month reading: 150
Enter Current month(1-12): 6
Amount to pay: 37
Month: 6
-- program is finished running --
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.