assemblylittle-man-computer

Output and Reset Lists in LMC


I am working at this coding challenge:

Write a program for the Little Man Computer that allows the user to manage a list of values. It should start with an empty list and then process input as follows:

If the input is:

  • less than 100: add this value to a list, unless the list already has 10 values, in which case the value is ignored
  • 995: make the list empty
  • 996: output the number of values the list currently has
  • 997: output each value the list currently has, in the order they were added to it
  • 998: output each value the list currently has, in reversed order
  • 999: end the program
  • Any other value is ignored

The processing of input values continues as long as the input value is not 999.

I'm having issues getting the code to print the stored list in forward order when 997 is input. I think I may have the ADD and SUB instructions confused. I also can't get the stored list to reset correctly when 995 is input.

Everything else I was able to program correctly.

Below is my code:

START   INP
        STA TEMP
        SUB NINES
        BRZ end

        LDA TEMP
        SUB EIGHT
        BRZ PRIT

        lda temp
        sub seven
        brz printf

        LDA TEMP
        SUB SIX
        BRZ DOOUT

        LDA TEMP
        SUB FIVE
        BRZ RESET

        LDA COUNT
        SUB TEN
        BRZ START

        LDA TEMP
        SUB HUND
        BRP START

SIT     LDA SINST
        ADD COUNT
        STA SLOC
        LDA TEMP
SLOC    DAT 0
        LDA COUNT
        ADD ONE
        STA COUNT
        BRA START
PRIT    LDA COUNT
        BRZ END
PRINTR  LDA LINST
        ADD COUNT
        SUB ONE
        STA LDIT
LDIT    DAT 0
        OUT
        LDA COUNT
        SUB ONE
        STA COUNT
        BRZ END
        BRA PRINTR

---PRINTF  LDA LINST
        ADD COUNT
        add ONE
        STA LDIT
LDIT    DAT 0
        OUT
        LDA COUNT
        SUB ONE
        STA COUNT
        BRZ END
        BRA PRINTF 

doout   lda count
        out
        bra start

reset   lda zero
        sta count
        bra start

END     HLT

TEMP    DAT 0
COUNT   DAT 0
ONE     DAT 1
TWO     DAT 2
TEN     DAT 10
HUND    DAT 100
SINST   DAT 380
LINST   DAT 580
five    dat 995
six     dat 996
seven   dat 997
eight   dat 998
NINES   DAT 999

Solution

  • The issues in your program can be categorised in:

    1. Issues related to labels that a good simulator should detect before running your program
    2. Issues related to program logic, that make the program produce the wrong output
    3. Issues related to code style

    1. Labels

    A good simulator should not accept the following errors:

    2. Logic

    This will fix the semantic issues in your program.

    3. Code style

    Corrected program

    Taking all of the above into account, the program could look like this:

    #input:1 2 3 997 998 999
    clear_list        LDA zero # Start by resetting the list
                      STA list_size
    
    start             INP
                      STA input
                      SUB menu_nine
                      BRP end
    
                      LDA input
                      SUB menu_eight
                      BRP output_reversed
    
                      LDA input
                      SUB menu_seven
                      BRP output_forward
    
                      LDA input
                      SUB menu_six
                      BRP output_size
    
                      LDA input
                      SUB menu_five
                      BRP clear_list
    
                      LDA list_size
                      SUB max_list_size
                      BRP start
    
                      LDA input
                      SUB input_limit
                      BRP start
    
                      LDA store_instruction
                      ADD list_size
                      STA store
                      LDA input
    store             DAT 0
                      LDA list_size
                      ADD one
                      STA list_size
                      BRA start
    
    output_reversed   LDA load_instruction
                      ADD list_size
    loop_reversed     SUB one
                      STA load_reversed
                      SUB load_instruction # are we still within the list?
                      BRP load_reversed # yes, continue printing
                      BRA start
    load_reversed     DAT 0
                      OUT
                      LDA load_reversed # decrement the dynamic LDA instruction
                      BRA loop_reversed
    
    output_forward    LDA load_instruction
    loop_forward      STA load_forward
                      SUB load_instruction # get relative offset in the list
                      SUB list_size # are we still within the list?
                      BRP start # no, stop printing
    load_forward      DAT 0
                      OUT
                      LDA load_forward # increment the dynamic LDA instruction
                      ADD one
                      BRA loop_forward
    
    output_size       LDA list_size
                      OUT
                      BRA start
    
    end               HLT
    
    # constants
    zero              DAT 0
    one               DAT 1
    two               DAT 2
    max_list_size     DAT 10
    input_limit       DAT 100
    load_instruction  LDA list
    store_instruction STA list
    menu_five         DAT 995
    menu_six          DAT 996
    menu_seven        DAT 997
    menu_eight        DAT 998
    menu_nine         DAT 999
    # variables
    input             DAT 0
    list_size         DAT 0
    list              DAT
                      DAT
                      DAT
                      DAT
                      DAT
                      DAT
                      DAT
                      DAT
                      DAT
                      DAT
    
    
    
    <script src="https://cdn.jsdelivr.net/gh/trincot/lmc@v0.816/lmc.js"></script>

    You can run the code right here, using this snippet. Click Run Code Snippet to activate a LMC simulator, and then use the panel at the right side to interact with it.