[3] [40 pts] Convert a four-bit binary number to decimal. (40 pts - 20 pts for f
ID: 3741634 • Letter: #
Question
[3] [40 pts] Convert a four-bit binary number to decimal. (40 pts - 20 pts for flowchart, 20 for code) Draw a flowchart and write an LC-3 assembly language program that reads a four-bit unsigned binary number from the user, and prints the equivalent decimal number on the next line. The user types four bits or 0), followed by linefeed. Assume that the user will type exactly four characters before the linefeed, and that only 1' and '0' will be typed. Accumulate the bits into a register as they are typed. One good way to do that is: (1) Start with a value of zero. (2) When a new bit is typed, shift the value to the left and add 1 or 0 to the least significant bit. The result will be a value between 0 and 15, inclusive. Print the characters that represent the . decimal number. The one-digit numbers are easy (based on the first problem). Think about hovw to deal with 10-15.Explanation / Answer
; Init R2 to 0
AND R2, R2, 0
; Loop 4 times, so keep 4 in R4
ADD R4, 0, 4
ADD R3, 0, 1
NOT R3, R3
; Load the first bit from x0:
LOOP: ADD R2 , R2 , x0
; Perform Left shift, by adding number to itself (i.e. multiply by 2)
ADD R2, R2, R2
; Now do that 4 times
ADD R4, R4, R3
BRnz LOOP
; After 4 loops we have shifted the input to 4 left
; The result is in R2
Depending on how you print, you can take the value in this register and print.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.