Algorithms
Below are some helpful comparisons between different ways of acheiving the same result, starting from design, move on to high-level language, into low-level language and finally machine code.
This guide was originally intended to help with LMC implementation, but is also useful for general python deisgn/coding.
Comparison
Topic | FlowChart | PseudoCode | Python | Assembler | Machine Code |
---|---|---|---|---|---|
Assigning a variable | a ← 5 | a=5 | a DAT 5 | ||
Input | a ← USERINPUT | a=input() | INP STA a |
901 3xx |
|
Output | a ← 5 | a=5 | LDA a OUT |
5xx 902 |
|
Subtraction | a ← b - c | a=b-c | LDA b SUB c STA a |
5xx 2xx 3xx |
|
Selection: Greater than |
IF a > b THEN funcA() ELSE funcB() ENDIF |
a > b: funcA() else: funcB() |
LDA b SUB a BRP funcA BRA funB |
5xx 2xx 8xx 6xx |
|
Selection: Less than |
IF a < b THEN funcA() ELSE funcB() ENDIF |
a < b: funcA() else: funcB() |
LDA a SUB b BRP funcB BRA funcA |
5xx 2xx 8xx 6xx |
|
Selection: Equal to |
IF a = b THEN funcA() ELSE funcB() ENDIF |
if a == b: funcA() else: funcB() |
LDA a SUB b BRZ funcA BRA funcB |
5xx 2xx 7xx 6xx |
|
Selection: Not equal to |
IF a ≠ b THEN funcA() ELSE funcB() ENDIF |
if a ≠ b: funcA() else: funcB() |
LDA a SUB b BRZ funcB BRA funcA |
5xx 2xx 7xx 6xx |
|
Iteration: While loop |
WHILE a ≠ b OUTPUT b a ← USERINPUT ENDWHILE |
while a != b: print(b) a = input() |
while LDA a SUB b BRZ endwhile LDA b OUT INPU STA a BRA while endwhile HLT a DAT 0 b DAT 5 |
5xx 2xx 7xx 5xx 902 901 3xx 6xx 000 |
|
Iteration: For loop (count-up) |
FOR i ← 0 TO 9 OUTPUT i ENDFOR |
for i in range(10): print(i) |
for LDA i OUT ADD one STA i SUB ten BRZ endfor BRA for endfor HLT i DAT 0 one DAT 1 ten DAT 10 |
5xx 902 1xx 3xx 2xx 7xx 6xx 000 |
|
Iteration: For loop (count-down) |
FOR 1 ← 10 TO 1 OUTPUT i ENDFOR |
for i in range(10, 0, -1): print(i) |
for LDA i OUT SUB one STA i BRZ endfor BRA for endfor HLT 1 DAT 10 one DAT 1 |
5xx 902 2xx 3xx 7xx 6xx 000 |
Links
Some useful links for both LMC & Pseudocode.
LMC Instructions
Simple explanations for the various LMC commands.
Instructions | Mneumonic | Machine Code | Explanation |
---|---|---|---|
Add | ADD | 1xx | Adds the contents of memory xx to the ACCUMULATOR. |
Subtract | SUB | 2xx | Subtracts the contents of memory xx from the ACCUMULATOR. |
Store | STA | 3xx | Copies the contents of the ACCUMULATOR to memory xx. |
Load | LDA | 5xx | Copies the contents of memory xx to the ACCUMULATOR. |
Branch ALWAYS | BRA | 6xx | Always set the program counter to xx. The program continues from the instruction at memory xx. |
Branch if ZERO | BRZ | 7xx | Set the program counter to xx only of the ACCUMULATOR is zero. |
Branch if POSITIVE | BRP | 8xx | Set the program counter to xx only if the ACCUMULATOR is zero or positive (greater than zero). |
Input | INP | 901 | Copy the contents of the INBOX to the ACCUMULATOR. |
Output | OUT | 902 | Copy the contents of the ACCUMULATOR to the OUTBOX. |
Define Variable | DAT | Reserves this memory slot as a variable, settinmg its contents to the value specified. | |
Halt | HLT | 000 | Halts execution of the program. |