Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an LC-3 assembly language program that counts the number of 1s in the valu

ID: 3739639 • Letter: W

Question

Write an LC-3 assembly language program that counts the number of 1s in the value stored in RO and stores the result in R1. For example, if RO contains 0001001101110000 (x1370), then after the program executes, the value stored in R1 would be 110 (decimal 6) Here are some hints from my solution ORIG x3000 AND R5, R5?#0 ADD R5, R5, #1.R5 will act as a mask to mask out the unneeded bit AND R 1, R 1, #0 zero out the result register AND R2, R2, #0 ,R2 will act as a counter LD R3, NegSixt MskLoop AND R4, RO, RS mask off the bit Add code here.. . if you want more than a 0-) HALT NegSat FILL , 16 END Mask logic 100 CREDIT Solicit the number to be inspected via the keyboard 10 points for a single digit (e g 5) decimal number, which must be converted to binary 20 points for a two digit (e g 55) decimal number, which must be converted to binary

Explanation / Answer

Code:

.ORIG x3000

;initialise the registers we are using

LD R0, X     

AND R1, R1, #0   

AND R2, R1, #0

;check if R0 is 0

LOOP    ADD R0, R0, #0   

BRz DONE

;count the bits

ADD R1, R1, #1

ADD R2, R0, #-1

AND R0, R0, R2

BRnzp LOOP

DONE    HALT

;X    .FILL x2ACA    ; 0010101011001010 answer should be 7

;X    .FILL x5555    ; 0001001101110000 answer should be 6

;X    .FILL x882    ; 0000100010000010 answer should be 3

;X    .FILL xFFFF    ; 1111111111111111 answer should be 16

.END

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote