assemblylittle-man-computer

Input three numbers into the LMC and output them largest to smallest?


So, I've managed to get the bulk of the problem done however I can only output from largest to smallest if the input values are ascending? first = 10, second = 20, third = 30. How would I output this properly if the values were different? (Say, 5,1,3? for example)

This is what I have so far:

        INP
        STA first
        INP
        STA second 
        INP
        STA third
        SUB first
        BRP branch1 
        LDA second 
        STA third 
branch1 LDA third 
        SUB second
        BRP branch2 
        LDA first
        STA third 
branch2 LDA third 
        OUT
        LDA first
        SUB second
        BRP branch3
        LDA second
        OUT
branch3 LDA first
        OUT
        HLT
first   DAT
second DAT
third   DAT

Solution

  • Some issues:

    Here is the corrected script, which you can run here:

    #input: 3 1 2
            INP
            STA first
            INP
            STA second 
            INP
            STA third
            SUB first
            BRP branch1
            LDA first   # don't deal with second, but with first
            STA temp    # use temp to perform swap first <--> third
            LDA third
            STA first
            LDA temp
            STA third
    branch1 LDA third   # at this point we know first <= third
            SUB second
            BRP branch2 
            LDA second  # don't deal with first, but with second
            STA temp    # use temp to perform swap second <--> third
            LDA third
            STA second
            LDA temp
            STA third 
    branch2 LDA third   # at this point we know first <= third && second <= third 
            OUT
            LDA first
            SUB second
            BRP branch3
            LDA second
            OUT
            LDA first   # We cannot use branch3 here, as there we still output second
            OUT
            HLT
    branch3 LDA first
            OUT
            LDA second
            OUT
            HLT
    first   DAT
    second  DAT
    third   DAT
    temp    DAT
    
    <script src="https://cdn.jsdelivr.net/gh/trincot/lmc@v0.816/lmc.js"></script>