Runtime.mod 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. IMPLEMENTATION MODULE Runtime ;
  2. (* 8086 runtime library, assembled byte by byte. Each little emitter below is
  3. one 8086 instruction; the ModR/M byte is spelled out in the comment so the
  4. encoding can be checked by hand against an 8086 table. This is the same
  5. approach the compiler itself takes in Compiler.mod (Ebyte/Eword/EmCall),
  6. so the library needs no external assembler and no binary artifact in the
  7. tree - RT_Build assembles it every run, which is why the entry offsets are
  8. known exactly and are not guesses.
  9. Memory model: a .COM image, so CS = DS = ES = SS = 0 and the whole thing
  10. lives in one 64K segment. The runtime occupies image offsets
  11. RT_Build's base .. base + RT_Size - 1, the generated program follows, and
  12. the program's globals follow at that + 1000H.
  13. The base is a parameter rather than an assumption, and that is not
  14. decoration. The image begins with a three-byte JMP - a .COM is entered at
  15. file offset 0, and without it a .COM built by this compiler starts by
  16. executing initmem - so the runtime does NOT sit at image offset 0. Every
  17. address the runtime bakes into its own code (its data block, and the entry
  18. offsets it hands back) therefore has to be biased by that base. Two places
  19. do it, and only two: FixUp for the data block, and RT_Entry for the
  20. entries. The emitted BYTES are identical for any base, because both are
  21. computed after assembly; tests/check_runtime.py and tests/runtime.golden
  22. pass base 0 and are unaffected.
  23. Calling conventions, matching Compiler.IoCall:
  24. WrInt/WrChar/WrBool/WrReal one 16-bit value on the stack (caller pops)
  25. RdInt/RdChar/RdBool one address on the stack (caller pops)
  26. WrLn/RdLn/StackChk nothing
  27. InitMem AX = offset of the program header word block
  28. ProgEnd/Halt nothing; exits with code 0
  29. All entries preserve BP and SP, and every register except the documented
  30. result, so they can be called from the middle of an expression. *)
  31. FROM SYSTEM IMPORT BYTE ;
  32. CONST
  33. MaxRt = 4096 ;
  34. MaxLbl = 64 ;
  35. MaxFix = 400 ;
  36. MaxNm = 15 ;
  37. (* Offsets inside the runtime's own data block. These must agree with the
  38. bytes EmitData emits, and EmitData ASSERTS that they do - because the
  39. only other way to find out is to run a program, and a D_ constant that
  40. is one too high does not crash: the $ that terminates the string it
  41. points at is simply somewhere else, and a $-string read from the wrong
  42. address runs on until it happens to find a 24h. wrln did exactly that
  43. and printed a screenful of memory, which is what found this. D_TRUE was
  44. the only one that was right, and the golden (instructions only) could
  45. not see any of it. *)
  46. D_NUM = 0 ; (* 8 bytes, decimal conversion scratch *)
  47. D_TRUE = 8 ; (* "TRUE$" - 5 bytes, 8..12 *)
  48. D_FALSE = 13 ; (* "FALSE$" - 6 bytes, 13..18 *)
  49. D_CRLF = 19 ; (* CR LF '$' - 3 bytes, 19..21 *)
  50. D_REAL = 22 ; (* "?REAL?" - 6 bytes, 22..27; reals are not formatted yet *)
  51. D_PUSH = 28 ; (* 2 bytes, 28..29: the one-character pushback. The high
  52. byte is 1 whenever a character is pending, so the
  53. word is 0 exactly when the slot is empty - a NUL in
  54. the input would otherwise be indistinguishable from
  55. "nothing pushed back". Byte 30 is unused. *)
  56. D_END = 31 ; (* the block is padded to here *)
  57. (* THE LOAD BIAS. Everything above is an offset into the image as this
  58. compiler numbers it: the entry JMP is at image 0. But a DOS .COM is
  59. NOT loaded at that offset - DOS loads it at CS:0100, because 0000..00FF
  60. is the PSP - so image offset K lives at CS:(K + 0100h), and since a
  61. .COM has CS = DS, every address baked into the image as a literal has
  62. to carry that +0100h.
  63. Getting this wrong is the single most confusing failure this compiler
  64. can have, because nothing crashes. The entry JMP is *relative*, so it
  65. still lands on the prologue; every CALL is relative, so every call
  66. still lands on the right runtime entry; initmem still runs and still
  67. zeroes the data area. The program therefore starts, runs, and prints
  68. its first field correctly - and then a $ that is 0100h too low resolves
  69. to code instead of to data, so the string walk runs on through the
  70. runtime's own instructions until it happens to hit a 24h. That is
  71. exactly what writeln('hi') did: `hi' out of wrtin's inline data, then
  72. 249 bytes of runtime machine code, stopping only at the '$' inside
  73. "TRUE$". See tests/fixtures/t02_writeln.out.
  74. The bias is a CONSTANT, not a variable to be relocated at load time,
  75. because the 8086 has no way to relocode an image in place and no way
  76. to set a segment register to a sub-paragraph boundary. It is the same
  77. +0100h the original's own output has: TPSRC7 opendest copies the
  78. generated image to 100h and runs it there, so the original's addresses
  79. are 0100h-relative too. The difference is only in how the bytes are
  80. *numbered* - ours are 0-based in the file, the original's are
  81. segment-relative - and this constant is where that difference stops.
  82. The constant itself is declared in Runtime.def, because that is the only
  83. declaration site a compiler can import - an implementation module does not
  84. re-declare what its definition module already declared. *)
  85. TYPE
  86. LblRec = RECORD
  87. nm : ARRAY [0..MaxNm] OF CHAR ;
  88. off : CARDINAL ;
  89. END ;
  90. FixRec = RECORD
  91. kind : CARDINAL ; (* 0 = rel8, 1 = rel16, 2 = data address *)
  92. place : CARDINAL ; (* offset of the displacement/address field *)
  93. nm : ARRAY [0..MaxNm] OF CHAR ;
  94. val : CARDINAL ;
  95. END ;
  96. VAR
  97. rt : ARRAY [0..MaxRt - 1] OF BYTE ;
  98. rpos : CARDINAL ;
  99. lbl : ARRAY [0..MaxLbl - 1] OF LblRec ;
  100. ltop : CARDINAL ;
  101. fix : ARRAY [0..MaxFix - 1] OF FixRec ;
  102. nfix : CARDINAL ;
  103. dataAt : CARDINAL ;
  104. rtBase : CARDINAL ;
  105. built : BOOLEAN ;
  106. entNm : ARRAY [0..13] OF ARRAY [0..MaxNm] OF CHAR ;
  107. (* ---------------------------------------------------------------- *)
  108. (* name helpers *)
  109. (* ---------------------------------------------------------------- *)
  110. PROCEDURE StrEq (a, b : ARRAY OF CHAR ) : BOOLEAN ;
  111. VAR i : CARDINAL ;
  112. BEGIN
  113. i := 0 ;
  114. WHILE (i <= HIGH (a)) AND (i <= HIGH (b)) DO
  115. IF a [i] # b [i] THEN
  116. RETURN FALSE
  117. END ;
  118. INC (i)
  119. END ;
  120. RETURN TRUE
  121. END StrEq ;
  122. PROCEDURE SetStr (VAR dst : ARRAY OF CHAR ; src : ARRAY OF CHAR ) ;
  123. VAR i : CARDINAL ;
  124. BEGIN
  125. i := 0 ;
  126. WHILE (i <= HIGH (dst)) AND (i <= HIGH (src)) DO
  127. dst [i] := src [i] ;
  128. INC (i)
  129. END ;
  130. IF i <= HIGH (dst) THEN
  131. dst [i] := 0C
  132. END
  133. END SetStr ;
  134. (* ---------------------------------------------------------------- *)
  135. (* primitive emitters *)
  136. (* ---------------------------------------------------------------- *)
  137. PROCEDURE B (b : CARDINAL ) ;
  138. (* emit exactly ONE byte. A two-byte opcode must be written as two B calls -
  139. B masks to 100H, so B (8BE4H) would silently emit just E4. *)
  140. BEGIN
  141. IF rpos >= MaxRt THEN
  142. RETURN (* blob is oversized: drop the byte *)
  143. END ;
  144. rt [rpos] := VAL (BYTE, b MOD 100H) ;
  145. INC (rpos)
  146. END B ;
  147. PROCEDURE W (w : CARDINAL ) ;
  148. BEGIN
  149. B (w MOD 100H) ;
  150. B ((w DIV 100H) MOD 100H) (* little endian *)
  151. END W ;
  152. PROCEDURE M (nm : ARRAY OF CHAR ) ;
  153. (* mark: nm is the current offset *)
  154. BEGIN
  155. IF ltop < MaxLbl THEN
  156. SetStr (lbl [ltop].nm, nm) ;
  157. lbl [ltop].off := rpos ;
  158. INC (ltop)
  159. END
  160. END M ;
  161. PROCEDURE AddFix (kind, place : CARDINAL ; nm : ARRAY OF CHAR ; val : CARDINAL ) ;
  162. BEGIN
  163. IF nfix < MaxFix THEN
  164. fix [nfix].kind := kind ;
  165. fix [nfix].place := place ;
  166. fix [nfix].val := val ;
  167. SetStr (fix [nfix].nm, nm) ;
  168. INC (nfix)
  169. END
  170. END AddFix ;
  171. PROCEDURE LblOff (nm : ARRAY OF CHAR ) : CARDINAL ;
  172. VAR i : CARDINAL ;
  173. BEGIN
  174. i := 0 ;
  175. WHILE i < ltop DO
  176. IF StrEq (lbl [i].nm, nm) THEN
  177. RETURN lbl [i].off
  178. END ;
  179. INC (i)
  180. END ;
  181. RETURN 0
  182. END LblOff ;
  183. PROCEDURE J8 (nm : ARRAY OF CHAR ) ;
  184. BEGIN
  185. B (0EBH) ; (* JMP rel8 *)
  186. AddFix (0, rpos, nm, 0) ;
  187. B (0)
  188. END J8 ;
  189. PROCEDURE C8 (nm : ARRAY OF CHAR ) ;
  190. BEGIN
  191. B (0E8H) ; (* CALL rel16 *)
  192. AddFix (1, rpos, nm, 0) ;
  193. W (0)
  194. END C8 ;
  195. PROCEDURE Jcc (code : CARDINAL ; nm : ARRAY OF CHAR ) ;
  196. BEGIN
  197. B (code) ; (* Jcc rel8 *)
  198. AddFix (0, rpos, nm, 0) ;
  199. B (0)
  200. END Jcc ;
  201. PROCEDURE Dd (delta : CARDINAL ) ;
  202. (* emit a 16-bit address into the runtime's data block; the value is only
  203. known once the data block has been placed, so it is a fixup *)
  204. BEGIN
  205. AddFix (2, rpos, "", delta) ;
  206. W (0)
  207. END Dd ;
  208. (* --- 8-bit / 16-bit register and memory forms, one instruction each --- *)
  209. (* THE 16-BIT ModR/M EFFECTIVE-ADDRESS TABLE, MEASURED NOT REMEMBERED.
  210. Every address form below was confirmed by EXECUTING it on a real 8086
  211. (qemu-system-i386) with a probe that stores a marker through the candidate
  212. encoding and then reports which physical address received it; see
  213. tests/probe/modrm19.s. The mod=11 column is not an address at all -- it
  214. names a register -- so it is confirmed instead by asking GNU as to
  215. ENCODE the eight register moves and checking FCML's decode of the result
  216. (tests/probe/modrm11.py, which also carries the hand-checking anchors).
  217. Do not "fix" any of these from memory. The version that comes to mind is
  218. wrong in exactly the cells called out below, and every one of those
  219. mistakes shipped as code that decoded cleanly:
  220. r/m mod=00 mod=01 mod=10 mod=11
  221. 000 [BX+SI] [BX+SI]+disp [BX+SI]+disp AX
  222. 001 [BX+DI] [BX+DI]+disp [BX+DI]+disp CX
  223. 010 [BP+SI] [BP+SI]+disp [BP+SI]+disp DX
  224. 011 [BP+DI] [BP+DI]+disp [BP+DI]+disp BX
  225. 100 [SI] [SI]+disp [SI]+disp SP
  226. 101 [DI] [DI]+disp [DI]+disp BP
  227. 110 disp16 [BP]+disp [BP]+disp SI
  228. 111 [BX] [BX]+disp [BX]+disp DI
  229. "disp" is disp8 for mod=01 and disp16 for mod=10.
  230. In mod=11 BOTH fields name a register and BOTH use the same list,
  231. AX CX DX BX SP BP SI DI -- the reg field and the r/m field do not
  232. differ, and there is no second BX at code 7. The memorable version
  233. drops AX off the front and invents a duplicate BX at the end, which
  234. shifts every code down by one; that is precisely how MovSiBx and
  235. CmpSiBx came to be written "89 DC" and "39 DC", which are MOV SP,BX
  236. and CMP SP,BX. Note what the shifted table gets right by luck: SP is
  237. at 100 either way, so the mistake is invisible until you check a
  238. register below it. rm=100 is SP, and SI is rm=110, not rm=100.
  239. And reg/rm swap direction with the opcode, which is the other trap:
  240. 88 /r MOV r/m8,r8 89 /r MOV r/m16,r16 reg is the SOURCE
  241. 8A /r MOV r8,r/m8 8B /r MOV r16,r/m16 reg is the TARGET
  242. So ModRM C6 names "DH and AL" either way, but 88 C6 is DH:=AL while
  243. 8A C6 is AL:=DH. The 8-bit register list is also its own, and it is not
  244. the word list:
  245. 000 001 010 011 100 101 110 111 = AL CL DL BL AH CH DH BH
  246. The two lists agree at every code except 100, where the byte form is AH
  247. and the word form is SP. Reading the same ModRM byte as the other size
  248. silently swaps AH for SP, and 88 C2 is DL:=AL while 88 17 is [BX]<-DL.
  249. The word-form list, and the four anchors nobody writes by hand:
  250. 83 C4 08 ADD SP, 8 rm=100 -> SP
  251. 83 C6 02 ADD SI, 2 rm=110 -> SI
  252. 8B EC MOV BP, SP reg=101 rm=100
  253. 8B E5 MOV SP, BP reg=100 rm=101
  254. Asking "which register is rm=100?" and answering SI is the single most
  255. common error in this file's history.
  256. Concrete recipes for opcode 8r / 9r (r/m = rm, 16-bit form):
  257. mod=00 rm=110 -> 06 <disp16> the only direct form
  258. mod=00 rm=111 -> 07 = [BX] 2 bytes
  259. mod=00 rm=101 -> 05 = [DI] 2 bytes
  260. mod=01 rm=110 -> 46 disp8 = [BP]+disp8
  261. mod=01 rm=111 -> 47 disp8 = [BX]+disp8
  262. mod=01 rm=101 -> 45 disp8 = [DI]+disp8
  263. There is no [SP] form in 16-bit mode: SIB bytes are 386-only. Anything
  264. wanting the top of the stack has to go through BP. *)
  265. PROCEDURE PushBp ; BEGIN B (55H) END PushBp ;
  266. PROCEDURE PopBp ; BEGIN B (5DH) END PopBp ;
  267. PROCEDURE MovBpSp ; BEGIN B (8BH) ; B (0ECH) END MovBpSp ; (* 8B EC: MOV BP,SP *)
  268. PROCEDURE MovSpBp ; BEGIN B (89H) ; B (0ECH) END MovSpBp ; (* 89 EC: MOV SP,BP *)
  269. PROCEDURE LeaveR ; BEGIN B (0C9H) END LeaveR ;
  270. PROCEDURE RetR ; BEGIN B (0C3H) END RetR ;
  271. PROCEDURE Int21 ; BEGIN B (0CDH) ; B (21H) END Int21 ;
  272. PROCEDURE PushDs ; BEGIN B (1EH) END PushDs ;
  273. PROCEDURE PopEs ; BEGIN B (7H) END PopEs ;
  274. PROCEDURE PushAx ; BEGIN B (50H) END PushAx ;
  275. PROCEDURE PopAx ; BEGIN B (58H) END PopAx ;
  276. PROCEDURE PushBx ; BEGIN B (53H) END PushBx ;
  277. PROCEDURE PopBx ; BEGIN B (5BH) END PopBx ;
  278. PROCEDURE PushCx ; BEGIN B (51H) END PushCx ;
  279. PROCEDURE PopCx ; BEGIN B (59H) END PopCx ;
  280. PROCEDURE PushDx ; BEGIN B (52H) END PushDx ;
  281. PROCEDURE PopDx ; BEGIN B (5AH) END PopDx ;
  282. PROCEDURE PushDi ; BEGIN B (57H) END PushDi ;
  283. PROCEDURE PopDi ; BEGIN B (5FH) END PopDi ;
  284. PROCEDURE XorAxAx ; BEGIN B (31H) ; B (0C0H) END XorAxAx ; (* 11 000 000 *)
  285. PROCEDURE XorCxCx ; BEGIN B (31H) ; B (0C9H) END XorCxCx ; (* 11 001 001 *)
  286. PROCEDURE XorDxDx ; BEGIN B (31H) ; B (0D2H) END XorDxDx ; (* 11 010 010 *)
  287. PROCEDURE XorDiDi ; BEGIN B (31H) ; B (0FFH) END XorDiDi ; (* 11 111 111 *)
  288. PROCEDURE IncCx ; BEGIN B (41H) END IncCx ;
  289. PROCEDURE IncSi ; BEGIN B (46H) END IncSi ;
  290. PROCEDURE DecSi ; BEGIN B (4EH) END DecSi ;
  291. PROCEDURE AddDi2 ; BEGIN B (83H) ; B (0C7H) ; B (2) END AddDi2 ; (* 11 000 111 *)
  292. PROCEDURE CmpAl (v : CARDINAL ) ; BEGIN B (3CH) ; B (v) END CmpAl ;
  293. PROCEDURE CmpAx0 ; BEGIN B (83H) ; B (0F8H) ; B (0) END CmpAx0 ;
  294. PROCEDURE CmpCx0 ; BEGIN B (83H) ; B (0F9H) ; B (0) END CmpCx0 ;
  295. PROCEDURE CmpArg2W0 ;
  296. BEGIN
  297. B (8BH) ; B (0ECH) ; (* MOV BP,SP *)
  298. B (83H) ; B (7EH) ; B (2) ; B (0) ; (* CMP WORD [BP+2],0 *)
  299. B (89H) ; B (0ECH) (* MOV SP,BP *)
  300. END CmpArg2W0 ;
  301. PROCEDURE CmpArg4W0 ;
  302. (* CMP WORD PTR [BP+4],0 - the same comparison, in the frame where the entry
  303. pushed BP first.
  304. The 2 and the 4 are the whole point of having two names. [SP] is not
  305. encodable in 16-bit mode, so an entry that takes one 16-bit argument has to
  306. borrow BP to reach it, and how far up the argument then sits depends on
  307. whether BP was saved first: 2 bytes without the PUSH, 4 with it. A single
  308. emitter taking the displacement as a parameter is the obvious way to write
  309. this and the wrong one -- a parameter is invisible to tests/audit_helpers.py,
  310. which reads the NAME and compares it against the decode, so `CmpArgW0 (2)'
  311. and `CmpArgW0 (4)' would differ by one byte with nothing in the codebase
  312. able to tell which was meant. In the name, the audit pins it: a helper
  313. called CmpArg4W0 that emitted 83 7E 02 00 would fail the run.
  314. Note the sandwich: MOV BP,SP before and MOV SP,BP after, and no POP BP, so
  315. the two forms are the ONLY difference. (Was 83 7C 24 00 00, which decodes
  316. as CMP WORD [SI+24h],0.) The imm8 of the 83 form is sign-extended to 16
  317. bits, so 0 really is a 16-bit zero. *)
  318. BEGIN
  319. B (8BH) ; B (0ECH) ; (* MOV BP,SP *)
  320. B (83H) ; B (7EH) ; B (4) ; B (0) ; (* CMP WORD [BP+4],0 *)
  321. B (89H) ; B (0ECH) (* MOV SP,BP *)
  322. END CmpArg4W0 ;
  323. PROCEDURE CmpSiBx ; BEGIN B (39H) ; B (0DEH) END CmpSiBx ; (* 39 DE: CMP SI,BX *)
  324. PROCEDURE CmpCxDx ; BEGIN B (39H) ; B (0D1H) END CmpCxDx ; (* 11 010 001 *)
  325. PROCEDURE CmpDiCx ; BEGIN B (39H) ; B (0CFH) END CmpDiCx ; (* 11 001 111 *)
  326. PROCEDURE CmpBx0 ; BEGIN B (83H) ; B (0FBH) ; B (0) END CmpBx0 ;
  327. PROCEDURE CmpCxV (v : CARDINAL ) ; BEGIN B (83H) ; B (0F9H) ; B (v) END CmpCxV ;
  328. PROCEDURE AddDl (v : CARDINAL ) ; BEGIN B (80H) ; B (0C2H) ; B (v) END AddDl ;
  329. PROCEDURE SubAl (v : CARDINAL ) ; BEGIN B (2CH) ; B (v) END SubAl ;
  330. PROCEDURE NegAx ; BEGIN B (0F7H) ; B (0D8H) END NegAx ;
  331. PROCEDURE NegDi ; BEGIN B (0F7H) ; B (0DFH) END NegDi ;
  332. PROCEDURE DivCx ; BEGIN B (0F7H) ; B (0F1H) END DivCx ; (* 11 110 001 *)
  333. PROCEDURE MulBx ; BEGIN B (0F7H) ; B (0E3H) END MulBx ; (* 11 100 011 *)
  334. PROCEDURE MovAh (v : CARDINAL ) ; BEGIN B (0B4H) ; B (v) END MovAh ;
  335. PROCEDURE MovDl (v : CARDINAL ) ; BEGIN B (0B2H) ; B (v) END MovDl ;
  336. (* The runtime's own data block, addressed by absolute image address. Four
  337. emitters cover it, and the difference between them is the whole subject of
  338. the mistakes recorded below, so they are named systematically:
  339. Mov<reg>Vx MOV reg, Vx -- reg := the ADDRESS (BB / BA + Dd)
  340. Ld<reg>Vx MOV reg, [Vx] -- reg := the CONTENTS (8B 1E + Dd)
  341. StVx<reg> MOV [Vx], reg -- [Vx] := reg (89 1E + Dd)
  342. "Vx" is the operand word for "a 16-bit absolute address into the data
  343. block", spelled with a V precisely so it cannot be confused with a base
  344. register: on the 8086 there is no [BX] memory form, so the address has to
  345. go through mod=00 / rm=110, and rm=111 is [BX+SI] - a different, perfectly
  346. decodable instruction. tests/audit_helpers.py checks all four.
  347. The two traps here, both of which happened:
  348. - Dd versus W. Dd turns a D_ offset into a FIXUP, so the address can be
  349. placed only once the data block has been given its final position in the
  350. image. W emits the number as it stands. The two produce the SAME
  351. instruction, and getch once used MovBxImm where it wanted the address, so
  352. `MOV BX,D_PUSH' came out as `MOV BX,001Ch' - the raw offset 1Ch, inside
  353. the entry JMP. Non-zero, so the pushback slot was never examined, getch
  354. served a byte of the runtime's own code forever, and readln hung. Hence
  355. the Imm suffix: the raw-immediate emitters are named for what they do.
  356. - address versus contents. LdBxVx and MovBxVx are both `8B`/`BB` + Dd to
  357. the eye and completely different instructions. getch's first cut used
  358. MovBxVx where it wanted the contents, so it tested the ADDRESS for zero,
  359. found 02AFh every time, and returned the low byte of memory 02AFh - 0,
  360. the value it had just stored there - instead of asking DOS. *)
  361. PROCEDURE MovBxImm (v : CARDINAL ) ; BEGIN B (0BBH) ; W (v) END MovBxImm ;
  362. PROCEDURE MovCxImm (v : CARDINAL ) ; BEGIN B (0B9H) ; W (v) END MovCxImm ;
  363. PROCEDURE MovDxImm (v : CARDINAL ) ; BEGIN B (0BAH) ; W (v) END MovDxImm ;
  364. PROCEDURE MovBxVx (delta : CARDINAL ) ; BEGIN B (0BBH) ; Dd (delta) END MovBxVx ;
  365. PROCEDURE MovDxVx (delta : CARDINAL ) ; BEGIN B (0BAH) ; Dd (delta) END MovDxVx ;
  366. PROCEDURE LdBxVx (delta : CARDINAL ) ;
  367. (* 8B 1E lo hi: BX := WORD PTR [Vx] - mod=00 / rm=110, the only 16-bit form
  368. that can carry a bare absolute address. *)
  369. BEGIN
  370. B (8BH) ; B (01EH) ; Dd (delta)
  371. END LdBxVx ;
  372. PROCEDURE MovSiAx ; BEGIN B (8BH) ; B (0F0H) END MovSiAx ; (* 8B F0: MOV SI,AX *)
  373. PROCEDURE MovAxDi ; BEGIN B (8BH) ; B (0C7H) END MovAxDi ; (* 11 000 111 *)
  374. PROCEDURE MovCxSi6 ; BEGIN B (8BH) ; B (4CH) ; B (6) END MovCxSi6 ;
  375. PROCEDURE MovDxSi2 ; BEGIN B (8BH) ; B (54H) ; B (2) END MovDxSi2 ;
  376. (* The program header is a block of words laid out by the compiler:
  377. +0 hdrFlag 1 = image is valid
  378. +2 hdrCS end of the generated code, in bytes
  379. +4 hdrDS base of the data area <- initmem wants these two
  380. +6 hdrHeap end of the data area <-
  381. +8 hdrMax max open files
  382. initmem must read the words the COMPILER WRITES, and those are +4 (hdrDS,
  383. the data base) and +6 (hdrHeap, the data end). It used to read +8, which
  384. is hdrMax -- and the compiler patches that to 0 -- so CX came out as 0,
  385. "cmp cx,dx / jbe im_done" fired immediately, and initmem silently zeroed
  386. nothing at all. A 3-byte instruction, so no entry offset moved either
  387. way; only the header offset in the byte changed. Nothing caught it
  388. because the loop was well formed - it just did nothing. The cross-check
  389. that would have: tests/run_com_tests.sh now asserts the runtime's SI-relative
  390. reads against the same header offsets it verifies the words at. *)
  391. PROCEDURE MovDxSi4 ; BEGIN B (8BH) ; B (54H) ; B (4) END MovDxSi4 ;
  392. PROCEDURE MovAxBp4 ; BEGIN B (8BH) ; B (46H) ; B (4) END MovAxBp4 ; (* AX:=[BP+4] *)
  393. PROCEDURE MovBxBp4 ; BEGIN B (8BH) ; B (5EH) ; B (4) END MovBxBp4 ; (* BX:=[BP+4] *)
  394. PROCEDURE MovAlDh ; BEGIN B (8AH) ; B (0C6H) END MovAlDh ; (* 8A C6: AL:=DH *)
  395. PROCEDURE MovDlSi ; BEGIN B (8AH) ; B (14H) END MovDlSi ;
  396. PROCEDURE MovDlArg2 ; BEGIN B (8AH) ; B (56H) ; B (2) END MovDlArg2 ; (* DL:=[BP+2] *)
  397. (* The AL twins of the three above. INT 21h AH=02h displays the character in
  398. AL, not DL, so every character this runtime writes has to arrive in AL.
  399. DL is the natural register for the digit scratch in wrint and for a stack
  400. argument in wrchar, and both of them were loading DL and then calling
  401. AH=02h - which printed whatever happened to be left in AL. For wrint that
  402. was the QUOTIENT's low byte from the preceding DIV, so writeln(1) printed a
  403. NUL and writeln(2) printed a NUL, and a two-digit number printed its
  404. quotient instead of itself. A wrong register is not a wrong encoding: the
  405. golden bytes were right, `mov dl,[si]' is a perfectly good instruction, and
  406. every byte-level check stayed green. Only running it found this. *)
  407. PROCEDURE MovAlD (v : CARDINAL ) ; BEGIN B (0B0H) ; B (v) END MovAlD ; (* MOV AL,imm8 *)
  408. PROCEDURE MovAlSi ; BEGIN B (8AH) ; B (04H) END MovAlSi ; (* 8A 04: AL:=[SI] *)
  409. PROCEDURE MovAlArg2 ; BEGIN B (8AH) ; B (46H) ; B (2) END MovAlArg2 ; (* AL:=[BP+2] *)
  410. PROCEDURE MovAlArg4 ; BEGIN B (8AH) ; B (46H) ; B (4) END MovAlArg4 ; (* AL:=[BP+4] *)
  411. PROCEDURE MovAlDl ; BEGIN B (8AH) ; B (0C2H) END MovAlDl ; (* 8A C2: AL:=DL *)
  412. PROCEDURE StDiAx ; BEGIN B (89H) ; B (5H) END StDiAx ; (* 89 05: [DI]:=AX *)
  413. PROCEDURE StSiDl ; BEGIN B (88H) ; B (14H) END StSiDl ; (* 88 14: [SI]:=DL *)
  414. PROCEDURE StDiDl ; BEGIN B (88H) ; B (15H) END StDiDl ; (* 88 15: [DI]:=DL *)
  415. PROCEDURE StDiCx ; BEGIN B (89H) ; B (0DH) END StDiCx ; (* 89 0D: [DI]:=CX *)
  416. PROCEDURE MovDiBp4 ; BEGIN B (8BH) ; B (7EH) ; B (4) END MovDiBp4 ; (* DI:=[BP+4] *)
  417. (* There is NO "store through BX" instruction on the 8086, and writing one
  418. anyway is the single most expensive mistake this file has produced, so the
  419. reasoning is recorded rather than left in the probe history.
  420. In 16-bit addressing the r/m column is a LOCATION, not a register list: for
  421. mod=00, rm=000..101 are [BX+SI] [BX+DI] [BP+SI] [BP+DI] [SI] [DI], and
  422. rm=110 is the only one that means "a direct displacement". rm=111 is
  423. [BX+SI], NOT [BX]. So `89 1D' - the ModR/M the first version of this used
  424. for "MOV [BX],CX" - is mod=00 reg=BX r/m=DI, i.e. MOV [DI],BX: the two
  425. operands swapped AND [BX] inexpressible. GNU as and objdump in -m i8086
  426. agree, and the bytes are perfectly well formed, which is why nothing
  427. objected. What the program saw was its own address being written over the
  428. interrupt vector table, and the variable it was asked to fill left holding
  429. whatever the loader put there: readln(n) then printed 0, and readln(c)
  430. printed a NUL. Correct by comparison: the caller puts the address in BX,
  431. and the store has to go through DI or SI, so the sequence is
  432. MOV DI,[BP+4] / MOV [DI],<value> - see StoreThrough below. *)
  433. PROCEDURE MovDlAl ; BEGIN B (88H) ; B (0C2H) END MovDlAl ;
  434. PROCEDURE XchgAxDi ; BEGIN B (87H) ; B (0C7H) END XchgAxDi ; (* 87 C7: AX<->DI *)
  435. PROCEDURE AddAxDi ; BEGIN B (03H) ; B (0C7H) END AddAxDi ; (* 03 C7: AX:=AX+DI *)
  436. PROCEDURE MovSiBx ; BEGIN B (89H) ; B (0DEH) END MovSiBx ; (* 89 DE: MOV SI,BX *)
  437. PROCEDURE MovDiDx ; BEGIN B (89H) ; B (0D7H) END MovDiDx ;
  438. PROCEDURE MovDiAx ; BEGIN B (89H) ; B (0C7H) END MovDiAx ;
  439. PROCEDURE AddDiAx ; BEGIN B (1H) ; B (0C7H) END AddDiAx ;
  440. PROCEDURE IncBx ; BEGIN B (43H) END IncBx ;
  441. PROCEDURE LdAlBx ; BEGIN B (8AH) ; B (07H) END LdAlBx ; (* 8A 07: AL:=[BX] *)
  442. PROCEDURE MovAlBl ; BEGIN B (8AH) ; B (0C3H) END MovAlBl ; (* 8A C3: AL:=BL *)
  443. (* 8A 07 and 8A C3 are one letter apart and do opposite things: the
  444. first reads the byte AT the pointer in BX, the second takes the low
  445. byte OF BX. getch's pushback path wanted the second and used the
  446. first, so it read memory 010Ah - the address of the character - and
  447. returned whatever was there. Hence the Ld/Mov split. *)
  448. PROCEDURE MovClBx ; BEGIN B (8AH) ; B (0FH) END MovClBx ; (* 8A 0F: CL:=[BX] *)
  449. PROCEDURE XorBxBx ; BEGIN B (31H) ; B (0DBH) END XorBxBx ; (* 31 DB: BX:=0 *)
  450. PROCEDURE MovBlDl ; BEGIN B (8AH) ; B (0DAH) END MovBlDl ; (* 8A DA: BL:=DL *)
  451. PROCEDURE StVxBx (delta : CARDINAL ) ;
  452. (* 89 1E lo hi: MOV [Vx],BX - the mirror of LdBxVx above, and it must use the
  453. same mod=00 / rm=110 and the same Dd fixup kind, or one of the two
  454. addresses a different place. *)
  455. BEGIN
  456. B (89H) ; B (01EH) ; Dd (delta)
  457. END StVxBx ;
  458. PROCEDURE JmpBx ; BEGIN B (0FFH) ; B (0E3H) END JmpBx ; (* FF E3: JMP BX *)
  459. (* JE 74 JNE 75 JB 72 JBE 76 JGE 7D *)
  460. PROCEDURE Je8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (74H, nm) END Je8 ;
  461. PROCEDURE Jne8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (75H, nm) END Jne8 ;
  462. PROCEDURE Jb8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (72H, nm) END Jb8 ;
  463. PROCEDURE Jbe8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (76H, nm) END Jbe8 ;
  464. PROCEDURE Jge8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (7DH, nm) END Jge8 ;
  465. PROCEDURE Ja8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (77H, nm) END Ja8 ;
  466. PROCEDURE Jcxz8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (0E3H, nm) END Jcxz8 ;
  467. PROCEDURE Loop8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (0E2H, nm) END Loop8 ;
  468. (* ---------------------------------------------------------------- *)
  469. (* the entries *)
  470. (* ---------------------------------------------------------------- *)
  471. PROCEDURE EmitInitMem ;
  472. (* AX = offset of the program header. The header holds, at +4 the base of
  473. the program's data area and at +8 its end, so the globals can be zeroed -
  474. Pascal leaves them undefined, TP3's runtime clears them. Also makes
  475. ES = DS so that any string instruction in the library would work. *)
  476. BEGIN
  477. M ("initmem") ;
  478. MovSiAx ; (* SI = AX = the header offset the caller passed *)
  479. MovDxSi4 ; (* DX = [SI+4] = hdrDS = data base *)
  480. MovCxSi6 ; (* CX = [SI+6] = hdrHeap = data end *)
  481. CmpCxDx ;
  482. Jbe8 ("im_done") ;
  483. MovDiDx ; (* DI = data base *)
  484. M ("im_zero") ;
  485. XorAxAx ; (* AX = 0: the value written into every global.
  486. The caller passes the header offset in AX, so
  487. without this the "zeroing" loop would write the
  488. header offset into all of them. *)
  489. StDiAx ;
  490. AddDi2 ;
  491. CmpDiCx ;
  492. Jb8 ("im_zero") ;
  493. M ("im_done") ;
  494. PushDs ; PopEs ;
  495. RetR
  496. END EmitInitMem ;
  497. PROCEDURE EmitEnd ;
  498. (* progend and halt are the same code: the compiler already zeroes AX for
  499. progend, and a HALT argument is discarded at compile time, so both leave
  500. with exit code 0. *)
  501. BEGIN
  502. M ("progend") ;
  503. M ("halt") ;
  504. XorAxAx ;
  505. MovAh (4CH) ;
  506. Int21 ;
  507. RetR
  508. END EmitEnd ;
  509. PROCEDURE EmitStackChk ;
  510. (* called from every procedure prologue. Range and stack checking are not
  511. compiled in yet, so this must do nothing at all - in particular it must
  512. not touch a register, because the call site is in the middle of a
  513. partially evaluated expression. *)
  514. BEGIN
  515. M ("stackchk") ;
  516. RetR
  517. END EmitStackChk ;
  518. PROCEDURE EmitGetCh ;
  519. (* AL = next character, 1Ah at end of input. INT 21h AH=08h reads without
  520. echoing, so a redirected stdin behaves the same as a keyboard.
  521. This is TP3's `getbyte' (TPSRC4:94), not a bare INT 21h. getbyte serves a
  522. buffered character from the file record without advancing the buffer
  523. pointer when the "char pre-read" flag ($02) is set, and readnum sets that
  524. flag on the character that ENDS a number - the terminating blank is *not*
  525. consumed. xreadln is then called and it is the xreadln that consumes the
  526. newline.
  527. This matters because `readln(n)' is emitted as xrdint followed by xreadln
  528. (TPSRC8 prdrdln emits xreadln whenever rdlnflg is set). A getch that
  529. always consumed its character left xreadln starting on the *following*
  530. line, so `readln(n); readln(c)' read the char from line 3 of the input.
  531. One byte of pushback reproduces the flag. *)
  532. BEGIN
  533. M ("getch") ;
  534. LdBxVx (D_PUSH) ; (* BX := the slot's contents *)
  535. CmpBx0 ;
  536. Je8 ("gc_dos") ;
  537. MovAlBl ; (* AL := the pending character, i.e. the
  538. low byte OF BX - not the byte at [BX] *)
  539. XorBxBx ;
  540. StVxBx (D_PUSH) ; (* and empty the slot *)
  541. RetR ;
  542. M ("gc_dos") ;
  543. MovAh (08H) ;
  544. Int21 ;
  545. RetR
  546. END EmitGetCh ;
  547. PROCEDURE EmitUnGetCh ;
  548. (* put AL back, so the next getch returns it again. TP3 does this by
  549. rewinding the buffer pointer; a one-byte slot is the same thing. *)
  550. BEGIN
  551. M ("ungetch") ;
  552. MovDlAl ;
  553. MovBxImm (1) ; (* BH := 1 marks the slot occupied, so
  554. a pushed-back NUL is still a
  555. pushed-back NUL *)
  556. MovBlDl ;
  557. StVxBx (D_PUSH) ;
  558. RetR
  559. END EmitUnGetCh ;
  560. PROCEDURE EmitWrInt ;
  561. (* one signed 16-bit value on the stack. Div CX gives the remainder in DX,
  562. which is turned into a digit and stored backwards from the end of the
  563. scratch area, then printed forwards. *)
  564. BEGIN
  565. M ("wrint") ;
  566. PushBp ; MovBpSp ;
  567. MovAxBp4 ;
  568. CmpAx0 ;
  569. Jge8 ("wi_pos") ;
  570. PushAx ;
  571. MovAlD (ORD ("-")) ; MovAh (2) ; Int21 ;
  572. PopAx ;
  573. NegAx ;
  574. M ("wi_pos") ;
  575. MovCxImm (10) ;
  576. MovBxVx (D_NUM + 8) ; (* BX = one past the last digit *)
  577. MovSiBx ;
  578. M ("wi_dig") ;
  579. XorDxDx ;
  580. DivCx ;
  581. AddDl (ORD ("0")) ;
  582. DecSi ;
  583. StSiDl ;
  584. CmpAx0 ;
  585. Jne8 ("wi_dig") ;
  586. M ("wi_out") ;
  587. CmpSiBx ;
  588. Je8 ("wi_done") ;
  589. MovAlSi ; (* the digit: AH=02h wants it in AL *)
  590. MovAh (02H) ; Int21 ;
  591. IncSi ;
  592. J8 ("wi_out") ;
  593. M ("wi_done") ;
  594. MovSpBp ; PopBp ; RetR
  595. END EmitWrInt ;
  596. PROCEDURE EmitWrChar ;
  597. (* the low byte of the one 16-bit argument, which sits above the return
  598. address. There is no [SP] addressing in 16-bit mode, so BP stands in for
  599. the stack pointer -- and it has to be SAVED, because BP is the one register
  600. a caller is entitled to expect back: the test driver keeps its cursor into
  601. the case record there, so an entry that borrows BP without returning it
  602. sends the next call to a garbage address. That is not hypothetical: wrchar
  603. and wrbool both did it, and "writeln(42) writeln TRUE in one program" hung
  604. the machine with the record header printed twice, because the driver had
  605. been sent back to the top of its own loop. Load BP, use it, hand it back.
  606. MovAlArg4, not MovAlArg2: BP is pushed first, so the argument is four bytes
  607. up, not two. AL, not DL: see the AL twins above. *)
  608. BEGIN
  609. M ("wrchar") ;
  610. PushBp ; MovBpSp ;
  611. MovAlArg4 ;
  612. MovSpBp ; PopBp ;
  613. MovAh (02H) ;
  614. Int21 ;
  615. RetR
  616. END EmitWrChar ;
  617. PROCEDURE EmitWrInl ;
  618. (* Write an inline string literal - TP3 TPSRC4 "xwrtinl".
  619. The compiler emits
  620. CALL wrtinl <length byte> <character>...
  621. so the return address on the stack points at the length byte that follows
  622. the call. POP BX takes that address, CX picks up the length, and the
  623. routine finishes with JMP BX - returning to just past the last character.
  624. That is the whole trick: the literal is self-delimiting, so it needs no
  625. terminator, no length table and no space in the data segment, and it costs
  626. the code stream only the characters themselves (TPSRC10 "estring" emits
  627. exactly <length byte><chars> for the same reason).
  628. Consequently this entry has NO stack argument, unlike WrInt/WrChar: the
  629. return address has already been consumed by the POP. *)
  630. BEGIN
  631. M ("wrtinl") ;
  632. PopBx ; (* BX := address of the length byte *)
  633. XorCxCx ;
  634. MovClBx ; (* CX := length *)
  635. IncBx ; (* BX -> first character *)
  636. MovAh (02H) ; (* INT 21h/02h: put character, AL *)
  637. Jcxz8 ("wn_end") ; (* empty string -> nothing to do *)
  638. M ("wn_loop") ;
  639. LdAlBx ; (* AL := next character *)
  640. Int21 ; (* (preserves every register but AL) *)
  641. IncBx ;
  642. Loop8 ("wn_loop") ;
  643. M ("wn_end") ;
  644. JmpBx (* resume past the string; no RET here,
  645. the return address is already gone *)
  646. END EmitWrInl ;
  647. PROCEDURE EmitWrBool ;
  648. (* 4, not 2, for the reason given on CmpArg4W0: BP is pushed before it is
  649. borrowed, so the argument is four bytes up. POP BP before the RET, and
  650. note that CmpArg4W0 has already handed SP back, so the pop lands on the
  651. saved BP and not on the argument. *)
  652. BEGIN
  653. M ("wrbool") ;
  654. PushBp ;
  655. CmpArg4W0 ;
  656. Jne8 ("wb_t") ;
  657. MovDxVx (D_FALSE) ;
  658. J8 ("wb_o") ;
  659. M ("wb_t") ;
  660. MovDxVx (D_TRUE) ;
  661. M ("wb_o") ;
  662. MovAh (09H) ;
  663. Int21 ;
  664. PopBp ;
  665. RetR
  666. END EmitWrBool ;
  667. PROCEDURE EmitWrLn ;
  668. BEGIN
  669. M ("wrln") ;
  670. MovDxVx (D_CRLF) ;
  671. MovAh (09H) ;
  672. Int21 ;
  673. RetR
  674. END EmitWrLn ;
  675. PROCEDURE EmitWrReal ;
  676. (* the 6-byte real is on the stack but is not formatted: the compiler does
  677. not yet load real operands into a form the runtime could read. A visible
  678. marker beats printing the mantissa as an integer. *)
  679. BEGIN
  680. M ("wrreal") ;
  681. MovDxVx (D_REAL) ;
  682. MovAh (09H) ;
  683. Int21 ;
  684. RetR
  685. END EmitWrReal ;
  686. PROCEDURE EmitRdInt ;
  687. (* address on the stack. A transcription of TP3 TPSRC4 xrdint/readnum:
  688. rnspace: getbyte ; ^Z ? -> rnend
  689. consume ; <= 20h ? -> rnspace
  690. rndig: [buf]=char ; getbyte ; <= 20h ? -> rnend (NOT consumed)
  691. consume ; -> rndig
  692. rnend: buf[0] := 0
  693. Two behaviours are load-bearing and neither is obvious:
  694. - the character that ENDS the scan is left pending, because readnum tests
  695. it *before* clearing getbyte's pre-read flag. xreadln, which the
  696. compiler emits next for a readln, is what consumes the newline. See the
  697. note on getch.
  698. - ^Z before any digit reaches rnend with "nothing entered", and xrdint
  699. (JZ rdierr) then returns WITHOUT touching the variable. CX carries both
  700. the sign and that state: 0 = positive, 1 = negative, 2 = nothing read.
  701. Sign in CX, value in DI. *)
  702. BEGIN
  703. M ("rdint") ;
  704. PushBp ; MovBpSp ;
  705. PushAx ; PushBx ; PushCx ; PushDx ; PushDi ;
  706. M ("ri_lead") ;
  707. C8 ("getch") ;
  708. CmpAl (1AH) ; (* ^Z: end of input *)
  709. Je8 ("ri_none") ;
  710. CmpAl (20H) ; (* every control char and the space.
  711. 20H, not 20: readnum skips
  712. everything <= #$20, and a literal
  713. that READS like hex but is
  714. decimal compares AL against 14h,
  715. so a leading space is never
  716. skipped and the scan ends with
  717. nothing read. Every other
  718. literal in this module is
  719. explicit for the same reason. *)
  720. Jbe8 ("ri_lead") ;
  721. XorCxCx ;
  722. CmpAl (ORD ("-")) ;
  723. Jne8 ("ri_nos") ;
  724. IncCx ;
  725. C8 ("getch") ;
  726. J8 ("ri_dig0") ;
  727. M ("ri_nos") ;
  728. CmpAl (ORD ("+")) ;
  729. Jne8 ("ri_dig0") ;
  730. C8 ("getch") ;
  731. M ("ri_dig0") ;
  732. XorDiDi ;
  733. M ("ri_dig") ;
  734. CmpAl (ORD ("0")) ;
  735. Jb8 ("ri_done") ;
  736. CmpAl (ORD ("9")) ;
  737. Ja8 ("ri_done") ;
  738. SubAl (ORD ("0")) ;
  739. MovAh (0) ; (* AX := the digit, 0..9 *)
  740. XchgAxDi ; (* AX := the value so far, DI := the digit.
  741. The digit has to survive MUL, and the 8-bit
  742. reg field of 8A/88 has only AL/CL/DL/BL/AH/
  743. CH/DH/BH - there is no SI or DI byte to park
  744. it in. MUL r/m16 writes DX, so the previous
  745. version's "keep the digit across the multiply"
  746. comment was describing a register the multiply
  747. owns. Every read of an integer therefore
  748. returned 0: the digit was computed, moved to
  749. DH, and multiplied away one instruction later. *)
  750. MovBxImm (10) ;
  751. MulBx ; (* DX:AX := value * 10 *)
  752. AddAxDi ; (* AX := low word + digit. A carry out of bit 15
  753. is dropped, which is what a 16-bit INTEGER
  754. does anyway - the high word of the product is
  755. never stored. *)
  756. MovDiAx ;
  757. C8 ("getch") ;
  758. J8 ("ri_dig") ;
  759. M ("ri_done") ;
  760. C8 ("ungetch") ; (* the delimiter stays pending *)
  761. CmpCxV (2) ;
  762. Je8 ("ri_out") ;
  763. CmpCx0 ;
  764. Je8 ("ri_st") ;
  765. NegDi ;
  766. M ("ri_st") ;
  767. MovAxDi ; (* the parsed value out of DI, into AX... *)
  768. MovDiBp4 ; (* ...the caller's address into DI... *)
  769. StDiAx ; (* ...and store. The three are needed
  770. because there is no [BX] form; see the
  771. note on the store emitters. *)
  772. M ("ri_out") ;
  773. PopDi ; PopDx ; PopCx ; PopBx ; PopAx ;
  774. MovSpBp ; PopBp ; RetR ;
  775. M ("ri_none") ; (* ^Z first: leave the variable alone *)
  776. PopDi ; PopDx ; PopCx ; PopBx ; PopAx ;
  777. MovSpBp ; PopBp ; RetR
  778. END EmitRdInt ;
  779. PROCEDURE EmitRdChar ;
  780. BEGIN
  781. M ("rdchar") ;
  782. PushBp ; MovBpSp ;
  783. PushAx ; PushBx ; PushDi ;
  784. C8 ("getch") ;
  785. MovDlAl ;
  786. MovDiBp4 ; (* no [BX] form: the address goes in DI *)
  787. StDiDl ;
  788. PopDi ; PopBx ; PopAx ;
  789. MovSpBp ; PopBp ; RetR
  790. END EmitRdChar ;
  791. PROCEDURE EmitRdBool ;
  792. (* one character, classified the way TP3 does: T/t/Y/y/1 true, anything else
  793. false. *)
  794. BEGIN
  795. M ("rdbool") ;
  796. PushBp ; MovBpSp ;
  797. PushAx ; PushBx ; PushCx ; PushDi ;
  798. C8 ("getch") ;
  799. XorCxCx ;
  800. CmpAl (ORD ("T")) ; Je8 ("rb_t") ;
  801. CmpAl (ORD ("t")) ; Je8 ("rb_t") ;
  802. CmpAl (ORD ("Y")) ; Je8 ("rb_t") ;
  803. CmpAl (ORD ("y")) ; Je8 ("rb_t") ;
  804. CmpAl (ORD ("1")) ; Je8 ("rb_t") ;
  805. J8 ("rb_s") ;
  806. M ("rb_t") ;
  807. IncCx ;
  808. M ("rb_s") ;
  809. MovDiBp4 ; (* DI = the caller's address, because there is
  810. no [BX] to store through *)
  811. StDiCx ;
  812. PopDi ; PopCx ; PopBx ; PopAx ;
  813. MovSpBp ; PopBp ; RetR
  814. END EmitRdBool ;
  815. PROCEDURE EmitRdLn ;
  816. (* discard the rest of the line, including the terminator *)
  817. BEGIN
  818. M ("rdln") ;
  819. PushAx ;
  820. M ("rl_loop") ;
  821. C8 ("getch") ;
  822. CmpAl (0DH) ; Je8 ("rl_e") ;
  823. CmpAl (0AH) ; Je8 ("rl_e") ;
  824. CmpAl (1AH) ; Je8 ("rl_e") ; (* ^Z: end of input *)
  825. J8 ("rl_loop") ;
  826. M ("rl_e") ;
  827. PopAx ;
  828. RetR
  829. END EmitRdLn ;
  830. PROCEDURE Str5 (s : ARRAY OF CHAR ; n : CARDINAL) ;
  831. (* emit n characters, so the strings below are visibly the same length as the
  832. D_ constants claim and an off-by-one in either place is a compile-time
  833. mismatch rather than a wrong pointer nobody notices *)
  834. VAR i : CARDINAL ;
  835. BEGIN
  836. i := 0 ;
  837. WHILE i < n DO
  838. B (ORD (s [i])) ;
  839. INC (i)
  840. END
  841. END Str5 ;
  842. PROCEDURE EmitData ;
  843. (* The runtime's data block. The D_ constants name the offsets in it and are
  844. ASSERTED against what is emitted here, three ways: the starting offset of
  845. each string, the offset just past it, and the block's total length. A D_
  846. constant is a 16-bit immediate inside a MOV, so a wrong one is a
  847. perfectly well-formed instruction that reads the wrong memory - see the
  848. note on the D_ block. *)
  849. BEGIN
  850. dataAt := rpos ;
  851. (* Each D_ is checked BEFORE the string it names is emitted, because it
  852. names that string's START. *)
  853. IF dataAt + D_NUM # rpos THEN
  854. HALT
  855. END ;
  856. (* 8 bytes of scratch, never read before written *)
  857. B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ;
  858. IF dataAt + D_TRUE # rpos THEN
  859. HALT
  860. END ;
  861. Str5 ("TRUE$", 5) ;
  862. IF dataAt + D_FALSE # rpos THEN
  863. HALT
  864. END ;
  865. Str5 ("FALSE$", 6) ;
  866. IF dataAt + D_CRLF # rpos THEN
  867. HALT
  868. END ;
  869. B (13) ; B (10) ; B (ORD ("$")) ;
  870. IF dataAt + D_REAL # rpos THEN
  871. HALT
  872. END ;
  873. Str5 ("?REAL?", 6) ;
  874. IF dataAt + D_PUSH # rpos THEN
  875. HALT
  876. END ;
  877. B (0) ; B (0) ; (* the pushback slot, empty to begin with *)
  878. WHILE rpos < dataAt + D_END DO
  879. B (0)
  880. END ;
  881. (* the total, so D_END is checked too and not just used as a pad target *)
  882. IF rpos - dataAt # D_END THEN
  883. HALT
  884. END
  885. END EmitData ;
  886. PROCEDURE FixUp ;
  887. VAR i, t, rel : CARDINAL ;
  888. BEGIN
  889. i := 0 ;
  890. WHILE i < nfix DO
  891. IF fix [i].kind = 2 THEN
  892. (* A data address, and the ONE kind that is absolute rather than
  893. relative: rtBase says where the blob sits in the image, val says
  894. where inside the blob, and LoadBias says where the image itself
  895. sits once DOS has loaded it. All three are needed; omitting any
  896. one produces a readable-looking address into the wrong bytes. *)
  897. t := (dataAt + rtBase + fix [i].val + LoadBias) MOD 10000H ;
  898. rt [fix [i].place] := VAL (BYTE, t MOD 100H) ;
  899. rt [fix [i].place + 1] := VAL (BYTE, (t DIV 100H) MOD 100H)
  900. ELSE
  901. t := LblOff (fix [i].nm) ;
  902. IF fix [i].kind = 0 THEN
  903. (* rel8 is measured from the end of the instruction, i.e. one
  904. byte past the displacement field *)
  905. rel := (t + 100H - (fix [i].place + 1)) MOD 100H ;
  906. rt [fix [i].place] := VAL (BYTE, rel)
  907. ELSE
  908. rel := (t + 10000H - (fix [i].place + 2)) MOD 10000H ;
  909. rt [fix [i].place] := VAL (BYTE, rel MOD 100H) ;
  910. rt [fix [i].place + 1] := VAL (BYTE, (rel DIV 100H) MOD 100H)
  911. END
  912. END ;
  913. INC (i)
  914. END
  915. END FixUp ;
  916. (* ---------------------------------------------------------------- *)
  917. (* public interface *)
  918. (* ---------------------------------------------------------------- *)
  919. PROCEDURE RT_Build (base : CARDINAL) ;
  920. BEGIN
  921. IF built THEN
  922. RETURN
  923. END ;
  924. rtBase := base ; (* must precede every Emit*, they read it *)
  925. rpos := 0 ; ltop := 0 ; nfix := 0 ; dataAt := 0 ;
  926. EmitInitMem ;
  927. EmitEnd ;
  928. EmitStackChk ;
  929. EmitWrInt ; EmitWrChar ; EmitWrBool ; EmitWrReal ; EmitWrLn ;
  930. EmitWrInl ;
  931. EmitRdInt ; EmitRdChar ; EmitRdBool ; EmitRdLn ;
  932. EmitGetCh ; EmitUnGetCh ;
  933. EmitData ;
  934. FixUp ;
  935. SetStr (entNm [0], "initmem") ;
  936. SetStr (entNm [1], "progend") ;
  937. SetStr (entNm [2], "stackchk") ;
  938. SetStr (entNm [3], "wrint") ;
  939. SetStr (entNm [4], "wrchar") ;
  940. SetStr (entNm [5], "wrbool") ;
  941. SetStr (entNm [6], "wrreal") ;
  942. SetStr (entNm [7], "wrln") ;
  943. SetStr (entNm [8], "rdint") ;
  944. SetStr (entNm [9], "rdchar") ;
  945. SetStr (entNm [10], "rdbool") ;
  946. SetStr (entNm [11], "rdln") ;
  947. SetStr (entNm [12], "halt") ;
  948. SetStr (entNm [13], "wrtinl") ;
  949. built := TRUE
  950. END RT_Build ;
  951. PROCEDURE RT_Size () : CARDINAL ;
  952. BEGIN
  953. IF NOT built THEN
  954. RT_Build (0)
  955. END ;
  956. RETURN rpos
  957. END RT_Size ;
  958. PROCEDURE RT_Byte (i : CARDINAL ) : BYTE ;
  959. BEGIN
  960. IF NOT built THEN
  961. RT_Build (0)
  962. END ;
  963. IF i >= rpos THEN
  964. RETURN 0
  965. END ;
  966. RETURN rt [i]
  967. END RT_Byte ;
  968. PROCEDURE RT_Entry (i : CARDINAL ) : CARDINAL ;
  969. BEGIN
  970. IF NOT built THEN
  971. RT_Build (0)
  972. END ;
  973. IF i > 13 THEN
  974. RETURN 0
  975. END ;
  976. RETURN rtBase + LblOff (entNm [i])
  977. END RT_Entry ;
  978. PROCEDURE RT_CodeEnd () : CARDINAL ;
  979. BEGIN
  980. IF NOT built THEN
  981. RT_Build (0)
  982. END ;
  983. RETURN dataAt
  984. END RT_CodeEnd ;
  985. END Runtime.