将寄存器的内容打印到LC3程序集中的控制台 [英] Print contents of register to console in LC3 Assembly

查看:142
本文介绍了将寄存器的内容打印到LC3程序集中的控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Say I have a value (eg. 1234) that I load into R0. How could I print this value to the console?

推荐答案

我假设您想在控制台上打印一个数字,但是如果有的话,您会得到随机字符.

I'm assuming you would like to print a number out to the console but you're getting random characters if anything.

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

This happens when the LC3 tries to interpret your number as an ASCII character. Example: The number 8 in ASCII is the backspace character.

要使程序正常工作,您需要在电话号码上添加 48(十进制) x30(十六进制),然后才能将其打印到控制台上.

To make your program work you will need to add 48 (decimal) or x30 (hex) to your number before you can print it to the console.

.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循环.


In your example you want to print off an array of characters like 1234. The concept for this is pretty similar but we'll need to work with pointers and a for loop.

.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

这篇关于将寄存器的内容打印到LC3程序集中的控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