sectorlisp.S 7.8 KB

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