Design an algorithm to input 3 numbers and then output the highest in little man computing

Janine Hawkins picture Janine Hawkins · Apr 18, 2016 · Viewed 13.9k times · Source

I can program it to output the highest of 2 numbers but am stuck on how to output the highest of 3 numbers input. Can anyone help please?

Answer

Paul Hankin picture Paul Hankin · Apr 18, 2016

In words:

read input into mailboxes M0, M1, M2
if M1 > M2
    store M1 into M2
if M0 > M2
    store M0 into M2
output M2

In little man computer assembler:

    INP
    STA M0
    INP
    STA M1
    INP
    STA M2

    SUB M1
    BRP J1
    LDA M1
    STA M2
J1  LDA M2
    SUB M0
    BRP J2
    LDA M0
    STA M2
J2  LDA M2
    OUT
    HLT
M0  DAT
M1  DAT
M2  DAT

You can run the program here: Max of 3 in LMC Emulator