Skip to content

Sample programs

Pavel Dovgalyuk edited this page Feb 27, 2019 · 57 revisions

Table of Contents

Fibonacci sequence

 ; A, B, or C contain successive elements of the sequence
  ORG 0
  MOVI B, 1    ; 00: 1000 0001 0000 0001 (8101h)
 Label:
  ADD C, B, A  ; 01: 0100 1010 0001 1000 (4B18h)
  MOV A, B     ; 02: 0001 1000 0001 0000 (1810h)
  MOV B, C     ; 03: 0001 1001 0010 0000 (1920h)
  JMP Label    ; 04: 1000 0111 0000 0001 (8701h)
 int a = 0;
 int b = 1;
 while (1=1)
 {
  c = a + b;
  a = b;
  b = c;
 }

https://www.youtube.com/watch?v=P9jCTSYUaJ8

Euclid's algorithm

  ; A, B - operands, A - result
  ORG 0
  MOVI A, op1 ; 00: 10000000 iiiiiiii
  MOVI B, op2 ; 01: 10000001 iiiiiiii
 Loop:
  OR F, A, A  ; 02: 01101000 00000000
  JMP Z, End  ; 03: 10010111 00001100
  OR F, B, B  ; 04: 01101000 00010001
  JMP Z, End  ; 05: 10010111 00001100
  SUB F, A, B ; 06: 01011000 00000001
  JMP C, L1   ; 07: 10110111 00001010
  SUB A, A, B ; 08: 01011000 00001001
  JMP Loop    ; 09: 10000111 00000010
 L1:
  SUB B, B, A ; 0A: 01011001 00011000
  JMP Loop    ; 0B: 10000111 00000010
 End:
  ADD A, A, B ; 0C: 01001000 00001001
  HALT        ; 0D: 00010000 00000000

https://www.youtube.com/watch?v=RIoIWR7b1Ps

Multiplication subroutines

TODO

8x8 -> 16

 ; C, D - operands, pair AB - result
  MOVI C, op1
  MOVI D, op2
  MOVI A, 0
  MOVI B, 0
  MOVI M, 0
 Loop:
  SHR C, C
  JMP NC, Next
  ADD A, A, D
  ADC B, B, M
 Next:
  ADD D, D, D
  ADC M, M, M
  OR C, C, C
  JMP NZ,Loop
  HALT

8x8 -> 8

 ; C, D - operands, A - result
  MOVI C, op1  ; 00: 10000010 iiiiiiii
  MOVI D, op2  ; 01: 10000011 iiiiiiii
  MOVI A, 0    ; 02: 10000000 00000000
 Loop:
  SHR C, C     ; 03: 01111010 -0101100
  JMP NC, Next ; 04: 11000111 00000110
  ADD A, A, D  ; 05: 01001000 00001011
 Next:
  ADD D, D, D  ; 06: 01001011 00111011
  OR F, C, C   ; 07: 01101000 00100010
  JMP NZ,Loop  ; 08: 11100111 00000011
  HALT         ; 09: 00010000 00000000

https://www.youtube.com/watch?v=hHF_ab6-ruQ

Division subroutines

8/8 -> 8

 ; D, M - operands, D - result, A - remainder
  MOVI D, op1  ; 00: 10000011 iiiiiiii
  MOVI M, op2  ; 01: 10000100 iiiiiiii
  MOVI A, 0    ; 02: 10000000 00000000
  MOVI C, 0    ; 03: 10000010 00000000
  MOVI B, 8    ; 04: 10000001 00001000
 Loop:
  SHR C, C     ; 05: 01111010 00101100
  ADC D, D, D  ; 06: 01000011 00111011
  ADC A, A, A  ; 07: 01000000 00001000
  SUB F, A, M  ; 08: 01011000 00000100
  ADC C, C, 0  ; 09: 01000010 10101000
  XOR C, C, 1  ; 0a: 01110010 10101001
  JMP Z, Next  ; 0b: 10010111 00001101
  SUB A, A, M  ; 0c: 01011000 00001100
 Next:
  SUB B, B, 1  ; 0d: 01011001 10011001
  JMP NZ, Loop ; 0e: 11100111 00000101
  SHR C, C     ; 0f: 01111010 00101100
  ADC D, D, D  ; 10: 01000011 00111011
  HALT         ; 11: 00010000 00000000

