2
0

sectorlisp.S 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. // This is the friendly extended version
  24. // This adds (FOO . BAR) support to Read
  25. // It print errors on undefined behavior
  26. // It can also DEFINE persistent binding
  27. .code16
  28. .globl _start
  29. _start: .asciz "NIL" # dec %si ; dec %cx ; dec %sp
  30. kT: .asciz "T" # add %dl,(%si) boot A:\ DL=0
  31. start: ljmp $0x7c00>>4,$begin # cs = 0x7c00 is boot address
  32. .asciz ""
  33. kDefine:.asciz "DEFINE"
  34. kCond: .asciz "COND"
  35. kQuote: .asciz "QUOTE"
  36. kCar: .asciz "CAR" # ordering matters
  37. kCdr: .asciz "CDR" # ordering matters
  38. kCons: .asciz "CONS" # ordering matters
  39. kEq: .asciz "EQ" # ordering matters
  40. kAtom: .asciz "ATOM" # needs to be last
  41. GetToken: # GetToken():al
  42. mov %cx,%di
  43. 1: mov %dl,%al
  44. cmp $' ',%al
  45. jbe 2f
  46. stosb
  47. xchg %ax,%si
  48. 2: call GetChar # exchanges dx and ax
  49. cmp $' ',%al
  50. jbe 1b
  51. cmp $')',%al
  52. jbe 3f
  53. cmp $')',%dl
  54. ja 1b
  55. 3: mov %bh,(%di) # bh is zero
  56. xchg %si,%ax
  57. ret
  58. .PrintList:
  59. mov $'(',%al
  60. 2: push (%bx,%si)
  61. mov (%si),%si
  62. call .PutObject
  63. mov $' ',%al
  64. pop %si # restore 1
  65. test %si,%si
  66. js 2b # jump if cons
  67. jz 4f # jump if nil
  68. mov $249,%al # bullet (A∙B)
  69. call .PutObject
  70. 4: mov $')',%al
  71. jmp PutChar
  72. .PutObject: # .PutObject(c:al,x:si)
  73. .PrintString: # nul-terminated in si
  74. call PutChar # preserves si
  75. PrintObject: # PrintObject(x:si,a:di)
  76. test %si,%si # set sf=1 if cons
  77. js .PrintList # jump if not cons
  78. .PrintAtom:
  79. lodsb
  80. test %al,%al # test for nul terminator
  81. jnz .PrintString # -> ret
  82. ret
  83. GetObject: # called just after GetToken
  84. cmp $'(',%al
  85. je GetList
  86. # jmp Intern
  87. Intern: push %cx # Intern(cx,di): ax
  88. sub %cx,%di
  89. inc %di
  90. push %di
  91. xor %di,%di
  92. 1: pop %cx
  93. pop %si
  94. push %si
  95. push %cx
  96. mov %di,%ax
  97. cmp %bh,(%di)
  98. je 8f
  99. rep cmpsb # memcmp(di,si,cx)
  100. je 9f
  101. xor %ax,%ax
  102. dec %di
  103. 2: scasb
  104. jne 2b
  105. jmp 1b
  106. 8: rep movsb # memcpy(di,si,cx)
  107. 9: pop %cx
  108. pop %cx
  109. ret
  110. GetChar:xor %ax,%ax # GetChar→al:dl
  111. int $0x16 # get keystroke
  112. PutChar:mov $0x0e,%ah # prints CP-437
  113. push %bp # scroll up bug
  114. int $0x10 # vidya service
  115. pop %bp # scroll up bug
  116. cmp $'\r',%al # don't clobber
  117. jne .retDx # look xchg ret
  118. mov $'\n',%al
  119. jmp PutChar
  120. ////////////////////////////////////////////////////////////////////////////////
  121. Gc: cmp %dx,%di # Gc(x:di,A:dx,B:si):ax
  122. jb .retDi # we assume immutable cells
  123. push (%bx,%di) # mark prevents negative gc
  124. mov (%di),%di
  125. call Gc
  126. pop %di
  127. push %ax
  128. call Gc
  129. pop %di
  130. call Cons
  131. sub %si,%ax # ax -= C - B
  132. add %dx,%ax
  133. ret
  134. Evlis: test %di,%di # Evlis(m:di,a:dx):ax
  135. jz .retDi # jump if nil
  136. push (%bx,%di) # save 1 Cdr(m)
  137. mov (%di),%ax
  138. call Eval
  139. pop %di # restore 1
  140. push %ax # save 2
  141. call Evlis
  142. # jmp xCons
  143. xCons: pop %di # restore 2
  144. Cons: xchg %di,%cx # Cons(m:di,a:ax):ax
  145. mov %cx,(%di) # must preserve si
  146. mov %ax,(%bx,%di)
  147. lea 4(%di),%cx
  148. .retDi: xchg %di,%ax
  149. ret
  150. GetList:call GetToken
  151. cmp $')',%al
  152. je .retF
  153. cmp $'.',%al # FRIENDLY FEATURE
  154. je 1f # CONS DOT LITERAL
  155. call GetObject
  156. push %ax # popped by xCons
  157. call GetList
  158. jmp xCons
  159. 1: call Read
  160. push %ax
  161. call GetList
  162. pop %ax
  163. ret
  164. .retDx: xchg %dx,%ax
  165. ret
  166. .resolv:push %si
  167. call Assoc # do (fn si) → ((λ ...) si)
  168. pop %si
  169. Apply: test %ax,%ax # Apply(fn:ax,x:si:a:dx):ax
  170. jns .switch # jump if atom
  171. xchg %ax,%di # di = fn
  172. .lambda:mov (%bx,%di),%di # di = Cdr(fn)
  173. push %di # for .EvCadr
  174. mov (%di),%di # di = Cadr(fn)
  175. Pairlis:test %di,%di # Pairlis(x:di,y:si,a:dx):dx
  176. jz .EvCadr # return if x is nil
  177. xor %ax,%ax # FRIENDLY FEATURE
  178. test %si,%si # DEFAULT NIL ARGS
  179. jz 1f
  180. lodsw # ax = Car(y)
  181. mov (%si),%si # si = Cdr(y)
  182. 1: push (%bx,%di) # push Cdr(x)
  183. mov (%di),%di # di = Car(x)
  184. call Cons # Cons(Car(x),Car(y))
  185. xchg %ax,%di
  186. xchg %dx,%ax
  187. call Cons # Cons(Cons(Car(x),Car(y)),a)
  188. xchg %ax,%dx # a = new list
  189. pop %di # grab Cdr(x)
  190. jmp Pairlis
  191. .switch:cmp $kAtom,%ax # eq is last builtin atom
  192. ja .resolv # ah is zero if not above
  193. mov (%si),%di # di = Car(x)
  194. je .ifAtom
  195. cmp $kCons,%ax
  196. jae .ifCons
  197. test %di,%di # FRIENDLY FEATURE
  198. jns .retF # CAR/CDR(NIL)→NIL
  199. .ifCar: cmp $kCar,%al
  200. je Car
  201. .ifCdr: jmp Cdr
  202. .ifCons:mov (%bx,%si),%si # si = Cdr(x)
  203. lodsw # si = Cadr(x)
  204. je Cons
  205. .isEq: xor %di,%ax # we know for certain it's eq
  206. jne .retF
  207. .retT: mov $kT,%al
  208. ret
  209. .ifAtom:test %di,%di # test if atom
  210. jns .retT
  211. .retF: xor %ax,%ax # ax = nil
  212. ret
  213. Define: xchg %dx,%ax
  214. call Cons
  215. jmp .retDx
  216. Assoc: mov %dx,%si # Assoc(x:ax,y:dx):ax
  217. 1: test %si,%si # FRIENDLY FEATURE
  218. jns Undef # PRINT ?X IF X∉DX
  219. mov (%si),%di
  220. mov (%bx,%si),%si
  221. scasw
  222. jne 1b
  223. .byte 0xf6
  224. Cadr: mov (%bx,%di),%di # contents of decrement register
  225. .byte 0x3C # cmp §scasw,%al (nop next byte)
  226. Cdr: scasw # increments our data index by 2
  227. Car: mov (%di),%ax # contents of address register!!
  228. ret
  229. 1: mov (%bx,%di),%di # di = Cdr(c)
  230. Evcon: push %di # save c
  231. mov (%di),%si # di = Car(c)
  232. lodsw # ax = Caar(c)
  233. call Eval
  234. pop %di # restore c
  235. test %ax,%ax # nil test
  236. jz 1b
  237. push (%di) # push Car(c)
  238. .EvCadr:pop %di
  239. 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. jb Define
  252. push %dx # save a
  253. push %cx # save A
  254. push %ax
  255. call Evlis
  256. xchg %ax,%si
  257. pop %ax
  258. call Apply
  259. pop %dx # restore A
  260. mov %cx,%si # si = B
  261. xchg %ax,%di
  262. call Gc
  263. mov %dx,%di # di = A
  264. sub %si,%cx # cx = C - B
  265. rep movsb
  266. mov %di,%cx # cx = A + (C - B)
  267. pop %dx # restore a
  268. 1: ret
  269. Undef: push %ax
  270. mov $'?',%al
  271. call PutChar
  272. pop %ax
  273. jmp Catch
  274. Read: call GetToken
  275. call GetObject
  276. ret
  277. begin: mov $0x8000,%sp
  278. push %cs
  279. pop %ds
  280. push %cs
  281. pop %es
  282. push %cs
  283. pop %ss
  284. mov $2,%bx
  285. mov %sp,%cx
  286. xor %bp,%bp
  287. main: xor %dx,%dx
  288. call Read
  289. mov %bp,%dx
  290. call Eval
  291. mov %dx,%bp
  292. cmp $kDefine,%ax
  293. je main
  294. Catch: xchg %ax,%si
  295. call PrintObject
  296. mov $'\r',%al
  297. call PutChar
  298. jmp main
  299. .sig: .fill 510 - (. - _start), 1, 0xce
  300. .word 0xAA55
  301. .type .sig,@object
  302. .type kDefine,@object
  303. .type kQuote,@object
  304. .type kCond,@object
  305. .type kAtom,@object
  306. .type kCar,@object
  307. .type kCdr,@object
  308. .type kCons,@object
  309. .type kEq,@object