在 LC3 程序集中将寄存器的内容打印到控制台

Print contents of register to console in LC3 Assembly

假设我有一个值(例如 1234)加载到 R0 中。我如何将这个值打印到控制台?

我假设您想向控制台输出一个数字,但如果有的话,您得到的是随机字符。

当 LC3 试图将您的号码解释为 ASCII 字符时,就会发生这种情况。示例:ASCII 中的数字 8 是退格字符。

要使您的程序正常运行,您需要先将 48(十进制)x30(十六进制) 添加到您的号码中将其打印到控制台。

.ORIG x3000
  AND R0, R0, #0    ; Clear R0
  LD R0, NUM        ; load our number into R0
  LD R2, ASCII      ; load the ascii offset into R2
  ADD R0, R0, R2    
  OUT
HALT                ; Trap x25

NUM   .fill  x02    ; Our Number to print
ASCII .fill  x30    ; Our ASCII offset
.END

在您的示例中,您想要打印一个字符数组,例如 1234。这个概念非常相似,但我们需要使用指针和 for 循环。

.ORIG x3000
  AND R0, R0, #0    ; Clear R0
  AND R1, R1, #0    ; Clear R1
  AND R3, R3, #0    ; Clear R3
  LEA R0, NUM       ; pointer [mem]NUM
  ADD R1, R1, R0    ; Store the pointer address of R0 into R1
  LD R2, ASCII      ; load the ascii offset into R2

FOR_LOOP
  LDR R4, R1, #0    ; load the contents of mem address of R1 into R4
  BRz END_LOOP
  ADD R4, R4, R2    ; Add our number to the ASCII offset
  STR R4, R1, #0    ; Store the new value in R4 into [mem] address R1
  ADD R1, R1, #1    ; move our memory pointer down one
  BRnzp FOR_LOOP    ; loop again until we get an x00 char
END_LOOP

  PUTs              ; print our string starting from [mem]address in R0
HALT                ; Trap x25

ASCII .fill  x30    ; Our ASCII offset
NUM   .fill  x01    ; Our Number to print
      .fill  x02     
      .fill  x03
      .fill  x04
.END