Games

Guess the number

First edition

Program tries to guess the number (1..100) by showing the guesses in A register and halting. User must set 'guess' field to non-zero if A is greater than the number and press Start button to continue.

  MOVI B, 1       ; 00: 10000001 00000001
  MOVI C, 101     ; 01: 10000010 01100101
 Loop:
  ADD A, B, C     ; 02: 01001000 00011010
  SHR A, A        ; 03: 01111000 00001100
  HALT            ; 04: 00010000 00000000
  MOVI D, guess   ; 05: 10000011 iiiiiiii
  OR F, D, D      ; 06: 01101000 00110011
  JMP NZ, Greater ; 07: 11100111 00001010
  MOV B, A        ; 08: 00011001 00000000
  JMP Loop        ; 09: 10000111 00000010
 Greater:
  MOV C, A        ; 0a: 00011010 00000000
  JMP Loop        ; 0b: 10000111 00000010

Second edition

Program tries to guess the number (1..100) by showing the guesses in A register and halting. User must reset D to zero if A is less than the number and press Start button to continue.

  MOVI B, 1       ; 00: 10000001 00000001
  MOVI C, 101     ; 01: 10000010 01100101
 Loop:
  ADD A, B, C     ; 02: 01001000 00011010
  SHR A, A        ; 03: 01111000 00001100
  MOVI D, 1       ; 04: 10000011 00000001
  HALT            ; 05: 00010000 00000000
  OR F, D, D      ; 06: 01101000 00110011
  JMP NZ, Greater ; 07: 11100111 00001010
  MOV B, A        ; 08: 00011001 00000000
  JMP Loop        ; 09: 10000111 00000010
 Greater:
  MOV C, A        ; 0a: 00011010 00000000
  JMP Loop        ; 0b: 10000111 00000010

The subtraction game (21 game)

There are 21 matches. Two players take 1..3 matches every turn. The player who takes the last match wins. The computer is the second player.

Program ends when A=0x00 or A=0xff. In the first case the winner is computer, in the second case - you are the winner.

First edition

When the execution stops, the number of matches is in register A, and the player's move should be encoded in the lower 8 bits of instruction at address 02 (which is the field 'move').

  MOVI A, 21   ; 00: 10000000 00001101
 Loop:
  HALT         ; 01: 00010000 00000000
  MOVI B, move ; 02: 10000001 000000mm
  SUB A, A, B  ; 03: 01011000 00001001
  AND C, A, 3  ; 04: 01100010 10001011
  MOVI Z, C, 1 ; 05: 10010010 00000001
  SUB A, A, C  ; 06: 01011000 00001010
  JMP Loop     ; 07: 10000111 00000001

Second edition

When the execution stops, player must take the 'matches' - reset 1 to 2 of B, C, D registers. Every register stores 1 'match' and the computer will subtract non-zero registers from A.

  MOVI A, 21   ; 00: 10000000 00010101
 Loop:
  MOVI B, 1    ; 01: 10000001 00000001
  MOVI C, 1    ; 02: 10000010 00000001
  MOVI D, 1    ; 03: 10000011 00000001
  HALT         ; 04: 00010000 00000000
  SUB A, A, B  ; 05: 01011000 00001001
  SUB A, A, C  ; 06: 01011000 00001010
  SUB A, A, D  ; 07: 01011000 00001011
  AND C, A, 3  ; 08: 01100010 10001011
  MOVI Z, C, 1 ; 09: 10010010 00000001
  SUB A, A, C  ; 0a: 01011000 00001010
  JC Exit      ; 0b: 10110111 00001101
  JNZ Loop     ; 0c: 11100111 00000001
 Exit:
  HALT         ; 0d: 00010000 00000000

Clone this wiki locally