sectorlisp.S 8.0 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 "" # interned strings
  29. kQuote: .asciz "QUOTE" # builtin for eval
  30. kCond: .asciz "COND" # builtin for eval
  31. kCar: .asciz "CAR" # builtin to apply
  32. kCdr: .asciz "CDR" # ordering matters
  33. kCons: .asciz "CONS" # must be 3rd last
  34. kEq: .asciz "EQ" # must be 2nd last
  35. kAtom: .asciz "ATOM" # 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. dec %di
  119. xor %ax,%ax
  120. 2: scasb # rawmemchr(di,al)
  121. jne 2b
  122. jmp 1b
  123. 8: rep movsb # memcpy(di,si,cx)
  124. 9: pop %cx
  125. ret
  126. GetChar:xor %ax,%ax # GetChar→al:dl
  127. int $0x16 # get keystroke
  128. PutChar:mov $0x0e,%ah # prints CP-437
  129. int $0x10 # vidya service
  130. cmp $'\r',%al # don't clobber
  131. jne .RetDx # look xchg ret
  132. mov $'\n',%al
  133. jmp PutChar
  134. .RetDx: xchg %dx,%ax
  135. ret
  136. ////////////////////////////////////////////////////////////////////////////////
  137. Evlis: test %di,%di # Evlis(m:di,a:dx):ax
  138. jz .RetDi # jump if nil
  139. push (%bx,%di) # save 1 Cdr(m)
  140. mov (%di),%ax
  141. call Eval
  142. pop %di # restore 1
  143. push %ax # save 2
  144. call Evlis
  145. # jmp xCons
  146. xCons: pop %di # restore 2
  147. Cons: xchg %di,%cx # Cons(m:di,a:ax):ax
  148. mov %cx,(%di) # must preserve si
  149. mov %ax,(%bx,%di)
  150. lea 4(%di),%cx
  151. .RetDi: xchg %di,%ax
  152. ret
  153. GetList:call GetToken
  154. cmp $')',%al
  155. je .retF
  156. call GetObject
  157. push %ax # popped by xCons
  158. call GetList
  159. jmp xCons
  160. Gc: cmp %dx,%di # Gc(x:di,A:dx,B:si):ax
  161. jb .RetDi # we assume immutable cells
  162. push (%bx,%di) # mark prevents negative gc
  163. mov (%di),%di
  164. call Gc
  165. pop %di
  166. push %ax
  167. call Gc
  168. pop %di
  169. call Cons
  170. sub %si,%ax
  171. add %dx,%ax
  172. ret
  173. .resolv:push %si
  174. call Eval # do (fn si) → ((λ ...) si)
  175. pop %si
  176. Apply: test %ax,%ax # Apply(fn:ax,x:si:a:dx):ax
  177. jns .switch # jump if atom
  178. xchg %ax,%di # di = fn
  179. .lambda:mov (%bx,%di),%di # di = Cdr(fn)
  180. push %di # for .EvCadr
  181. mov (%di),%di # di = Cadr(fn)
  182. Pairlis:test %di,%di # Pairlis(x:di,y:si,a:dx):dx
  183. jz .EvCadr # return if x is nil
  184. lodsw # ax = Car(y)
  185. push (%bx,%di) # push Cdr(x)
  186. mov (%di),%di # di = Car(x)
  187. mov (%si),%si # si = Cdr(y)
  188. call Cons # Cons(Car(x),Car(y))
  189. xchg %ax,%di
  190. xchg %dx,%ax
  191. call Cons # Cons(Cons(Car(x),Car(y)),a)
  192. xchg %ax,%dx # a = new list
  193. pop %di # grab Cdr(x)
  194. jmp Pairlis
  195. .switch:cmp $kAtom,%ax # atom: last builtin atom
  196. ja .resolv # ah is zero if not above
  197. mov (%si),%di # di = Car(x)
  198. je .ifAtom
  199. cmp $kCons,%al
  200. jae .ifCons
  201. .ifCar: cmp $kCar,%al
  202. je Car
  203. .ifCdr: jmp Cdr
  204. .ifCons:mov (%bx,%si),%si # si = Cdr(x)
  205. lodsw # si = Cadr(x)
  206. je Cons
  207. .isEq: xor %ax,%di
  208. jne .retF
  209. .retT: mov $kT,%al
  210. ret
  211. .ifAtom:test %di,%di # test if atom
  212. jns .retT
  213. .retF: xor %ax,%ax # ax = nil
  214. ret
  215. Assoc: mov %dx,%si # Assoc(x:ax,y:dx):ax
  216. 1: mov (%si),%di
  217. mov (%bx,%si),%si
  218. scasw
  219. jne 1b
  220. .byte 0xF6 # testb §i8,i16(%bp,%di) jmp Car
  221. Cadr: mov (%bx,%di),%di # contents of decrement register
  222. .byte 0x3C # cmp §scasw,%al (nop next byte)
  223. Cdr: scasw # increments our data index by 2
  224. Car: mov (%di),%ax # contents of address register!!
  225. ret
  226. 1: mov (%bx,%di),%di # di = Cdr(c)
  227. Evcon: push %di # save c
  228. mov (%di),%si # di = Car(c)
  229. lodsw # ax = Caar(c)
  230. call Eval
  231. pop %di # restore c
  232. test %ax,%ax # nil test
  233. jz 1b
  234. push (%di) # push Car(c)
  235. .EvCadr:pop %di
  236. 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 512 - (2f - 1f) - (. - _start), 1, 0xce
  266. 1: .ascii " SECTORLISP v2 "
  267. .word 0xAA55
  268. 2: .type .sig,@object
  269. .type kQuote,@object
  270. .type kCond,@object
  271. .type kAtom,@object
  272. .type kCar,@object
  273. .type kCdr,@object
  274. .type kCons,@object
  275. .type kEq,@object