Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
CPEN 231L
Simple Arithmetic Calculator
Objectives
• To implement an assembly program that simulates a simple calculator.
• To be familiar with debugging tools (step through the code, check values of variables, check
the status of registers, etc.)
Procedure
Here is an example of how to display information on LCD.
1. Initially, the calculator prompts the user to input the first operand and waits for such operand
to be entered through the keypad.
2. After the first operand has been input, the calculator prompts the user to input the operation
and waits for it to be entered through the keypad.
3. After the operation has been input, the calculator prompts the user to input the second operand
and waits for it to be entered through the keypad. 2
4. After the second operand has been input, the calculator works out the mathematical operation
and displays the result if it falls in the range specified above. Otherwise, it displays an error
message and goes back to Step 2 (i.e., asks the user to re-enter the operation and second
operand).
How to input an operand
• Each operand is an unsigned integer that is input through the keypad in a BCD format. The
operand can be up to 5 BCD digits in length. Hence, you can input numbers from 0 to
99999. The string of the BCD input number is terminated either by pressing “#” (ASCII of
0x23) or when the size (5) is reached.
• Save the BCD ASCII digits into a buffer and use the following library subroutine
String_ASCII_BCD2Hex_Lib to convert it into its equivalent hex number in R0.
• When an operand is being entered, any keypad press on ‘A’, ‘B’, ‘D’, or ‘*’ is ignored.
• A press on ‘C’ clears everything and takes the system back to Step 1 (i.e., start over).
• A press on ‘#’ terminates the operand input process. E.g., if the sequence ‘2 3 #’ is pressed
on the keypad, then the operand entered is 23.
• Each operand should be stored in the memory. 3
• Pressing ‘C’ instead clears the whole process and takes the system back to Step 1.
• Pressing any other character than ‘A’, ‘B’, ‘C’, ‘D’, or ‘*’ is ignored.
• After getting the operation character from the keypad, it should be stored in a memory
location so that the calculator can refer to it when computing the result.
How to compute the result
• Load the hex values of the two operands in two registers.
• Identify the operation from the memory location it is stored in.
• Carry out the operation using the right instruction from ADD, SUB, MUL, or UDIV.
Clearly, in case of subtraction or division, the first operand is the minuend or dividend
whereas the second operand is the subtrahend or divisor, respectively.
• If the resulting number does not fall in the range from 0 to 65536 decimal, or in case of
division by 0, an error flag (should be defined in the memory) mush be set. Otherwise, the
error flag must be cleared.
How to display the result
• First, check on the error flag. If set, display an error message and ask the user to re-input
the operation and second operand. This prompt message should last for 2 seconds before
the system moves back to Step 1.
• Else, when the error flag is clear, the operation should be displayed on the first line of the
LCD while the result is displayed on the second line as shown in Table 1 above.
• To display the result, its hex decimal value should be converted to the equivalent BCD
ASCII code before being sent to the LCD.
The subroutine Hex2DecChar_Lib provided below converts a hex value in register R1
to an ASCII string of BCD characters in a memory location pointed to by register R0.
; Subroutine Hex2DecChar_Lib- Converts a hex value in R1 into an ASCII ;
You will need to create a buffer of size 6 bytes in the memory to hold the ASCII codes of the
result. Recall, 5 bytes for the maximum output value that can be at most 5 digits, and one
NULL byte to be used while displaying the result as a stopping criterion.
Before you call the Hex2DecChar_Lib make sure the result in hex is kept in register R1
and that register R0 points to the 6-byte buffer you have created.
• After the ASCII codes of the result have been generated, treat the content of the 6-byte
buffer as a message to be displayed on the LCD. Set its position on the second line of the
LCD and display it.