sectorlisp.S 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
  2. │vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│
  3. ╞══════════════════════════════════════════════════════════════════════════════╡
  4. │ Copyright 2020 Justine Alexandra Roberts Tunney │
  5. │ Copyright 2021 Alain Greppin │
  6. │ Some size optimisations by Peter Ferrie │
  7. │ │
  8. │ Permission to use, copy, modify, and/or distribute this software for │
  9. │ any purpose with or without fee is hereby granted, provided that the │
  10. │ above copyright notice and this permission notice appear in all copies. │
  11. │ │
  12. │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
  13. │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
  14. │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
  15. │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
  16. │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
  17. │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
  18. │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
  19. │ PERFORMANCE OF THIS SOFTWARE. │
  20. ╚─────────────────────────────────────────────────────────────────────────────*/
  21. // LISP meta-circular evaluator in a MBR
  22. // Compatible with the original hardware
  23. .code16
  24. .globl _start
  25. _start: .asciz "NIL" # dec %si ; dec %cx ; dec %sp
  26. kT: .asciz "T" # add %dl,(%si) boot A:\ DL=0
  27. start: ljmp $0x7c00>>4,$begin # cs = 0x7c00 is boot address
  28. .asciz ""
  29. kQuote: .asciz "QUOTE"
  30. kCond: .asciz "COND"
  31. kAtom: .asciz "ATOM" # ordering matters
  32. kCar: .asciz "CAR" # ordering matters
  33. kCdr: .asciz "CDR" # ordering matters
  34. kCons: .asciz "CONS" # ordering matters
  35. kEq: .asciz "EQ" # needs to be last
  36. begin: push %cs # that means ss = ds = es = cs
  37. pop %ds # noting ljmp set cs to 0x7c00
  38. push %cs # that's the bios load address
  39. pop %es # therefore NULL points to NUL
  40. push %cs # terminated NIL string above!
  41. pop %ss # errata exists but don't care
  42. xor %sp,%sp # use highest address as stack
  43. mov $2,%bx
  44. main: mov $0x8000,%cx # dl (g_look) is zero or cr
  45. call GetToken
  46. call GetObject
  47. call Eval
  48. xchg %ax,%si
  49. call PrintObject
  50. mov $'\r',%al
  51. call PutChar
  52. jmp main
  53. GetToken: # GetToken():al, dl is g_look
  54. mov %cx,%di
  55. 1: mov %dl,%al
  56. cmp $' ',%al
  57. jbe 2f
  58. stosb
  59. xchg %ax,%si
  60. 2: call GetChar # exchanges dx and ax
  61. cmp $' ',%al
  62. jbe 1b
  63. cmp $')',%al
  64. jbe 3f
  65. cmp $')',%dl # dl = g_look
  66. ja 1b
  67. 3: mov %bh,(%di) # bh is zero
  68. xchg %si,%ax
  69. ret
  70. .PrintList:
  71. mov $'(',%al
  72. 2: push (%bx,%si)
  73. mov (%si),%si
  74. call .PutObject
  75. mov $' ',%al
  76. pop %si # restore 1
  77. test %si,%si
  78. js 2b # jump if cons
  79. jz 4f # jump if nil
  80. mov $249,%al # bullet (A∙B)
  81. call .PutObject
  82. 4: mov $')',%al
  83. jmp PutChar
  84. .PutObject: # .PutObject(c:al,x:si)
  85. .PrintString: # nul-terminated in si
  86. call PutChar # preserves si
  87. PrintObject: # PrintObject(x:si)
  88. test %si,%si # set sf=1 if cons
  89. js .PrintList # jump if not cons
  90. .PrintAtom:
  91. lodsb
  92. test %al,%al # test for nul terminator
  93. jnz .PrintString # -> ret
  94. ret
  95. GetObject: # called just after GetToken
  96. cmp $'(',%al
  97. je GetList
  98. # jmp Intern
  99. Intern: push %cx # Intern(cx,di): ax
  100. mov %di,%bp
  101. sub %cx,%bp
  102. inc %bp
  103. xor %di,%di
  104. 1: pop %si
  105. push %si
  106. mov %bp,%cx
  107. mov %di,%ax
  108. cmp %bh,(%di)
  109. je 2f
  110. rep cmpsb # memcmp(di,si,cx)
  111. je 9f
  112. not %cx
  113. xor %ax,%ax
  114. repne scasb # memchr(di,al,cx)
  115. jmp 1b
  116. 2: rep movsb # memcpy(di,si,cx)
  117. 9: pop %cx
  118. ret
  119. GetChar:xor %ax,%ax # GetChar→al:dl
  120. int $0x16 # get keystroke
  121. PutChar:mov $0x0e,%ah # prints CP-437
  122. int $0x10 # vidya service
  123. cmp $'\r',%al # don't clobber
  124. jne 1f # look xchg ret
  125. mov $'\n',%al
  126. jmp PutChar
  127. ////////////////////////////////////////////////////////////////////////////////
  128. Pairlis:test %di,%di # Pairlis(x:di,y:si,a:dx):ax
  129. jz 1f # jump if nil
  130. push (%bx,%di) # save 1 Cdr(x)
  131. lodsw
  132. push (%si) # save 2 Cdr(y)
  133. mov (%di),%di
  134. call Cons # preserves dx
  135. pop %si # restore 2
  136. pop %di # restore 1
  137. push %ax # save 3
  138. call Pairlis
  139. jmp xCons # can be inlined here
  140. 1: xchg %dx,%ax
  141. ret
  142. Evlis: test %di,%di # Evlis(m:di,a:dx):ax
  143. jz 1f # jump if nil
  144. push (%bx,%di) # save 1 Cdr(m)
  145. mov (%di),%ax
  146. call Eval
  147. pop %di # restore 1
  148. push %ax # save 2
  149. call Evlis
  150. # jmp xCons
  151. xCons: pop %di # restore 2
  152. Cons: xchg %di,%cx # Cons(m:di,a:ax):ax
  153. mov %cx,(%di)
  154. mov %ax,(%bx,%di)
  155. lea 4(%di),%cx
  156. 1: xchg %di,%ax
  157. ret
  158. Gc: cmp %dx,%di # Gc(x:di,A:dx,B:si):ax
  159. jb 1b # we assume immutable cells
  160. push (%bx,%di) # mark prevents negative gc
  161. mov (%di),%di
  162. call Gc
  163. pop %di
  164. push %ax
  165. call Gc
  166. pop %di
  167. call Cons
  168. sub %si,%ax # ax -= C - B
  169. add %dx,%ax
  170. ret
  171. GetList:call GetToken
  172. cmp $')',%al
  173. je .retF
  174. call GetObject
  175. push %ax # popped by xCons
  176. call GetList
  177. jmp xCons
  178. .dflt1: push %si # save x
  179. call Eval
  180. pop %si # restore x
  181. # jmp Apply
  182. Apply: test %ax,%ax # Apply(fn:ax,x:si:a:dx):ax
  183. jns .switch # jump if atom
  184. xchg %ax,%di # di = fn
  185. .lambda:mov (%bx,%di),%di # di = Cdr(fn)
  186. push %di # save 1
  187. mov (%di),%di # di = Cadr(fn)
  188. call Pairlis
  189. xchg %ax,%dx
  190. pop %di # restore 1
  191. jmp .EvCadr
  192. .switch:cmp $kEq,%ax # eq is last builtin atom
  193. ja .dflt1 # ah is zero if not above
  194. mov (%si),%di # di = Car(x)
  195. .ifCar: cmp $kCar,%al
  196. je Car
  197. .ifCdr: cmp $kCdr,%al
  198. je Cdr
  199. .ifAtom:cmp $kAtom,%al
  200. jne .ifCons
  201. test %di,%di # test if atom
  202. jns .retT
  203. .retF: xor %ax,%ax # ax = nil
  204. ret
  205. .ifCons:cmp $kCons,%al
  206. mov (%bx,%si),%si # si = Cdr(x)
  207. lodsw # si = Cadr(x)
  208. je Cons
  209. .isEq: cmp %di,%ax # we know for certain it's eq
  210. jne .retF
  211. .retT: mov $kT,%ax
  212. ret
  213. Cadr: mov (%bx,%di),%di # contents of decrement register
  214. .byte 0x3C # cmp §scasw,%al (nop next byte)
  215. Cdr: scasw # increments our data index by 2
  216. Car: mov (%di),%ax # contents of address register!!
  217. 2: ret
  218. Assoc: mov %dx,%si # Assoc(x:ax,y:dx):ax
  219. 1: mov (%si),%di
  220. mov (%bx,%si),%si
  221. scasw
  222. jne 1b
  223. jmp Car
  224. 1: mov (%bx,%di),%di # di = Cdr(c)
  225. Evcon: push %di # save c
  226. mov (%di),%si # di = Car(c)
  227. lodsw # ax = Caar(c)
  228. call Eval
  229. pop %di # restore c
  230. test %ax,%ax # nil test
  231. jz 1b
  232. mov (%di),%di # di = Car(c)
  233. .EvCadr:call Cadr # ax = Cadar(c)
  234. # jmp Eval
  235. Eval: test %ax,%ax # Eval(e:ax,a:dx):ax
  236. jz 1f
  237. jns Assoc # lookup val if atom
  238. xchg %ax,%si # di = e
  239. lodsw # ax = Car(e)
  240. cmp $kQuote,%ax # maybe CONS
  241. mov (%si),%di # di = Cdr(e)
  242. je Car
  243. cmp $kCond,%ax
  244. je Evcon # ABC Garbage Collector
  245. push %dx # save a
  246. push %cx # save A
  247. push %ax
  248. call Evlis
  249. xchg %ax,%si
  250. pop %ax
  251. call Apply
  252. pop %dx # restore A
  253. mov %cx,%si # si = B
  254. xchg %ax,%di
  255. call Gc
  256. mov %dx,%di # di = A
  257. sub %si,%cx # cx = C - B
  258. rep movsb
  259. mov %di,%cx # cx = A + (C - B)
  260. pop %dx # restore a
  261. 1: ret
  262. .sig: .fill 510 - (. - _start), 1, 0xce
  263. .word 0xAA55
  264. .type .sig,@object
  265. .type kQuote,@object
  266. .type kCond,@object
  267. .type kAtom,@object
  268. .type kCar,@object
  269. .type kCdr,@object
  270. .type kCons,@object
  271. .type kEq,@object