Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

NAME: AHMAD HAZIQ FIKRI BIN MOHD YUSOF

MAT RIC NUMBER: 4223005161


COURSE: BACHELOR OF COMPUTER SCIENCE(HONS.)

QUESTION

Melati own a beverage stall that selling varieties of beverage types with different prices. Each order
will require customer name, quantity and beverage type. Payment of the transaction can be done
by using cash only. Each transaction will be charge 5% of tax. Create a program using assembly
language to design this purchasing system. Each transaction will be earned a free cup of tea as a
promotion by displaying a pop-up message.
ANSWER

SOURCE CODE:
INCLUDE Irvine32.inc

.data

welcomeMsg BYTE " Have a nice day with Melati's Beverage Stall! ", 0dh,
0ah
beverageTypes BYTE " Beverage Types: ", 0dh, 0ah
BYTE " 1. ExtraJoss Anggur - RM7 ", 0dh,
0ah
BYTE " 2. Caramel Latte - RM9
", 0dh, 0ah
BYTE " 3. Cappucino - RM4
", 0dh, 0ah
BYTE " 4. Matcha Latte - RM9
", 0dh, 0ah
promptName BYTE "Enter your name: ", 0
promptBeverage BYTE "Enter beverage type (1-4): ", 0
promptQuantity BYTE "Enter quantity: ", 0
promptPayment BYTE "Enter cash payment: RM", 0
errorMsg BYTE "Invalid input. Please try again.", 0dh, 0ah
taxMsg BYTE "Tax (5%): RM", 0
totalMsg BYTE "Total amount: RM", 0
freeTeaMsg BYTE "Congratulations! You have earned a free cup of tea!", 0dh, 0ah
thankYouMsg BYTE "Thank you for your purchase!", 0dh, 0ah
customerName BYTE 30 DUP(0)
beveragePrice DWORD 7, 9, 4, 9 ; Price of Beverage
beverageChoice DWORD ?
quantity DWORD ?
totalCost DWORD 0
tax DWORD 0
payment DWORD ?
balanceMsg BYTE "Balance: RM", 0
balance DWORD ?

.code
main PROC

; Display welcome message and beverage types


call Crlf
mov edx, OFFSET welcomeMsg
call WriteString

; Get customer name

mov ecx, SIZEOF customerName


call ReadString

; Get beverage choice


GetBeverage:
mov edx, OFFSET promptBeverage
call WriteString
call ReadInt
mov beverageChoice, eax
cmp eax, 1
jb InvalidInput
cmp eax, 4
ja InvalidInput

; Get quantity
GetQuantity:
mov edx, OFFSET promptQuantity
call WriteString
call ReadInt
mov quantity, eax
cmp eax, 0
je InvalidInput

; Calculate total cost


mov eax, beverageChoice
dec eax ; Adjust for array indexing
mov ebx, beveragePrice[eax*4]
mov ecx,quantity
imul ecx,ebx
mov totalCost, ecx

; Calculate tax
mov eax, totalCost
imul eax, 5 ; Multiply total cost by 5%
mov ebx, 100
xor edx, edx ; Clear any previous remainder
div ebx ; Divide by 100
mov tax, eax

; Get payment
GetPayment:
mov edx, OFFSET promptPayment
call WriteString
call ReadInt
mov payment, eax

; Check if payment is sufficient


mov eax, totalCost
add eax, tax
cmp eax, payment
jg GetPayment

; Display tax and total amount


mov edx, OFFSET taxMsg
call WriteString
mov eax, tax
call WriteDec
call Crlf
mov edx, OFFSET totalMsg
call WriteString
mov eax, totalCost
add eax, tax
call WriteDec
call Crlf

; Check if payment is sufficient


mov eax, totalCost
add eax, tax
cmp eax, payment
jg GetPayment

; Display tax and total amount


; ... (display tax and total amount as before)

; Calculate balance
mov eax, payment
sub eax, totalCost
sub eax, tax
mov balance, eax

; Display balance
mov edx, OFFSET balanceMsg
call WriteString
mov eax, balance
call WriteDec
call Crlf

; Display free tea message


mov edx, OFFSET freeTeaMsg
call WriteString
call Crlf
; Display thank you message
mov ecx, SIZEOF thankYouMsg
call ReadString
call Crlf

exit

InvalidInput:
mov edx, OFFSET errorMsg
call WriteString
mov edx, OFFSET beverageTypes ; Redisplay beverage types after invalid input
call WriteString
jmp GetBeverage

main ENDP
END main
THE OUTPUT:

You might also like