Search code examples
loopslc3

LC-3 Decimal to Binary Converter


I have to create a program that converts the given user input (decimal) into its binary match when the user presses enter or exit when they press X. Can anyone give a pointer on how to start this assignment. We are required to use masks and loops, but I do not know where to start with this.


Solution

  • I wrote this because there aren't a whole lot of examples of binary masks.

    A binary mask is useful in assembly because it gives us the power to examine a single bit in a decimal or hex value.

    Example: If we look at the decimal number 5 we know its binary value is 0101, when we AND R4, R4, R5 on our first loop we compare 1000 with 0101. Since bit[3] is a zero in 0101 we then tell the LC3 simulator to print out an ASCII char "0". We then repeat the process with the next binary mask 0100.

    This can be a tricky concept to get at first so I would first look at a few examples of loops and bit masks.

    LC3 Bit Counter

    How do I write a program that prints out “Hello World”, 5 times using a loop in LC3?

    .ORIG x3000
    
        LEA R0, PROMPT
        PUTs                ; TRAP x22
        LD R0, ENTER
        OUT                 ; TRAP x21
        IN                  ; TRAP x23
    
        AND R5, R5, #0      ; clear R5
        ADD R5, R5, R0      ; Store the user input into R5
    
        AND R1, R1, #0      ; clear R1, R1 is our loop count
        LD R2, MASK_COUNT   ; load our mask limit into R2
        NOT R2, R2          ; Invert the bits in R2
        ADD R2, R2, #1      ; because of 2's compliment we have
                            ; to add 1 to R2 to get -4
    WHILE_LOOP
        ADD R3, R1, R2      ; Adding R1, and R2 to see if they'll
                            ; will equal zero
        BRz LOOP_END        ; If R1+R2=0 then we've looped 4
                            ; times and need to exit
    
        LEA R3, BINARY      ; load the first memory location 
                            ; in our binary mask array
        ADD R3, R3, R1      ; use R1 as our array index and
                            ; add that to the first array location
        LDR R4, R3, #0      ; load the next binary mask into R4
    
        AND R4, R4, R5      ; AND the user input with the 
                            ; binary mask
        BRz NO_BIT
        LD R0, ASCII_ONE
        OUT                 ; TRAP x21
        ADD R1, R1, #1      ; add one to our loop counter
        BRnzp WHILE_LOOP    ; loop again
    NO_BIT
        LD R0, ASCII_ZERO
        OUT                 ; TRAP x21
    
        ADD R1, R1, #1      ; add one to our loop counter
        BRnzp WHILE_LOOP    ; loop again
    LOOP_END
    
        LD R0, ENTER
        OUT                 ; TRAP x21
        HALT                ; TRAP x25
    
    ; Binary Maps
    BINARY  .FILL   b0000000000001000
            .FILL   b0000000000000100
            .FILL   b0000000000000010
            .FILL   b0000000000000001
            .FILL   b0000000000000000
    
    ; Stored Values
    ENTER       .FILL   x000A
    ASCII_ZERO  .FILL   x0030
    ASCII_ONE   .FILL   x0031
    MASK_COUNT  .FILL   x04     ; loop limit = 4
    PROMPT      .STRINGZ "Enter a number from 0-9"
    
    .END