sectorlisp.S 7.6 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. 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: movb %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. cmpb $'(',%al
  97. je GetList
  98. .Intern:
  99. mov %cx,%si
  100. xor %di,%di
  101. xor %al,%al
  102. 0: push %di # save 1
  103. 1: cmpsb
  104. jne 2f
  105. dec %di
  106. scasb
  107. jne 1b
  108. jmp 5f
  109. 2: pop %bp # drop 1
  110. mov %cx,%si
  111. 3: scasb
  112. jne 3b
  113. cmp (%di),%al
  114. jne 0b
  115. push %di # StpCpy
  116. 4: movsb
  117. dec %di
  118. scasb
  119. jnz 4b
  120. 5: pop %ax # restore 1
  121. .ret: ret
  122. GetChar:xor %ax,%ax # GetChar→al:dl
  123. int $0x16 # get keystroke
  124. PutChar:mov $0x0e,%ah # prints CP-437
  125. int $0x10 # vidya service
  126. cmp $'\r',%al # don't clobber
  127. jne 1f # look xchg ret
  128. mov $'\n',%al
  129. jmp PutChar
  130. ////////////////////////////////////////////////////////////////////////////////
  131. Pairlis:test %di,%di # Pairlis(x:di,y:si,a:dx):ax
  132. jz 1f # jump if nil
  133. push (%bx,%di) # save 1 Cdr(x)
  134. lodsw
  135. push (%si) # save 2 Cdr(y)
  136. mov (%di),%di
  137. call Cons # preserves dx
  138. pop %si # restore 2
  139. pop %di # restore 1
  140. push %ax # save 3
  141. call Pairlis
  142. jmp xCons # can be inlined here
  143. 1: xchg %dx,%ax
  144. ret
  145. Evlis: test %di,%di # Evlis(m:di,a:dx):ax
  146. jz 1f # jump if nil
  147. push (%bx,%di) # save 1 Cdr(m)
  148. mov (%di),%ax
  149. call Eval
  150. pop %di # restore 1
  151. push %ax # save 2
  152. call Evlis
  153. # jmp xCons
  154. xCons: pop %di # restore 2
  155. Cons: xchg %di,%cx # Cons(m:di,a:ax):ax
  156. mov %cx,(%di)
  157. mov %ax,(%bx,%di)
  158. lea 4(%di),%cx
  159. 1: xchg %di,%ax
  160. ret
  161. Gc: cmp %dx,%di # Gc(x:di,A:dx,B:si):ax
  162. jb 1b # we assume immutable cells
  163. push (%bx,%di) # mark prevents negative gc
  164. mov (%di),%di
  165. call Gc
  166. pop %di
  167. push %ax
  168. call Gc
  169. pop %di
  170. call Cons
  171. sub %si,%ax # ax -= C - B
  172. add %dx,%ax
  173. ret
  174. GetList:call GetToken
  175. cmpb $')',%al
  176. je .retF
  177. call GetObject
  178. push %ax # popped by xCons
  179. call GetList
  180. jmp xCons
  181. .dflt1: push %si # save x
  182. call Eval
  183. pop %si # restore x
  184. # jmp Apply
  185. Apply: test %ax,%ax # Apply(fn:ax,x:si:a:dx):ax
  186. jns .switch # jump if atom
  187. xchg %ax,%di # di = fn
  188. .lambda:mov (%bx,%di),%di # di = Cdr(fn)
  189. push %di # save 1
  190. mov (%di),%di # di = Cadr(fn)
  191. call Pairlis
  192. xchg %ax,%dx
  193. pop %di # restore 1
  194. jmp .EvCadr
  195. .switch:cmp $kEq,%ax # eq is last builtin atom
  196. ja .dflt1 # ah is zero if not above
  197. mov (%si),%di # di = Car(x)
  198. .ifCar: cmp $kCar,%al
  199. je Car
  200. .ifCdr: cmp $kCdr,%al
  201. je Cdr
  202. .ifAtom:cmp $kAtom,%al
  203. jne .ifCons
  204. test %di,%di # test if atom
  205. jns .retT
  206. .retF: xor %ax,%ax # ax = nil
  207. ret
  208. .ifCons:cmp $kCons,%al
  209. mov (%bx,%si),%si # si = Cdr(x)
  210. lodsw # si = Cadr(x)
  211. je Cons
  212. .isEq: cmp %di,%ax # we know for certain it's eq
  213. jne .retF
  214. .retT: mov $kT,%ax
  215. ret
  216. Cadr: mov (%bx,%di),%di # contents of decrement register
  217. .byte 0x3C # cmp §scasw,%al (nop next byte)
  218. Cdr: scasw # increments our data index by 2
  219. Car: mov (%di),%ax # contents of address register!!
  220. 2: ret
  221. Assoc: mov %dx,%si # Assoc(x:ax,y:dx):ax
  222. 1: mov (%si),%di
  223. mov (%bx,%si),%si
  224. scasw
  225. jne 1b
  226. jmp Car
  227. 1: mov (%bx,%di),%di # di = Cdr(c)
  228. Evcon: push %di # save c
  229. mov (%di),%si # di = Car(c)
  230. lodsw # ax = Caar(c)
  231. call Eval
  232. pop %di # restore c
  233. test %ax,%ax # nil test
  234. jz 1b
  235. mov (%di),%di # di = Car(c)
  236. .EvCadr:call Cadr # ax = Cadar(c)
  237. # jmp Eval
  238. Eval: test %ax,%ax # Eval(e:ax,a:dx):ax
  239. jz 1f
  240. jns Assoc # lookup val if atom
  241. xchg %ax,%si # di = e
  242. lodsw # ax = Car(e)
  243. cmp $kQuote,%ax # maybe CONS
  244. mov (%si),%di # di = Cdr(e)
  245. je Car
  246. cmp $kCond,%ax
  247. je Evcon # ABC Garbage Collector
  248. push %dx # save a
  249. push %cx # save A
  250. push %ax
  251. call Evlis
  252. xchg %ax,%si
  253. pop %ax
  254. call Apply
  255. pop %dx # restore A
  256. mov %cx,%si # si = B
  257. xchg %ax,%di
  258. call Gc
  259. mov %dx,%di # di = A
  260. sub %si,%cx # cx = C - B
  261. rep movsb
  262. mov %di,%cx # cx = A + (C - B)
  263. pop %dx # restore a
  264. 1: ret
  265. .sig: .fill 510 - (. - _start), 1, 0xce
  266. .word 0xAA55
  267. .type .sig,@object
  268. .type kQuote,@object
  269. .type kCond,@object
  270. .type kAtom,@object
  271. .type kCar,@object
  272. .type kCdr,@object
  273. .type kCons,@object
  274. .type kEq,@object