2015-10-28 146 views
1

说我有一个值(例如1234),我加载到R0中。我怎么能打印这个值到控制台?在LC3中打印控制台寄存器的内容汇编

+0

查阅您正在使用的模拟器/仿真器的手册,看看是否有任何可调用的打印整数的例程。否则,看看是否有任何例程打印字符串。在这种情况下,您必须首先将您的整数转换为字符串。 – Michael

+0

我可以使用PUTS打印一个字符串,它以存储在R0中的值开始。在那种情况下,我如何将我的值转换为字符串? – sa044512

回答

1

我假设你想打印出一个数字到控制台,但你会得到随机字符,如果有的话。

当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 
相关问题