ASCII to Decimal Conversion THIS IS TO BE WRITTEN IN LC-3 ASSEMBLY. Get 4 ASCII
ID: 656713 • Letter: A
Question
ASCII to Decimal Conversion
THIS IS TO BE WRITTEN IN LC-3 ASSEMBLY.
Get 4 ASCII keys from the keyboard representing a single four digit number and translate that number into decimal. At the end I want a decimal number stored in register 0. Use Trap 20 to get the number then store that into memory.
As an example, if the user types in 0 then 1, then 2, then 5, you have to translate the ASCII codes 30, 31, 32, 35 into the decimal number 125.
The number can be entered as decimal or hex digits. If the first char is X assume hex.
Assume only capital letters will be entered for hex values. Assume good input. That is you can assume the user will only type in 0-9, A-F, X. Assume four numbers. The number 5 will have to be entered as the characters 0005.
I want you to use the procedure I have outlined below. Don't add extra error detection. Don't add extra characters.
1. Create a subroutine named GET_NUM
2. GET_NUM must get a four digit number from the user and convert the ASCII characters to a binary number. That number should be stored in R0 at the end of the subroutine.
3. GET_NUM must save and restore all registers used.
4. Read a character from the keyboard using TRAP x20 and print it to the screen.
5. If R0 is an X jump to code for hex input.
6. Code for decimal input. (You may use a loop for this if you like.)
a. R0 contains the first character/digit of the number.
b. Convert R0 to a numeric value (it is currently ASCII).
c. Multiply what is in R0 by 1000 and add to sum.
d. Get next character/digit using TRAP x20 and print it to the screen. e. Convert R0 to a numeric value (it is currently ASCII).
f. Multiply what is in R0 by 100 and add to sum. g. Get next character/digit using TRAP x20 and print it to the screen.
h. Convert R0 to a numeric value (it is currently ASCII).
i. Multiply what is in R0 by 10 and add to sum.
j. Get next character/digit using TRAP x20 and print it to the screen.
k. Convert R0 to a numeric value (it is currently ASCII).
l. Add R0 to sum.
m. Put sum in R0
7. Code for hex input.
a. R0 contains X.
b. Get first character/digit using TRAP x20 and print it to the screen.
c. Convert R0 to a numeric value (it is currently ASCII).
d. Multiply what is in R0 by _________ and add to sum (figure it out).
e. Repeat (a - d) for the next three characters/digits.
f. Store sum in R0
8. Below are ASCII values you will need.
Character ASCII Value (hex) 0 30 1 31 2 32 3 33 4 34 5 35 6 36 7 37 8 38 9 39 A 41 B 42 C 43 D 44 E 45 F 46 X 58Explanation / Answer
.ORIG x3000
GET_NUM
TRAP X20 ;get char in R0
OUT
ADD R1, R0, #0 ;store input value(ascii) to r1
ADD R1, R1, R5 ;get real value of r1
ADD R4, R2, #0 ;fill counter with multiplier
MULTIPLICATION:
ADD R3, R3, R1 ;add to sum
ADD R4, R4, #-1 ;decrease counter by one
BRp MULTIPLICATION ;continue loop until multiplier is 0
TRAP X20 ;get char in R0
OUT
ADD R1, R0, #0 ;store input value(ascii) to r1
ADD R1, R1, R5 ;get real value of r1
ADD R4, R2, #0 ;fill counter with multiplier
MULTIPLICATION:
ADD R3, R3, R1 ;add to sum
ADD R4, R4, #-1 ;decrease counter by one
BRp MULTIPLICATION ;continue loop until multiplier is 0
TRAP X20 ;get char in R0
OUT
ADD R1, R0, #0 ;store input value(ascii) to r1
ADD R1, R1, R5 ;get real value of r1
ADD R4, R2, #0 ;fill counter with multiplier
MULTIPLICATION:
ADD R3, R3, R1 ;add to sum
ADD R4, R4, #-1 ;decrease counter by one
BRp MULTIPLICATION ;continue loop until multiplier is 0
ADD R0, R3, R6 ;move result to r0
OUT ;print result
HALT
SUM .FILL 0
THOUSAND .FILL 1000
.END
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.