LC3Tools ✡ Assignment3 Lavet udfra 1).asm BRnp IS PRIME ; Save the original value of R1 Handle the case when the input is 0 ; Increment the counter ; Check for divisibility by counter + counter ; Divide the input by (counter + counter) ; If the remainder is not 0, the number is prime 2 1 .ORIG x3000 isPrime 3 ST R1, SAVE_R1 4 5 AND R1, R1, #0 BRZ ZERO_CASE ; Clear R1 (to use as a counter) 6 LOOP 7 ADD R1, R1, #1 8 ADD R1, R1, #1 9 DIV RØ, R1 10 11 12 IS_PRIME 13 14 #1 ; 15 16 NOT PRIME 17 18 BRnzp DONE 19 ZERO_CASE 20 #0 21 DONE BRZ NOT PRIME AND RO, RO, #0 ADD RO, RO, BRnzp DONE AND RO, RO, #0 AND RO, RO, LD R1, SAVE_R1 RET SAVE_R1 .BLKW 1 22 23 24 25 .END 26 ; If the remainder is 0, the number is not prime ; Clear RO Set RØ to 1 (prime) ; Branch to DONE ; Clear RO (not prime) ; Branch to DONE ; Clear RO (0 is not prime) ; Restore the original value of R1 ; Return from the function ; Storage for saving R1
Can someone help me draw a flow-diagram of this code in LC3 assembly language.
Would appreciate it.
.ORIG x3000
isPrime
ST R1, SAVE_R1 ; Save the original value of R1
AND R1, R1, #0 ; Clear R1 (to use as a counter)
BRz ZERO_CASE ; Handle the case when the input is 0
LOOP
ADD R1, R1, #1 ; Increment the counter
ADD R1, R1, #1 ; Check for divisibility by counter + counter
DIV R0, R1 ; Divide the input by (counter + counter)
BRnp IS_PRIME ; If the remainder is not 0, the number is prime
BRz NOT_PRIME ; If the remainder is 0, the number is not prime
IS_PRIME
AND R0, R0, #0 ; Clear R0
ADD R0, R0, #1 ; Set R0 to 1 (prime)
BRnzp DONE ; Branch to DONE
NOT_PRIME
AND R0, R0, #0 ; Clear R0 (not prime)
BRnzp DONE ; Branch to DONE
ZERO_CASE
AND R0, R0, #0 ; Clear R0 (0 is not prime)
DONE
LD R1, SAVE_R1 ; Restore the original value of R1
RET ; Return from the function
SAVE_R1 .BLKW 1 ; Storage for saving R1
.END


Trending now
This is a popular solution!
Step by step
Solved in 1 steps with 1 images









