sectorlisp.S 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
  2. │ vi: set noet 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. .set .partition, 1 # set to one (1) to build with
  40. # partition table, for maximum
  41. # compatibility with hardware;
  42. # else zero for build to be as
  43. # small as possible
  44. begin: mov $0x8000,%sp # uses higher address as stack
  45. # and set independently of SS!
  46. # 8088 doesn't stop interrupts
  47. # after SS is set, and PC BIOS
  48. # sets SP to a value that will
  49. # damage our code if int fires
  50. # between it setting SS and SP
  51. push %cs # that means ss = ds = es = cs
  52. pop %ds # noting ljmp set cs to 0x7c00
  53. push %cs # that's the bios load address
  54. pop %es # therefore NULL points to NUL
  55. push %cs # terminated NIL string above!
  56. pop %ss # errata exists but don't care
  57. mov $2,%bx
  58. main: mov %sp,%cx
  59. mov $'\r',%al
  60. call PutChar # call first to initialize %dx
  61. call Read
  62. call Eval
  63. xchg %si,%ax
  64. call PrintObject
  65. jmp main
  66. GetToken: # GetToken():al, dl is g_look
  67. mov %cx,%di
  68. 1: mov %dl,%al
  69. cmp $' ',%al
  70. jbe 2f
  71. stosb
  72. xchg %ax,%si
  73. 2: call GetChar # exchanges dx and ax
  74. cmp $' ',%al
  75. jbe 1b
  76. cmp $')',%al
  77. jbe 3f
  78. cmp $')',%dl # dl = g_look
  79. ja 1b
  80. 3: mov %bh,(%di) # bh is zero
  81. xchg %si,%ax
  82. ret
  83. .PrintList:
  84. mov $'(',%al
  85. 2: push (%bx,%si)
  86. mov (%si),%si
  87. call .PutObject
  88. mov $' ',%al
  89. pop %si # restore 1
  90. test %si,%si
  91. js 2b # jump if cons
  92. jz 4f # jump if nil
  93. mov $249,%al # bullet (A∙B)
  94. call .PutObject
  95. 4: mov $')',%al
  96. jmp PutChar
  97. .ifPrint:
  98. xchg %di,%si # Print(x:si)
  99. test %di,%di
  100. jnz PrintObject # print newline for empty args
  101. mov $'\r',%al
  102. .PutObject: # .PutObject(c:al,x:si)
  103. .PrintString: # nul-terminated in si
  104. call PutChar # preserves si
  105. PrintObject: # PrintObject(x:si)
  106. test %si,%si # set sf=1 if cons
  107. js .PrintList # jump if not cons
  108. .PrintAtom:
  109. lodsb
  110. test %al,%al # test for nul terminator
  111. jnz .PrintString # -> ret
  112. ret
  113. .ifRead:mov %bp,%dx # get cached character
  114. Read: call GetToken
  115. # jmp GetObject
  116. GetObject: # called just after GetToken
  117. cmp $'(',%al
  118. je GetList
  119. # jmp Intern
  120. Intern: push %cx # Intern(cx,di): ax
  121. mov %di,%bp
  122. sub %cx,%bp
  123. inc %bp
  124. xor %di,%di
  125. 1: pop %si
  126. push %si
  127. mov %bp,%cx
  128. mov %di,%ax
  129. cmp %bh,(%di)
  130. je 8f
  131. rep cmpsb # memcmp(di,si,cx)
  132. je 9f
  133. dec %di
  134. xor %ax,%ax
  135. 2: scasb # rawmemchr(di,al)
  136. jne 2b
  137. jmp 1b
  138. 8: rep movsb # memcpy(di,si,cx)
  139. 9: pop %cx
  140. ret
  141. GetChar:xor %ax,%ax # GetChar→al:dl
  142. int $0x16 # get keystroke
  143. mov %ax,%bp # used for READ
  144. PutChar:mov $0x0e,%ah # prints CP-437
  145. int $0x10 # vidya service
  146. cmp $'\r',%al # don't clobber
  147. jne .RetDx # look xchg ret
  148. mov $'\n',%al
  149. jmp PutChar
  150. .RetDx: xchg %dx,%ax
  151. ret
  152. ////////////////////////////////////////////////////////////////////////////////
  153. Evlis: test %di,%di # Evlis(m:di,a:dx):ax
  154. jz .RetDi # jump if nil
  155. push (%bx,%di) # save 1 Cdr(m)
  156. mov (%di),%ax
  157. call Eval
  158. pop %di # restore 1
  159. push %ax # save 2
  160. call Evlis
  161. # jmp xCons
  162. xCons: pop %di # restore 2
  163. Cons: xchg %di,%cx # Cons(m:di,a:ax):ax
  164. mov %cx,(%di) # must preserve si
  165. mov %ax,(%bx,%di)
  166. lea 4(%di),%cx
  167. .RetDi: xchg %di,%ax
  168. ret
  169. Builtin:cmp $kAtom,%ax # atom: last builtin atom
  170. ja .resolv # ah is zero if not above
  171. mov (%si),%di # di = Car(x)
  172. je .ifAtom
  173. cmp $kPrint,%al
  174. je .ifPrint
  175. cmp $kRead,%al
  176. je .ifRead
  177. .ifCar: cmp $kCar,%al
  178. je Car
  179. cmp $kCons,%al
  180. jb Cdr
  181. .ifCons:mov (%bx,%si),%si # si = Cdr(x)
  182. lodsw # si = Cadr(x)
  183. je Cons
  184. .isEq: xor %di,%ax
  185. jne .retF
  186. .retT: mov $kT,%al
  187. ret
  188. GetList:call GetToken
  189. cmp $')',%al
  190. je .retF
  191. call GetObject
  192. push %ax # popped by xCons
  193. call GetList
  194. jmp xCons
  195. Gc: cmp %dx,%di # Gc(x:di,A:dx,B:si):ax
  196. jb .RetDi # we assume immutable cells
  197. push (%bx,%di) # mark prevents negative gc
  198. mov (%di),%di
  199. call Gc
  200. pop %di
  201. push %ax
  202. call Gc
  203. pop %di
  204. call Cons
  205. sub %si,%ax
  206. add %dx,%ax
  207. ret
  208. .resolv:push %si
  209. call Assoc # do (fn si) → ((λ ...) si)
  210. pop %si
  211. Apply: test %ax,%ax # Apply(fn:ax,x:si:a:dx):ax
  212. jns Builtin # jump if atom
  213. xchg %ax,%di # di = fn
  214. .lambda:mov (%bx,%di),%di # di = Cdr(fn)
  215. push %di # for .EvCadr
  216. mov (%di),%di # di = Cadr(fn)
  217. Pairlis:test %di,%di # Pairlis(x:di,y:si,a:dx):dx
  218. jz .EvCadr # return if x is nil
  219. lodsw # ax = Car(y)
  220. push (%bx,%di) # push Cdr(x)
  221. mov (%di),%di # di = Car(x)
  222. mov (%si),%si # si = Cdr(y)
  223. call Cons # Cons(Car(x),Car(y))
  224. xchg %ax,%di
  225. xchg %dx,%ax
  226. call Cons # Cons(Cons(Car(x),Car(y)),a)
  227. xchg %ax,%dx # a = new list
  228. pop %di # grab Cdr(x)
  229. jmp Pairlis
  230. .ifAtom:test %di,%di # test if atom
  231. jns .retT
  232. .retF: xor %ax,%ax # ax = nil
  233. ret
  234. Assoc: mov %dx,%si # Assoc(x:ax,y:dx):ax
  235. 1: mov (%si),%di
  236. mov (%bx,%si),%si
  237. scasw
  238. jne 1b
  239. .byte 0xA9 # shifted ip; reads as test, cmp
  240. Cadr: mov (%bx,%di),%di # contents of decrement register
  241. .byte 0x3C # cmp §scasw,%al (nop next byte)
  242. Cdr: scasw # increments our data index by 2
  243. Car: mov (%di),%ax # contents of address register!!
  244. ret
  245. 1: mov (%bx,%di),%di # di = Cdr(c)
  246. Evcon: push %di # save c
  247. mov (%di),%si # di = Car(c)
  248. lodsw # ax = Caar(c)
  249. call Eval
  250. pop %di # restore c
  251. test %ax,%ax # nil test
  252. jz 1b
  253. push (%di) # push Car(c)
  254. .EvCadr:pop %di
  255. call Cadr # ax = Cadar(c)
  256. # jmp Eval
  257. Eval: test %ax,%ax # Eval(e:ax,a:dx):ax
  258. jz 1f
  259. jns Assoc # lookup val if atom
  260. xchg %ax,%si # di = e
  261. lodsw # ax = Car(e)
  262. cmp $kQuote,%ax # maybe CONS
  263. mov (%si),%di # di = Cdr(e)
  264. je Car
  265. cmp $kCond,%ax
  266. je Evcon # ABC Garbage Collector
  267. push %dx # save a
  268. push %cx # save A
  269. push %ax
  270. call Evlis
  271. xchg %ax,%si
  272. pop %ax
  273. call Apply
  274. .if .partition
  275. .fill 0x1BE - (. - _start), 1, 0x90 # to have this boot from a USB
  276. # drive on a modern PC, make a
  277. # degenerate "partition table"
  278. # where this sector starts the
  279. # bootable partition; inactive
  280. # partition table entries must
  281. # also be empty, or have valid
  282. # starting sector LBA numbers!
  283. # * 1st partition entry *
  284. .byte 0x00 # - bootable indicator
  285. .byte 0b11010010 # reads as add %dl,%dl
  286. .endif
  287. pop %dx # restore A
  288. mov %cx,%si # si = B
  289. xchg %ax,%di
  290. call Gc
  291. mov %dx,%di # di = A
  292. .if .partition
  293. .byte 0x00 # - hi8(c₀*Cₙ + h₀*Hₙ + s₀*Sₙ)
  294. .byte 0b11010010 # reads as add %dl,%dl
  295. .endif
  296. sub %si,%cx # cx = C - B
  297. .if .partition
  298. .byte 0x3C # cmp $0,%al
  299. # * 2nd partition entry *
  300. .byte 0x00 # - bootable indicator
  301. .endif
  302. rep movsb
  303. mov %di,%cx # cx = A + (C - B)
  304. pop %dx # restore a
  305. 1: ret
  306. .if .partition
  307. .fill 0x1CE + 0x8 - (. - _start), 1, 0xce
  308. .long 0 # - c₀*Cₙ + h₀*Hₙ + s₀*Sₙ
  309. .fill 0x1DE - (. - _start), 1, 0xce # * 3rd partition entry *
  310. .byte 0x80 # - bootable indicator
  311. .byte 0, 1, 0 # - h₀, s₀ (& c₀ hi bits), c₀
  312. .byte 0x7F # - OS or filesystem indicator
  313. .byte 0xFF, 0xFF, 0xFF # - h₉, s₉ (& c₉ hi bits), c₉
  314. .long 0 # - c₀*Cₙ + h₀*Hₙ + s₀*Sₙ
  315. .fill 0x1EE - (. - _start), 1, 0xce # * 4th partition entry *
  316. .byte 0x00 # - bootable indicator
  317. .endif
  318. .sig: .fill 0x200 - (2f - 1f) - (. - _start), 1, 0xce
  319. 1: .ascii "SECTORLISP"
  320. .byte 0 # - hi8(c₀*Cₙ + h₀*Hₙ + s₀*Sₙ)
  321. .ascii " v2 "
  322. .word 0xAA55
  323. 2: .type .sig,@object
  324. .type kQuote,@object
  325. .type kCond,@object
  326. .type kRead,@object
  327. .type kPrint,@object
  328. .type kAtom,@object
  329. .type kCar,@object
  330. .type kCdr,@object
  331. .type kCons,@object
  332. .type kEq,@object