2
0

sectorlisp.S 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. │ │
  7. │ Permission to use, copy, modify, and/or distribute this software for │
  8. │ any purpose with or without fee is hereby granted, provided that the │
  9. │ above copyright notice and this permission notice appear in all copies. │
  10. │ │
  11. │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
  12. │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
  13. │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
  14. │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
  15. │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
  16. │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
  17. │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
  18. │ PERFORMANCE OF THIS SOFTWARE. │
  19. ╚─────────────────────────────────────────────────────────────────────────────*/
  20. // LISP meta-circular evaluator in a MBR
  21. .set NIL, 1
  22. .set ATOM_T, 9
  23. .set ATOM_QUOTE, 13
  24. .set ATOM_COND, 25
  25. .set ATOM_ATOM, 35
  26. .set ATOM_CAR, 45
  27. .set ATOM_CDR, 53
  28. .set ATOM_CONS, 61
  29. .set ATOM_EQ, 71
  30. .set g_token, 0x4000
  31. .set g_str, 0x4080
  32. .set boot, 0x7c00
  33. ////////////////////////////////////////////////////////////////////////////////
  34. .section .text,"ax",@progbits
  35. .globl _start
  36. .code16
  37. _start: jmp .init # some bios scan for short jump
  38. .type kSymbols,@object;
  39. kSymbols:
  40. .ascii "NIL\0T\0QUOTE\0COND\0ATOM\0CAR\0CDR\0CONS\0EQ"
  41. .type .init,@function
  42. .init: ljmp $0x600>>4,$_begin # end of bios data roundup page
  43. _begin: push %cs # memory model cs=ds=es = 0x600
  44. push %cs
  45. push %cs
  46. pop %ds
  47. pop %es
  48. pop %ss
  49. mov $0x7c00-0x600,%cx
  50. mov %cx,%sp
  51. cld
  52. xor %ax,%ax
  53. mov %ax,%fs # fs = &g_mem
  54. xor %di,%di
  55. rep stosb # clears our bss memory
  56. main: mov $g_str,%di
  57. mov $kSymbols,%si
  58. mov $37,%cx
  59. rep movsb
  60. 0: mov $'\n',%dl
  61. call GetToken
  62. call GetObject
  63. mov $NIL,%dx
  64. call Eval
  65. call PrintObject
  66. mov $'\r',%al
  67. call PutChar
  68. jmp 0b
  69. GetToken: # GetToken():al, dl is g_look
  70. mov $g_token,%di
  71. 1: mov %dl,%al
  72. cmp $' ',%al
  73. jbe 2f
  74. stosb
  75. xchg %ax,%cx
  76. 2: call GetChar # bh = 0 after PutChar
  77. xchg %ax,%dx # dl = g_look
  78. cmp $' ',%al
  79. jbe 1b
  80. cmp $')',%al
  81. jbe 3f
  82. cmp $')',%dl
  83. ja 1b
  84. 3: movb %bh,(%di)
  85. xchg %cx,%ax
  86. ret
  87. GetObject: # called just after GetToken
  88. cmpb $'(',%al
  89. je GetList
  90. mov $g_token,%si
  91. .Intern:
  92. mov %si,%bx # save s
  93. mov $g_str,%di
  94. xor %al,%al
  95. 0: mov $-1,%cl
  96. push %di # save 1
  97. 1: cmpsb
  98. jne 2f
  99. cmp -1(%di),%al
  100. jne 1b
  101. jmp 4f
  102. 2: pop %si # drop 1
  103. mov %bx,%si # restore s
  104. repne scasb
  105. cmp (%di),%al
  106. jne 0b
  107. push %di # StpCpy
  108. 3: lodsb
  109. stosb
  110. test %al,%al
  111. jnz 3b
  112. 4: pop %ax # restore 1
  113. add $-g_str,%ax # stc
  114. adc %ax,%ax # ax = 2 * ax + carry
  115. .ret: ret
  116. PrintObject: # PrintObject(x:ax)
  117. test $1,%al
  118. xchg %ax,%di
  119. jz .PrintList
  120. .PrintAtom:
  121. shr %di
  122. lea g_str(%di),%si
  123. .PrintString: # nul-terminated in si
  124. lodsb
  125. test %al,%al
  126. jz .ret # -> ret
  127. call PutChar
  128. jmp .PrintString
  129. .PrintList:
  130. mov $'(',%al
  131. 2: push 2(%di) # save 1 Cdr(x)
  132. mov (%di),%di # di = Car(x)
  133. call .PutObject
  134. pop %ax # restore 1
  135. cmp $NIL,%ax
  136. je 4f
  137. test $1,%al
  138. xchg %ax,%di
  139. mov $' ',%al
  140. jz 2b
  141. mov $249,%al # bullet (A∙B)
  142. call .PutObject
  143. 4: mov $')',%al
  144. jmp PutChar
  145. .PutObject: # .PutObject(c:al,x:di)
  146. call PutChar # preserves di
  147. xchg %di,%ax
  148. jmp PrintObject
  149. GetChar:
  150. xor %ax,%ax # get keystroke
  151. int $0x16 # keyboard service
  152. # ah is bios scancode
  153. # al is ascii character
  154. PutChar:
  155. # push %bx # don't clobber di,si,cx,dx
  156. # push %bp # original ibm pc scroll up bug
  157. mov $7,%bx # normal mda/cga style page zero
  158. mov $0x0e,%ah # teletype output al cp437
  159. int $0x10 # vidya service
  160. # pop %bp # preserves al
  161. # pop %bx
  162. cmp $'\r',%al # don't clobber stuff
  163. jne .ret
  164. mov $'\n',%al
  165. jmp PutChar # bx volatile, bp never used
  166. GetList:call GetToken
  167. cmpb $')',%al
  168. je .retF
  169. call GetObject
  170. push %ax # save 1
  171. call GetList
  172. xchg %ax,%si
  173. pop %di # restore 1
  174. jmp Cons
  175. ////////////////////////////////////////////////////////////////////////////////
  176. Evlis: cmp $NIL,%di # Evlis(m:di,a:dx):ax
  177. je 1f
  178. push 2(%di) # save 1 Cdr(m)
  179. mov (%di),%ax
  180. push %dx # save a
  181. call Eval
  182. pop %dx # restore a
  183. pop %di # restore 1
  184. push %ax # save 2
  185. call Evlis
  186. xchg %ax,%si
  187. pop %di # restore 2
  188. # jmp Cons
  189. Cons: xchg %di,%ax
  190. mov %fs,%di
  191. push %di
  192. stosw
  193. xchg %si,%ax
  194. stosw
  195. mov %di,%fs
  196. pop %ax
  197. ret
  198. 1: xchg %di,%ax
  199. ret
  200. Pairlis:cmp $NIL,%di # Pairlis(x:di,y:si,a:dx):ax
  201. je 1f
  202. push 2(%di) # save 1 Cdr(x)
  203. push 2(%si) # save 2 Cdr(y)
  204. mov (%di),%di
  205. mov (%si),%si
  206. call Cons # preserves dx
  207. pop %si # restore 2
  208. pop %di # restore 1
  209. push %ax # save 3
  210. call Pairlis
  211. xchg %ax,%si
  212. pop %di # restore 3
  213. jmp Cons # can be inlined here
  214. 1: xchg %dx,%ax
  215. ret
  216. Apply: test $1,%al # Apply(fn:ax,x:si:a:dx):ax
  217. jnz .switch
  218. xchg %ax,%di # di = fn
  219. .lambda:mov 2(%di),%di # di = Cdr(fn)
  220. push %di # save 1
  221. mov (%di),%di # di = Cadr(fn)
  222. call Pairlis
  223. xchg %ax,%dx
  224. pop %di # restore 1
  225. jmp .EvCadr
  226. .switch:cmp $ATOM_EQ,%ax
  227. ja .dflt1
  228. mov (%si),%di # di = Car(x)
  229. .ifCar: cmp $ATOM_CAR,%al
  230. jne .ifCdr
  231. mov (%di),%ax
  232. ret
  233. .ifCdr: cmp $ATOM_CDR,%al
  234. jne .ifAtom
  235. mov 2(%di),%ax
  236. ret
  237. .ifAtom:cmp $ATOM_ATOM,%al
  238. jne .ifCons
  239. test $1,%di
  240. jnz .retT
  241. .retF: mov $NIL,%ax # ax = NIL
  242. ret
  243. .ifCons:mov 2(%si),%si # si = Cdr(x)
  244. mov (%si),%si # si = Cadr(x)
  245. cmp $ATOM_CONS,%al
  246. je Cons
  247. .isEq: cmp %di,%si
  248. jne .retF
  249. .retT: mov $ATOM_T,%al # ax = ATOM_T
  250. ret
  251. .dflt1: push %si # save x
  252. push %dx # save a
  253. call Eval
  254. pop %dx # restore a
  255. pop %si # restore x
  256. jmp Apply
  257. Eval: test $1,%al # Eval(e:ax,a:dx):ax
  258. jnz Assoc
  259. xchg %ax,%di # di = e
  260. mov (%di),%ax # ax = Car(e)
  261. cmp $ATOM_QUOTE,%ax # maybe CONS
  262. je Cadr
  263. mov 2(%di),%di # di = Cdr(e)
  264. cmp $ATOM_COND,%ax
  265. je Evcon
  266. .Ldflt2:push %ax # save 2
  267. call Evlis # preserves dx
  268. xchg %ax,%si
  269. pop %ax # restore 2
  270. jmp Apply
  271. Cadr: mov 2(%di),%di # contents of decrement register
  272. mov (%di),%ax # contents of address register
  273. ret
  274. Evcon: push %di # save c
  275. mov (%di),%di # di = Car(c)
  276. mov (%di),%ax # ax = Caar(c)
  277. push %dx # save a
  278. call Eval
  279. pop %dx # restore a
  280. pop %di # restore c
  281. cmp $NIL,%ax
  282. jne 2f
  283. mov 2(%di),%di # di = Cdr(c)
  284. jmp Evcon
  285. 2: mov (%di),%di # di = Car(c)
  286. .EvCadr:call Cadr # ax = Cadar(c)
  287. jmp Eval
  288. Assoc: cmp $NIL,%dx # Assoc(x:ax,y:dx):ax
  289. mov %dx,%si
  290. je .retF
  291. mov (%si),%bx # bx = Car(y)
  292. mov (%bx),%cx # cx = Caar(y)
  293. cmp %cx,%ax
  294. jne 1f
  295. mov 2(%bx),%ax # ax = Cdar(y)
  296. ret
  297. 1: mov 2(%si),%dx # dx = Cdr(y)
  298. jmp Assoc
  299. .type .sig,@object;
  300. .sig:
  301. .fill 510 - (. - _start), 1, 0xce
  302. .word 0xAA55