1

所以我是第一次学习 LC-3 机器的组装,所以我还是个新手。我试图让我的程序读取 2 个字符,比较它们,然后输出较小的字符。到目前为止,我所做的是将第一个字符存储在寄存器 1 中,将第二个字符存储在寄存器 2 中。然后,我想减去 ASCII 值,然后如果结果 < 0,则第一个字符会更小,如果它 > 0 第二个会更小,如果它 = 0,那么它们将是相同的字符。

我的问题是我不知道如何减去寄存器中的 2 个字符。我尝试过在线搜索,查看我的笔记,但找不到任何可以回答我的问题的东西。到目前为止,这是我的代码:

.orig x3000
LEA R0, msg ;loads message into R0
PUTS    ;puts message to screen

GETC    ;reads a char
OUT ;puts char to screen
ADD R1, R1, R0  ;R1 <- R0

LEA R0, msg ;loads message into R0 again
PUTS    ;puts message to screen
GETC    ;reads another char
OUT ;puts char to screen
ADD R2, R2, R0  ;R2 <- R0

ADD R1, R1, -R2 ;subtract second character from first character
                ;This line here is my problem!!!!

BRP elseif  ;if it's positive, the second character is larger
BRZ else    ;if it's zero, they are the same

if  
    LEA R0, msg2    ;loads second message
    PUTS    ;puts second message on screen
    AND R0, R0, #0  ;clears R0
    ADD R0, R0, R1  ;R0 <- R1
    OUT ;print the first char
    br endif

elseif  
    LEA R0, msg2    ;loads second message
    PUTS    ;puts second message on screen
    AND R0, R0, #0  ;clears R0
    ADD R0, R0, R2  ;R0 <- R2
    OUT ;print the second char
    br endif

else    
    LEA R0, msg3    ;loads third message
    br endif

endif

    HALT

msg .STRINGZ "\nEnter any character: "
msg2    .STRINGZ "\nThe smallest character is: "
msg3    .STRINGZ "\nThe characters are the same."
    .END

请记住,这就像我的第一个汇编程序,所以它可能非常可怕。^_^" 但是由于我还在学习,所以我想保持简单直接,即使它是一种愚蠢的实现方式。

因此,在我的 break 语句之前的那一行是我遇到麻烦的地方。如上所示,我尝试添加寄存器 1 和寄存器 2 的负数,但这不起作用,所以基本上我的问题是,有没有办法可以减去/比较存储在 2 个寄存器中的两个 char 值?

任何帮助/建议将不胜感激!!:)

4

1 回答 1

1

没有使用 LC3 进行减法的直接运算符。您需要使用 2 的补码。

R1 = R2-R3;  //This is not possible.
// using 2's Complement method
R1 <- NOT R3;
R1 <- R1+1;
R1 <- R2+R1;

寄存器 R1 将有减法结果。您可以使用比较代码打印出较小的数字

于 2015-02-02T04:47:36.357 回答