Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Utilizing these two subroutines create a third subroutine per instructions. PRINTDEC - Print a 1 6 - bit number as decimal 2 . A 1

Utilizing these two subroutines create a third subroutine per instructions.
PRINTDEC - Print a 16-bit number as decimal
2. A 16-bit number will be stored in R0. You will have to figure out each digit one at a time and print
each.
3. DO NOT PRINT leading zeros. Loop until a non-zero digit is found and then print the rest of the digits.
YOU CAN NOT ignore all zeros. Only ignore leading zeros.
4. DO NOT PRINT a newline at the end.
5. Your program should print a single 0 if R0 is 0.
Assume you have a number like 12345 in R0.
Divide 12345 by 10000 to get 1, add 48 and print it.
Divide 12345 by 1000 to get 12. Mod 12 by 10 to get 2, add 48 and print it.
Divide 12345 by 100 to get 123. Mod 123 by 10 to get 3, add 48 and print it.
Divide 12345 by 10 to get 1234. Mod 1234 by 10 to get 4, add 48 and print it.
Divide 12345 by 1 to get 12345. Mod 12345 by 10 to get 5, add 48 and print it.
Assume you have a number like 123 in R0.
Divide 123 by 10000 to get 0. Ignore it.
Divide 123 by 1000 to get 0. Ignore it.
Divide 123 by 100 to get 1. Mod 1 by 10 to get 1, add 48 and print it.
Divide 123 by 10 to get 12. Mod 12 by 10 to get 2, add 48 and print it.
Divide 123 by 1 to get 123. Mod 123 by 10 to get 3, add 48 and print it
; Division subroutine
DIV
ST R0, PUSH_R0 ; Save R0
ST R1, PUSH_R1 ; Save R1
ST R2, PUSH_R2 ; Save R2
ST R3, PUSH_R3 ; Save R3
ST R4, PUSH_R4 ; Save R4
AND R0, R0, #0 ; Clear R0 to use as counter/result
; Check for division by zero or negative inputs
ADD R3, R2, #0 ; Prepare to check R2
BRz DIV_ERROR ; Division by zero
BRn DIV_ERROR ; Negative divisor
ADD R3, R1, #0 ; Prepare to check R1
BRn DIV_ERROR ; Negative dividend
; Main division loop
DIV_LOOP
ADD R3, R1, #0 ; Check if we can still subtract
NOT R4, R2
ADD R4, R4, #1
ADD R3, R3, R4 ; R3= R1- R2
BRn DIV_END ; If R3 is negative, we're done
ADD R1, R1, R4 ; Subtract R2 from R1
ADD R0, R0, #1 ; Increment the result
BR DIV_LOOP ; Repeat the loop
DIV_ERROR
LD R0, NEG_ONE ; Set R0 to -1 to indicate error
DIV_END
LD R1, PUSH_R1 ; Restore R1
LD R2, PUSH_R2 ; Restore R2
LD R3, PUSH_R3 ; Restore R3
LD R4, PUSH_R4 ; Restore R4
LD R0, PUSH_R0 ; Restore R0
RET ; Return from the subroutine
; Modulo subroutine
MOD
ST R0, PUSH_R0 ; Save R0
ST R1, PUSH_R1 ; Save R1
ST R2, PUSH_R2 ; Save R2
ST R3, PUSH_R3 ; Save R3
ST R4, PUSH_R4 ; Save R4
AND R0, R0, #0 ; Clear R0 to use for the result
; Modulo checks and operations
ADD R3, R2, #0 ; Check if divisor (R2) is zero
BRz MOD_ERROR ; If zero, cannot mod by zero, error
BRn MOD_ERROR ; Negative divisor, error
ADD R3, R1, #0 ; Check if dividend (R1) is negative
BRn MOD_ERROR ; Negative dividend, error
MOD_LOOP
ADD R3, R1, #0 ; Prepare for subtraction
NOT R4, R2
ADD R4, R4, #1 ; Get -R2 for subtraction
ADD R3, R3, R4 ; Try subtracting R2 from R1
BRn MOD_END ; If R1- R2<0, we have our remainder
ADD R1, R1, R4 ; Subtract R2 from R1 and continue loop
BR MOD_LOOP
MOD_ERROR
LD R0, NEG_ONE ; Error, set R0 to -1
MOD_END
LD R1, PUSH_R1 ; Restore R1
LD R2, PUSH_R2 ; Restore R2
LD R3, PUSH_R3 ; Restore R3
LD R4, PUSH_R4 ; Restore R4
LD R0, PUSH_R0 ; Restore R0
RET ; Return from subroutine

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 2 Lncs 13427

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124251, 978-3031124259

More Books

Students also viewed these Databases questions

Question

recognise typical interviewer errors and explain how to avoid them

Answered: 1 week ago

Question

identify and evaluate a range of recruitment and selection methods

Answered: 1 week ago

Question

understand the role of competencies and a competency framework

Answered: 1 week ago