###################
# INSTRUCTION-SET #
###################

# INSTR   IR15 IR14 IR13 IR12 IR11 IR10 IR09 IR08 IR07 IR06 IR05 IR04 IR03 IR02 IR01 IR00  
# MOVE    0    0    0    0    RD   RD   X    X    K    K    K    K    K    K    K    K
# ADD     0    0    0    1    RD   RD   X    X    K    K    K    K    K    K    K    K
# SUB     0    0    1    0    RD   RD   X    X    K    K    K    K    K    K    K    K
# AND     0    0    1    1    RD   RD   X    X    K    K    K    K    K    K    K    K

# LOAD    0    1    0    0    A    A    A    A    A    A    A    A    A    A    A    A
# STORE   0    1    0    1    A    A    A    A    A    A    A    A    A    A    A    A
# ADDM    0    1    1    0    A    A    A    A    A    A    A    A    A    A    A    A
# SUBM    0    1    1    1    A    A    A    A    A    A    A    A    A    A    A    A

# JUMPU   1    0    0    0    A    A    A    A    A    A    A    A    A    A    A    A
# JUMPZ   1    0    0    1    A    A    A    A    A    A    A    A    A    A    A    A
# JUMPNZ  1    0    1    0    A    A    A    A    A    A    A    A    A    A    A    A
# JUMPC   1    0    1    1    A    A    A    A    A    A    A    A    A    A    A    A 

# CALL    1    1    0    0    A    A    A    A    A    A    A    A    A    A    A    A

# OR      1    1    0    1    RD   RD   X    X    K    K    K    K    K    K    K    K  -- Version 1.2
# XOP1    1    1    1    0    U    U    U    U    U    U    U    U    U    U    U    U  -- NOT IMPLEMENTED

# RET     1    1    1    1    X    X    X    X    X    X    X    X    0    0    0    0
# MOVE    1    1    1    1    RD   RD   RS   RS   X    X    X    X    0    0    0    1
# LOAD    1    1    1    1    RD   RD   RS   RS   X    X    X    X    0    0    1    0  -- REG INDIRECT
# STORE   1    1    1    1    RD   RD   RS   RS   X    X    X    X    0    0    1    1  -- REG INDIRECT   
# ROL     1    1    1    1    RSD  RSD  X    X    X    X    X    X    0    1    0    0  -- Version 1.1

# ROR     1    1    1    1    RSD  RSD  X    X    X    X    X    X    0    1    0    1  -- NOT IMPLEMENTED
# ADD     1    1    1    1    RD   RD   RS   RS   X    X    X    X    0    1    1    0  -- NOT IMPLEMENTED
# SUB     1    1    1    1    RD   RD   RS   RS   X    X    X    X    0    1    1    1  -- NOT IMPLEMENTED
# AND     1    1    1    1    RD   RD   RS   RS   X    X    X    X    1    0    0    0  -- NOT IMPLEMENTED
# OR      1    1    1    1    RD   RD   RS   RS   X    X    X    X    1    0    0    1  -- NOT IMPLEMENTED
# XOR     1    1    1    1    RD   RD   RS   RS   X    X    X    X    1    0    1    0  -- Version 1.1
# ASL     1    1    1    1    RD   RD   RS   RS   X    X    X    X    1    0    1    1  -- Version 1.2

# XOP2    1    1    1    1    RD   RD   RS   RS   X    X    X    X    1    1    0    0  -- NOT IMPLEMENTED REG INDIRECT
# XOP3    1    1    1    1    RD   RD   RS   RS   X    X    X    X    1    1    0    1  -- NOT IMPLEMENTED
# XOP4    1    1    1    1    RD   RD   RS   RS   X    X    X    X    1    1    1    0  -- NOT IMPLEMENTED REG INDIRECT
# XOP5    1    1    1    1    RD   RD   RS   RS   X    X    X    X    1    1    1    1  -- NOT IMPLEMENTED


########
# CODE #
########

start:
  move ra 1         # ra = 1 
  move rb 2         # rb = 2
  move rc 3         # rc = 3
  move rd 4         # rd = 4

  move ra rd        # ra = 4
  move rb rc        # rb = 3
  move rc rb        # rc = 3
  move rd ra        # rd = 4

  add ra 1          # ra = 5
  add rb 2          # rb = 5
  add rc 3          # rc = 6
  add rd 4          # rd = 8

  sub ra 4          # ra = 1
  sub rb 3          # ra = 2
  sub rc 2          # ra = 4
  sub rd 1          # ra = 7

  and ra 1          # ra = 1
  and rb 2          # rb = 0
  and rc 3          # rc = 0
  and rd 4          # rd = 0

  move ra 1         # ra = 1
  store ra A        # A  = 1 
  move ra 2         # ra = 2
  store ra B        # B  = 2
  move ra 3         # ra = 3
  store ra C        # C  = 3
  move ra 4         # ra = 4
  store ra D        # D  = 4

  load ra A         # ra = 1
  load ra B         # ra = 2
  load ra C         # ra = 3
  load ra D         # ra = 4 

  addm ra A         # ra = 5
  addm ra B         # ra = 7
  addm ra C         # ra = 10
  addm ra D         # ra = 14 

  subm ra A         # ra = 13 
  subm ra B         # ra = 11 
  subm ra C         # ra = 8
  subm ra D         # ra = 4 

  and ra 0          # ra = 0
  jumpz b1          # TAKEN
  move rb 1         # set rb to 1 if error

b1:
  add ra 1          # ra = 1
  jumpnz b2         # TAKEN
  move rb 1         # set rb to 1 if error

b2:
  move ra 255       # ra = 0xFFFF
  add ra 1          # ra = 0 c=1
  jumpc b3          # TAKEN
  move rb 1         # set rb to 1 if error

b3:
  and ra 0          # ra = 0
  jumpnz b4         # FALSE
  jumpu b5          # unconditional jump
b4:
  move rb 1         # set rb to 1 if error

b5:
  add ra 1          # ra = 1
  jumpz b6          # FALSE
  jumpu b7          # unconditional jump
b6:
  move rb 1         # set rb to 1 if error

b7:
  move ra 255       # ra = 0xFFFF
  add ra 0          
  jumpc b8          # FALSE
  jumpu b9          # unconditional jump
b8:
  move rb 1         # set rb to 1 if error

b9:
  call sub_a        # call subroutine sub_a
  jumpu start       # jump back to start

sub_a:
  move ra 1         # ra = 1
  call sub_b        # call subroutine sub_b
  ret               # return to main program

sub_b:
  move ra 2         # ra = 2
  call sub_c        # call subroutine sub_c
  ret               # return to subroutine sub_a

sub_c:
  move ra 3         # ra = 3
  call sub_d        # call subroutine sub_d
  ret               # return to subroutine sub_b

sub_d:           
  move ra 4         # ra = 4
  ret               # return to subroutine sub_c

########
# DATA #
########

A:
    .data 0
B:
    .data 0
C:
    .data 0
D:
    .data 0


















