sectorlisp.S 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. .set ONE, %bp
  23. .set NIL, 1
  24. .set ATOM_T, 23
  25. .set ATOM_QUOTE, 27
  26. .set ATOM_COND, 39
  27. .set ATOM_ATOM, 49
  28. .set ATOM_CAR, 59
  29. .set ATOM_CDR, 67
  30. .set ATOM_CONS, 75
  31. .set ATOM_EQ, 85
  32. .set g_token, 0x7800
  33. .set g_str, 0x0
  34. .set g_mem, 0x8000
  35. .set boot, 0x7c00
  36. ////////////////////////////////////////////////////////////////////////////////
  37. // Currently requires i686+ in real mode
  38. // Can be easily tuned for the IBM PC XT
  39. // Quoth xed -r -isa-set -i sectorlisp.o
  40. .section .text,"ax",@progbits
  41. .globl _start
  42. .code16
  43. _start:
  44. .type kSymbols,@object;
  45. kSymbols:
  46. .ascii "NIL\0\xC0"
  47. .type .init,@function
  48. .init: ljmp $0x7c00>>4,$_begin
  49. .ascii "\0T\0QUOTE\0COND\0ATOM\0CAR\0CDR\0CONS\0EQ\0"
  50. _begin: mov $g_mem,%cx
  51. mov %cx,%fs # fs = &g_mem
  52. xor %ax,%ax
  53. mov %cx,%di
  54. cld
  55. rep stosb # clears our bss memory
  56. push %cs # memory model cs=ds=es = 0x7c0
  57. push %cs
  58. push %cs
  59. pop %ds
  60. pop %es
  61. pop %ss
  62. mov %cx,%sp
  63. inc %ax
  64. xchg %ax,ONE # mov $NIL,ONE
  65. main: mov $'\n',%dl
  66. call GetToken
  67. call GetObject
  68. mov ONE,%dx
  69. call Eval
  70. call PrintObject
  71. mov $'\r',%al
  72. call PutChar
  73. jmp main
  74. GetToken: # GetToken():al, dl is g_look
  75. mov %fs,%di # mov $g_token,%di
  76. mov %di,%si
  77. 1: mov %dl,%al
  78. cmp $' ',%al
  79. jbe 2f
  80. stosb
  81. xchg %ax,%cx
  82. 2: call GetChar # bh = 0 after PutChar
  83. xchg %ax,%dx # dl = g_look
  84. cmp $' ',%al
  85. jbe 1b
  86. cmp $')',%al
  87. jbe 3f
  88. cmp $')',%dl
  89. ja 1b
  90. 3: movb %bh,(%di)
  91. xchg %cx,%ax
  92. ret
  93. .PutObject: # .PutObject(c:al,x:di)
  94. call PutChar # preserves di
  95. xchg %di,%ax
  96. # jmp PrintObject
  97. PrintObject: # PrintObject(x:ax)
  98. test $1,%al
  99. xchg %ax,%di
  100. jz .PrintList
  101. .PrintAtom:
  102. shr %di
  103. mov %di,%si # lea g_str(%di),%si
  104. .PrintString: # nul-terminated in si
  105. lodsb
  106. test %al,%al
  107. jz .ret # -> ret
  108. call PutChar
  109. jmp .PrintString
  110. .PrintList:
  111. mov $'(',%al
  112. 2: push 2(%di) # save 1 Cdr(x)
  113. mov (%di),%di # di = Car(x)
  114. call .PutObject
  115. pop %ax # restore 1
  116. cmp ONE,%ax
  117. je 4f
  118. test $1,%al
  119. xchg %ax,%di
  120. mov $' ',%al
  121. jz 2b
  122. mov $249,%al # bullet (A∙B)
  123. call .PutObject
  124. 4: mov $')',%al
  125. jmp PutChar
  126. GetObject: # called just after GetToken
  127. cmpb $'(',%al
  128. je GetList
  129. .Intern:
  130. xor %di,%di # mov $g_str,%di
  131. xor %al,%al
  132. 0: push %di # save 1
  133. 1: cmpsb
  134. jne 2f
  135. dec %di
  136. scasb
  137. jne 1b
  138. jmp 5f
  139. 2: pop %si # drop 1
  140. mov %fs,%si # mov $g_token,%si
  141. 3: scasb
  142. jne 3b
  143. cmp (%di),%al
  144. jne 0b
  145. push %di # StpCpy
  146. 4: movsb
  147. dec %di
  148. scasb
  149. jnz 4b
  150. 5: pop %ax # restore 1
  151. # add $-g_str,%ax
  152. add %ax,%ax # ax = 2 * ax
  153. inc %ax # + 1
  154. .ret: ret
  155. GetChar:
  156. xor %ax,%ax # get keystroke
  157. int $0x16 # keyboard service
  158. # ah is bios scancode
  159. # al is ascii character
  160. PutChar:
  161. # push %bx # don't clobber di,si,cx,dx
  162. # push %bp # original ibm pc scroll up bug
  163. xor %bx,%bx # normal mda/cga style page zero
  164. mov $0x0e,%ah # teletype output al cp437
  165. int $0x10 # vidya service
  166. # pop %bp # preserves al
  167. # pop %bx
  168. cmp $'\r',%al # don't clobber stuff
  169. jne .ret
  170. mov $'\n',%al
  171. jmp PutChar # bx volatile
  172. ////////////////////////////////////////////////////////////////////////////////
  173. Pairlis:cmp ONE,%di # Pairlis(x:di,y:si,a:dx):ax
  174. je 1f # it's zip() basically
  175. push 2(%di) # save 1 Cdr(x)
  176. lodsw
  177. push (%si) # save 2 Cdr(y)
  178. mov (%di),%di
  179. xchg %ax,%si
  180. call Cons # preserves dx
  181. pop %si # restore 2
  182. pop %di # restore 1
  183. push %ax # save 3
  184. call Pairlis
  185. xchg %ax,%si
  186. pop %di # restore 3
  187. jmp Cons # can be inlined here
  188. 1: xchg %dx,%ax
  189. ret
  190. Evlis: cmp ONE,%di # Evlis(m:di,a:dx):ax
  191. je 1f
  192. push 2(%di) # save 1 Cdr(m)
  193. mov (%di),%ax
  194. push %dx # save a
  195. call Eval
  196. pop %dx # restore a
  197. pop %di # restore 1
  198. push %ax # save 2
  199. call Evlis
  200. xCons: xchg %ax,%si
  201. pop %di # restore 2
  202. # jmp Cons
  203. Cons: xchg %di,%ax
  204. mov %fs,%di
  205. stosw
  206. xchg %si,%ax
  207. stosw
  208. xchg %di,%ax
  209. mov %fs,%di
  210. mov %ax,%fs
  211. 1: xchg %di,%ax
  212. ret
  213. GetList:call GetToken
  214. cmpb $')',%al
  215. je .retF
  216. call GetObject
  217. push %ax # save 1
  218. call GetList
  219. jmp xCons
  220. 1: mov 2(%di),%di # di = Cdr(c)
  221. Evcon: push %di # save c
  222. mov (%di),%di # di = Car(c)
  223. mov (%di),%ax # ax = Caar(c)
  224. push %dx # save a
  225. call Eval
  226. pop %dx # restore a
  227. pop %di # restore c
  228. cmp ONE,%ax
  229. jz 1b
  230. mov (%di),%di # di = Car(c)
  231. .EvCadr:call Cadr # ax = Cadar(c)
  232. # jmp Eval
  233. Eval: test $1,%al # Eval(e:ax,a:dx):ax
  234. jnz Assoc
  235. xchg %ax,%di # di = e
  236. mov (%di),%ax # ax = Car(e)
  237. cmp $ATOM_QUOTE,%ax # maybe CONS
  238. mov 2(%di),%di # di = Cdr(e)
  239. je .retA
  240. cmp $ATOM_COND,%ax
  241. je Evcon
  242. .Ldflt2:push %ax # save 2
  243. call Evlis # preserves dx
  244. xchg %ax,%si
  245. pop %ax # restore 2
  246. # jmp Apply
  247. Apply: test $1,%al # Apply(fn:ax,x:si:a:dx):ax
  248. jnz .switch
  249. xchg %ax,%di # di = fn
  250. .lambda:mov 2(%di),%di # di = Cdr(fn)
  251. push %di # save 1
  252. mov (%di),%di # di = Cadr(fn)
  253. call Pairlis
  254. xchg %ax,%dx
  255. pop %di # restore 1
  256. jmp .EvCadr
  257. .ifCons:mov 2(%si),%si # si = Cdr(x)
  258. mov (%si),%si # si = Cadr(x)
  259. cmp $ATOM_CONS,%al
  260. je Cons
  261. .isEq: cmp %di,%si
  262. jne .retF
  263. .retT: mov $ATOM_T,%al # ax = ATOM_T
  264. ret
  265. .switch:cmp $ATOM_EQ,%ax
  266. ja .dflt1
  267. mov (%si),%di # di = Car(x)
  268. .ifCar: cmp $ATOM_CAR,%al
  269. je .retA
  270. .ifCdr: cmp $ATOM_CDR,%al
  271. cmove 2(%di),%ax # i686+
  272. je .retD
  273. .ifAtom:cmp $ATOM_ATOM,%al
  274. jne .ifCons
  275. test ONE,%di
  276. jnz .retT
  277. .retF: mov ONE,%ax # ax = NIL
  278. .retD: ret
  279. .dflt1: push %si # save x
  280. push %dx # save a
  281. call Eval
  282. pop %dx # restore a
  283. pop %si # restore x
  284. jmp Apply
  285. Cadr: mov 2(%di),%di # contents of decrement register
  286. .retA: mov (%di),%ax # contents of address register
  287. ret
  288. 1: mov 2(%si),%dx # dx = Cdr(y)
  289. Assoc: cmp ONE,%dx # Assoc(x:ax,y:dx):ax
  290. mov %dx,%si
  291. je .retF
  292. mov (%si),%bx # bx = Car(y)
  293. cmp %ax,(%bx) # (%bx) = Caar(y)
  294. jne 1b
  295. mov 2(%bx),%ax # ax = Cdar(y)
  296. ret
  297. .type .sig,@object;
  298. .sig:
  299. .fill 510 - (. - _start), 1, 0xce
  300. .word 0xAA55