我怎么写一个简单的LC-3程序

时间:2014-09-13 00:28:46

标签: lc3

如何编写一个简单的LC-3程序,比较R1和R2中的两个数字,如果R1 = R2则将值0置于R0中,如果R1> 1,则将值0置于R0。如果R1 <1,则R2和-1。 R2。

2 个答案:

答案 0 :(得分:2)

使用简单算法进行比较。

在我的例子中,我们比较2和6,你知道结果是什么。

           LD R1, NUMBER1    ;load NUMBER1 into R1
           LD R2, NUMBER2    ;load NUMBER1 into R2

           AND R6,R6,#0      ;initialize R0 with 0

           NOT R3, R2        ;R3 = -R2  (we negate NUMBER2)

           ADD R4, R3, R1    ;R4 = R1 - R2

           BRz Equals        ;we jump to Equals if NUMBER1 = NUMBER2  (we can just jump directly to END)
           BRn GreaterR2     ;we jump to GreaterR2 if NUMBER1 < NUMBER2
           BRp GreaterR1     ;we jump to GreaterR2 if NUMBER1 > NUMBER2

Equals     BRnzp End         ;nothing to do, because R0=0 (THIS IS NOT NECCESARY)

GreaterR2  ADD R0, R0, #-1   ;R0 = -1
           BRnzp End

GreaterR1  ADD R0, R0, #1    ;R0 = 1
           BRnzp End

Done       HALT               ;THE END


NUMBER1 .FILL #2              ;/ Here we declare the numbers we want to compare
NUMBER1 .FILL #6              ;\

答案 1 :(得分:0)

.ORIG x3000
AND R1, R1, x0
AND R2, R2, x0
LD R6, RESET
LEA R0, LINE1
PUTS
GETC 
OUT
ADD R1, R6, R0

LEA R0, LINE2
PUTS
GETC
OUT
ADD R2, R6, R0

JSR COMPARE
HALT
;////////// COMPARE FUNCTION BEGINS /////////////
COMPARE 
      AND R3, R3, x0
      NOT R2, R2
      ADD R2, R2, x1
      ADD R3, R1, R2
     BRn NEG
      ADD R3, R3, x0
     BRp POS
      ADD R3, R3, x0
     BRz EQ
      AND R5, R5, x0
      ADD R5, R5, R1
     RET
  NEG LEA R0, N ; triggers when R3 IS NEGATIVE
      PUTS
      RET
  POS LEA R0, P ; triggers when R3 IS POSITIVE
      PUTS
      RET
  EQ  LEA R0, E ; triggers when R3 IS ZERO
      PUTS
      RET

N .STRINGZ "\nX IS LESS THAN Y"
P .STRINGZ "\nX IS GREATER THAN Y"
E .STRINGZ "\nX IS EQUAL TO Y"
RESET .FILL xFFD0; RESET = -48 AS THIS IS ASCII RESETER FOR OUR PROGRAM
LINE1 .STRINGZ "ENTER X : "
LINE2 .STRINGZ "\nENTER Y : "
.END