Runtime.mod 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  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 and MaxFix were 64 and 400, and M/AddFix used to DROP once full.
  35. That limit does not announce itself: a dropped label leaves its bytes in
  36. place, so the runtime still has the right LENGTH and the only symptom is
  37. one branch whose rel8 came out as LblOff's "not found" value. String
  38. concatenation added the four labels that crossed 64, and strcat's
  39. `jcxz sc_done' ended up pointing 47 bytes past the code - a wrong target
  40. that ran the machine into the zero gap. Sizing them with margin fixes
  41. that case; the fill checks below make any future overflow a HALT instead
  42. of wrong code. tests/check_runtime.py's branch-boundary check is what
  43. caught this one. *)
  44. MaxLbl = 160 ; (* the MBF core's labels took the count past 96 *)
  45. MaxFix = 768 ;
  46. MaxNm = 15 ;
  47. (* Offsets inside the runtime's own data block. These must agree with the
  48. bytes EmitData emits, and EmitData ASSERTS that they do - because the
  49. only other way to find out is to run a program, and a D_ constant that
  50. is one too high does not crash: the $ that terminates the string it
  51. points at is simply somewhere else, and a $-string read from the wrong
  52. address runs on until it happens to find a 24h. wrln did exactly that
  53. and printed a screenful of memory, which is what found this. D_TRUE was
  54. the only one that was right, and the golden (instructions only) could
  55. not see any of it. *)
  56. D_NUM = 0 ; (* 8 bytes, decimal conversion scratch *)
  57. D_TRUE = 8 ; (* "TRUE$" - 5 bytes, 8..12 *)
  58. D_FALSE = 13 ; (* "FALSE$" - 6 bytes, 13..18 *)
  59. D_CRLF = 19 ; (* CR LF '$' - 3 bytes, 19..21 *)
  60. D_REAL = 22 ; (* "?REAL?" - 6 bytes, 22..27; reals are not formatted yet *)
  61. D_PUSH = 28 ; (* 2 bytes, 28..29: the one-character pushback. The high
  62. byte is 1 whenever a character is pending, so the
  63. word is 0 exactly when the slot is empty - a NUL in
  64. the input would otherwise be indistinguishable from
  65. "nothing pushed back". Byte 30 is unused. *)
  66. D_RSIGN = 30 ; (* 2 bytes: the result sign word; D_RESAVE (31) is its
  67. high byte, which is the sign the reference keeps *)
  68. D_RESAVE = 31 ; (* the high byte of D_RSIGN: the saved sign *)
  69. D_RMANT = 32 ; (* 2 bytes: the second operand's saved MSB *)
  70. D_RMANT1 = 33 ; (* its high byte, mixed with operand 1's sign *)
  71. D_CVDEC = 34 ; (* 1 byte: the second exponent during alignment *)
  72. D_END = 35 ; (* the block is padded to here *)
  73. (* THE LOAD BIAS. Everything above is an offset into the image as this
  74. compiler numbers it: the entry JMP is at image 0. But a DOS .COM is
  75. NOT loaded at that offset - DOS loads it at CS:0100, because 0000..00FF
  76. is the PSP - so image offset K lives at CS:(K + 0100h), and since a
  77. .COM has CS = DS, every address baked into the image as a literal has
  78. to carry that +0100h.
  79. Getting this wrong is the single most confusing failure this compiler
  80. can have, because nothing crashes. The entry JMP is *relative*, so it
  81. still lands on the prologue; every CALL is relative, so every call
  82. still lands on the right runtime entry; initmem still runs and still
  83. zeroes the data area. The program therefore starts, runs, and prints
  84. its first field correctly - and then a $ that is 0100h too low resolves
  85. to code instead of to data, so the string walk runs on through the
  86. runtime's own instructions until it happens to hit a 24h. That is
  87. exactly what writeln('hi') did: `hi' out of wrtin's inline data, then
  88. 249 bytes of runtime machine code, stopping only at the '$' inside
  89. "TRUE$". See tests/fixtures/t02_writeln.out.
  90. The bias is a CONSTANT, not a variable to be relocated at load time,
  91. because the 8086 has no way to relocode an image in place and no way
  92. to set a segment register to a sub-paragraph boundary. It is the same
  93. +0100h the original's own output has: TPSRC7 opendest copies the
  94. generated image to 100h and runs it there, so the original's addresses
  95. are 0100h-relative too. The difference is only in how the bytes are
  96. *numbered* - ours are 0-based in the file, the original's are
  97. segment-relative - and this constant is where that difference stops.
  98. The constant itself is declared in Runtime.def, because that is the only
  99. declaration site a compiler can import - an implementation module does not
  100. re-declare what its definition module already declared. *)
  101. TYPE
  102. LblRec = RECORD
  103. nm : ARRAY [0..MaxNm] OF CHAR ;
  104. off : CARDINAL ;
  105. END ;
  106. FixRec = RECORD
  107. kind : CARDINAL ; (* 0 = rel8, 1 = rel16, 2 = data address *)
  108. place : CARDINAL ; (* offset of the displacement/address field *)
  109. nm : ARRAY [0..MaxNm] OF CHAR ;
  110. val : CARDINAL ;
  111. END ;
  112. VAR
  113. rt : ARRAY [0..MaxRt - 1] OF BYTE ;
  114. rpos : CARDINAL ;
  115. lbl : ARRAY [0..MaxLbl - 1] OF LblRec ;
  116. ltop : CARDINAL ;
  117. fix : ARRAY [0..MaxFix - 1] OF FixRec ;
  118. nfix : CARDINAL ;
  119. dataAt : CARDINAL ;
  120. rtBase : CARDINAL ;
  121. built : BOOLEAN ;
  122. entNm : ARRAY [0..23] OF ARRAY [0..MaxNm] OF CHAR ;
  123. (* ---------------------------------------------------------------- *)
  124. (* name helpers *)
  125. (* ---------------------------------------------------------------- *)
  126. PROCEDURE StrEq (a, b : ARRAY OF CHAR ) : BOOLEAN ;
  127. VAR i : CARDINAL ;
  128. BEGIN
  129. i := 0 ;
  130. WHILE (i <= HIGH (a)) AND (i <= HIGH (b)) DO
  131. IF a [i] # b [i] THEN
  132. RETURN FALSE
  133. END ;
  134. INC (i)
  135. END ;
  136. RETURN TRUE
  137. END StrEq ;
  138. PROCEDURE SetStr (VAR dst : ARRAY OF CHAR ; src : ARRAY OF CHAR ) ;
  139. VAR i : CARDINAL ;
  140. BEGIN
  141. i := 0 ;
  142. WHILE (i <= HIGH (dst)) AND (i <= HIGH (src)) DO
  143. dst [i] := src [i] ;
  144. INC (i)
  145. END ;
  146. IF i <= HIGH (dst) THEN
  147. dst [i] := 0C
  148. END
  149. END SetStr ;
  150. (* ---------------------------------------------------------------- *)
  151. (* primitive emitters *)
  152. (* ---------------------------------------------------------------- *)
  153. PROCEDURE B (b : CARDINAL ) ;
  154. (* emit exactly ONE byte. A two-byte opcode must be written as two B calls -
  155. B masks to 100H, so B (8BE4H) would silently emit just E4. *)
  156. BEGIN
  157. IF rpos >= MaxRt THEN
  158. RETURN (* blob is oversized: drop the byte *)
  159. END ;
  160. rt [rpos] := VAL (BYTE, b MOD 100H) ;
  161. INC (rpos)
  162. END B ;
  163. PROCEDURE W (w : CARDINAL ) ;
  164. BEGIN
  165. B (w MOD 100H) ;
  166. B ((w DIV 100H) MOD 100H) (* little endian *)
  167. END W ;
  168. PROCEDURE M (nm : ARRAY OF CHAR ) ;
  169. (* mark: nm is the current offset *)
  170. BEGIN
  171. (* A dropped label is not a smaller runtime, it is a WRONG one: its bytes
  172. are still emitted, so only a branch that names it goes off target. That
  173. shows up in neither the size nor the entry table, so refuse to build
  174. rather than drop. *)
  175. IF ltop >= MaxLbl THEN
  176. HALT
  177. END ;
  178. SetStr (lbl [ltop].nm, nm) ;
  179. lbl [ltop].off := rpos ;
  180. INC (ltop)
  181. END M ;
  182. PROCEDURE AddFix (kind, place : CARDINAL ; nm : ARRAY OF CHAR ; val : CARDINAL ) ;
  183. (* Record a displacement to be patched once every label is known. A dropped
  184. fixup is an address that is never patched - the same silent-wrong-code
  185. failure the check in M exists to prevent. *)
  186. BEGIN
  187. IF nfix >= MaxFix THEN
  188. HALT
  189. END ;
  190. fix [nfix].kind := kind ;
  191. fix [nfix].place := place ;
  192. fix [nfix].val := val ;
  193. SetStr (fix [nfix].nm, nm) ;
  194. INC (nfix)
  195. END AddFix ;
  196. PROCEDURE LblOff (nm : ARRAY OF CHAR ) : CARDINAL ;
  197. VAR i : CARDINAL ;
  198. BEGIN
  199. i := 0 ;
  200. WHILE i < ltop DO
  201. IF StrEq (lbl [i].nm, nm) THEN
  202. RETURN lbl [i].off
  203. END ;
  204. INC (i)
  205. END ;
  206. RETURN 0
  207. END LblOff ;
  208. PROCEDURE J8 (nm : ARRAY OF CHAR ) ;
  209. BEGIN
  210. B (0EBH) ; (* JMP rel8 *)
  211. AddFix (0, rpos, nm, 0) ;
  212. B (0)
  213. END J8 ;
  214. PROCEDURE C8 (nm : ARRAY OF CHAR ) ;
  215. BEGIN
  216. B (0E8H) ; (* CALL rel16 *)
  217. AddFix (1, rpos, nm, 0) ;
  218. W (0)
  219. END C8 ;
  220. PROCEDURE Jcc (code : CARDINAL ; nm : ARRAY OF CHAR ) ;
  221. BEGIN
  222. B (code) ; (* Jcc rel8 *)
  223. AddFix (0, rpos, nm, 0) ;
  224. B (0)
  225. END Jcc ;
  226. PROCEDURE Dd (delta : CARDINAL ) ;
  227. (* emit a 16-bit address into the runtime's data block; the value is only
  228. known once the data block has been placed, so it is a fixup *)
  229. BEGIN
  230. AddFix (2, rpos, "", delta) ;
  231. W (0)
  232. END Dd ;
  233. (* --- 8-bit / 16-bit register and memory forms, one instruction each --- *)
  234. (* THE 16-BIT ModR/M EFFECTIVE-ADDRESS TABLE, MEASURED NOT REMEMBERED.
  235. Every address form below was confirmed by EXECUTING it on a real 8086
  236. (qemu-system-i386) with a probe that stores a marker through the candidate
  237. encoding and then reports which physical address received it; see
  238. tests/probe/modrm19.s. The mod=11 column is not an address at all -- it
  239. names a register -- so it is confirmed instead by asking GNU as to
  240. ENCODE the eight register moves and checking FCML's decode of the result
  241. (tests/probe/modrm11.py, which also carries the hand-checking anchors).
  242. Do not "fix" any of these from memory. The version that comes to mind is
  243. wrong in exactly the cells called out below, and every one of those
  244. mistakes shipped as code that decoded cleanly:
  245. r/m mod=00 mod=01 mod=10 mod=11
  246. 000 [BX+SI] [BX+SI]+disp [BX+SI]+disp AX
  247. 001 [BX+DI] [BX+DI]+disp [BX+DI]+disp CX
  248. 010 [BP+SI] [BP+SI]+disp [BP+SI]+disp DX
  249. 011 [BP+DI] [BP+DI]+disp [BP+DI]+disp BX
  250. 100 [SI] [SI]+disp [SI]+disp SP
  251. 101 [DI] [DI]+disp [DI]+disp BP
  252. 110 disp16 [BP]+disp [BP]+disp SI
  253. 111 [BX] [BX]+disp [BX]+disp DI
  254. "disp" is disp8 for mod=01 and disp16 for mod=10.
  255. In mod=11 BOTH fields name a register and BOTH use the same list,
  256. AX CX DX BX SP BP SI DI -- the reg field and the r/m field do not
  257. differ, and there is no second BX at code 7. The memorable version
  258. drops AX off the front and invents a duplicate BX at the end, which
  259. shifts every code down by one; that is precisely how MovSiBx and
  260. CmpSiBx came to be written "89 DC" and "39 DC", which are MOV SP,BX
  261. and CMP SP,BX. Note what the shifted table gets right by luck: SP is
  262. at 100 either way, so the mistake is invisible until you check a
  263. register below it. rm=100 is SP, and SI is rm=110, not rm=100.
  264. And reg/rm swap direction with the opcode, which is the other trap:
  265. 88 /r MOV r/m8,r8 89 /r MOV r/m16,r16 reg is the SOURCE
  266. 8A /r MOV r8,r/m8 8B /r MOV r16,r/m16 reg is the TARGET
  267. So ModRM C6 names "DH and AL" either way, but 88 C6 is DH:=AL while
  268. 8A C6 is AL:=DH. The 8-bit register list is also its own, and it is not
  269. the word list:
  270. 000 001 010 011 100 101 110 111 = AL CL DL BL AH CH DH BH
  271. The two lists agree at every code except 100, where the byte form is AH
  272. and the word form is SP. Reading the same ModRM byte as the other size
  273. silently swaps AH for SP, and 88 C2 is DL:=AL while 88 17 is [BX]<-DL.
  274. The word-form list, and the four anchors nobody writes by hand:
  275. 83 C4 08 ADD SP, 8 rm=100 -> SP
  276. 83 C6 02 ADD SI, 2 rm=110 -> SI
  277. 8B EC MOV BP, SP reg=101 rm=100
  278. 8B E5 MOV SP, BP reg=100 rm=101
  279. Asking "which register is rm=100?" and answering SI is the single most
  280. common error in this file's history.
  281. Concrete recipes for opcode 8r / 9r (r/m = rm, 16-bit form):
  282. mod=00 rm=110 -> 06 <disp16> the only direct form
  283. mod=00 rm=111 -> 07 = [BX] 2 bytes
  284. mod=00 rm=101 -> 05 = [DI] 2 bytes
  285. mod=01 rm=110 -> 46 disp8 = [BP]+disp8
  286. mod=01 rm=111 -> 47 disp8 = [BX]+disp8
  287. mod=01 rm=101 -> 45 disp8 = [DI]+disp8
  288. There is no [SP] form in 16-bit mode: SIB bytes are 386-only. Anything
  289. wanting the top of the stack has to go through BP. *)
  290. PROCEDURE PushBp ; BEGIN B (55H) END PushBp ;
  291. PROCEDURE PopBp ; BEGIN B (5DH) END PopBp ;
  292. PROCEDURE MovBpSp ; BEGIN B (8BH) ; B (0ECH) END MovBpSp ; (* 8B EC: MOV BP,SP *)
  293. PROCEDURE MovSpBp ; BEGIN B (89H) ; B (0ECH) END MovSpBp ; (* 89 EC: MOV SP,BP *)
  294. PROCEDURE LeaveR ; BEGIN B (0C9H) END LeaveR ;
  295. PROCEDURE RetR ; BEGIN B (0C3H) END RetR ;
  296. PROCEDURE Int21 ; BEGIN B (0CDH) ; B (21H) END Int21 ;
  297. PROCEDURE PushDs ; BEGIN B (1EH) END PushDs ;
  298. PROCEDURE PopEs ; BEGIN B (7H) END PopEs ;
  299. PROCEDURE PushAx ; BEGIN B (50H) END PushAx ;
  300. PROCEDURE PopAx ; BEGIN B (58H) END PopAx ;
  301. PROCEDURE PushBx ; BEGIN B (53H) END PushBx ;
  302. PROCEDURE PopBx ; BEGIN B (5BH) END PopBx ;
  303. PROCEDURE PushCx ; BEGIN B (51H) END PushCx ;
  304. PROCEDURE PopCx ; BEGIN B (59H) END PopCx ;
  305. PROCEDURE PushDx ; BEGIN B (52H) END PushDx ;
  306. PROCEDURE PopDx ; BEGIN B (5AH) END PopDx ;
  307. PROCEDURE PushDi ; BEGIN B (57H) END PushDi ;
  308. PROCEDURE PopDi ; BEGIN B (5FH) END PopDi ;
  309. PROCEDURE PushSi ; BEGIN B (56H) END PushSi ;
  310. PROCEDURE PopSi ; BEGIN B (5EH) END PopSi ;
  311. PROCEDURE XorAxAx ; BEGIN B (31H) ; B (0C0H) END XorAxAx ; (* 11 000 000 *)
  312. PROCEDURE XorCxCx ; BEGIN B (31H) ; B (0C9H) END XorCxCx ; (* 11 001 001 *)
  313. PROCEDURE XorDxDx ; BEGIN B (31H) ; B (0D2H) END XorDxDx ; (* 11 010 010 *)
  314. PROCEDURE XorDiDi ; BEGIN B (31H) ; B (0FFH) END XorDiDi ; (* 11 111 111 *)
  315. PROCEDURE IncCx ; BEGIN B (41H) END IncCx ;
  316. PROCEDURE IncSi ; BEGIN B (46H) END IncSi ;
  317. PROCEDURE IncDi ; BEGIN B (47H) END IncDi ;
  318. PROCEDURE DecSi ; BEGIN B (4EH) END DecSi ;
  319. PROCEDURE AddDi2 ; BEGIN B (83H) ; B (0C7H) ; B (2) END AddDi2 ; (* 11 000 111 *)
  320. PROCEDURE CmpAl (v : CARDINAL ) ; BEGIN B (3CH) ; B (v) END CmpAl ;
  321. PROCEDURE CmpAx0 ; BEGIN B (83H) ; B (0F8H) ; B (0) END CmpAx0 ;
  322. PROCEDURE CmpCx0 ; BEGIN B (83H) ; B (0F9H) ; B (0) END CmpCx0 ;
  323. PROCEDURE CmpArg2W0 ;
  324. BEGIN
  325. B (8BH) ; B (0ECH) ; (* MOV BP,SP *)
  326. B (83H) ; B (7EH) ; B (2) ; B (0) ; (* CMP WORD [BP+2],0 *)
  327. B (89H) ; B (0ECH) (* MOV SP,BP *)
  328. END CmpArg2W0 ;
  329. PROCEDURE CmpArg4W0 ;
  330. (* CMP WORD PTR [BP+4],0 - the same comparison, in the frame where the entry
  331. pushed BP first.
  332. The 2 and the 4 are the whole point of having two names. [SP] is not
  333. encodable in 16-bit mode, so an entry that takes one 16-bit argument has to
  334. borrow BP to reach it, and how far up the argument then sits depends on
  335. whether BP was saved first: 2 bytes without the PUSH, 4 with it. A single
  336. emitter taking the displacement as a parameter is the obvious way to write
  337. this and the wrong one -- a parameter is invisible to tests/audit_helpers.py,
  338. which reads the NAME and compares it against the decode, so `CmpArgW0 (2)'
  339. and `CmpArgW0 (4)' would differ by one byte with nothing in the codebase
  340. able to tell which was meant. In the name, the audit pins it: a helper
  341. called CmpArg4W0 that emitted 83 7E 02 00 would fail the run.
  342. Note the sandwich: MOV BP,SP before and MOV SP,BP after, and no POP BP, so
  343. the two forms are the ONLY difference. (Was 83 7C 24 00 00, which decodes
  344. as CMP WORD [SI+24h],0.) The imm8 of the 83 form is sign-extended to 16
  345. bits, so 0 really is a 16-bit zero. *)
  346. BEGIN
  347. B (8BH) ; B (0ECH) ; (* MOV BP,SP *)
  348. B (83H) ; B (7EH) ; B (4) ; B (0) ; (* CMP WORD [BP+4],0 *)
  349. B (89H) ; B (0ECH) (* MOV SP,BP *)
  350. END CmpArg4W0 ;
  351. PROCEDURE CmpSiBx ; BEGIN B (39H) ; B (0DEH) END CmpSiBx ; (* 39 DE: CMP SI,BX *)
  352. PROCEDURE CmpCxDx ; BEGIN B (39H) ; B (0D1H) END CmpCxDx ; (* 11 010 001 *)
  353. PROCEDURE CmpAlDl ; BEGIN B (3AH) ; B (0C2H) END CmpAlDl ; (* 3A C2: CMP AL,DL.
  354. The byte-sized comparator cmpstr's loop needs, and the two opcodes are NOT
  355. interchangeable: 3A /r is `CMP r8,r/m8' (reg is the TARGET) and 38 /r is
  356. `CMP r/m8,r8' (reg is the SOURCE). AL is reg=000 and DL is r/m=010, so
  357. 3A C2 is CMP AL,DL and 38 C2 would be CMP DL,AL - the same flags negated,
  358. which turns every `<` into a `>'. And there is no `CMP AL,[SI]' name
  359. pattern in tests/audit_helpers.py (only the Ld/St memory names are), so the
  360. right-hand character is loaded into DL first rather than compared in
  361. memory. *)
  362. PROCEDURE CmpDiCx ; BEGIN B (39H) ; B (0CFH) END CmpDiCx ; (* 11 001 111 *)
  363. PROCEDURE CmpBx0 ; BEGIN B (83H) ; B (0FBH) ; B (0) END CmpBx0 ;
  364. PROCEDURE CmpCxV (v : CARDINAL ) ; BEGIN B (83H) ; B (0F9H) ; B (v) END CmpCxV ;
  365. PROCEDURE AddDl (v : CARDINAL ) ; BEGIN B (80H) ; B (0C2H) ; B (v) END AddDl ;
  366. PROCEDURE SubAl (v : CARDINAL ) ; BEGIN B (2CH) ; B (v) END SubAl ;
  367. PROCEDURE SubAxBx ; BEGIN B (2BH) ; B (0C3H) END SubAxBx ; (* 2B C3: AX:=AX-BX *)
  368. PROCEDURE NegAx ; BEGIN B (0F7H) ; B (0D8H) END NegAx ;
  369. PROCEDURE NegDi ; BEGIN B (0F7H) ; B (0DFH) END NegDi ;
  370. PROCEDURE DivCx ; BEGIN B (0F7H) ; B (0F1H) END DivCx ; (* 11 110 001 *)
  371. PROCEDURE MulBx ; BEGIN B (0F7H) ; B (0E3H) END MulBx ; (* 11 100 011 *)
  372. PROCEDURE MovAh (v : CARDINAL ) ; BEGIN B (0B4H) ; B (v) END MovAh ;
  373. PROCEDURE MovDl (v : CARDINAL ) ; BEGIN B (0B2H) ; B (v) END MovDl ;
  374. (* The runtime's own data block, addressed by absolute image address. Four
  375. emitters cover it, and the difference between them is the whole subject of
  376. the mistakes recorded below, so they are named systematically:
  377. Mov<reg>Vx MOV reg, Vx -- reg := the ADDRESS (BB / BA + Dd)
  378. Ld<reg>Vx MOV reg, [Vx] -- reg := the CONTENTS (8B 1E + Dd)
  379. StVx<reg> MOV [Vx], reg -- [Vx] := reg (89 1E + Dd)
  380. "Vx" is the operand word for "a 16-bit absolute address into the data
  381. block", spelled with a V precisely so it cannot be confused with a base
  382. register: on the 8086 there is no [BX] memory form, so the address has to
  383. go through mod=00 / rm=110, and rm=111 is [BX+SI] - a different, perfectly
  384. decodable instruction. tests/audit_helpers.py checks all four.
  385. The two traps here, both of which happened:
  386. - Dd versus W. Dd turns a D_ offset into a FIXUP, so the address can be
  387. placed only once the data block has been given its final position in the
  388. image. W emits the number as it stands. The two produce the SAME
  389. instruction, and getch once used MovBxImm where it wanted the address, so
  390. `MOV BX,D_PUSH' came out as `MOV BX,001Ch' - the raw offset 1Ch, inside
  391. the entry JMP. Non-zero, so the pushback slot was never examined, getch
  392. served a byte of the runtime's own code forever, and readln hung. Hence
  393. the Imm suffix: the raw-immediate emitters are named for what they do.
  394. - address versus contents. LdBxVx and MovBxVx are both `8B`/`BB` + Dd to
  395. the eye and completely different instructions. getch's first cut used
  396. MovBxVx where it wanted the contents, so it tested the ADDRESS for zero,
  397. found 02AFh every time, and returned the low byte of memory 02AFh - 0,
  398. the value it had just stored there - instead of asking DOS. *)
  399. PROCEDURE MovBxImm (v : CARDINAL ) ; BEGIN B (0BBH) ; W (v) END MovBxImm ;
  400. PROCEDURE MovCxImm (v : CARDINAL ) ; BEGIN B (0B9H) ; W (v) END MovCxImm ;
  401. PROCEDURE MovDxImm (v : CARDINAL ) ; BEGIN B (0BAH) ; W (v) END MovDxImm ;
  402. PROCEDURE MovBxVx (delta : CARDINAL ) ; BEGIN B (0BBH) ; Dd (delta) END MovBxVx ;
  403. PROCEDURE MovDxVx (delta : CARDINAL ) ; BEGIN B (0BAH) ; Dd (delta) END MovDxVx ;
  404. PROCEDURE LdBxVx (delta : CARDINAL ) ;
  405. (* 8B 1E lo hi: BX := WORD PTR [Vx] - mod=00 / rm=110, the only 16-bit form
  406. that can carry a bare absolute address. *)
  407. BEGIN
  408. B (8BH) ; B (01EH) ; Dd (delta)
  409. END LdBxVx ;
  410. PROCEDURE MovSiAx ; BEGIN B (8BH) ; B (0F0H) END MovSiAx ; (* 8B F0: MOV SI,AX *)
  411. PROCEDURE MovAxDi ; BEGIN B (8BH) ; B (0C7H) END MovAxDi ; (* 11 000 111 *)
  412. PROCEDURE MovCxSi6 ; BEGIN B (8BH) ; B (4CH) ; B (6) END MovCxSi6 ;
  413. PROCEDURE MovCxDx ; BEGIN B (8BH) ; B (0CAH) END MovCxDx ; (* 8B CA: MOV CX,DX *)
  414. PROCEDURE MovDxSi2 ; BEGIN B (8BH) ; B (54H) ; B (2) END MovDxSi2 ;
  415. (* The program header is a block of words laid out by the compiler:
  416. +0 hdrFlag 1 = image is valid
  417. +2 hdrCS end of the generated code, in bytes
  418. +4 hdrDS base of the data area <- initmem wants these two
  419. +6 hdrHeap end of the data area <-
  420. +8 hdrMax max open files
  421. initmem must read the words the COMPILER WRITES, and those are +4 (hdrDS,
  422. the data base) and +6 (hdrHeap, the data end). It used to read +8, which
  423. is hdrMax -- and the compiler patches that to 0 -- so CX came out as 0,
  424. "cmp cx,dx / jbe im_done" fired immediately, and initmem silently zeroed
  425. nothing at all. A 3-byte instruction, so no entry offset moved either
  426. way; only the header offset in the byte changed. Nothing caught it
  427. because the loop was well formed - it just did nothing. The cross-check
  428. that would have: tests/run_com_tests.sh now asserts the runtime's SI-relative
  429. reads against the same header offsets it verifies the words at. *)
  430. PROCEDURE MovDxSi4 ; BEGIN B (8BH) ; B (54H) ; B (4) END MovDxSi4 ;
  431. PROCEDURE MovAxBp4 ; BEGIN B (8BH) ; B (46H) ; B (4) END MovAxBp4 ; (* AX:=[BP+4] *)
  432. PROCEDURE MovAxBp6 ; BEGIN B (8BH) ; B (46H) ; B (6) END MovAxBp6 ; (* AX:=[BP+6] *)
  433. PROCEDURE MovBxBp4 ; BEGIN B (8BH) ; B (5EH) ; B (4) END MovBxBp4 ; (* BX:=[BP+4] *)
  434. PROCEDURE MovBxBp6 ; BEGIN B (8BH) ; B (5EH) ; B (6) END MovBxBp6 ; (* BX:=[BP+6] *)
  435. PROCEDURE MovAlDh ; BEGIN B (8AH) ; B (0C6H) END MovAlDh ; (* 8A C6: AL:=DH *)
  436. PROCEDURE MovDlSi ; BEGIN B (8AH) ; B (14H) END MovDlSi ;
  437. PROCEDURE MovDlArg2 ; BEGIN B (8AH) ; B (56H) ; B (2) END MovDlArg2 ; (* DL:=[BP+2] *)
  438. (* The AL twins of the three above. INT 21h AH=02h displays the character in
  439. AL, not DL, so every character this runtime writes has to arrive in AL.
  440. DL is the natural register for the digit scratch in wrint and for a stack
  441. argument in wrchar, and both of them were loading DL and then calling
  442. AH=02h - which printed whatever happened to be left in AL. For wrint that
  443. was the QUOTIENT's low byte from the preceding DIV, so writeln(1) printed a
  444. NUL and writeln(2) printed a NUL, and a two-digit number printed its
  445. quotient instead of itself. A wrong register is not a wrong encoding: the
  446. golden bytes were right, `mov dl,[si]' is a perfectly good instruction, and
  447. every byte-level check stayed green. Only running it found this. *)
  448. PROCEDURE MovAlD (v : CARDINAL ) ; BEGIN B (0B0H) ; B (v) END MovAlD ; (* MOV AL,imm8 *)
  449. PROCEDURE MovAlSi ; BEGIN B (8AH) ; B (04H) END MovAlSi ; (* 8A 04: AL:=[SI] *)
  450. PROCEDURE MovAlArg2 ; BEGIN B (8AH) ; B (46H) ; B (2) END MovAlArg2 ; (* AL:=[BP+2] *)
  451. PROCEDURE MovAlArg4 ; BEGIN B (8AH) ; B (46H) ; B (4) END MovAlArg4 ; (* AL:=[BP+4] *)
  452. PROCEDURE MovAlDl ; BEGIN B (8AH) ; B (0C2H) END MovAlDl ; (* 8A C2: AL:=DL *)
  453. PROCEDURE StDiAx ; BEGIN B (89H) ; B (5H) END StDiAx ; (* 89 05: [DI]:=AX *)
  454. PROCEDURE StDiAl ; BEGIN B (88H) ; B (5H) END StDiAl ; (* 88 05: [DI]:=AL *)
  455. PROCEDURE StSiDl ; BEGIN B (88H) ; B (14H) END StSiDl ; (* 88 14: [SI]:=DL *)
  456. PROCEDURE StDiDl ; BEGIN B (88H) ; B (15H) END StDiDl ; (* 88 15: [DI]:=DL *)
  457. PROCEDURE StDiCx ; BEGIN B (89H) ; B (0DH) END StDiCx ; (* 89 0D: [DI]:=CX *)
  458. PROCEDURE MovDiBp4 ; BEGIN B (8BH) ; B (7EH) ; B (4) END MovDiBp4 ; (* DI:=[BP+4] *)
  459. (* There is NO "store through BX" instruction on the 8086, and writing one
  460. anyway is the single most expensive mistake this file has produced, so the
  461. reasoning is recorded rather than left in the probe history.
  462. In 16-bit addressing the r/m column is a LOCATION, not a register list: for
  463. mod=00, rm=000..101 are [BX+SI] [BX+DI] [BP+SI] [BP+DI] [SI] [DI], and
  464. rm=110 is the only one that means "a direct displacement". rm=111 is
  465. [BX+SI], NOT [BX]. So `89 1D' - the ModR/M the first version of this used
  466. for "MOV [BX],CX" - is mod=00 reg=BX r/m=DI, i.e. MOV [DI],BX: the two
  467. operands swapped AND [BX] inexpressible. GNU as and objdump in -m i8086
  468. agree, and the bytes are perfectly well formed, which is why nothing
  469. objected. What the program saw was its own address being written over the
  470. interrupt vector table, and the variable it was asked to fill left holding
  471. whatever the loader put there: readln(n) then printed 0, and readln(c)
  472. printed a NUL. Correct by comparison: the caller puts the address in BX,
  473. and the store has to go through DI or SI, so the sequence is
  474. MOV DI,[BP+4] / MOV [DI],<value> - see StoreThrough below. *)
  475. PROCEDURE MovDlAl ; BEGIN B (88H) ; B (0C2H) END MovDlAl ;
  476. PROCEDURE MovDlCl ; BEGIN B (8AH) ; B (0D1H) END MovDlCl ; (* 8A D1: MOV DL,CL *)
  477. PROCEDURE XchgAxDi ; BEGIN B (87H) ; B (0C7H) END XchgAxDi ; (* 87 C7: AX<->DI *)
  478. PROCEDURE AddAxDi ; BEGIN B (03H) ; B (0C7H) END AddAxDi ; (* 03 C7: AX:=AX+DI *)
  479. PROCEDURE MovSiBx ; BEGIN B (89H) ; B (0DEH) END MovSiBx ; (* 89 DE: MOV SI,BX *)
  480. PROCEDURE MovDiDx ; BEGIN B (89H) ; B (0D7H) END MovDiDx ;
  481. PROCEDURE MovDiAx ; BEGIN B (89H) ; B (0C7H) END MovDiAx ;
  482. PROCEDURE AddDiAx ; BEGIN B (1H) ; B (0C7H) END AddDiAx ;
  483. (* The register-pair emitters strcat (EmitStrCat) needs. Two registers, so
  484. the audit's two-register pattern reads them positionally: `AddDxAx' is
  485. 01 C2 = ADD DX,AX (01 /r is `op r/m16,r16'', DX is r/m and AX is reg), and
  486. the mirror `SubDxAx' is 29 C2. MovCxAx is 89 C1 (89 /r: reg is the
  487. source, so CX is r/m and AX is reg). MovDiSi is 8B FE (8B /r: reg is the
  488. destination). *)
  489. PROCEDURE MovCxAx ; BEGIN B (89H) ; B (0C1H) END MovCxAx ; (* 89 C1: MOV CX,AX *)
  490. PROCEDURE MovDiSi ; BEGIN B (8BH) ; B (0FEH) END MovDiSi ; (* 8B FE: MOV DI,SI *)
  491. PROCEDURE AddDxAx ; BEGIN B (1H) ; B (0C2H) END AddDxAx ; (* 01 C2: DX:=DX+AX *)
  492. PROCEDURE SubDxAx ; BEGIN B (29H) ; B (0C2H) END SubDxAx ; (* 29 C2: DX:=DX-AX *)
  493. PROCEDURE AddDiDx ; BEGIN B (1H) ; B (0D7H) END AddDiDx ; (* 01 D7: DI:=DI+DX *)
  494. PROCEDURE StSiCl ; BEGIN B (88H) ; B (0CH) END StSiCl ; (* 88 0C: [SI]:=CL *)
  495. PROCEDURE IncBx ; BEGIN B (43H) END IncBx ;
  496. PROCEDURE LdAlBx ; BEGIN B (8AH) ; B (07H) END LdAlBx ; (* 8A 07: AL:=[BX] *)
  497. PROCEDURE MovAlBl ; BEGIN B (8AH) ; B (0C3H) END MovAlBl ; (* 8A C3: AL:=BL *)
  498. (* 8A 07 and 8A C3 are one letter apart and do opposite things: the
  499. first reads the byte AT the pointer in BX, the second takes the low
  500. byte OF BX. getch's pushback path wanted the second and used the
  501. first, so it read memory 010Ah - the address of the character - and
  502. returned whatever was there. Hence the Ld/Mov split. *)
  503. PROCEDURE MovClBx ; BEGIN B (8AH) ; B (0FH) END MovClBx ; (* 8A 0F: CL:=[BX] *)
  504. PROCEDURE XorBxBx ; BEGIN B (31H) ; B (0DBH) END XorBxBx ; (* 31 DB: BX:=0 *)
  505. PROCEDURE MovBlDl ; BEGIN B (8AH) ; B (0DAH) END MovBlDl ; (* 8A DA: BL:=DL *)
  506. PROCEDURE MovBhAh ; BEGIN B (88H) ; B (0E7H) END MovBhAh ; (* 88 E7: BH:=AH *)
  507. PROCEDURE MovDxAx ; BEGIN B (89H) ; B (0C2H) END MovDxAx ; (* 89 C2: DX:=AX *)
  508. PROCEDURE MovSiBp4 ; BEGIN B (8BH) ; B (76H) ; B (4) END MovSiBp4 ; (* SI:=[BP+4] *)
  509. PROCEDURE MovSiBp6 ; BEGIN B (8BH) ; B (76H) ; B (6) END MovSiBp6 ; (* SI:=[BP+6] *)
  510. PROCEDURE LdAxSi ; BEGIN B (8BH) ; B (04H) END LdAxSi ; (* 8B 04: AX:=[SI] *)
  511. PROCEDURE StDiOffBx (off : CARDINAL) ;
  512. BEGIN B (89H) ; B (5DH) ; B (off MOD 256) END StDiOffBx ; (* MOV [DI+o],BX *)
  513. PROCEDURE StDiOffDx (off : CARDINAL) ;
  514. BEGIN B (89H) ; B (55H) ; B (off MOD 256) END StDiOffDx ; (* MOV [DI+o],DX *)
  515. PROCEDURE Js8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (78H, nm) END Js8 ;
  516. PROCEDURE Jns8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (79H, nm) END Jns8 ;
  517. PROCEDURE Jmp8 (nm : ARRAY OF CHAR ) ;
  518. BEGIN B (0EBH) ; AddFix (0, rpos, nm, 0) ; B (0) END Jmp8 ;
  519. PROCEDURE MovAxImm (v : CARDINAL ) ; BEGIN B (0B8H) ; W (v) END MovAxImm ;
  520. PROCEDURE XchgDlDh ; BEGIN B (86H) ; B (0D6H) END XchgDlDh ; (* 86 D6: DL<->DH *)
  521. PROCEDURE DecAl ; BEGIN B (0FEH) ; B (0C8H) END DecAl ; (* FE C8 *)
  522. PROCEDURE ShlDx1 ; BEGIN B (0D1H) ; B (0E2H) END ShlDx1 ; (* D1 E2: SHL DX,1 *)
  523. PROCEDURE NegDx ; BEGIN B (0F7H) ; B (0DAH) END NegDx ; (* F7 DA: NEG DX *)
  524. PROCEDURE CmpDh0 ; BEGIN B (80H) ; B (0FEH) ; B (0) END CmpDh0 ; (* 80 FE 00 *)
  525. PROCEDURE CmpBh0 ; BEGIN B (80H) ; B (0FFH) ; B (0) END CmpBh0 ; (* 80 FF 00 *)
  526. PROCEDURE CmpDx0 ; BEGIN B (83H) ; B (0FAH) ; B (0) END CmpDx0 ; (* 83 FA 00 *)
  527. PROCEDURE CmpAl0 ; BEGIN B (3CH) ; B (0) END CmpAl0 ; (* 3C 00 *)
  528. PROCEDURE AndDhImm (v : CARDINAL) ;
  529. BEGIN B (80H) ; B (0E6H) ; B (v MOD 256) END AndDhImm ; (* 80 E6 imm *)
  530. (* --- the MBF core's register moves and flag helpers --------------------- *)
  531. PROCEDURE MovAxCx ; BEGIN B (89H) ; B (0C8H) END MovAxCx ; (* AX:=CX *)
  532. PROCEDURE MovBxSi ; BEGIN B (89H) ; B (0F3H) END MovBxSi ; (* BX:=SI *)
  533. PROCEDURE MovDxDi ; BEGIN B (89H) ; B (0FAH) END MovDxDi ; (* DX:=DI *)
  534. PROCEDURE MovBxCx ; BEGIN B (89H) ; B (0CBH) END MovBxCx ; (* BX:=CX *)
  535. PROCEDURE MovBxDx ; BEGIN B (89H) ; B (0D3H) END MovBxDx ; (* BX:=DX *)
  536. PROCEDURE MovAhBh ; BEGIN B (88H) ; B (0FCH) END MovAhBh ; (* AH:=BH *)
  537. PROCEDURE MovAhBl ; BEGIN B (88H) ; B (0DCH) END MovAhBl ; (* AH:=BL *)
  538. PROCEDURE MovBlBh ; BEGIN B (88H) ; B (0FBH) END MovBlBh ; (* BL:=BH *)
  539. PROCEDURE MovBhDl ; BEGIN B (88H) ; B (0D7H) END MovBhDl ; (* BH:=DL *)
  540. PROCEDURE MovDlDh ; BEGIN B (88H) ; B (0F2H) END MovDlDh ; (* DL:=DH *)
  541. PROCEDURE MovDhDl ; BEGIN B (88H) ; B (0D6H) END MovDhDl ; (* DH:=DL *)
  542. PROCEDURE MovDlBh ; BEGIN B (88H) ; B (0FAH) END MovDlBh ; (* DL:=BH *)
  543. PROCEDURE MovBlAh ; BEGIN B (88H) ; B (0E3H) END MovBlAh ; (* BL:=AH *)
  544. PROCEDURE XchgAxCx ; BEGIN B (91H) END XchgAxCx ; (* XCHG AX,CX *)
  545. PROCEDURE XchgBxSi ; BEGIN B (87H) ; B (0F3H) END XchgBxSi ;
  546. PROCEDURE XchgDxDi ; BEGIN B (87H) ; B (0FAH) END XchgDxDi ;
  547. PROCEDURE XchgAhCh ; BEGIN B (86H) ; B (0ECH) END XchgAhCh ;
  548. PROCEDURE XorDhDh ; BEGIN B (30H) ; B (0F6H) END XorDhDh ; (* XOR DH,DH *)
  549. PROCEDURE XorAhAh ; BEGIN B (30H) ; B (0E4H) END XorAhAh ; (* XOR AH,AH *)
  550. PROCEDURE NotAh ; BEGIN B (0F6H) ; B (0D4H) END NotAh ; (* NOT AH *)
  551. PROCEDURE NotBx ; BEGIN B (0F7H) ; B (0D3H) END NotBx ; (* NOT BX *)
  552. PROCEDURE NotDx ; BEGIN B (0F7H) ; B (0D2H) END NotDx ; (* NOT DX *)
  553. PROCEDURE ShrDx1 ; BEGIN B (0D1H) ; B (0EAH) END ShrDx1 ; (* SHR DX,1 *)
  554. PROCEDURE RcrBx1 ; BEGIN B (0D1H) ; B (0DBH) END RcrBx1 ; (* RCR BX,1 *)
  555. PROCEDURE RcrDx1 ; BEGIN B (0D1H) ; B (0DAH) END RcrDx1 ; (* RCR DX,1 *)
  556. PROCEDURE RcrAh1 ; BEGIN B (0D0H) ; B (0DCH) END RcrAh1 ; (* RCR AH,1 *)
  557. PROCEDURE RclBx1 ; BEGIN B (0D1H) ; B (0D3H) END RclBx1 ; (* RCL BX,1 *)
  558. PROCEDURE RclDx1 ; BEGIN B (0D1H) ; B (0D2H) END RclDx1 ; (* RCL DX,1 *)
  559. PROCEDURE ShlAh1 ; BEGIN B (0D0H) ; B (0E4H) END ShlAh1 ; (* SHL AH,1 *)
  560. PROCEDURE IncAl ; BEGIN B (0FEH) ; B (0C0H) END IncAl ; (* INC AL *)
  561. PROCEDURE DecCl ; BEGIN B (0FEH) ; B (0C9H) END DecCl ; (* DEC CL *)
  562. PROCEDURE Stc ; BEGIN B (0F9H) END Stc ; (* STC *)
  563. PROCEDURE CmpAlCl ; BEGIN B (38H) ; B (0C8H) END CmpAlCl ; (* CMP AL,CL *)
  564. PROCEDURE CmpCl0 ; BEGIN B (80H) ; B (0F9H) ; B (0) END CmpCl0 ; (* 80 F9 00 *)
  565. PROCEDURE MovBhBl ; BEGIN B (88H) ; B (0DFH) END MovBhBl ; (* BH:=BL *)
  566. PROCEDURE SubAhCh ; BEGIN B (28H) ; B (0ECH) END SubAhCh ; (* AH:=AH-CH *)
  567. PROCEDURE MovClImm (v : CARDINAL) ;
  568. BEGIN B (0B1H) ; B (v MOD 256) END MovClImm ;
  569. PROCEDURE CmpClImm (v : CARDINAL) ;
  570. BEGIN B (80H) ; B (0F9H) ; B (v MOD 256) END CmpClImm ;
  571. PROCEDURE SubClAl ; BEGIN B (28H) ; B (0C1H) END SubClAl ; (* CL:=CL-AL *)
  572. PROCEDURE SubClImm (v : CARDINAL) ;
  573. BEGIN B (80H) ; B (0E9H) ; B (v MOD 256) END SubClImm ;
  574. PROCEDURE AddAhCh ; BEGIN B (00H) ; B (0ECH) END AddAhCh ; (* AH:=AH+CH *)
  575. PROCEDURE AddAhImm (v : CARDINAL) ;
  576. BEGIN B (80H) ; B (0C4H) ; B (v MOD 256) END AddAhImm ;
  577. PROCEDURE AdcBxSi ; BEGIN B (11H) ; B (0F3H) END AdcBxSi ;
  578. PROCEDURE AdcDxDi ; BEGIN B (11H) ; B (0FAH) END AdcDxDi ;
  579. PROCEDURE AdcBxImm (v : CARDINAL) ;
  580. BEGIN B (83H) ; B (0D3H) ; B (v MOD 256) END AdcBxImm ;
  581. PROCEDURE AdcDxImm (v : CARDINAL) ;
  582. BEGIN B (83H) ; B (0D2H) ; B (v MOD 256) END AdcDxImm ;
  583. PROCEDURE SbbBxSi ; BEGIN B (19H) ; B (0F3H) END SbbBxSi ;
  584. PROCEDURE SbbDxDi ; BEGIN B (19H) ; B (0FAH) END SbbDxDi ;
  585. PROCEDURE OrDiImm (v : CARDINAL) ;
  586. BEGIN B (81H) ; B (0CFH) ; W (v) END OrDiImm ; (* DI:=DI OR imm16 *)
  587. PROCEDURE OrDhImm (v : CARDINAL) ;
  588. BEGIN B (80H) ; B (0CEH) ; B (v MOD 256) END OrDhImm ;
  589. PROCEDURE TestDhImm (v : CARDINAL) ;
  590. BEGIN B (0F6H) ; B (0C6H) ; B (v MOD 256) END TestDhImm ;
  591. PROCEDURE StDataImm (delta, v : CARDINAL) ;
  592. BEGIN B (0C7H) ; B (06H) ; Dd (delta) ; W (v) END StDataImm ;
  593. PROCEDURE StDataCl (delta : CARDINAL) ;
  594. BEGIN B (88H) ; B (0EH) ; Dd (delta) END StDataCl ;
  595. PROCEDURE StDataDi (delta : CARDINAL) ;
  596. BEGIN B (89H) ; B (3EH) ; Dd (delta) END StDataDi ;
  597. PROCEDURE LdAlData (delta : CARDINAL) ;
  598. BEGIN B (0A0H) ; Dd (delta) END LdAlData ;
  599. PROCEDURE LdClData (delta : CARDINAL) ;
  600. BEGIN B (8AH) ; B (0EH) ; Dd (delta) END LdClData ;
  601. PROCEDURE XorDiData (delta : CARDINAL) ;
  602. BEGIN B (33H) ; B (3EH) ; Dd (delta) END XorDiData ; (* XOR DI,[Vx] *)
  603. PROCEDURE XorDataDh (delta : CARDINAL) ;
  604. BEGIN B (30H) ; B (36H) ; Dd (delta) END XorDataDh ; (* XOR [Vx],DH *)
  605. PROCEDURE XorDhData (delta : CARDINAL) ;
  606. BEGIN B (32H) ; B (36H) ; Dd (delta) END XorDhData ; (* XOR DH,[Vx] *)
  607. PROCEDURE XorDataImm (delta, v : CARDINAL) ;
  608. BEGIN B (80H) ; B (36H) ; Dd (delta) ; B (v MOD 256) END XorDataImm ;
  609. PROCEDURE AndDataImm (delta, v : CARDINAL) ;
  610. BEGIN B (80H) ; B (26H) ; Dd (delta) ; B (v MOD 256) END AndDataImm ;
  611. PROCEDURE TestDataImm (delta, v : CARDINAL) ;
  612. BEGIN B (0F6H) ; B (06H) ; Dd (delta) ; B (v MOD 256) END TestDataImm ;
  613. PROCEDURE LdCxSiOff (off : CARDINAL) ;
  614. BEGIN B (8BH) ; B (4CH) ; B (off MOD 256) END LdCxSiOff ;
  615. PROCEDURE LdDxSiOff (off : CARDINAL) ;
  616. BEGIN B (8BH) ; B (54H) ; B (off MOD 256) END LdDxSiOff ;
  617. PROCEDURE LdCxDiOff (off : CARDINAL) ;
  618. BEGIN B (8BH) ; B (4DH) ; B (off MOD 256) END LdCxDiOff ;
  619. PROCEDURE LdSiDiOff (off : CARDINAL) ;
  620. BEGIN B (8BH) ; B (75H) ; B (off MOD 256) END LdSiDiOff ;
  621. PROCEDURE LdDiDiOff (off : CARDINAL) ;
  622. BEGIN B (8BH) ; B (7DH) ; B (off MOD 256) END LdDiDiOff ;
  623. PROCEDURE PushBp4 ; BEGIN B (0FFH) ; B (76H) ; B (4) END PushBp4 ; (* PUSH [BP+4] *)
  624. PROCEDURE Call (nm : ARRAY OF CHAR) ;
  625. BEGIN B (0E8H) ; AddFix (1, rpos, nm, 0) ; W (0) END Call ;
  626. PROCEDURE StVxBx (delta : CARDINAL ) ;
  627. (* 89 1E lo hi: MOV [Vx],BX - the mirror of LdBxVx above, and it must use the
  628. same mod=00 / rm=110 and the same Dd fixup kind, or one of the two
  629. addresses a different place. *)
  630. BEGIN
  631. B (89H) ; B (01EH) ; Dd (delta)
  632. END StVxBx ;
  633. PROCEDURE JmpBx ; BEGIN B (0FFH) ; B (0E3H) END JmpBx ; (* FF E3: JMP BX *)
  634. (* JE 74 JNE 75 JB 72 JBE 76 JGE 7D *)
  635. PROCEDURE Je8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (74H, nm) END Je8 ;
  636. PROCEDURE Jne8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (75H, nm) END Jne8 ;
  637. PROCEDURE Jb8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (72H, nm) END Jb8 ;
  638. PROCEDURE Jbe8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (76H, nm) END Jbe8 ;
  639. PROCEDURE Jge8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (7DH, nm) END Jge8 ;
  640. PROCEDURE Jle8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (7EH, nm) END Jle8 ;
  641. PROCEDURE Ja8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (77H, nm) END Ja8 ;
  642. PROCEDURE Jae8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (73H, nm) END Jae8 ;
  643. PROCEDURE Jcxz8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (0E3H, nm) END Jcxz8 ;
  644. PROCEDURE Loop8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (0E2H, nm) END Loop8 ;
  645. (* ---------------------------------------------------------------- *)
  646. (* the entries *)
  647. (* ---------------------------------------------------------------- *)
  648. PROCEDURE EmitInitMem ;
  649. (* AX = offset of the program header. The header holds, at +4 the base of
  650. the program's data area and at +8 its end, so the globals can be zeroed -
  651. Pascal leaves them undefined, TP3's runtime clears them. Also makes
  652. ES = DS so that any string instruction in the library would work. *)
  653. BEGIN
  654. M ("initmem") ;
  655. MovSiAx ; (* SI = AX = the header offset the caller passed *)
  656. MovDxSi4 ; (* DX = [SI+4] = hdrDS = data base *)
  657. MovCxSi6 ; (* CX = [SI+6] = hdrHeap = data end *)
  658. CmpCxDx ;
  659. Jbe8 ("im_done") ;
  660. MovDiDx ; (* DI = data base *)
  661. M ("im_zero") ;
  662. XorAxAx ; (* AX = 0: the value written into every global.
  663. The caller passes the header offset in AX, so
  664. without this the "zeroing" loop would write the
  665. header offset into all of them. *)
  666. StDiAx ;
  667. AddDi2 ;
  668. CmpDiCx ;
  669. Jb8 ("im_zero") ;
  670. M ("im_done") ;
  671. PushDs ; PopEs ;
  672. RetR
  673. END EmitInitMem ;
  674. PROCEDURE EmitEnd ;
  675. (* progend and halt are the same code: the compiler already zeroes AX for
  676. progend, and a HALT argument is discarded at compile time, so both leave
  677. with exit code 0. *)
  678. BEGIN
  679. M ("progend") ;
  680. M ("halt") ;
  681. XorAxAx ;
  682. MovAh (4CH) ;
  683. Int21 ;
  684. RetR
  685. END EmitEnd ;
  686. PROCEDURE EmitStackChk ;
  687. (* called from every procedure prologue. Range and stack checking are not
  688. compiled in yet, so this must do nothing at all - in particular it must
  689. not touch a register, because the call site is in the middle of a
  690. partially evaluated expression. *)
  691. BEGIN
  692. M ("stackchk") ;
  693. RetR
  694. END EmitStackChk ;
  695. PROCEDURE EmitGetCh ;
  696. (* AL = next character, 1Ah at end of input. INT 21h AH=08h reads without
  697. echoing, so a redirected stdin behaves the same as a keyboard.
  698. This is TP3's `getbyte' (TPSRC4:94), not a bare INT 21h. getbyte serves a
  699. buffered character from the file record without advancing the buffer
  700. pointer when the "char pre-read" flag ($02) is set, and readnum sets that
  701. flag on the character that ENDS a number - the terminating blank is *not*
  702. consumed. xreadln is then called and it is the xreadln that consumes the
  703. newline.
  704. This matters because `readln(n)' is emitted as xrdint followed by xreadln
  705. (TPSRC8 prdrdln emits xreadln whenever rdlnflg is set). A getch that
  706. always consumed its character left xreadln starting on the *following*
  707. line, so `readln(n); readln(c)' read the char from line 3 of the input.
  708. One byte of pushback reproduces the flag. *)
  709. BEGIN
  710. M ("getch") ;
  711. LdBxVx (D_PUSH) ; (* BX := the slot's contents *)
  712. CmpBx0 ;
  713. Je8 ("gc_dos") ;
  714. MovAlBl ; (* AL := the pending character, i.e. the
  715. low byte OF BX - not the byte at [BX] *)
  716. XorBxBx ;
  717. StVxBx (D_PUSH) ; (* and empty the slot *)
  718. RetR ;
  719. M ("gc_dos") ;
  720. MovAh (08H) ;
  721. Int21 ;
  722. RetR
  723. END EmitGetCh ;
  724. PROCEDURE EmitUnGetCh ;
  725. (* put AL back, so the next getch returns it again. TP3 does this by
  726. rewinding the buffer pointer; a one-byte slot is the same thing. *)
  727. BEGIN
  728. M ("ungetch") ;
  729. MovDlAl ;
  730. MovBxImm (1) ; (* BH := 1 marks the slot occupied, so
  731. a pushed-back NUL is still a
  732. pushed-back NUL *)
  733. MovBlDl ;
  734. StVxBx (D_PUSH) ;
  735. RetR
  736. END EmitUnGetCh ;
  737. PROCEDURE EmitWrInt ;
  738. (* one signed 16-bit value on the stack. Div CX gives the remainder in DX,
  739. which is turned into a digit and stored backwards from the end of the
  740. scratch area, then printed forwards. *)
  741. BEGIN
  742. M ("wrint") ;
  743. PushBp ; MovBpSp ;
  744. MovAxBp4 ;
  745. CmpAx0 ;
  746. Jge8 ("wi_pos") ;
  747. PushAx ;
  748. MovAlD (ORD ("-")) ; MovAh (2) ; Int21 ;
  749. PopAx ;
  750. NegAx ;
  751. M ("wi_pos") ;
  752. MovCxImm (10) ;
  753. MovBxVx (D_NUM + 8) ; (* BX = one past the last digit *)
  754. MovSiBx ;
  755. M ("wi_dig") ;
  756. XorDxDx ;
  757. DivCx ;
  758. AddDl (ORD ("0")) ;
  759. DecSi ;
  760. StSiDl ;
  761. CmpAx0 ;
  762. Jne8 ("wi_dig") ;
  763. M ("wi_out") ;
  764. CmpSiBx ;
  765. Je8 ("wi_done") ;
  766. MovAlSi ; (* the digit: AH=02h wants it in AL *)
  767. MovAh (02H) ; Int21 ;
  768. IncSi ;
  769. J8 ("wi_out") ;
  770. M ("wi_done") ;
  771. MovSpBp ; PopBp ; RetR
  772. END EmitWrInt ;
  773. PROCEDURE EmitWrIntW ;
  774. (* `writeln(I:2)': an integer in a field of `width' columns, right-justified.
  775. The caller pushes the VALUE first and the WIDTH second, so the value is at
  776. [BP+6] and the width at [BP+4]. The number of characters the signed
  777. decimal will occupy is counted first (one extra when negative); the pad is
  778. max(width - chars, 0) spaces. The digit loop is wrint's, duplicated rather
  779. than shared: the two entries have different argument offsets, and a jump
  780. into the middle of the other entry's frame would be a convention nobody
  781. could see from either name. *)
  782. BEGIN
  783. M ("wrintw") ;
  784. PushBp ; MovBpSp ;
  785. (* BX := number of characters: sign + digits *)
  786. MovBxImm (0) ;
  787. MovAxBp6 ;
  788. CmpAx0 ;
  789. Jge8 ("wiw_abs") ;
  790. IncBx ; (* the '-' counts as one column *)
  791. NegAx ;
  792. M ("wiw_abs") ;
  793. MovCxImm (10) ;
  794. M ("wiw_cnt") ;
  795. IncBx ;
  796. XorDxDx ;
  797. DivCx ;
  798. CmpAx0 ;
  799. Jne8 ("wiw_cnt") ;
  800. (* pad := width - chars; only a positive pad prints *)
  801. MovAxBp4 ;
  802. SubAxBx ;
  803. CmpAx0 ;
  804. Jle8 ("wiw_body") ;
  805. MovCxAx ;
  806. M ("wiw_pad") ;
  807. MovAlD (ORD (' ')) ; MovAh (02H) ; Int21 ;
  808. Loop8 ("wiw_pad") ;
  809. M ("wiw_body") ;
  810. (* the signed decimal, exactly as wrint prints it *)
  811. MovAxBp6 ;
  812. CmpAx0 ;
  813. Jge8 ("wiw_pos") ;
  814. PushAx ;
  815. MovAlD (ORD ('-')) ; MovAh (2) ; Int21 ;
  816. PopAx ;
  817. NegAx ;
  818. M ("wiw_pos") ;
  819. MovCxImm (10) ;
  820. MovBxVx (D_NUM + 8) ;
  821. MovSiBx ;
  822. M ("wiw_dig") ;
  823. XorDxDx ;
  824. DivCx ;
  825. AddDl (ORD ('0')) ;
  826. DecSi ;
  827. StSiDl ;
  828. CmpAx0 ;
  829. Jne8 ("wiw_dig") ;
  830. M ("wiw_out") ;
  831. CmpSiBx ;
  832. Je8 ("wiw_done") ;
  833. MovAlSi ;
  834. MovAh (02H) ; Int21 ;
  835. IncSi ;
  836. J8 ("wiw_out") ;
  837. M ("wiw_done") ;
  838. MovSpBp ; PopBp ; RetR
  839. END EmitWrIntW ;
  840. PROCEDURE EmitWrChar ;
  841. (* the low byte of the one 16-bit argument, which sits above the return
  842. address. There is no [SP] addressing in 16-bit mode, so BP stands in for
  843. the stack pointer -- and it has to be SAVED, because BP is the one register
  844. a caller is entitled to expect back: the test driver keeps its cursor into
  845. the case record there, so an entry that borrows BP without returning it
  846. sends the next call to a garbage address. That is not hypothetical: wrchar
  847. and wrbool both did it, and "writeln(42) writeln TRUE in one program" hung
  848. the machine with the record header printed twice, because the driver had
  849. been sent back to the top of its own loop. Load BP, use it, hand it back.
  850. MovAlArg4, not MovAlArg2: BP is pushed first, so the argument is four bytes
  851. up, not two. AL, not DL: see the AL twins above. *)
  852. BEGIN
  853. M ("wrchar") ;
  854. PushBp ; MovBpSp ;
  855. MovAlArg4 ;
  856. MovSpBp ; PopBp ;
  857. MovAh (02H) ;
  858. Int21 ;
  859. RetR
  860. END EmitWrChar ;
  861. PROCEDURE EmitWrInl ;
  862. (* Write an inline string literal - TP3 TPSRC4 "xwrtinl".
  863. The compiler emits
  864. CALL wrtinl <length byte> <character>...
  865. so the return address on the stack points at the length byte that follows
  866. the call. POP BX takes that address, CX picks up the length, and the
  867. routine finishes with JMP BX - returning to just past the last character.
  868. That is the whole trick: the literal is self-delimiting, so it needs no
  869. terminator, no length table and no space in the data segment, and it costs
  870. the code stream only the characters themselves (TPSRC10 "estring" emits
  871. exactly <length byte><chars> for the same reason).
  872. Consequently this entry has NO stack argument, unlike WrInt/WrChar: the
  873. return address has already been consumed by the POP. *)
  874. BEGIN
  875. M ("wrtinl") ;
  876. PopBx ; (* BX := address of the length byte *)
  877. XorCxCx ;
  878. MovClBx ; (* CX := length *)
  879. IncBx ; (* BX -> first character *)
  880. MovAh (02H) ; (* INT 21h/02h: put character, AL *)
  881. Jcxz8 ("wn_end") ; (* empty string -> nothing to do *)
  882. M ("wn_loop") ;
  883. LdAlBx ; (* AL := next character *)
  884. Int21 ; (* (preserves every register but AL) *)
  885. IncBx ;
  886. Loop8 ("wn_loop") ;
  887. M ("wn_end") ;
  888. JmpBx (* resume past the string; no RET here,
  889. the return address is already gone *)
  890. END EmitWrInl ;
  891. PROCEDURE EmitWrStr ;
  892. (* Write a string VARIABLE - TP3 TPSRC4 "xwrtstr", minus the field-width
  893. formatting this compiler does not compile yet.
  894. The compiler emits
  895. <PUSH the string's address> from EmPushVarAddr
  896. CALL wrstr
  897. so the address is the one 16-bit argument on the stack, read through BP
  898. exactly as WrChar reads its value (there is no [SP] form). The first byte
  899. at that address is the current LENGTH and the characters follow it, which
  900. is the layout TPSRC8's `string' type gives a variable: TPSRC8:83 does
  901. `INC BX / MOV varsize,BX' for `string [n]', so n+1 bytes, length byte
  902. first.
  903. The loop is wrtinl's, pointed at the caller's bytes instead of at the
  904. bytes behind the call: CL := [BX] is the length, BX moves to the first
  905. character, and one INT 21h/02h prints each one until CX runs out. A
  906. length of 0 prints nothing, so an empty string is silent. *)
  907. BEGIN
  908. M ("wrstr") ;
  909. PushBp ; MovBpSp ;
  910. PushBx ; PushCx ; PushAx ;
  911. MovBxBp4 ; (* BX := the caller's string address *)
  912. XorCxCx ;
  913. MovClBx ; (* CX := the length byte at [BX] *)
  914. IncBx ; (* BX -> the first character *)
  915. MovAh (02H) ; (* INT 21h/02h: put the character in AL *)
  916. Jcxz8 ("ws_end") ; (* empty string -> nothing to do *)
  917. M ("ws_loop") ;
  918. LdAlBx ; (* AL := next character *)
  919. Int21 ; (* preserves every register but AL *)
  920. IncBx ;
  921. Loop8 ("ws_loop") ;
  922. M ("ws_end") ;
  923. PopAx ; PopCx ; PopBx ;
  924. MovSpBp ; PopBp ;
  925. RetR
  926. END EmitWrStr ;
  927. PROCEDURE EmitWrBool ;
  928. (* 4, not 2, for the reason given on CmpArg4W0: BP is pushed before it is
  929. borrowed, so the argument is four bytes up. POP BP before the RET, and
  930. note that CmpArg4W0 has already handed SP back, so the pop lands on the
  931. saved BP and not on the argument. *)
  932. BEGIN
  933. M ("wrbool") ;
  934. PushBp ;
  935. CmpArg4W0 ;
  936. Jne8 ("wb_t") ;
  937. MovDxVx (D_FALSE) ;
  938. J8 ("wb_o") ;
  939. M ("wb_t") ;
  940. MovDxVx (D_TRUE) ;
  941. M ("wb_o") ;
  942. MovAh (09H) ;
  943. Int21 ;
  944. PopBp ;
  945. RetR
  946. END EmitWrBool ;
  947. PROCEDURE EmitWrLn ;
  948. BEGIN
  949. M ("wrln") ;
  950. MovDxVx (D_CRLF) ;
  951. MovAh (09H) ;
  952. Int21 ;
  953. RetR
  954. END EmitWrLn ;
  955. PROCEDURE EmitWrReal ;
  956. (* the 6-byte real is on the stack but is not formatted: the compiler does
  957. not yet load real operands into a form the runtime could read. A visible
  958. marker beats printing the mantissa as an integer. *)
  959. BEGIN
  960. M ("wrreal") ;
  961. MovDxVx (D_REAL) ;
  962. MovAh (09H) ;
  963. Int21 ;
  964. RetR
  965. END EmitWrReal ;
  966. PROCEDURE EmitRdInt ;
  967. (* Read a number into a 16-bit variable: the caller's address is on the
  968. stack. The scan itself is EmitRdScan, shared with rdbyte; an entry owns
  969. its frame, its address load and its store, nothing else. *)
  970. BEGIN
  971. M ("rdint") ;
  972. PushBp ; MovBpSp ;
  973. PushAx ; PushBx ; PushCx ; PushDx ; PushDi ;
  974. C8 ("rdscan") ;
  975. CmpCxV (2) ;
  976. Je8 ("ri_skip") ; (* nothing entered: leave the variable
  977. alone, as TP3's xrdint at rdierr does *)
  978. MovDiBp4 ; (* ...the caller's address into DI... *)
  979. StDiAx ; (* ...and store the word. MovDiBp4 and
  980. not a [BX] form: there is none; see
  981. the note on the store emitters. *)
  982. M ("ri_skip") ;
  983. PopDi ; PopDx ; PopCx ; PopBx ; PopAx ;
  984. MovSpBp ; PopBp ; RetR
  985. END EmitRdInt ;
  986. PROCEDURE EmitRdScan ;
  987. (* The number reader, called by rdint (word store) and by rdbyte (byte
  988. store). A transcription of TP3 TPSRC4 xrdint/readnum, minus the store:
  989. rnspace: getbyte ; ^Z ? -> rnend
  990. consume ; <= 20h ? -> rnspace
  991. rndig: [buf]=char ; getbyte ; <= 20h ? -> rnend (NOT consumed)
  992. consume ; -> rndig
  993. rnend: buf[0] := 0
  994. It builds NO frame of its own and touches neither BP nor SP, so it is
  995. called from the caller's frame and that frame's [BP+4] is still the
  996. caller's argument when it returns. It clobbers AX BX CX DX DI, which the
  997. caller has already saved.
  998. Returns AX = the value, and CX = 0 positive, 1 negative, 2 nothing read.
  999. CX = 2 is set HERE, on the ^Z path: it is the state both callers test to
  1000. decide not to store, and before this split the check existed only as a
  1001. jump to the exit, so the state itself was never produced. AX is
  1002. meaningless when CX = 2.
  1003. Two behaviours are load-bearing and neither is obvious:
  1004. - the character that ENDS the scan is left pending, because readnum tests
  1005. it *before* clearing getbyte's pre-read flag. xreadln, which the
  1006. compiler emits next for a readln, is what consumes the newline. See the
  1007. note on getch.
  1008. - ^Z before any digit returns without a value, and the caller then returns
  1009. WITHOUT touching the variable: TP3's xrdint reaches rdierr and RETs.
  1010. Sign in CX, value in DI, value in AX on the way out. *)
  1011. BEGIN
  1012. M ("rdscan") ;
  1013. M ("ri_lead") ;
  1014. C8 ("getch") ;
  1015. CmpAl (1AH) ; (* ^Z: end of input *)
  1016. Je8 ("ri_none") ;
  1017. CmpAl (20H) ; (* every control char and the space.
  1018. 20H, not 20: readnum skips
  1019. everything <= #$20, and a literal
  1020. that READS like hex but is
  1021. decimal compares AL against 14h,
  1022. so a leading space is never
  1023. skipped and the scan ends with
  1024. nothing read. Every other
  1025. literal in this module is
  1026. explicit for the same reason. *)
  1027. Jbe8 ("ri_lead") ;
  1028. XorCxCx ;
  1029. CmpAl (ORD ("-")) ;
  1030. Jne8 ("ri_nos") ;
  1031. IncCx ;
  1032. C8 ("getch") ;
  1033. J8 ("ri_dig0") ;
  1034. M ("ri_nos") ;
  1035. CmpAl (ORD ("+")) ;
  1036. Jne8 ("ri_dig0") ;
  1037. C8 ("getch") ;
  1038. M ("ri_dig0") ;
  1039. XorDiDi ;
  1040. M ("ri_dig") ;
  1041. CmpAl (ORD ("0")) ;
  1042. Jb8 ("ri_done") ;
  1043. CmpAl (ORD ("9")) ;
  1044. Ja8 ("ri_done") ;
  1045. SubAl (ORD ("0")) ;
  1046. MovAh (0) ; (* AX := the digit, 0..9 *)
  1047. XchgAxDi ; (* AX := the value so far, DI := the digit.
  1048. The digit has to survive MUL, and the 8-bit
  1049. reg field of 8A/88 has only AL/CL/DL/BL/AH/
  1050. CH/DH/BH - there is no SI or DI byte to park
  1051. it in. MUL r/m16 writes DX, so the previous
  1052. version's "keep the digit across the multiply"
  1053. comment was describing a register the multiply
  1054. owns. Every read of an integer therefore
  1055. returned 0: the digit was computed, moved to
  1056. DH, and multiplied away one instruction later. *)
  1057. MovBxImm (10) ;
  1058. MulBx ; (* DX:AX := value * 10 *)
  1059. AddAxDi ; (* AX := low word + digit. A carry out of bit 15
  1060. is dropped, which is what a 16-bit INTEGER
  1061. does anyway - the high word of the product is
  1062. never stored. *)
  1063. MovDiAx ;
  1064. C8 ("getch") ;
  1065. J8 ("ri_dig") ;
  1066. M ("ri_done") ;
  1067. C8 ("ungetch") ; (* the delimiter stays pending *)
  1068. CmpCx0 ;
  1069. Je8 ("ri_pos") ;
  1070. NegDi ;
  1071. M ("ri_pos") ;
  1072. MovAxDi ; (* the parsed value out of DI and into AX,
  1073. where both callers store it from *)
  1074. RetR ;
  1075. M ("ri_none") ; (* ^Z first: no value, and CX says so *)
  1076. MovCxImm (2) ;
  1077. RetR
  1078. END EmitRdScan ;
  1079. PROCEDURE EmitRdByte ;
  1080. (* Read a number into a BYTE variable: the same scan, a one-byte store.
  1081. TPSRC8 prdnum dispatches on the variable's size - `CMP varsize,#$01 /
  1082. JA prdemit' keeps xrdint for anything wider and falls through to
  1083. `MOV AX,#xrdbyte' for a one-byte variable - and TPSRC4's two entries
  1084. differ in exactly one store: xrdbyte is `CLC', xrdint is `STC', and rdi1
  1085. stores AX or AL on `JNB rdibyt'.
  1086. The flag form is not transcribed. It would put the STC at the head of the
  1087. rdint entry, and the BP contract in tests/rt_exec.py requires an entry
  1088. that borrows BP to *start* with `55 8B EC' and to hand BP back from inside
  1089. its own slice - which is what an entry owning its own frame does. Two
  1090. frames and two stores around one scan is the same idea, with that
  1091. constraint respected.
  1092. The store is the whole reason this entry exists: rdint writes two bytes,
  1093. and the second one is the byte that FOLLOWS the variable, so
  1094. `readln (b)' was reading into b and into whatever the compiler had put
  1095. next to it. *)
  1096. BEGIN
  1097. M ("rdbyte") ;
  1098. PushBp ; MovBpSp ;
  1099. PushAx ; PushBx ; PushCx ; PushDx ; PushDi ;
  1100. C8 ("rdscan") ;
  1101. CmpCxV (2) ;
  1102. Je8 ("rby_skip") ; (* nothing entered: leave it alone *)
  1103. MovDiBp4 ; (* the caller's address into DI *)
  1104. StDiAl ; (* ONE byte - 88 05, AL not AX *)
  1105. M ("rby_skip") ;
  1106. PopDi ; PopDx ; PopCx ; PopBx ; PopAx ;
  1107. MovSpBp ; PopBp ; RetR
  1108. END EmitRdByte ;
  1109. PROCEDURE EmitRdChar ;
  1110. BEGIN
  1111. M ("rdchar") ;
  1112. PushBp ; MovBpSp ;
  1113. PushAx ; PushBx ; PushDi ;
  1114. C8 ("getch") ;
  1115. MovDlAl ;
  1116. MovDiBp4 ; (* no [BX] form: the address goes in DI *)
  1117. StDiDl ;
  1118. PopDi ; PopBx ; PopAx ;
  1119. MovSpBp ; PopBp ; RetR
  1120. END EmitRdChar ;
  1121. PROCEDURE EmitRdBool ;
  1122. (* one character, classified the way TP3 does: T/t/Y/y/1 true, anything else
  1123. false. *)
  1124. BEGIN
  1125. M ("rdbool") ;
  1126. PushBp ; MovBpSp ;
  1127. PushAx ; PushBx ; PushCx ; PushDi ;
  1128. C8 ("getch") ;
  1129. XorCxCx ;
  1130. CmpAl (ORD ("T")) ; Je8 ("rb_t") ;
  1131. CmpAl (ORD ("t")) ; Je8 ("rb_t") ;
  1132. CmpAl (ORD ("Y")) ; Je8 ("rb_t") ;
  1133. CmpAl (ORD ("y")) ; Je8 ("rb_t") ;
  1134. CmpAl (ORD ("1")) ; Je8 ("rb_t") ;
  1135. J8 ("rb_s") ;
  1136. M ("rb_t") ;
  1137. IncCx ;
  1138. M ("rb_s") ;
  1139. MovDiBp4 ; (* DI = the caller's address, because there is
  1140. no [BX] to store through *)
  1141. StDiCx ;
  1142. PopDi ; PopCx ; PopBx ; PopAx ;
  1143. MovSpBp ; PopBp ; RetR
  1144. END EmitRdBool ;
  1145. PROCEDURE EmitRdLn ;
  1146. (* discard the rest of the line, including the terminator *)
  1147. BEGIN
  1148. M ("rdln") ;
  1149. PushAx ;
  1150. M ("rl_loop") ;
  1151. C8 ("getch") ;
  1152. CmpAl (0DH) ; Je8 ("rl_e") ;
  1153. CmpAl (0AH) ; Je8 ("rl_e") ;
  1154. CmpAl (1AH) ; Je8 ("rl_e") ; (* ^Z: end of input *)
  1155. J8 ("rl_loop") ;
  1156. M ("rl_e") ;
  1157. PopAx ;
  1158. RetR
  1159. END EmitRdLn ;
  1160. PROCEDURE EmitRdStr ;
  1161. (* readln(s)/read(s) of a string variable: TPSRC4's xrdstr, the runtime entry
  1162. behind both prdstr and prdrdln. The caller pushes the variable's address
  1163. (one stack word, read here via BP), sets CX = max_len = varsize-1, and pops
  1164. the word after the call.
  1165. Keyed to ours rather than TP3's exactly: TP3 emits MOV CL,imm8 and clears
  1166. CH the length load; this runtime has no byte-register move into CH/CL, so
  1167. the compiler emits a full MOV CX,imm16 and CX *is* the limit (Loop8 / the
  1168. count arithmetic below rely on it).
  1169. Characters come from getch, which clobbers only AX and BX, so the running
  1170. count lives in BX behind a push/pop around each call. Reading stops at CR,
  1171. LF or ^Z - LF because our input files are LF-terminated - and the terminator
  1172. is left PENDING via ungetch, exactly as TP3's xrdstr leaves its CR pending
  1173. through the pre-read flag: the readln wrapper then consumes the newline,
  1174. while a bare read(s) leaves it for the next read. The LOOP truncates a line
  1175. longer than max_len. dest[0] := length; dest[1..] := chars. *)
  1176. BEGIN
  1177. M ("rdstr") ;
  1178. PushBp ; MovBpSp ;
  1179. PushAx ; PushBx ; PushCx ; PushDx ; PushDi ;
  1180. MovDiBp4 ; (* DI := dest base, for the length byte *)
  1181. MovAxBp4 ; MovSiAx ; (* SI := dest, walking *)
  1182. IncSi ; (* SI := dest + 1: chars live at dest[1..], the
  1183. length byte at dest[0] is stored at the end *)
  1184. XorBxBx ; (* BX := 0: count of characters read *)
  1185. M ("rs_loop") ;
  1186. PushBx ; (* getch clobbers BX (and AX) *)
  1187. C8 ("getch") ;
  1188. PopBx ;
  1189. CmpAl (0DH) ; Je8 ("rs_term") ; (* CR *)
  1190. CmpAl (0AH) ; Je8 ("rs_term") ; (* LF *)
  1191. CmpAl (1AH) ; Je8 ("rs_term") ; (* ^Z *)
  1192. MovDlAl ; (* DL := char *)
  1193. IncBx ; (* count := count + 1 *)
  1194. StSiDl ; (* dest[i] := char *)
  1195. IncSi ;
  1196. Loop8 ("rs_loop") ; (* CX--; loop while CX # 0: truncation *)
  1197. M ("rs_end") ;
  1198. MovAlBl ; (* AL := count *)
  1199. StDiAl ; (* dest[0] := length *)
  1200. PopDi ; PopDx ; PopCx ; PopBx ; PopAx ;
  1201. MovSpBp ; PopBp ; RetR ;
  1202. M ("rs_term") ;
  1203. PushBx ; (* ungetch clobbers BX too *)
  1204. C8 ("ungetch") ;
  1205. PopBx ;
  1206. J8 ("rs_end")
  1207. END EmitRdStr ;
  1208. PROCEDURE EmitStrStore ;
  1209. (* s := t for string variables: TPSRC2's strstore with TPSRC8 asnvar's stack
  1210. shape. The caller pushes BOTH addresses - destination first, source second,
  1211. so the LAST push (dest) sits at [BP+4] and the source at [BP+6] - sets
  1212. CX = max_len = varsize-1 of the DESTINATION, calls, and pops both words
  1213. (this runtime is caller-pops, unlike TP3's strstore which pops itself).
  1214. TP3's strstore rewrites the length byte of the stack copy of the source to
  1215. min(src[0], max), then copies length byte + chars, truncating at max, with
  1216. the destination TAIL past the new length left untouched. Here, with both
  1217. variables in memory and no stack copy, that becomes: count := min(src[0],
  1218. max); dest[0] := count; copy count chars src[1..] -> dest[1..]. The source
  1219. variable is never written - truncation is expressed in the destination's own
  1220. length byte only. A source longer than max is truncated; a shorter one is
  1221. copied whole (CX := src length, count including the empty case where LOOP
  1222. simply never runs). *)
  1223. BEGIN
  1224. M ("strstore") ;
  1225. PushBp ; MovBpSp ;
  1226. PushAx ; PushBx ; PushCx ; PushDx ;
  1227. MovAxBp4 ; MovSiAx ; (* SI := dest, walking *)
  1228. MovBxBp6 ; (* BX := source *)
  1229. XorDxDx ;
  1230. LdAlBx ; MovDlAl ; (* DL := src[0], the source length *)
  1231. IncBx ; (* BX -> first source character *)
  1232. CmpCxDx ; (* CMP CX (max), DX (src length) *)
  1233. Jb8 ("ss_trunc") ;
  1234. MovCxDx ; (* not truncated: CX := src length *)
  1235. J8 ("ss_len") ;
  1236. M ("ss_trunc") ;
  1237. MovDlCl ; (* truncated: DL := max, for the length store *)
  1238. M ("ss_len") ;
  1239. StSiDl ; (* dest[0] := length *)
  1240. IncSi ; (* dest -> first character *)
  1241. Jcxz8 ("ss_done") ; (* an empty source copies nothing; LOOP with
  1242. CX = 0 would run 65536 times, not zero *)
  1243. M ("ss_loop") ;
  1244. LdAlBx ; (* AL := src char *)
  1245. MovDlAl ; (* DL := char *)
  1246. StSiDl ; (* dest char := src char *)
  1247. IncBx ; IncSi ;
  1248. Loop8 ("ss_loop") ; (* CX--; loop while CX # 0 *)
  1249. M ("ss_done") ;
  1250. PopDx ; PopCx ; PopBx ; PopAx ;
  1251. MovSpBp ; PopBp ; RetR
  1252. END EmitStrStore ;
  1253. PROCEDURE EmitCmpStr ;
  1254. (* cmpstr: lexicographic comparison of two strings, TPSRC2's cmpstr with the
  1255. addresses taken as arguments rather than the strings pushed by value.
  1256. The caller pushes BOTH addresses - the FIRST (left) operand first, so it
  1257. sits at [BP+6], the second (right) at [BP+4], the same order strstore takes
  1258. its two - calls, and pops both words (caller-pops, unlike TP3's cmpstr,
  1259. which pops itself). It returns AX = -1, 0 or +1 for left < right, left =
  1260. right, left > right.
  1261. Why AX and not the flags, when TP3's cmpstr leaves them set? Because TP3's
  1262. cmpstr also REMOVES its own arguments (`MOV SP,BX', TPSRC2 960), so its six
  1263. wrappers (csteq/cstne/cstge/cstle/cstg/cstl, TPSRC2 890-925) can branch on
  1264. the flags immediately after the CALL. Here the caller pushes the two
  1265. addresses, and dropping them with ADD SP,4 - the one-instruction way - is
  1266. in the CPU's arithmetic group and DESTROYS the flags. (POP does not, but
  1267. then the flags would have to survive the caller's next emission, which is
  1268. exactly the invisible coupling the notes in this file keep warning about.)
  1269. A signed value in AX costs a few instructions inside the entry and keeps the
  1270. flag convention the compiler's own business, where the six-arm table already
  1271. lives: the caller runs OR AX,AX and the same EmSetcc it runs after every
  1272. numeric comparison, so the two paths cannot drift apart.
  1273. That tail is not invented. A TP3 wrapper is `MOV AX,#1 / Jcc / DEC AX /
  1274. OR AX,AX' - the same EmSetcc our numeric arm emits, followed by the same
  1275. OR AX,AX - and the only thing this design moves is WHERE it happens: out of
  1276. six one-per-operator runtime entries and into the one shared table. TP3
  1277. needs the six because its cmpstr is reached through vcompare's vector table
  1278. (TPSRC9 133) by type; we already have the operator as a number.
  1279. The comparison is TPSRC2's, translated to an explicit byte loop because
  1280. this runtime has no REPZ CMPS: min(lenL,lenR) bytes are compared in order,
  1281. the first difference decides, and if the common prefix is equal the LENGTHS
  1282. decide (a prefix is less than the string that continues it). The byte
  1283. compare is unsigned - CF is the borrow out of AL-DL - which is what
  1284. "lexicographic" means for characters. The tiebreak re-reads the lengths
  1285. from the arguments rather than keeping them in registers across the loop,
  1286. so CX is free to be LOOP's count, and the empty string is the case where
  1287. that count is 0 and LOOP would run 65536 times instead of none. *)
  1288. BEGIN
  1289. M ("cmpstr") ;
  1290. PushBp ; MovBpSp ;
  1291. PushBx ; PushCx ; PushDx ; (* AX is NOT saved: it is the result *)
  1292. XorCxCx ; (* CX := 0, to make the CL load below a length *)
  1293. XorDxDx ;
  1294. MovBxBp6 ; (* BX := the first (left) string's address *)
  1295. MovClBx ; (* CL := lenL *)
  1296. MovAxBp4 ; MovSiAx ; (* SI := the second (right) string's address *)
  1297. MovDlSi ; (* DL := lenR *)
  1298. CmpCxDx ; (* CMP lenL, lenR *)
  1299. Jbe8 ("cs_min") ; (* lenL <= lenR: CX is already the shorter *)
  1300. MovCxDx ; (* CX := lenR *)
  1301. M ("cs_min") ; (* CX = min (lenL,lenR) *)
  1302. Jcxz8 ("cs_len") ; (* an empty operand: no bytes to compare *)
  1303. IncBx ; IncSi ; (* past the length byte, to the characters *)
  1304. M ("cs_loop") ;
  1305. LdAlBx ; (* AL := left[i] *)
  1306. MovDlSi ; (* DL := right[i] *)
  1307. CmpAlDl ; (* CMP AL,DL - unsigned, so CF is the order *)
  1308. Jne8 ("cs_flag") ; (* the first difference decides *)
  1309. IncBx ; IncSi ;
  1310. Loop8 ("cs_loop") ;
  1311. M ("cs_len") ; (* equal over the common prefix: lengths decide *)
  1312. MovBxBp6 ; MovClBx ; (* CX := lenL again - BX walked past it *)
  1313. MovAxBp4 ; MovSiAx ;
  1314. XorDxDx ; MovDlSi ; (* DX := lenR *)
  1315. CmpCxDx ;
  1316. M ("cs_flag") ; (* flags are (left - right): CF=1 less, ZF=1 equal *)
  1317. MovAlD (0) ; MovAh (0) ; (* AX := 0. MOV leaves the flags alone, which
  1318. is what lets the two tests below still read
  1319. the comparison. *)
  1320. Jb8 ("cs_less") ;
  1321. Je8 ("cs_done") ;
  1322. MovAlD (1) ; (* greater *)
  1323. J8 ("cs_done") ;
  1324. M ("cs_less") ;
  1325. MovAlD (0FFH) ; MovAh (0FFH) ; (* AX := -1 *)
  1326. M ("cs_done") ;
  1327. PopDx ; PopCx ; PopBx ; (* but NOT AX: AX is the RESULT. The first
  1328. version pushed AX with the others and
  1329. popped it back here, which restored the
  1330. CALLER's AX - for `a < b' that is the
  1331. address of the RIGHT operand, still in AX
  1332. from its LEA - and every ordering test read
  1333. a positive number. `<', `<=', `=', `<>',
  1334. `>', `>=' over any left<right string came
  1335. back FALSE TRUE FALSE FALSE TRUE TRUE,
  1336. independently confirmed on qemu. Saving AX
  1337. at all is wrong: there is nothing to keep. *)
  1338. MovSpBp ; PopBp ; RetR
  1339. END EmitCmpStr ;
  1340. PROCEDURE EmitStrCat ;
  1341. (* strcat: dest := dest + src, TPSRC2's xconcat (963) with the two strings
  1342. taken as ADDRESSES rather than pushed by value.
  1343. The caller pushes the SOURCE first and the DESTINATION last, sets CX = the
  1344. destination's capacity in characters, calls, and pops both words (caller
  1345. pops). That is exactly strstore's argument shape, so the compiler's two
  1346. emitted calls for `a + b' differ only in which entry they name - see
  1347. Compiler.StrConcat.
  1348. TP3's xconcat adds the two length bytes (`ADD.B DL,CL') and takes the CARRY
  1349. as "string too long" (`JB concerr'), then concerr loads runerr's code 16;
  1350. the copy itself is the same append this does. So an overflowing
  1351. concatenation is a RUNTIME ERROR, not a silent truncation: if dest[0] +
  1352. src[0] exceeds CX the process leaves through INT 21h AH=4Ch with exit code
  1353. 16 (10H), which is where TP3's error 16 ends up. (strstore truncates
  1354. instead, which is also what TP3 does there - the two entries deliberately
  1355. do not agree, because TP3's two routines do not.)
  1356. AX, BX, CX, DX, SI and DI are all preserved: the compiler may hold a string
  1357. element's address in DI across the call that pushes the operand AFTER this
  1358. one (see Compiler.EmPushStrOperand), so strcat cannot be the entry that
  1359. quietly clobbers it. *)
  1360. BEGIN
  1361. M ("strcat") ;
  1362. PushBp ; MovBpSp ;
  1363. PushAx ; PushBx ; PushCx ; PushDx ; PushSi ; PushDi ;
  1364. MovAxBp4 ; MovSiAx ; (* SI := dest *)
  1365. MovBxBp6 ; (* BX := src *)
  1366. XorDxDx ;
  1367. MovDlSi ; (* DL := dest[0], the destination length *)
  1368. XorAxAx ;
  1369. LdAlBx ; (* AL := src[0]; AX is now the source length *)
  1370. AddDxAx ; (* DX := dest length + source length *)
  1371. CmpCxDx ; (* CMP CX (max), DX (total) *)
  1372. Jae8 ("sc_ok") ; (* max >= total: it fits *)
  1373. MovAlD (10H) ; (* 10H = 16: TP3's `String too long' error code *)
  1374. MovAh (4CH) ; (* AH = 4Ch: terminate *)
  1375. Int21 ; (* leaving now; the caller's stack is abandoned *)
  1376. M ("sc_ok") ;
  1377. MovCxDx ; (* CX := the new length *)
  1378. StSiCl ; (* dest[0] := new length (CL) *)
  1379. SubDxAx ; (* DX := total - source length = old dest length *)
  1380. MovDiSi ; (* DI := dest *)
  1381. IncDi ; (* -> first character *)
  1382. AddDiDx ; (* -> the append position, dest+1+old length *)
  1383. IncBx ; (* BX -> the source's first character *)
  1384. MovCxAx ; (* CX := source length, LOOP's count *)
  1385. Jcxz8 ("sc_done") ; (* an empty source appends nothing *)
  1386. M ("sc_loop") ;
  1387. LdAlBx ; (* AL := src char *)
  1388. StDiAl ; (* dest char := AL *)
  1389. IncBx ; IncDi ;
  1390. Loop8 ("sc_loop") ;
  1391. M ("sc_done") ;
  1392. PopDi ; PopSi ; PopDx ; PopCx ; PopBx ; PopAx ;
  1393. MovSpBp ; PopBp ; RetR
  1394. END EmitStrCat ;
  1395. PROCEDURE Str5 (s : ARRAY OF CHAR ; n : CARDINAL) ;
  1396. (* emit n characters, so the strings below are visibly the same length as the
  1397. D_ constants claim and an off-by-one in either place is a compile-time
  1398. mismatch rather than a wrong pointer nobody notices *)
  1399. VAR i : CARDINAL ;
  1400. BEGIN
  1401. i := 0 ;
  1402. WHILE i < n DO
  1403. B (ORD (s [i])) ;
  1404. INC (i)
  1405. END
  1406. END Str5 ;
  1407. PROCEDURE EmitRealFromInt ;
  1408. (* intreal (TPSRC3 130): an INTEGER -> the 6-byte MBF real.
  1409. ABI: the source word's ADDRESS is at [BP+6] and the destination's at
  1410. [BP+4]; the caller pushes the source first, so [BP+6] is the source. (The
  1411. reference keeps the real in AX/BX/DX, where AL is the exponent byte, AH
  1412. and BX the low 39 mantissa bits and DX the high 16 with the sign in DH's
  1413. bit 7. Packing is therefore just the two stores.)
  1414. The arithmetic is the reference's, instruction for instruction:
  1415. zero -> all six bytes zero
  1416. sign -> BH, DX := |value|
  1417. AX := 0090h (exponent 144, clear mantissa)
  1418. small value: exponent 88h and shift the mantissa 8
  1419. normalise: shift DX left and decrement AL until DX's sign bit is set
  1420. positive: clear DH bit 7; negative: leave it as the sign
  1421. clear the rest of the mantissa (BX)
  1422. The sign convention here is the packed one: bit 7 of the last byte is the
  1423. sign, and the implicit 1 is not stored. That is why intreal leaves DX's
  1424. top bit alone for negatives (it becomes the sign) and clears it for
  1425. positives. *)
  1426. BEGIN
  1427. M ("real_from_int") ;
  1428. PushBp ; MovBpSp ;
  1429. PushBx ; PushDx ; PushSi ; PushDi ;
  1430. MovSiBp6 ; (* SI := source word *)
  1431. LdAxSi ; (* AX := the integer *)
  1432. CmpAx0 ;
  1433. Jne8 ("rf_nz") ;
  1434. XorBxBx ;
  1435. XorDxDx ;
  1436. Jmp8 ("rf_store") ;
  1437. M ("rf_nz") ;
  1438. MovBhAh ; (* BH := the integer's sign byte *)
  1439. MovDxAx ;
  1440. CmpDx0 ;
  1441. Jns8 ("rf_pos") ;
  1442. NegDx ;
  1443. M ("rf_pos") ;
  1444. MovAxImm (0090H) ; (* AL = exponent 144, AH = 0 *)
  1445. CmpDh0 ;
  1446. Jne8 ("rf_16") ;
  1447. MovAlD (088H) ; (* 16-bit value in the top byte: bias 88 *)
  1448. XchgDlDh ;
  1449. M ("rf_16") ;
  1450. CmpDx0 ;
  1451. Js8 ("rf_done") ;
  1452. M ("rf_nrm") ;
  1453. DecAl ;
  1454. ShlDx1 ;
  1455. Jns8 ("rf_nrm") ;
  1456. M ("rf_done") ;
  1457. CmpBh0 ;
  1458. Js8 ("rf_pos2") ;
  1459. AndDhImm (7FH) ; (* positive: bit 7 of DH is not a sign *)
  1460. M ("rf_pos2") ;
  1461. XorBxBx ; (* only DX's 16 bits carry the value now *)
  1462. M ("rf_store") ;
  1463. MovDiBp4 ; (* DI := the destination *)
  1464. StDiAx ; (* bytes 0..1 *)
  1465. StDiOffBx (2) ; (* bytes 2..3 *)
  1466. StDiOffDx (4) ; (* bytes 4..5, sign in DH bit 7 *)
  1467. PopDi ; PopSi ; PopDx ; PopBx ;
  1468. MovSpBp ; PopBp ; RetR
  1469. END EmitRealFromInt ;
  1470. PROCEDURE EmitRealAddSub ;
  1471. (* readd/resub, TPSRC2 1421-1530, transcribed.
  1472. ABI: [BP+4] is the address of a 12-byte block holding a (6 bytes) then b
  1473. (6 bytes); the result overwrites a. Both operands are read before the
  1474. result is written, so the in-place destination is safe. The reference
  1475. keeps a in AX/BX/DX and b in CX/SI/DI, with AL the exponent, AH+BX the low
  1476. mantissa and DX the high 16 carrying the sign in DH bit 7. The scratch
  1477. bytes are the reference's own (resign/resave, remant/remant1, cvdecexp).
  1478. The sign of b is toggled for subtraction with XOR DI,resign, exactly as
  1479. resub does, and the rest is one shared core. An exponent overflow leaves
  1480. through the reference's STC + RET; no caller consumes that yet. *)
  1481. BEGIN
  1482. M ("real_add") ;
  1483. PushBp ; MovBpSp ;
  1484. PushBx ; PushDx ; PushSi ; PushDi ;
  1485. PushBp4 ; (* keep the block address for the store *)
  1486. MovSiBp4 ;
  1487. LdAxSi ;
  1488. LdCxSiOff (2) ; LdDxSiOff (4) ;
  1489. MovBxCx ;
  1490. MovDiSi ;
  1491. LdCxDiOff (6) ; LdSiDiOff (8) ; LdDiDiOff (10) ;
  1492. StDataImm (D_RSIGN, 0) ;
  1493. Call ("rd_core") ;
  1494. PopDi ;
  1495. StDiAx ; StDiOffBx (2) ; StDiOffDx (4) ;
  1496. PopDi ; PopSi ; PopDx ; PopBx ;
  1497. MovSpBp ; PopBp ; RetR ;
  1498. M ("real_sub") ;
  1499. PushBp ; MovBpSp ;
  1500. PushBx ; PushDx ; PushSi ; PushDi ;
  1501. PushBp4 ;
  1502. MovSiBp4 ;
  1503. LdAxSi ;
  1504. LdCxSiOff (2) ; LdDxSiOff (4) ;
  1505. MovBxCx ;
  1506. MovDiSi ;
  1507. LdCxDiOff (6) ; LdSiDiOff (8) ; LdDiDiOff (10) ;
  1508. StDataImm (D_RSIGN, 8000H) ;
  1509. Call ("rd_core") ;
  1510. PopDi ;
  1511. StDiAx ; StDiOffBx (2) ; StDiOffDx (4) ;
  1512. PopDi ; PopSi ; PopDx ; PopBx ;
  1513. MovSpBp ; PopBp ; RetR ;
  1514. M ("rd_core") ;
  1515. CmpCl0 ;
  1516. Je8 ("raret") ;
  1517. XorDiData (D_RSIGN) ;
  1518. CmpAl0 ;
  1519. Jne8 ("ranotriv") ;
  1520. M ("raretn2") ;
  1521. MovAxCx ; MovBxSi ; MovDxDi ;
  1522. M ("raret") ;
  1523. RetR ;
  1524. M ("ranotriv") ;
  1525. CmpAlCl ;
  1526. Jbe8 ("ranoswap") ;
  1527. XchgAxCx ; XchgBxSi ; XchgDxDi ;
  1528. M ("ranoswap") ;
  1529. StDataCl (D_CVDEC) ;
  1530. SubClAl ;
  1531. CmpClImm (28H) ;
  1532. Jb8 ("ranoundr") ;
  1533. LdClData (D_CVDEC) ;
  1534. Jmp8 ("raretn2") ;
  1535. M ("ranoundr") ;
  1536. StDataDi (D_RSIGN) ; (* resign; D_RESAVE is its high byte *)
  1537. AndDataImm (D_RESAVE, 80H) ;
  1538. StDataDi (D_RMANT) ;
  1539. XorDataDh (D_RMANT1) ; (* sign1 XOR sign2 in the high byte *)
  1540. OrDiImm (8000H) ; (* b's hidden 1 *)
  1541. OrDhImm (80H) ; (* a's hidden 1 *)
  1542. M ("raadj16l") ;
  1543. CmpClImm (10H) ;
  1544. Jb8 ("raadj8") ;
  1545. MovAhBh ; MovBxDx ; XorDxDx ; SubClImm (10H) ;
  1546. Jmp8 ("raadj16l") ;
  1547. M ("raadj8") ;
  1548. CmpClImm (08H) ;
  1549. Jb8 ("raadj8l") ;
  1550. MovAhBl ; MovBlBh ; MovBhDl ; MovDlDh ; XorDhDh ; SubClImm (08H) ;
  1551. M ("raadj8l") ;
  1552. CmpCl0 ;
  1553. Je8 ("raadjend") ;
  1554. M ("raadj1l") ;
  1555. ShrDx1 ; RcrBx1 ; RcrAh1 ; DecCl ; Jne8 ("raadj1l") ;
  1556. M ("raadjend") ;
  1557. LdAlData (D_CVDEC) ;
  1558. TestDataImm (D_RMANT1, 80H) ;
  1559. Jne8 ("radosub") ;
  1560. AddAhCh ; AdcBxSi ; AdcDxDi ;
  1561. Jae8 ("rasign") ;
  1562. RcrDx1 ; RcrBx1 ; RcrAh1 ; IncAl ; Jne8 ("rasign") ;
  1563. Stc ; RetR ;
  1564. M ("radosub") ;
  1565. XchgAhCh ; XchgBxSi ; XchgDxDi ;
  1566. SubAhCh ; SbbBxSi ; SbbDxDi ;
  1567. Jae8 ("ranoneg") ;
  1568. XorDataImm (D_RESAVE, 80H) ;
  1569. NotAh ; NotBx ; NotDx ;
  1570. AddAhImm (1) ; AdcBxImm (0) ; AdcDxImm (0) ;
  1571. M ("ranoneg") ;
  1572. MovClImm (5) ;
  1573. M ("ranrm8l") ;
  1574. CmpDh0 ;
  1575. Jne8 ("ranrm1l") ;
  1576. MovDhDl ; MovDlBh ; MovBhBl ; MovBlAh ; XorAhAh ;
  1577. SubAl (08H) ;
  1578. Jb8 ("razero") ;
  1579. DecCl ;
  1580. Jne8 ("ranrm8l") ;
  1581. Jmp8 ("razero") ;
  1582. M ("ranrm1l") ;
  1583. TestDhImm (80H) ;
  1584. Jne8 ("rasign") ;
  1585. ShlAh1 ; RclBx1 ; RclDx1 ; DecAl ; Jne8 ("ranrm1l") ;
  1586. M ("razero") ;
  1587. XorAxAx ; XorBxBx ; XorDxDx ;
  1588. RetR ;
  1589. M ("rasign") ;
  1590. AndDhImm (7FH) ;
  1591. XorDhData (D_RESAVE) ;
  1592. RetR
  1593. END EmitRealAddSub ;
  1594. PROCEDURE EmitData ;
  1595. (* The runtime's data block. The D_ constants name the offsets in it and are
  1596. ASSERTED against what is emitted here, three ways: the starting offset of
  1597. each string, the offset just past it, and the block's total length. A D_
  1598. constant is a 16-bit immediate inside a MOV, so a wrong one is a
  1599. perfectly well-formed instruction that reads the wrong memory - see the
  1600. note on the D_ block. *)
  1601. BEGIN
  1602. dataAt := rpos ;
  1603. (* Each D_ is checked BEFORE the string it names is emitted, because it
  1604. names that string's START. *)
  1605. IF dataAt + D_NUM # rpos THEN
  1606. HALT
  1607. END ;
  1608. (* 8 bytes of scratch, never read before written *)
  1609. B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ;
  1610. IF dataAt + D_TRUE # rpos THEN
  1611. HALT
  1612. END ;
  1613. Str5 ("TRUE$", 5) ;
  1614. IF dataAt + D_FALSE # rpos THEN
  1615. HALT
  1616. END ;
  1617. Str5 ("FALSE$", 6) ;
  1618. IF dataAt + D_CRLF # rpos THEN
  1619. HALT
  1620. END ;
  1621. B (13) ; B (10) ; B (ORD ("$")) ;
  1622. IF dataAt + D_REAL # rpos THEN
  1623. HALT
  1624. END ;
  1625. Str5 ("?REAL?", 6) ;
  1626. IF dataAt + D_PUSH # rpos THEN
  1627. HALT
  1628. END ;
  1629. B (0) ; B (0) ; (* the pushback slot, empty to begin with *)
  1630. IF dataAt + D_RSIGN # rpos THEN
  1631. HALT
  1632. END ;
  1633. B (0) ; B (0) ; (* resign/resave *)
  1634. IF dataAt + D_RMANT # rpos THEN
  1635. HALT
  1636. END ;
  1637. B (0) ; B (0) ; (* remant/remant1 *)
  1638. IF dataAt + D_CVDEC # rpos THEN
  1639. HALT
  1640. END ;
  1641. B (0) ;
  1642. WHILE rpos < dataAt + D_END DO
  1643. B (0)
  1644. END ;
  1645. (* the total, so D_END is checked too and not just used as a pad target *)
  1646. IF rpos - dataAt # D_END THEN
  1647. HALT
  1648. END
  1649. END EmitData ;
  1650. PROCEDURE FixUp ;
  1651. VAR i, t, rel : CARDINAL ;
  1652. BEGIN
  1653. i := 0 ;
  1654. WHILE i < nfix DO
  1655. IF fix [i].kind = 2 THEN
  1656. (* A data address, and the ONE kind that is absolute rather than
  1657. relative: rtBase says where the blob sits in the image, val says
  1658. where inside the blob, and LoadBias says where the image itself
  1659. sits once DOS has loaded it. All three are needed; omitting any
  1660. one produces a readable-looking address into the wrong bytes. *)
  1661. t := (dataAt + rtBase + fix [i].val + LoadBias) MOD 10000H ;
  1662. rt [fix [i].place] := VAL (BYTE, t MOD 100H) ;
  1663. rt [fix [i].place + 1] := VAL (BYTE, (t DIV 100H) MOD 100H)
  1664. ELSE
  1665. t := LblOff (fix [i].nm) ;
  1666. IF fix [i].kind = 0 THEN
  1667. (* rel8 is measured from the end of the instruction, i.e. one
  1668. byte past the displacement field *)
  1669. rel := (t + 100H - (fix [i].place + 1)) MOD 100H ;
  1670. rt [fix [i].place] := VAL (BYTE, rel)
  1671. ELSE
  1672. rel := (t + 10000H - (fix [i].place + 2)) MOD 10000H ;
  1673. rt [fix [i].place] := VAL (BYTE, rel MOD 100H) ;
  1674. rt [fix [i].place + 1] := VAL (BYTE, (rel DIV 100H) MOD 100H)
  1675. END
  1676. END ;
  1677. INC (i)
  1678. END
  1679. END FixUp ;
  1680. (* ---------------------------------------------------------------- *)
  1681. (* public interface *)
  1682. (* ---------------------------------------------------------------- *)
  1683. PROCEDURE RT_Build (base : CARDINAL) ;
  1684. BEGIN
  1685. IF built THEN
  1686. RETURN
  1687. END ;
  1688. rtBase := base ; (* must precede every Emit*, they read it *)
  1689. rpos := 0 ; ltop := 0 ; nfix := 0 ; dataAt := 0 ;
  1690. EmitInitMem ;
  1691. EmitEnd ;
  1692. EmitStackChk ;
  1693. EmitWrInt ; EmitWrIntW ; EmitWrChar ; EmitWrBool ; EmitWrReal ; EmitWrLn ;
  1694. EmitWrInl ; EmitWrStr ;
  1695. EmitRdInt ; EmitRdScan ; EmitRdByte ;
  1696. EmitRdChar ; EmitRdBool ; EmitRdLn ;
  1697. EmitGetCh ; EmitUnGetCh ;
  1698. EmitRdStr ; EmitStrStore ; EmitCmpStr ; EmitStrCat ;
  1699. EmitRealFromInt ; EmitRealAddSub ;
  1700. EmitData ;
  1701. FixUp ;
  1702. SetStr (entNm [0], "initmem") ;
  1703. SetStr (entNm [1], "progend") ;
  1704. SetStr (entNm [2], "stackchk") ;
  1705. SetStr (entNm [3], "wrint") ;
  1706. SetStr (entNm [4], "wrchar") ;
  1707. SetStr (entNm [5], "wrbool") ;
  1708. SetStr (entNm [6], "wrreal") ;
  1709. SetStr (entNm [7], "wrln") ;
  1710. SetStr (entNm [8], "rdint") ;
  1711. SetStr (entNm [9], "rdchar") ;
  1712. SetStr (entNm [10], "rdbool") ;
  1713. SetStr (entNm [11], "rdln") ;
  1714. SetStr (entNm [12], "halt") ;
  1715. SetStr (entNm [13], "wrtinl") ;
  1716. SetStr (entNm [14], "rdbyte") ;
  1717. SetStr (entNm [15], "wrstr") ;
  1718. SetStr (entNm [16], "rdstr") ;
  1719. SetStr (entNm [17], "strstore") ;
  1720. SetStr (entNm [18], "cmpstr") ;
  1721. SetStr (entNm [19], "strcat") ;
  1722. SetStr (entNm [20], "wrintw") ;
  1723. SetStr (entNm [21], "real_from_int") ;
  1724. SetStr (entNm [22], "real_add") ;
  1725. SetStr (entNm [23], "real_sub") ;
  1726. built := TRUE
  1727. END RT_Build ;
  1728. PROCEDURE RT_Size () : CARDINAL ;
  1729. BEGIN
  1730. IF NOT built THEN
  1731. RT_Build (0)
  1732. END ;
  1733. RETURN rpos
  1734. END RT_Size ;
  1735. PROCEDURE RT_Byte (i : CARDINAL ) : BYTE ;
  1736. BEGIN
  1737. IF NOT built THEN
  1738. RT_Build (0)
  1739. END ;
  1740. IF i >= rpos THEN
  1741. RETURN 0
  1742. END ;
  1743. RETURN rt [i]
  1744. END RT_Byte ;
  1745. PROCEDURE RT_Entry (i : CARDINAL ) : CARDINAL ;
  1746. BEGIN
  1747. IF NOT built THEN
  1748. RT_Build (0)
  1749. END ;
  1750. IF i > 23 THEN
  1751. RETURN 0
  1752. END ;
  1753. RETURN rtBase + LblOff (entNm [i])
  1754. END RT_Entry ;
  1755. PROCEDURE RT_CodeEnd () : CARDINAL ;
  1756. BEGIN
  1757. IF NOT built THEN
  1758. RT_Build (0)
  1759. END ;
  1760. RETURN dataAt
  1761. END RT_CodeEnd ;
  1762. END Runtime.