horvathm

Day 08 Intcode (src)

Dec 8th, 2020 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # imports from libxib.a, which is the standard library
  2. # that comes with the xzintbit assembler
  3.  
  4. .IMPORT read_number
  5. .IMPORT get_input
  6. .IMPORT print_num
  7. .EXPORT report_libxib_error
  8.  
  9. # set up stack, call main function
  10.     arb stack
  11.  
  12.     call main
  13.     hlt
  14.  
  15. main:
  16. .FRAME acc, pos, tmp
  17.     arb -3
  18.  
  19.     add 0, 0, [rb + pos]
  20.  
  21. loop_parse:
  22.     # parse one line
  23.  
  24.     # read first character from the instruction
  25.     call get_input
  26.     eq [rb - 2], '.', [rb + tmp]
  27.     jnz [rb + tmp], end_parse
  28.  
  29.     add 10000, [rb + pos], [ip + 3]
  30.     add [rb - 2], 0, [0]
  31.  
  32.     # read rest of the instruction and space
  33.     call get_input
  34.     call get_input
  35.     call get_input
  36.  
  37.     # read sign into acc
  38.     add 1, 0, [rb + acc]
  39.     call get_input
  40.     eq  [rb - 2], '+', [rb + tmp]
  41.     jnz [rb + tmp], skip_neg
  42.     mul [rb + acc], -1, [rb + acc]
  43. skip_neg:
  44.  
  45.     # read the parameter
  46.     call read_number
  47.     #add [rb - 2], -'0', [rb - 2]
  48.     add 20000, [rb + pos], [ip + 3]
  49.     mul [rb - 2], [rb + acc], [0]
  50.  
  51.     # read line end
  52.     call get_input
  53.  
  54.     add [rb + pos], 1, [rb + pos]
  55.  
  56.     jz  0, loop_parse
  57.  
  58. end_parse:
  59.     add 0, 0, [rb + acc]
  60.     add 0, 0, [rb + pos]
  61.  
  62. loop_exec:
  63.     # mark visited instructions
  64.     add 30000, [rb + pos], [ip + 1]
  65.     jnz [0], done
  66.  
  67.     add 30000, [rb + pos], [ip + 3]
  68.     add 1, 0, [0]
  69.  
  70.     # decode instruction, execute it
  71.     add 10000, [rb + pos], [ip + 1]
  72.     eq  [0], 'n', [rb + tmp]
  73.     jnz [rb + tmp], exec_nop
  74.  
  75.     add 10000, [rb + pos], [ip + 1]
  76.     eq  [0], 'j', [rb + tmp]
  77.     jnz [rb + tmp], exec_jmp
  78.  
  79.     add 10000, [rb + pos], [ip + 1]
  80.     eq  [0], 'a', [rb + tmp]
  81.     jnz [rb + tmp], exec_acc
  82.  
  83. exec_nop:
  84.     add [rb + pos], 1, [rb + pos]
  85.     jz  0, loop_exec
  86.  
  87. exec_jmp:
  88.     add 20000, [rb + pos], [ip + 1]
  89.     add [0], [rb + pos], [rb + pos]
  90.  
  91.     jz  0, loop_exec
  92.  
  93. exec_acc:
  94.     add 20000, [rb + pos], [ip + 1]
  95.     add [0], [rb + acc], [rb + acc]
  96.  
  97.     add [rb + pos], 1, [rb + pos]
  98.  
  99.     jz  0, loop_exec
  100.  
  101. done:
  102.     # print the accumulator
  103.  
  104.     add [rb + acc], 0, [rb - 1]
  105.     arb -1
  106.     call print_num
  107.     out 10    # print '\n'
  108.  
  109.     arb 3
  110.     ret 0
  111. .ENDFRAME
  112.  
  113. # dummy function needed by libxib
  114. report_libxib_error:
  115.     hlt
  116.  
  117. # stack space, 50 memory locations, growing down
  118.     ds  50, 0
  119. stack:
  120.  
  121. .EOF
  122.  
Add Comment
Please, Sign In to add comment