Search code examples
lc3

How do i change the value of R2 into a negative so i can minus two numbers?


I'm trying to minus two numbers but when I do NOT r2, r2 isn't becoming a negative any help?

; Start calculation
        NOT         R2, R2
        ADD     R2, R3, R2      ; The second operand is at R3   
        JSR     CONV
        AND     R0, R0, #0
        ADD     R0, R0, #10     ; Print a new line
        OUT
        BRnzp   REDO        
;

Solution

  • LC3 has no opcode for subtraction. It relies on adding the two's complement of the subtrahend to the minuend, in the unsigned sense and ignoring overflow. Two's complement is not the same as bitwise complement (i.e. NOT). The two's complement of a number is its bitwise complement (relative to the applicable bit width), plus one:

    NOT R2, R2
    ADD R2, R2, #1 ; This is missing from your version
    ADD R2, R3, R2
    

    With that said, I'm not sure what you expected to see when you say that "r2 isn't becoming a negative". With negative numbers represented in two's complement form, the difference between negative numbers and large positive numbers is a matter of interpretation, not form. For values of R2 that would be considered positive in a two's complement system, NOT R2 would definitely be considered negative -- just not the correct additive inverse.