The program I am working on takes a 16-digit string of only 1s and 0s and stores that in memory labeled PATTERN
and counts the number of 1s in the string and stores that value in NUM_ONES
. I need to print the 16-digit string on the screen but am stuck on how to accomplish this.
I am storing the 16-digit string in a .BLKW
. I tried loading the address of PATTERN
into R0 and using PUTS
but that does not print anything out for me.
.orig x3100
; PROMPTING
LEA R0, PROMPT_STRING
PUTS
LEA R1, PATTERN ; saves the address of the memory
loop
GETC
OUT
ADD R3, R0, x0 ;
LD R6, DIFF ;
ADD R3,R3,R6 ;
STR R3, R1, #0 ;
ADD R1, R1, #1 ;
ADD R5, R0, #-10 ;
BRZ countOneStart
BR loop
countOneStart
ADD R2 R2 #15 ; Initialize loop count for CountDigits
AND R7,R7,#0 ; Clear R7
LEA R0, PATTERN ; Load Memory Space address into R0
LDR R3,R0,#0 ; Load contents of PATTERN into R3
AND R4,R3,#1 ; Check if digit is 1 or 0
BRP addOne ; if positive go to addOne
BRZ CountDigits ; if zero go to CountDigits
addOne
ADD R7,R7,#1 ; Increment R7 to keep count
BR CountDigits
CountDigits
ADD R2 R2 #-1 ;
BRN EndProgram
ADD R0,R0,#1 ; Increment address
LDR R3,R0,#0 ; Load contents
AND R4,R3,#1 ; Check if digit is 1 or 0
BRP addOne ; if positive go to addOne
BRZ CountDigits
EndProgram
ST R7, NUM_ONES
LEA R0 , PATTERN;
AND R7,R7,#0 ; Clear R7
STR R7,R0,#0 ; store Null at end of array
LEA R0, PATTERN ; store addr in R0
TRAP x22 ; display output from memory address from R0 untill it founds Null
HALT
PROMPT_STRING .STRINGZ "Please enter a 16-character string consisting of only 0s and 1s: "
PATTERN .blkw #17 ; declares empty space to store the string
DIFF .fill #-48
NUM_ONES
.END
Your algorithm looks ok, but you have missed some details.
If you don't know about debugging, now is the time to learn. You should be able to single step debug this. During single step, the idea is to determine the difference between what you expect to happen and what actually happened. We verify the execution of each instruction, because if any one instruction is wrong, the whole program won't work!
Here's what I did: skipping ahead a bit to the area you described as problematic, I put a breakpoint at EndProgram
. The count of 1's in R7 appears correct. Now, look at the string in data memory. You can see it at address x3166, and also you can see that it is a sequence of numeric values consisting of the numbers 1 and 0 — interpreted for character printing, those are control characters and won't print properly using either putc
or puts
. You need ascii characters for printing.
Next the code nul-terminates the "string", but that is in error, since it doesn't put the nul character at the correct position. You'll see if you look at the string immediately after the nul termination. Though this error is hiding by the fact that the string is not composed of ascii characters, so has many nul's already therein, and a 0 overwritten with a nul won't really show.
You'll have to decide, based on what they want the program to do, whether to use puts
with ascii characters (there at PATTERN
or copied to somewhere else), or to use a loop with putc
where you convert numbers from PATTERN
to ascii for output.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.