sectorlisp.S 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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: mov $0x8000,%sp # uses higher address as stack
  37. # and set independently of SS!
  38. # 8088 doesn't stop interrupts
  39. # after SS is set, and PC BIOS
  40. # sets SP to a value that will
  41. # damage our code if int fires
  42. # between it setting SS and SP
  43. push %cs # that means ss = ds = es = cs
  44. pop %ds # noting ljmp set cs to 0x7c00
  45. push %cs # that's the bios load address
  46. pop %es # therefore NULL points to NUL
  47. push %cs # terminated NIL string above!
  48. pop %ss # errata exists but don't care
  49. mov $2,%bx
  50. main: mov %sp,%cx
  51. call GetToken
  52. call GetObject
  53. call Eval
  54. xchg %ax,%si
  55. call PrintObject
  56. mov $'\r',%al
  57. call PutChar
  58. jmp main
  59. GetToken: # GetToken():al, dl is g_look
  60. mov %cx,%di
  61. 1: mov %dl,%al
  62. cmp $' ',%al
  63. jbe 2f
  64. stosb
  65. xchg %ax,%si
  66. 2: call GetChar # exchanges dx and ax
  67. cmp $' ',%al
  68. jbe 1b
  69. cmp $')',%al
  70. jbe 3f
  71. cmp $')',%dl # dl = g_look
  72. ja 1b
  73. 3: mov %bh,(%di) # bh is zero
  74. xchg %si,%ax
  75. ret
  76. .PrintList:
  77. mov $'(',%al
  78. 2: push (%bx,%si)
  79. mov (%si),%si
  80. call .PutObject
  81. mov $' ',%al
  82. pop %si # restore 1
  83. test %si,%si
  84. js 2b # jump if cons
  85. jz 4f # jump if nil
  86. mov $249,%al # bullet (A∙B)
  87. call .PutObject
  88. 4: mov $')',%al
  89. jmp PutChar
  90. .PutObject: # .PutObject(c:al,x:si)
  91. .PrintString: # nul-terminated in si
  92. call PutChar # preserves si
  93. PrintObject: # PrintObject(x:si)
  94. test %si,%si # set sf=1 if cons
  95. js .PrintList # jump if not cons
  96. .PrintAtom:
  97. lodsb
  98. test %al,%al # test for nul terminator
  99. jnz .PrintString # -> ret
  100. ret
  101. GetObject: # called just after GetToken
  102. cmp $'(',%al
  103. je GetList
  104. # jmp Intern
  105. Intern: push %cx # Intern(cx,di): ax
  106. mov %di,%bp
  107. sub %cx,%bp
  108. inc %bp
  109. xor %di,%di
  110. 1: pop %si
  111. push %si
  112. mov %bp,%cx
  113. mov %di,%ax
  114. cmp %bh,(%di)
  115. je 8f
  116. rep cmpsb # memcmp(di,si,cx)
  117. je 9f
  118. xor %ax,%ax
  119. 2: scasb # memchr(di,al,cx)
  120. jne 2b
  121. jmp 1b
  122. 8: rep movsb # memcpy(di,si,cx)
  123. 9: pop %cx
  124. ret
  125. GetChar:xor %ax,%ax # GetChar→al:dl
  126. int $0x16 # get keystroke
  127. PutChar:mov $0x0e,%ah # prints CP-437
  128. int $0x10 # vidya service
  129. cmp $'\r',%al # don't clobber
  130. jne 1f # look xchg ret
  131. mov $'\n',%al
  132. jmp PutChar
  133. ////////////////////////////////////////////////////////////////////////////////
  134. Pairlis:test %di,%di # Pairlis(x:di,y:si,a:dx):ax
  135. jz 1f # jump if nil
  136. push (%bx,%di) # save 1 Cdr(x)
  137. lodsw
  138. push (%si) # save 2 Cdr(y)
  139. mov (%di),%di
  140. call Cons # preserves dx
  141. pop %si # restore 2
  142. pop %di # restore 1
  143. push %ax # save 3
  144. call Pairlis
  145. jmp xCons # can be inlined here
  146. 1: xchg %dx,%ax
  147. ret
  148. Evlis: test %di,%di # Evlis(m:di,a:dx):ax
  149. jz 1f # jump if nil
  150. push (%bx,%di) # save 1 Cdr(m)
  151. mov (%di),%ax
  152. call Eval
  153. pop %di # restore 1
  154. push %ax # save 2
  155. call Evlis
  156. # jmp xCons
  157. xCons: pop %di # restore 2
  158. Cons: xchg %di,%cx # Cons(m:di,a:ax):ax
  159. mov %cx,(%di)
  160. mov %ax,(%bx,%di)
  161. lea 4(%di),%cx
  162. 1: xchg %di,%ax
  163. ret
  164. Gc: cmp %dx,%di # Gc(x:di,A:dx,B:si):ax
  165. jb 1b # we assume immutable cells
  166. push (%bx,%di) # mark prevents negative gc
  167. mov (%di),%di
  168. call Gc
  169. pop %di
  170. push %ax
  171. call Gc
  172. pop %di
  173. call Cons
  174. sub %si,%ax # ax -= C - B
  175. add %dx,%ax
  176. ret
  177. GetList:call GetToken
  178. cmp $')',%al
  179. je .retF
  180. call GetObject
  181. push %ax # popped by xCons
  182. call GetList
  183. jmp xCons
  184. .dflt1: push %si # save x
  185. call Eval
  186. pop %si # restore x
  187. # jmp Apply
  188. Apply: test %ax,%ax # Apply(fn:ax,x:si:a:dx):ax
  189. jns .switch # jump if atom
  190. xchg %ax,%di # di = fn
  191. .lambda:mov (%bx,%di),%di # di = Cdr(fn)
  192. push %di # save 1
  193. mov (%di),%di # di = Cadr(fn)
  194. call Pairlis
  195. xchg %ax,%dx
  196. pop %di # restore 1
  197. jmp .EvCadr
  198. .switch:cmp $kEq,%ax # eq is last builtin atom
  199. ja .dflt1 # ah is zero if not above
  200. mov (%si),%di # di = Car(x)
  201. .ifCar: cmp $kCar,%al
  202. je Car
  203. .ifCdr: cmp $kCdr,%al
  204. je Cdr
  205. .ifAtom:cmp $kAtom,%al
  206. jne .ifCons
  207. test %di,%di # test if atom
  208. jns .retT
  209. .retF: xor %ax,%ax # ax = nil
  210. ret
  211. .ifCons:cmp $kCons,%al
  212. mov (%bx,%si),%si # si = Cdr(x)
  213. lodsw # si = Cadr(x)
  214. je Cons
  215. .isEq: xor %di,%ax # we know for certain it's eq
  216. jne .retF
  217. .retT: mov $kT,%al
  218. ret
  219. Assoc: mov %dx,%si # Assoc(x:ax,y:dx):ax
  220. 1: mov (%si),%di
  221. mov (%bx,%si),%si
  222. scasw
  223. jne 1b
  224. .byte 0xF6 # testb §i8,i16(%bp,%di) jmp Car
  225. Cadr: mov (%bx,%di),%di # contents of decrement register
  226. .byte 0x3C # cmp §scasw,%al (nop next byte)
  227. Cdr: scasw # increments our data index by 2
  228. Car: mov (%di),%ax # contents of address register!!
  229. 2: ret
  230. 1: mov (%bx,%di),%di # di = Cdr(c)
  231. Evcon: push %di # save c
  232. mov (%di),%si # di = Car(c)
  233. lodsw # ax = Caar(c)
  234. call Eval
  235. pop %di # restore c
  236. test %ax,%ax # nil test
  237. jz 1b
  238. mov (%di),%di # di = Car(c)
  239. .EvCadr:call Cadr # ax = Cadar(c)
  240. # jmp Eval
  241. Eval: test %ax,%ax # Eval(e:ax,a:dx):ax
  242. jz 1f
  243. jns Assoc # lookup val if atom
  244. xchg %ax,%si # di = e
  245. lodsw # ax = Car(e)
  246. cmp $kQuote,%ax # maybe CONS
  247. mov (%si),%di # di = Cdr(e)
  248. je Car
  249. cmp $kCond,%ax
  250. je Evcon # ABC Garbage Collector
  251. push %dx # save a
  252. push %cx # save A
  253. push %ax
  254. call Evlis
  255. xchg %ax,%si
  256. pop %ax
  257. call Apply
  258. pop %dx # restore A
  259. mov %cx,%si # si = B
  260. xchg %ax,%di
  261. call Gc
  262. mov %dx,%di # di = A
  263. sub %si,%cx # cx = C - B
  264. rep movsb
  265. mov %di,%cx # cx = A + (C - B)
  266. pop %dx # restore a
  267. 1: ret
  268. .sig: .fill 512 - (2f - 1f) - (. - _start), 1, 0xce
  269. 1: .ascii " SECTORLISP v2 "
  270. .word 0xAA55
  271. 2: .type .sig,@object
  272. .type kQuote,@object
  273. .type kCond,@object
  274. .type kAtom,@object
  275. .type kCar,@object
  276. .type kCdr,@object
  277. .type kCons,@object
  278. .type kEq,@object