sectorlisp.S 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. │ Copyright 2022 Hikaru Ikuta │
  8. │ │
  9. │ Permission to use, copy, modify, and/or distribute this software for │
  10. │ any purpose with or without fee is hereby granted, provided that the │
  11. │ above copyright notice and this permission notice appear in all copies. │
  12. │ │
  13. │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
  14. │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
  15. │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
  16. │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
  17. │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
  18. │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
  19. │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
  20. │ PERFORMANCE OF THIS SOFTWARE. │
  21. ╚─────────────────────────────────────────────────────────────────────────────*/
  22. // LISP meta-circular evaluator in a MBR
  23. // Compatible with the original hardware
  24. .code16
  25. .globl _start
  26. _start: .asciz "NIL" # dec %si ; dec %cx ; dec %sp
  27. kT: .asciz "T" # add %dl,(%si) boot A:\ DL=0
  28. start: ljmp $0x7c00>>4,$begin # cs = 0x7c00 is boot address
  29. .asciz "" # interned strings
  30. kQuote: .asciz "QUOTE" # builtin for eval
  31. kCond: .asciz "COND" # builtin for eval
  32. kRead: .asciz "READ" # builtin to apply
  33. kPrint: .asciz "PRINT" # builtin to apply
  34. kCar: .asciz "CAR" # builtin to apply
  35. kCdr: .asciz "CDR" # ordering matters
  36. kCons: .asciz "CONS" # must be 3rd last
  37. kEq: .asciz "EQ" # must be 2nd last
  38. kAtom: .asciz "ATOM" # needs to be last
  39. begin: mov $0x8000,%sp # uses higher address as stack
  40. # and set independently of SS!
  41. # 8088 doesn't stop interrupts
  42. # after SS is set, and PC BIOS
  43. # sets SP to a value that will
  44. # damage our code if int fires
  45. # between it setting SS and SP
  46. push %cs # that means ss = ds = es = cs
  47. pop %ds # noting ljmp set cs to 0x7c00
  48. push %cs # that's the bios load address
  49. pop %es # therefore NULL points to NUL
  50. push %cs # terminated NIL string above!
  51. pop %ss # errata exists but don't care
  52. mov $2,%bx
  53. main: mov %sp,%cx
  54. mov $'\r',%al
  55. call PutChar # call first to initialize %dx
  56. call Read
  57. call Eval
  58. xchg %si,%ax
  59. call PrintObject
  60. jmp main
  61. GetToken: # GetToken():al, dl is g_look
  62. mov %cx,%di
  63. 1: mov %dl,%al
  64. cmp $' ',%al
  65. jbe 2f
  66. stosb
  67. xchg %ax,%si
  68. 2: call GetChar # exchanges dx and ax
  69. cmp $' ',%al
  70. jbe 1b
  71. cmp $')',%al
  72. jbe 3f
  73. cmp $')',%dl # dl = g_look
  74. ja 1b
  75. 3: mov %bh,(%di) # bh is zero
  76. xchg %si,%ax
  77. ret
  78. .PrintList:
  79. mov $'(',%al
  80. 2: push (%bx,%si)
  81. mov (%si),%si
  82. call .PutObject
  83. mov $' ',%al
  84. pop %si # restore 1
  85. test %si,%si
  86. js 2b # jump if cons
  87. jz 4f # jump if nil
  88. mov $249,%al # bullet (A∙B)
  89. call .PutObject
  90. 4: mov $')',%al
  91. jmp PutChar
  92. .ifPrint:
  93. xchg %di,%si # Print(x:si)
  94. test %di,%di
  95. jnz PrintObject # print newline for empty args
  96. mov $'\r',%al
  97. .PutObject: # .PutObject(c:al,x:si)
  98. .PrintString: # nul-terminated in si
  99. call PutChar # preserves si
  100. PrintObject: # PrintObject(x:si)
  101. test %si,%si # set sf=1 if cons
  102. js .PrintList # jump if not cons
  103. .PrintAtom:
  104. lodsb
  105. test %al,%al # test for nul terminator
  106. jnz .PrintString # -> ret
  107. ret
  108. .ifRead:mov %bp,%dx # get cached character
  109. Read: call GetToken
  110. # jmp GetObject
  111. GetObject: # called just after GetToken
  112. cmp $'(',%al
  113. je GetList
  114. # jmp Intern
  115. Intern: push %cx # Intern(cx,di): ax
  116. mov %di,%bp
  117. sub %cx,%bp
  118. inc %bp
  119. xor %di,%di
  120. 1: pop %si
  121. push %si
  122. mov %bp,%cx
  123. mov %di,%ax
  124. cmp %bh,(%di)
  125. je 8f
  126. rep cmpsb # memcmp(di,si,cx)
  127. je 9f
  128. dec %di
  129. xor %ax,%ax
  130. 2: scasb # rawmemchr(di,al)
  131. jne 2b
  132. jmp 1b
  133. 8: rep movsb # memcpy(di,si,cx)
  134. 9: pop %cx
  135. ret
  136. GetChar:xor %ax,%ax # GetChar→al:dl
  137. int $0x16 # get keystroke
  138. mov %ax,%bp # used for READ
  139. PutChar:mov $0x0e,%ah # prints CP-437
  140. int $0x10 # vidya service
  141. cmp $'\r',%al # don't clobber
  142. jne .RetDx # look xchg ret
  143. mov $'\n',%al
  144. jmp PutChar
  145. .RetDx: xchg %dx,%ax
  146. ret
  147. ////////////////////////////////////////////////////////////////////////////////
  148. Evlis: test %di,%di # Evlis(m:di,a:dx):ax
  149. jz .RetDi # jump if nil
  150. push (%bx,%di) # save 1 Cdr(m)
  151. mov (%di),%ax
  152. call Eval
  153. pop %di # restore 1
  154. push %ax # save 2
  155. call Evlis
  156. # jmp xCons
  157. xCons: pop %di # restore 2
  158. Cons: xchg %di,%cx # Cons(m:di,a:ax):ax
  159. mov %cx,(%di) # must preserve si
  160. mov %ax,(%bx,%di)
  161. lea 4(%di),%cx
  162. .RetDi: xchg %di,%ax
  163. ret
  164. Builtin:cmp $kAtom,%ax # atom: last builtin atom
  165. ja .resolv # ah is zero if not above
  166. mov (%si),%di # di = Car(x)
  167. je .ifAtom
  168. cmp $kPrint,%al
  169. je .ifPrint
  170. cmp $kRead,%al
  171. je .ifRead
  172. .ifCar: cmp $kCar,%al
  173. je Car
  174. cmp $kCons,%al
  175. jb 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. .fill 0x1BE - (. - _start), 1, 0x90 # to have this boot from a USB
  270. # drive on a modern PC, make a
  271. # degenerate "partition table"
  272. # where this sector starts the
  273. # bootable partition; inactive
  274. # partition table entries must
  275. # also be empty, or have valid
  276. # starting sector LBA numbers!
  277. # * 1st partition entry *
  278. .byte 0x00 # - bootable indicator
  279. .byte 0b11010010 # reads as add %dl,%dl
  280. pop %dx # restore A
  281. mov %cx,%si # si = B
  282. xchg %ax,%di
  283. call Gc
  284. mov %dx,%di # di = A
  285. .byte 0x00 # - hi8(c₀*Cₙ + h₀*Hₙ + s₀*Sₙ)
  286. .byte 0b11010010 # reads as add %dl,%dl
  287. sub %si,%cx # cx = C - B
  288. .byte 0x3C # cmp $0,%al
  289. # * 2nd partition entry *
  290. .byte 0x00 # - bootable indicator
  291. rep movsb
  292. mov %di,%cx # cx = A + (C - B)
  293. pop %dx # restore a
  294. 1: ret
  295. .fill 0x1CE + 0x8 - (. - _start), 1, 0xce
  296. .long 0 # - c₀*Cₙ + h₀*Hₙ + s₀*Sₙ
  297. .fill 0x1DE - (. - _start), 1, 0xce # * 3rd partition entry *
  298. .byte 0x80 # - bootable indicator
  299. .byte 0, 1, 0 # - h₀, s₀ (& c₀ hi bits), c₀
  300. .byte 0x7F # - OS or filesystem indicator
  301. .byte 0xFF, 0xFF, 0xFF # - h₉, s₉ (& c₉ hi bits), c₉
  302. .long 0 # - c₀*Cₙ + h₀*Hₙ + s₀*Sₙ
  303. .fill 0x1EE - (. - _start), 1, 0xce # * 4th partition entry *
  304. .byte 0x00 # - bootable indicator
  305. .sig: .fill 0x200 - (2f - 1f) - (. - _start), 1, 0xce
  306. 1: .ascii "SECTORLISP"
  307. .byte 0 # - hi8(c₀*Cₙ + h₀*Hₙ + s₀*Sₙ)
  308. .ascii " v2 "
  309. .word 0xAA55
  310. 2: .type .sig,@object
  311. .type kQuote,@object
  312. .type kCond,@object
  313. .type kRead,@object
  314. .type kPrint,@object
  315. .type kAtom,@object
  316. .type kCar,@object
  317. .type kCdr,@object
  318. .type kCons,@object
  319. .type kEq,@object