Question: Please fix the output of this program so that there is no trailing space before the newline current output: 1 0 1 0 1
Please fix the output of this program so that there is no trailing space before the newline
current output:
newlint
Required output:
newline no space
ORIG x ; Program begins here
;
;Instructions
;
LD R Valueptr ; R pointer to value to be displayed as binary
LDR R R # ; R value to be displayed as binary
; Initialize the loop counter and space handling counter
AND R R #
ADD R R # ; Loop counter initialized to
AND R R #
ADD R R # ; Space counter initialized to
; Main processing loop for bit display
MAINLOOP:
; Check if it's time to output a space
ADD R R #
BRz PREPARESPACE
; Shift left to get the next bit to the MSB
ADD R R R
; Check the negative flag to determine the bit if negative, if positive or zero
BRn OUTPUTONE
OUTPUTZERO:
LEA R ZeroString
TRAP x ; Output
BRnzp NEXTBIT
OUTPUTONE:
LEA R OneString
TRAP x ; Output
NEXTBIT:
ADD R R # ; Decrement the main loop counter
ADD R R # ; Decrement the space counter
BRzp MAINLOOP ; Loop if not done with all bits
; Final output handling to skip space and output newline
LEA R NewLine
PUTS ; Print newline
HALT ; End the program
PREPARESPACE:
; Only output space if not the last group
BRz FINISH ; Skip space if this is the last bit
LEA R SpaceChar
TRAP x ; Output a space
AND R R #
ADD R R #
BRnzp MAINLOOP ; Continue loop
FINISH:
LEA R NewLine
PUTS ; Print newline
HALT ; End the program
;
;Data
;
Valueptr FILL xCA ; The address where value to be displayed is stored
OneString STRINGZ ; String representing
ZeroString STRINGZ ; String representing
SpaceChar STRINGZ ; String representing a space
NewLine STRINGZ
; String representing a new line
END
ORIG xCA
Value FILL xABCD ; Value to be displayed as binary
END
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
