sectorlisp.S 8.4 KB

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