LC-3从.blkw输出字符串

发布于 2025-01-21 04:53:56 字数 1979 浏览 0 评论 0原文

我正在处理的程序中只需在存储器中存储的16位字符串,该字符串在存储器上标记为模式,并计算字符串中的1s数,并将其值存储在num_ones < /代码>。我需要在屏幕上打印16位字符串,但我坚持如何完成此操作。

我将16位字符串存储在.blkw中。我尝试将模式的地址加载到R0中,并使用puts,但这并没有为我打印出任何东西。

    .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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩发小公举 2025-01-28 04:53:56

您的算法看起来还不错,但是您错过了一些细节。

如果您不知道调试,现在是时候学习了。&nbsp;您应该能够单步调试。&nbsp;在单步中,这个想法是确定您期望发生的事情与实际发生的事情之间的区别。&nbsp;我们验证每个说明的执行,因为如果任何一个指令是错误的,则整个程序将无法正常工作!

这是我所做的:跳过您描述为有问题的领域,我在endprogram。 R7中的1个计数似乎正确。&nbsp;现在,查看数据存储器中的字符串。&nbsp;您可以在地址X3166上看到它,并且还可以看到它是由数字1和0组成的数字值的序列 - 解释为字符打印,这些是控制字符,并且不会使用任何一个putc正确打印puts。&nbsp;您需要ASCII字符来打印。

接下来,代码nul终止了“字符串”,但这是错误的,因为它没有使NUL字符处于正确的位置。&nbsp;您会查看nul终止后是否立即查看字符串。&nbsp;尽管该错误不是由ASCII字符组成的事实,但其中许多NUL已经在其中,而NUL的0覆盖却不会真正显示出来。

您必须根据他们希望该程序执行的操作来决定是否使用puts用ASCII字符(在模式或复制到其他地方),或要使用putc的循环,其中将数字从模式转换为ASCII以进行输出。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文