Runtime.mod 120 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061
  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 = 320 ; (* fmtreal/ascreal added ~70 labels to the MBF core's 160 *)
  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_RMUL11 = 35 ; (* a's saved AX; D_RMUL11+1 is its high byte (AH) *)
  73. D_RMUL12 = 37 ; (* a's saved BX *)
  74. D_RMUL13 = 39 ; (* a's saved DX *)
  75. D_RMUL21 = 41 ; (* b's saved CX (low byte is the exponent, skipped) *)
  76. D_RMUL22 = 43 ; (* b's saved SI *)
  77. D_RMUL23 = 45 ; (* b's saved DI *)
  78. D_CVOUT = 47 ; (* 12 bytes: realdec's decimal digits *)
  79. D_CVDECEXP = 59 ; (* realrang's decimal exponent, sign in bit 7 *)
  80. D_CVEXPCNT = 60 ; (* ascreal's decimal-point counter *)
  81. D_FLTDEC = 61 ; (* 10 x 6 bytes: 1e0, 1e4, ... 1e36 (TPSRC3) *)
  82. D_FMTBUF = 121 ; (* 64 bytes: fmtreal's unpadded output *)
  83. D_FMTLEN = 185 ; (* 2 bytes: its length *)
  84. D_FMTW = 187 ; (* 2 bytes: the requested field width *)
  85. D_FMTPAD = 189 ; (* 2 bytes: left padding *)
  86. D_FMTTOT = 191 ; (* 2 bytes: pad + length *)
  87. D_ASCSRC = 193 ; (* 2 bytes: ascreal's source pointer *)
  88. D_END = 195 ; (* the block is padded to here *)
  89. (* THE LOAD BIAS. Everything above is an offset into the image as this
  90. compiler numbers it: the entry JMP is at image 0. But a DOS .COM is
  91. NOT loaded at that offset - DOS loads it at CS:0100, because 0000..00FF
  92. is the PSP - so image offset K lives at CS:(K + 0100h), and since a
  93. .COM has CS = DS, every address baked into the image as a literal has
  94. to carry that +0100h.
  95. Getting this wrong is the single most confusing failure this compiler
  96. can have, because nothing crashes. The entry JMP is *relative*, so it
  97. still lands on the prologue; every CALL is relative, so every call
  98. still lands on the right runtime entry; initmem still runs and still
  99. zeroes the data area. The program therefore starts, runs, and prints
  100. its first field correctly - and then a $ that is 0100h too low resolves
  101. to code instead of to data, so the string walk runs on through the
  102. runtime's own instructions until it happens to hit a 24h. That is
  103. exactly what writeln('hi') did: `hi' out of wrtin's inline data, then
  104. 249 bytes of runtime machine code, stopping only at the '$' inside
  105. "TRUE$". See tests/fixtures/t02_writeln.out.
  106. The bias is a CONSTANT, not a variable to be relocated at load time,
  107. because the 8086 has no way to relocode an image in place and no way
  108. to set a segment register to a sub-paragraph boundary. It is the same
  109. +0100h the original's own output has: TPSRC7 opendest copies the
  110. generated image to 100h and runs it there, so the original's addresses
  111. are 0100h-relative too. The difference is only in how the bytes are
  112. *numbered* - ours are 0-based in the file, the original's are
  113. segment-relative - and this constant is where that difference stops.
  114. The constant itself is declared in Runtime.def, because that is the only
  115. declaration site a compiler can import - an implementation module does not
  116. re-declare what its definition module already declared. *)
  117. TYPE
  118. LblRec = RECORD
  119. nm : ARRAY [0..MaxNm] OF CHAR ;
  120. off : CARDINAL ;
  121. END ;
  122. FixRec = RECORD
  123. kind : CARDINAL ; (* 0 = rel8, 1 = rel16, 2 = data address *)
  124. place : CARDINAL ; (* offset of the displacement/address field *)
  125. nm : ARRAY [0..MaxNm] OF CHAR ;
  126. val : CARDINAL ;
  127. END ;
  128. VAR
  129. rt : ARRAY [0..MaxRt - 1] OF BYTE ;
  130. rpos : CARDINAL ;
  131. lbl : ARRAY [0..MaxLbl - 1] OF LblRec ;
  132. ltop : CARDINAL ;
  133. fix : ARRAY [0..MaxFix - 1] OF FixRec ;
  134. nfix : CARDINAL ;
  135. dataAt : CARDINAL ;
  136. rtBase : CARDINAL ;
  137. built : BOOLEAN ;
  138. entNm : ARRAY [0..30] OF ARRAY [0..MaxNm] OF CHAR ;
  139. (* ---------------------------------------------------------------- *)
  140. (* name helpers *)
  141. (* ---------------------------------------------------------------- *)
  142. PROCEDURE StrEq (a, b : ARRAY OF CHAR ) : BOOLEAN ;
  143. VAR i : CARDINAL ;
  144. BEGIN
  145. i := 0 ;
  146. WHILE (i <= HIGH (a)) AND (i <= HIGH (b)) DO
  147. IF a [i] # b [i] THEN
  148. RETURN FALSE
  149. END ;
  150. INC (i)
  151. END ;
  152. RETURN TRUE
  153. END StrEq ;
  154. PROCEDURE SetStr (VAR dst : ARRAY OF CHAR ; src : ARRAY OF CHAR ) ;
  155. VAR i : CARDINAL ;
  156. BEGIN
  157. i := 0 ;
  158. WHILE (i <= HIGH (dst)) AND (i <= HIGH (src)) DO
  159. dst [i] := src [i] ;
  160. INC (i)
  161. END ;
  162. IF i <= HIGH (dst) THEN
  163. dst [i] := 0C
  164. END
  165. END SetStr ;
  166. (* ---------------------------------------------------------------- *)
  167. (* primitive emitters *)
  168. (* ---------------------------------------------------------------- *)
  169. PROCEDURE B (b : CARDINAL ) ;
  170. (* emit exactly ONE byte. A two-byte opcode must be written as two B calls -
  171. B masks to 100H, so B (8BE4H) would silently emit just E4. *)
  172. BEGIN
  173. IF rpos >= MaxRt THEN
  174. RETURN (* blob is oversized: drop the byte *)
  175. END ;
  176. rt [rpos] := VAL (BYTE, b MOD 100H) ;
  177. INC (rpos)
  178. END B ;
  179. PROCEDURE W (w : CARDINAL ) ;
  180. BEGIN
  181. B (w MOD 100H) ;
  182. B ((w DIV 100H) MOD 100H) (* little endian *)
  183. END W ;
  184. PROCEDURE M (nm : ARRAY OF CHAR ) ;
  185. (* mark: nm is the current offset *)
  186. VAR i : CARDINAL ;
  187. BEGIN
  188. (* A dropped label is not a smaller runtime, it is a WRONG one: its bytes
  189. are still emitted, so only a branch that names it goes off target. That
  190. shows up in neither the size nor the entry table, so refuse to build
  191. rather than drop. *)
  192. IF ltop >= MaxLbl THEN
  193. HALT
  194. END ;
  195. (* A DUPLICATE label is worse than a missing one: LblOff returns the FIRST
  196. spelling, so the second body (and every branch meant for it) silently
  197. runs the first. That is exactly how real_div came to call readd. *)
  198. i := 0 ;
  199. WHILE i < ltop DO
  200. IF StrEq (lbl [i].nm, nm) THEN
  201. HALT
  202. END ;
  203. INC (i)
  204. END ;
  205. SetStr (lbl [ltop].nm, nm) ;
  206. lbl [ltop].off := rpos ;
  207. INC (ltop)
  208. END M ;
  209. PROCEDURE AddFix (kind, place : CARDINAL ; nm : ARRAY OF CHAR ; val : CARDINAL ) ;
  210. (* Record a displacement to be patched once every label is known. A dropped
  211. fixup is an address that is never patched - the same silent-wrong-code
  212. failure the check in M exists to prevent. *)
  213. BEGIN
  214. IF nfix >= MaxFix THEN
  215. HALT
  216. END ;
  217. fix [nfix].kind := kind ;
  218. fix [nfix].place := place ;
  219. fix [nfix].val := val ;
  220. SetStr (fix [nfix].nm, nm) ;
  221. INC (nfix)
  222. END AddFix ;
  223. PROCEDURE LblOff (nm : ARRAY OF CHAR ) : CARDINAL ;
  224. VAR i : CARDINAL ;
  225. BEGIN
  226. i := 0 ;
  227. WHILE i < ltop DO
  228. IF StrEq (lbl [i].nm, nm) THEN
  229. RETURN lbl [i].off
  230. END ;
  231. INC (i)
  232. END ;
  233. RETURN 0
  234. END LblOff ;
  235. PROCEDURE J8 (nm : ARRAY OF CHAR ) ;
  236. BEGIN
  237. B (0EBH) ; (* JMP rel8 *)
  238. AddFix (0, rpos, nm, 0) ;
  239. B (0)
  240. END J8 ;
  241. PROCEDURE C8 (nm : ARRAY OF CHAR ) ;
  242. BEGIN
  243. B (0E8H) ; (* CALL rel16 *)
  244. AddFix (1, rpos, nm, 0) ;
  245. W (0)
  246. END C8 ;
  247. PROCEDURE Jcc (code : CARDINAL ; nm : ARRAY OF CHAR ) ;
  248. BEGIN
  249. B (code) ; (* Jcc rel8 *)
  250. AddFix (0, rpos, nm, 0) ;
  251. B (0)
  252. END Jcc ;
  253. PROCEDURE Dd (delta : CARDINAL ) ;
  254. (* emit a 16-bit address into the runtime's data block; the value is only
  255. known once the data block has been placed, so it is a fixup *)
  256. BEGIN
  257. AddFix (2, rpos, "", delta) ;
  258. W (0)
  259. END Dd ;
  260. (* --- 8-bit / 16-bit register and memory forms, one instruction each --- *)
  261. (* THE 16-BIT ModR/M EFFECTIVE-ADDRESS TABLE, MEASURED NOT REMEMBERED.
  262. Every address form below was confirmed by EXECUTING it on a real 8086
  263. (qemu-system-i386) with a probe that stores a marker through the candidate
  264. encoding and then reports which physical address received it; see
  265. tests/probe/modrm19.s. The mod=11 column is not an address at all -- it
  266. names a register -- so it is confirmed instead by asking GNU as to
  267. ENCODE the eight register moves and checking FCML's decode of the result
  268. (tests/probe/modrm11.py, which also carries the hand-checking anchors).
  269. Do not "fix" any of these from memory. The version that comes to mind is
  270. wrong in exactly the cells called out below, and every one of those
  271. mistakes shipped as code that decoded cleanly:
  272. r/m mod=00 mod=01 mod=10 mod=11
  273. 000 [BX+SI] [BX+SI]+disp [BX+SI]+disp AX
  274. 001 [BX+DI] [BX+DI]+disp [BX+DI]+disp CX
  275. 010 [BP+SI] [BP+SI]+disp [BP+SI]+disp DX
  276. 011 [BP+DI] [BP+DI]+disp [BP+DI]+disp BX
  277. 100 [SI] [SI]+disp [SI]+disp SP
  278. 101 [DI] [DI]+disp [DI]+disp BP
  279. 110 disp16 [BP]+disp [BP]+disp SI
  280. 111 [BX] [BX]+disp [BX]+disp DI
  281. "disp" is disp8 for mod=01 and disp16 for mod=10.
  282. In mod=11 BOTH fields name a register and BOTH use the same list,
  283. AX CX DX BX SP BP SI DI -- the reg field and the r/m field do not
  284. differ, and there is no second BX at code 7. The memorable version
  285. drops AX off the front and invents a duplicate BX at the end, which
  286. shifts every code down by one; that is precisely how MovSiBx and
  287. CmpSiBx came to be written "89 DC" and "39 DC", which are MOV SP,BX
  288. and CMP SP,BX. Note what the shifted table gets right by luck: SP is
  289. at 100 either way, so the mistake is invisible until you check a
  290. register below it. rm=100 is SP, and SI is rm=110, not rm=100.
  291. And reg/rm swap direction with the opcode, which is the other trap:
  292. 88 /r MOV r/m8,r8 89 /r MOV r/m16,r16 reg is the SOURCE
  293. 8A /r MOV r8,r/m8 8B /r MOV r16,r/m16 reg is the TARGET
  294. So ModRM C6 names "DH and AL" either way, but 88 C6 is DH:=AL while
  295. 8A C6 is AL:=DH. The 8-bit register list is also its own, and it is not
  296. the word list:
  297. 000 001 010 011 100 101 110 111 = AL CL DL BL AH CH DH BH
  298. The two lists agree at every code except 100, where the byte form is AH
  299. and the word form is SP. Reading the same ModRM byte as the other size
  300. silently swaps AH for SP, and 88 C2 is DL:=AL while 88 17 is [BX]<-DL.
  301. The word-form list, and the four anchors nobody writes by hand:
  302. 83 C4 08 ADD SP, 8 rm=100 -> SP
  303. 83 C6 02 ADD SI, 2 rm=110 -> SI
  304. 8B EC MOV BP, SP reg=101 rm=100
  305. 8B E5 MOV SP, BP reg=100 rm=101
  306. Asking "which register is rm=100?" and answering SI is the single most
  307. common error in this file's history.
  308. Concrete recipes for opcode 8r / 9r (r/m = rm, 16-bit form):
  309. mod=00 rm=110 -> 06 <disp16> the only direct form
  310. mod=00 rm=111 -> 07 = [BX] 2 bytes
  311. mod=00 rm=101 -> 05 = [DI] 2 bytes
  312. mod=01 rm=110 -> 46 disp8 = [BP]+disp8
  313. mod=01 rm=111 -> 47 disp8 = [BX]+disp8
  314. mod=01 rm=101 -> 45 disp8 = [DI]+disp8
  315. There is no [SP] form in 16-bit mode: SIB bytes are 386-only. Anything
  316. wanting the top of the stack has to go through BP. *)
  317. PROCEDURE PushBp ; BEGIN B (55H) END PushBp ;
  318. PROCEDURE PopBp ; BEGIN B (5DH) END PopBp ;
  319. PROCEDURE MovBpSp ; BEGIN B (8BH) ; B (0ECH) END MovBpSp ; (* 8B EC: MOV BP,SP *)
  320. PROCEDURE MovSpBp ; BEGIN B (89H) ; B (0ECH) END MovSpBp ; (* 89 EC: MOV SP,BP *)
  321. PROCEDURE LeaveR ; BEGIN B (0C9H) END LeaveR ;
  322. PROCEDURE RetR ; BEGIN B (0C3H) END RetR ;
  323. PROCEDURE Int21 ; BEGIN B (0CDH) ; B (21H) END Int21 ;
  324. PROCEDURE PushDs ; BEGIN B (1EH) END PushDs ;
  325. PROCEDURE PopEs ; BEGIN B (7H) END PopEs ;
  326. PROCEDURE PushAx ; BEGIN B (50H) END PushAx ;
  327. PROCEDURE PopAx ; BEGIN B (58H) END PopAx ;
  328. PROCEDURE PushBx ; BEGIN B (53H) END PushBx ;
  329. PROCEDURE PopBx ; BEGIN B (5BH) END PopBx ;
  330. PROCEDURE PushCx ; BEGIN B (51H) END PushCx ;
  331. PROCEDURE PopCx ; BEGIN B (59H) END PopCx ;
  332. PROCEDURE PushDx ; BEGIN B (52H) END PushDx ;
  333. PROCEDURE PopDx ; BEGIN B (5AH) END PopDx ;
  334. PROCEDURE PushDi ; BEGIN B (57H) END PushDi ;
  335. PROCEDURE PopDi ; BEGIN B (5FH) END PopDi ;
  336. PROCEDURE PushSi ; BEGIN B (56H) END PushSi ;
  337. PROCEDURE PopSi ; BEGIN B (5EH) END PopSi ;
  338. PROCEDURE XorAxAx ; BEGIN B (31H) ; B (0C0H) END XorAxAx ; (* 11 000 000 *)
  339. PROCEDURE XorCxCx ; BEGIN B (31H) ; B (0C9H) END XorCxCx ; (* 11 001 001 *)
  340. PROCEDURE XorDxDx ; BEGIN B (31H) ; B (0D2H) END XorDxDx ; (* 11 010 010 *)
  341. PROCEDURE XorDiDi ; BEGIN B (31H) ; B (0FFH) END XorDiDi ; (* 11 111 111 *)
  342. PROCEDURE IncCx ; BEGIN B (41H) END IncCx ;
  343. PROCEDURE IncSi ; BEGIN B (46H) END IncSi ;
  344. PROCEDURE IncDi ; BEGIN B (47H) END IncDi ;
  345. PROCEDURE DecSi ; BEGIN B (4EH) END DecSi ;
  346. PROCEDURE AddDi2 ; BEGIN B (83H) ; B (0C7H) ; B (2) END AddDi2 ; (* 11 000 111 *)
  347. PROCEDURE CmpAl (v : CARDINAL ) ; BEGIN B (3CH) ; B (v) END CmpAl ;
  348. PROCEDURE CmpAx0 ; BEGIN B (83H) ; B (0F8H) ; B (0) END CmpAx0 ;
  349. PROCEDURE CmpCx0 ; BEGIN B (83H) ; B (0F9H) ; B (0) END CmpCx0 ;
  350. PROCEDURE CmpArg2W0 ;
  351. BEGIN
  352. B (8BH) ; B (0ECH) ; (* MOV BP,SP *)
  353. B (83H) ; B (7EH) ; B (2) ; B (0) ; (* CMP WORD [BP+2],0 *)
  354. B (89H) ; B (0ECH) (* MOV SP,BP *)
  355. END CmpArg2W0 ;
  356. PROCEDURE CmpArg4W0 ;
  357. (* CMP WORD PTR [BP+4],0 - the same comparison, in the frame where the entry
  358. pushed BP first.
  359. The 2 and the 4 are the whole point of having two names. [SP] is not
  360. encodable in 16-bit mode, so an entry that takes one 16-bit argument has to
  361. borrow BP to reach it, and how far up the argument then sits depends on
  362. whether BP was saved first: 2 bytes without the PUSH, 4 with it. A single
  363. emitter taking the displacement as a parameter is the obvious way to write
  364. this and the wrong one -- a parameter is invisible to tests/audit_helpers.py,
  365. which reads the NAME and compares it against the decode, so `CmpArgW0 (2)'
  366. and `CmpArgW0 (4)' would differ by one byte with nothing in the codebase
  367. able to tell which was meant. In the name, the audit pins it: a helper
  368. called CmpArg4W0 that emitted 83 7E 02 00 would fail the run.
  369. Note the sandwich: MOV BP,SP before and MOV SP,BP after, and no POP BP, so
  370. the two forms are the ONLY difference. (Was 83 7C 24 00 00, which decodes
  371. as CMP WORD [SI+24h],0.) The imm8 of the 83 form is sign-extended to 16
  372. bits, so 0 really is a 16-bit zero. *)
  373. BEGIN
  374. B (8BH) ; B (0ECH) ; (* MOV BP,SP *)
  375. B (83H) ; B (7EH) ; B (4) ; B (0) ; (* CMP WORD [BP+4],0 *)
  376. B (89H) ; B (0ECH) (* MOV SP,BP *)
  377. END CmpArg4W0 ;
  378. PROCEDURE CmpSiBx ; BEGIN B (39H) ; B (0DEH) END CmpSiBx ; (* 39 DE: CMP SI,BX *)
  379. PROCEDURE CmpCxDx ; BEGIN B (39H) ; B (0D1H) END CmpCxDx ; (* 11 010 001 *)
  380. PROCEDURE CmpAlDl ; BEGIN B (3AH) ; B (0C2H) END CmpAlDl ; (* 3A C2: CMP AL,DL.
  381. The byte-sized comparator cmpstr's loop needs, and the two opcodes are NOT
  382. interchangeable: 3A /r is `CMP r8,r/m8' (reg is the TARGET) and 38 /r is
  383. `CMP r/m8,r8' (reg is the SOURCE). AL is reg=000 and DL is r/m=010, so
  384. 3A C2 is CMP AL,DL and 38 C2 would be CMP DL,AL - the same flags negated,
  385. which turns every `<` into a `>'. And there is no `CMP AL,[SI]' name
  386. pattern in tests/audit_helpers.py (only the Ld/St memory names are), so the
  387. right-hand character is loaded into DL first rather than compared in
  388. memory. *)
  389. PROCEDURE CmpDiCx ; BEGIN B (39H) ; B (0CFH) END CmpDiCx ; (* 11 001 111 *)
  390. PROCEDURE CmpBx0 ; BEGIN B (83H) ; B (0FBH) ; B (0) END CmpBx0 ;
  391. PROCEDURE CmpCxV (v : CARDINAL ) ; BEGIN B (83H) ; B (0F9H) ; B (v) END CmpCxV ;
  392. PROCEDURE AddDl (v : CARDINAL ) ; BEGIN B (80H) ; B (0C2H) ; B (v) END AddDl ;
  393. PROCEDURE SubAl (v : CARDINAL ) ; BEGIN B (2CH) ; B (v) END SubAl ;
  394. PROCEDURE SubAxBx ; BEGIN B (2BH) ; B (0C3H) END SubAxBx ; (* 2B C3: AX:=AX-BX *)
  395. PROCEDURE NegAx ; BEGIN B (0F7H) ; B (0D8H) END NegAx ;
  396. PROCEDURE NegDi ; BEGIN B (0F7H) ; B (0DFH) END NegDi ;
  397. PROCEDURE DivCx ; BEGIN B (0F7H) ; B (0F1H) END DivCx ; (* 11 110 001 *)
  398. PROCEDURE MulBx ; BEGIN B (0F7H) ; B (0E3H) END MulBx ; (* 11 100 011 *)
  399. PROCEDURE MovAh (v : CARDINAL ) ; BEGIN B (0B4H) ; B (v) END MovAh ;
  400. PROCEDURE MovDl (v : CARDINAL ) ; BEGIN B (0B2H) ; B (v) END MovDl ;
  401. (* The runtime's own data block, addressed by absolute image address. Four
  402. emitters cover it, and the difference between them is the whole subject of
  403. the mistakes recorded below, so they are named systematically:
  404. Mov<reg>Vx MOV reg, Vx -- reg := the ADDRESS (BB / BA + Dd)
  405. Ld<reg>Vx MOV reg, [Vx] -- reg := the CONTENTS (8B 1E + Dd)
  406. StVx<reg> MOV [Vx], reg -- [Vx] := reg (89 1E + Dd)
  407. "Vx" is the operand word for "a 16-bit absolute address into the data
  408. block", spelled with a V precisely so it cannot be confused with a base
  409. register: on the 8086 there is no [BX] memory form, so the address has to
  410. go through mod=00 / rm=110, and rm=111 is [BX+SI] - a different, perfectly
  411. decodable instruction. tests/audit_helpers.py checks all four.
  412. The two traps here, both of which happened:
  413. - Dd versus W. Dd turns a D_ offset into a FIXUP, so the address can be
  414. placed only once the data block has been given its final position in the
  415. image. W emits the number as it stands. The two produce the SAME
  416. instruction, and getch once used MovBxImm where it wanted the address, so
  417. `MOV BX,D_PUSH' came out as `MOV BX,001Ch' - the raw offset 1Ch, inside
  418. the entry JMP. Non-zero, so the pushback slot was never examined, getch
  419. served a byte of the runtime's own code forever, and readln hung. Hence
  420. the Imm suffix: the raw-immediate emitters are named for what they do.
  421. - address versus contents. LdBxVx and MovBxVx are both `8B`/`BB` + Dd to
  422. the eye and completely different instructions. getch's first cut used
  423. MovBxVx where it wanted the contents, so it tested the ADDRESS for zero,
  424. found 02AFh every time, and returned the low byte of memory 02AFh - 0,
  425. the value it had just stored there - instead of asking DOS. *)
  426. PROCEDURE MovBxImm (v : CARDINAL ) ; BEGIN B (0BBH) ; W (v) END MovBxImm ;
  427. PROCEDURE MovCxImm (v : CARDINAL ) ; BEGIN B (0B9H) ; W (v) END MovCxImm ;
  428. PROCEDURE MovDxImm (v : CARDINAL ) ; BEGIN B (0BAH) ; W (v) END MovDxImm ;
  429. PROCEDURE MovBxVx (delta : CARDINAL ) ; BEGIN B (0BBH) ; Dd (delta) END MovBxVx ;
  430. PROCEDURE MovDxVx (delta : CARDINAL ) ; BEGIN B (0BAH) ; Dd (delta) END MovDxVx ;
  431. PROCEDURE LdBxVx (delta : CARDINAL ) ;
  432. (* 8B 1E lo hi: BX := WORD PTR [Vx] - mod=00 / rm=110, the only 16-bit form
  433. that can carry a bare absolute address. *)
  434. BEGIN
  435. B (8BH) ; B (01EH) ; Dd (delta)
  436. END LdBxVx ;
  437. PROCEDURE MovSiAx ; BEGIN B (8BH) ; B (0F0H) END MovSiAx ; (* 8B F0: MOV SI,AX *)
  438. PROCEDURE MovAxDi ; BEGIN B (8BH) ; B (0C7H) END MovAxDi ; (* 11 000 111 *)
  439. PROCEDURE MovCxSi6 ; BEGIN B (8BH) ; B (4CH) ; B (6) END MovCxSi6 ;
  440. PROCEDURE MovCxDx ; BEGIN B (8BH) ; B (0CAH) END MovCxDx ; (* 8B CA: MOV CX,DX *)
  441. PROCEDURE MovDxSi2 ; BEGIN B (8BH) ; B (54H) ; B (2) END MovDxSi2 ;
  442. (* The program header is a block of words laid out by the compiler:
  443. +0 hdrFlag 1 = image is valid
  444. +2 hdrCS end of the generated code, in bytes
  445. +4 hdrDS base of the data area <- initmem wants these two
  446. +6 hdrHeap end of the data area <-
  447. +8 hdrMax max open files
  448. initmem must read the words the COMPILER WRITES, and those are +4 (hdrDS,
  449. the data base) and +6 (hdrHeap, the data end). It used to read +8, which
  450. is hdrMax -- and the compiler patches that to 0 -- so CX came out as 0,
  451. "cmp cx,dx / jbe im_done" fired immediately, and initmem silently zeroed
  452. nothing at all. A 3-byte instruction, so no entry offset moved either
  453. way; only the header offset in the byte changed. Nothing caught it
  454. because the loop was well formed - it just did nothing. The cross-check
  455. that would have: tests/run_com_tests.sh now asserts the runtime's SI-relative
  456. reads against the same header offsets it verifies the words at. *)
  457. PROCEDURE MovDxSi4 ; BEGIN B (8BH) ; B (54H) ; B (4) END MovDxSi4 ;
  458. PROCEDURE MovAxBp4 ; BEGIN B (8BH) ; B (46H) ; B (4) END MovAxBp4 ; (* AX:=[BP+4] *)
  459. PROCEDURE MovAxBp6 ; BEGIN B (8BH) ; B (46H) ; B (6) END MovAxBp6 ; (* AX:=[BP+6] *)
  460. PROCEDURE MovBxBp4 ; BEGIN B (8BH) ; B (5EH) ; B (4) END MovBxBp4 ; (* BX:=[BP+4] *)
  461. PROCEDURE MovBxBp6 ; BEGIN B (8BH) ; B (5EH) ; B (6) END MovBxBp6 ; (* BX:=[BP+6] *)
  462. PROCEDURE MovAlDh ; BEGIN B (8AH) ; B (0C6H) END MovAlDh ; (* 8A C6: AL:=DH *)
  463. PROCEDURE MovDlSi ; BEGIN B (8AH) ; B (14H) END MovDlSi ;
  464. PROCEDURE MovDlArg2 ; BEGIN B (8AH) ; B (56H) ; B (2) END MovDlArg2 ; (* DL:=[BP+2] *)
  465. (* The AL twins of the three above. INT 21h AH=02h displays the character in
  466. AL, not DL, so every character this runtime writes has to arrive in AL.
  467. DL is the natural register for the digit scratch in wrint and for a stack
  468. argument in wrchar, and both of them were loading DL and then calling
  469. AH=02h - which printed whatever happened to be left in AL. For wrint that
  470. was the QUOTIENT's low byte from the preceding DIV, so writeln(1) printed a
  471. NUL and writeln(2) printed a NUL, and a two-digit number printed its
  472. quotient instead of itself. A wrong register is not a wrong encoding: the
  473. golden bytes were right, `mov dl,[si]' is a perfectly good instruction, and
  474. every byte-level check stayed green. Only running it found this. *)
  475. PROCEDURE MovAlD (v : CARDINAL ) ; BEGIN B (0B0H) ; B (v) END MovAlD ; (* MOV AL,imm8 *)
  476. PROCEDURE MovAlSi ; BEGIN B (8AH) ; B (04H) END MovAlSi ; (* 8A 04: AL:=[SI] *)
  477. PROCEDURE MovAlArg2 ; BEGIN B (8AH) ; B (46H) ; B (2) END MovAlArg2 ; (* AL:=[BP+2] *)
  478. PROCEDURE MovAlArg4 ; BEGIN B (8AH) ; B (46H) ; B (4) END MovAlArg4 ; (* AL:=[BP+4] *)
  479. PROCEDURE MovAlDl ; BEGIN B (8AH) ; B (0C2H) END MovAlDl ; (* 8A C2: AL:=DL *)
  480. PROCEDURE StDiAx ; BEGIN B (89H) ; B (5H) END StDiAx ; (* 89 05: [DI]:=AX *)
  481. PROCEDURE StDiAl ; BEGIN B (88H) ; B (5H) END StDiAl ; (* 88 05: [DI]:=AL *)
  482. PROCEDURE StSiDl ; BEGIN B (88H) ; B (14H) END StSiDl ; (* 88 14: [SI]:=DL *)
  483. PROCEDURE StDiDl ; BEGIN B (88H) ; B (15H) END StDiDl ; (* 88 15: [DI]:=DL *)
  484. PROCEDURE StDiCx ; BEGIN B (89H) ; B (0DH) END StDiCx ; (* 89 0D: [DI]:=CX *)
  485. PROCEDURE MovDiBp4 ; BEGIN B (8BH) ; B (7EH) ; B (4) END MovDiBp4 ; (* DI:=[BP+4] *)
  486. (* There is NO "store through BX" instruction on the 8086, and writing one
  487. anyway is the single most expensive mistake this file has produced, so the
  488. reasoning is recorded rather than left in the probe history.
  489. In 16-bit addressing the r/m column is a LOCATION, not a register list: for
  490. mod=00, rm=000..101 are [BX+SI] [BX+DI] [BP+SI] [BP+DI] [SI] [DI], and
  491. rm=110 is the only one that means "a direct displacement". rm=111 is
  492. [BX+SI], NOT [BX]. So `89 1D' - the ModR/M the first version of this used
  493. for "MOV [BX],CX" - is mod=00 reg=BX r/m=DI, i.e. MOV [DI],BX: the two
  494. operands swapped AND [BX] inexpressible. GNU as and objdump in -m i8086
  495. agree, and the bytes are perfectly well formed, which is why nothing
  496. objected. What the program saw was its own address being written over the
  497. interrupt vector table, and the variable it was asked to fill left holding
  498. whatever the loader put there: readln(n) then printed 0, and readln(c)
  499. printed a NUL. Correct by comparison: the caller puts the address in BX,
  500. and the store has to go through DI or SI, so the sequence is
  501. MOV DI,[BP+4] / MOV [DI],<value> - see StoreThrough below. *)
  502. PROCEDURE MovDlAl ; BEGIN B (88H) ; B (0C2H) END MovDlAl ;
  503. PROCEDURE MovDlCl ; BEGIN B (8AH) ; B (0D1H) END MovDlCl ; (* 8A D1: MOV DL,CL *)
  504. PROCEDURE XchgAxDi ; BEGIN B (87H) ; B (0C7H) END XchgAxDi ; (* 87 C7: AX<->DI *)
  505. PROCEDURE AddAxDi ; BEGIN B (03H) ; B (0C7H) END AddAxDi ; (* 03 C7: AX:=AX+DI *)
  506. PROCEDURE MovSiBx ; BEGIN B (89H) ; B (0DEH) END MovSiBx ; (* 89 DE: MOV SI,BX *)
  507. PROCEDURE MovDiDx ; BEGIN B (89H) ; B (0D7H) END MovDiDx ;
  508. PROCEDURE MovDiAx ; BEGIN B (89H) ; B (0C7H) END MovDiAx ;
  509. PROCEDURE AddDiAx ; BEGIN B (1H) ; B (0C7H) END AddDiAx ;
  510. (* The register-pair emitters strcat (EmitStrCat) needs. Two registers, so
  511. the audit's two-register pattern reads them positionally: `AddDxAx' is
  512. 01 C2 = ADD DX,AX (01 /r is `op r/m16,r16'', DX is r/m and AX is reg), and
  513. the mirror `SubDxAx' is 29 C2. MovCxAx is 89 C1 (89 /r: reg is the
  514. source, so CX is r/m and AX is reg). MovDiSi is 8B FE (8B /r: reg is the
  515. destination). *)
  516. PROCEDURE MovCxAx ; BEGIN B (89H) ; B (0C1H) END MovCxAx ; (* 89 C1: MOV CX,AX *)
  517. PROCEDURE MovDiSi ; BEGIN B (8BH) ; B (0FEH) END MovDiSi ; (* 8B FE: MOV DI,SI *)
  518. PROCEDURE AddDxAx ; BEGIN B (1H) ; B (0C2H) END AddDxAx ; (* 01 C2: DX:=DX+AX *)
  519. PROCEDURE SubDxAx ; BEGIN B (29H) ; B (0C2H) END SubDxAx ; (* 29 C2: DX:=DX-AX *)
  520. PROCEDURE AddDiDx ; BEGIN B (1H) ; B (0D7H) END AddDiDx ; (* 01 D7: DI:=DI+DX *)
  521. PROCEDURE StSiCl ; BEGIN B (88H) ; B (0CH) END StSiCl ; (* 88 0C: [SI]:=CL *)
  522. PROCEDURE IncBx ; BEGIN B (43H) END IncBx ;
  523. PROCEDURE LdAlBx ; BEGIN B (8AH) ; B (07H) END LdAlBx ; (* 8A 07: AL:=[BX] *)
  524. PROCEDURE MovAlBl ; BEGIN B (8AH) ; B (0C3H) END MovAlBl ; (* 8A C3: AL:=BL *)
  525. (* 8A 07 and 8A C3 are one letter apart and do opposite things: the
  526. first reads the byte AT the pointer in BX, the second takes the low
  527. byte OF BX. getch's pushback path wanted the second and used the
  528. first, so it read memory 010Ah - the address of the character - and
  529. returned whatever was there. Hence the Ld/Mov split. *)
  530. PROCEDURE MovClBx ; BEGIN B (8AH) ; B (0FH) END MovClBx ; (* 8A 0F: CL:=[BX] *)
  531. PROCEDURE XorBxBx ; BEGIN B (31H) ; B (0DBH) END XorBxBx ; (* 31 DB: BX:=0 *)
  532. PROCEDURE MovBlDl ; BEGIN B (8AH) ; B (0DAH) END MovBlDl ; (* 8A DA: BL:=DL *)
  533. PROCEDURE MovBhAh ; BEGIN B (88H) ; B (0E7H) END MovBhAh ; (* 88 E7: BH:=AH *)
  534. PROCEDURE MovDxAx ; BEGIN B (89H) ; B (0C2H) END MovDxAx ; (* 89 C2: DX:=AX *)
  535. PROCEDURE MovSiBp4 ; BEGIN B (8BH) ; B (76H) ; B (4) END MovSiBp4 ; (* SI:=[BP+4] *)
  536. PROCEDURE MovSiBp6 ; BEGIN B (8BH) ; B (76H) ; B (6) END MovSiBp6 ; (* SI:=[BP+6] *)
  537. PROCEDURE LdAxSi ; BEGIN B (8BH) ; B (04H) END LdAxSi ; (* 8B 04: AX:=[SI] *)
  538. PROCEDURE StDiOffBx (off : CARDINAL) ;
  539. BEGIN B (89H) ; B (5DH) ; B (off MOD 256) END StDiOffBx ; (* MOV [DI+o],BX *)
  540. PROCEDURE StDiOffDx (off : CARDINAL) ;
  541. BEGIN B (89H) ; B (55H) ; B (off MOD 256) END StDiOffDx ; (* MOV [DI+o],DX *)
  542. PROCEDURE Js8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (78H, nm) END Js8 ;
  543. PROCEDURE Jns8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (79H, nm) END Jns8 ;
  544. PROCEDURE Jmp8 (nm : ARRAY OF CHAR ) ;
  545. BEGIN B (0EBH) ; AddFix (0, rpos, nm, 0) ; B (0) END Jmp8 ;
  546. PROCEDURE MovAxImm (v : CARDINAL ) ; BEGIN B (0B8H) ; W (v) END MovAxImm ;
  547. PROCEDURE XchgDlDh ; BEGIN B (86H) ; B (0D6H) END XchgDlDh ; (* 86 D6: DL<->DH *)
  548. PROCEDURE DecAl ; BEGIN B (0FEH) ; B (0C8H) END DecAl ; (* FE C8 *)
  549. PROCEDURE ShlDx1 ; BEGIN B (0D1H) ; B (0E2H) END ShlDx1 ; (* D1 E2: SHL DX,1 *)
  550. PROCEDURE NegDx ; BEGIN B (0F7H) ; B (0DAH) END NegDx ; (* F7 DA: NEG DX *)
  551. PROCEDURE CmpDh0 ; BEGIN B (80H) ; B (0FEH) ; B (0) END CmpDh0 ; (* 80 FE 00 *)
  552. PROCEDURE CmpBh0 ; BEGIN B (80H) ; B (0FFH) ; B (0) END CmpBh0 ; (* 80 FF 00 *)
  553. PROCEDURE CmpDx0 ; BEGIN B (83H) ; B (0FAH) ; B (0) END CmpDx0 ; (* 83 FA 00 *)
  554. PROCEDURE CmpAl0 ; BEGIN B (3CH) ; B (0) END CmpAl0 ; (* 3C 00 *)
  555. PROCEDURE AndDhImm (v : CARDINAL) ;
  556. BEGIN B (80H) ; B (0E6H) ; B (v MOD 256) END AndDhImm ; (* 80 E6 imm *)
  557. (* --- the MBF core's register moves and flag helpers --------------------- *)
  558. PROCEDURE MovAxCx ; BEGIN B (89H) ; B (0C8H) END MovAxCx ; (* AX:=CX *)
  559. PROCEDURE MovBxSi ; BEGIN B (89H) ; B (0F3H) END MovBxSi ; (* BX:=SI *)
  560. PROCEDURE MovDxDi ; BEGIN B (89H) ; B (0FAH) END MovDxDi ; (* DX:=DI *)
  561. PROCEDURE MovBxCx ; BEGIN B (89H) ; B (0CBH) END MovBxCx ; (* BX:=CX *)
  562. PROCEDURE MovBxDx ; BEGIN B (89H) ; B (0D3H) END MovBxDx ; (* BX:=DX *)
  563. PROCEDURE MovAhBh ; BEGIN B (88H) ; B (0FCH) END MovAhBh ; (* AH:=BH *)
  564. PROCEDURE MovAhBl ; BEGIN B (88H) ; B (0DCH) END MovAhBl ; (* AH:=BL *)
  565. PROCEDURE MovBlBh ; BEGIN B (88H) ; B (0FBH) END MovBlBh ; (* BL:=BH *)
  566. PROCEDURE MovBhDl ; BEGIN B (88H) ; B (0D7H) END MovBhDl ; (* BH:=DL *)
  567. PROCEDURE MovDlDh ; BEGIN B (88H) ; B (0F2H) END MovDlDh ; (* DL:=DH *)
  568. PROCEDURE MovDhDl ; BEGIN B (88H) ; B (0D6H) END MovDhDl ; (* DH:=DL *)
  569. PROCEDURE MovDlBh ; BEGIN B (88H) ; B (0FAH) END MovDlBh ; (* DL:=BH *)
  570. PROCEDURE MovBlAh ; BEGIN B (88H) ; B (0E3H) END MovBlAh ; (* BL:=AH *)
  571. PROCEDURE XchgAxCx ; BEGIN B (91H) END XchgAxCx ; (* XCHG AX,CX *)
  572. PROCEDURE XchgBxSi ; BEGIN B (87H) ; B (0F3H) END XchgBxSi ;
  573. PROCEDURE XchgDxDi ; BEGIN B (87H) ; B (0FAH) END XchgDxDi ;
  574. PROCEDURE XchgAhCh ; BEGIN B (86H) ; B (0ECH) END XchgAhCh ;
  575. PROCEDURE XorDhDh ; BEGIN B (30H) ; B (0F6H) END XorDhDh ; (* XOR DH,DH *)
  576. PROCEDURE XorAhAh ; BEGIN B (30H) ; B (0E4H) END XorAhAh ; (* XOR AH,AH *)
  577. PROCEDURE NotAh ; BEGIN B (0F6H) ; B (0D4H) END NotAh ; (* NOT AH *)
  578. PROCEDURE NotBx ; BEGIN B (0F7H) ; B (0D3H) END NotBx ; (* NOT BX *)
  579. PROCEDURE NotDx ; BEGIN B (0F7H) ; B (0D2H) END NotDx ; (* NOT DX *)
  580. PROCEDURE ShrDx1 ; BEGIN B (0D1H) ; B (0EAH) END ShrDx1 ; (* SHR DX,1 *)
  581. PROCEDURE RcrBx1 ; BEGIN B (0D1H) ; B (0DBH) END RcrBx1 ; (* RCR BX,1 *)
  582. PROCEDURE RcrDx1 ; BEGIN B (0D1H) ; B (0DAH) END RcrDx1 ; (* RCR DX,1 *)
  583. PROCEDURE RcrAh1 ; BEGIN B (0D0H) ; B (0DCH) END RcrAh1 ; (* RCR AH,1 *)
  584. PROCEDURE RclBx1 ; BEGIN B (0D1H) ; B (0D3H) END RclBx1 ; (* RCL BX,1 *)
  585. PROCEDURE RclDx1 ; BEGIN B (0D1H) ; B (0D2H) END RclDx1 ; (* RCL DX,1 *)
  586. PROCEDURE ShlAh1 ; BEGIN B (0D0H) ; B (0E4H) END ShlAh1 ; (* SHL AH,1 *)
  587. PROCEDURE IncAl ; BEGIN B (0FEH) ; B (0C0H) END IncAl ; (* INC AL *)
  588. PROCEDURE DecCl ; BEGIN B (0FEH) ; B (0C9H) END DecCl ; (* DEC CL *)
  589. PROCEDURE Stc ; BEGIN B (0F9H) END Stc ; (* STC *)
  590. PROCEDURE CmpAlCl ; BEGIN B (38H) ; B (0C8H) END CmpAlCl ; (* CMP AL,CL *)
  591. PROCEDURE CmpCl0 ; BEGIN B (80H) ; B (0F9H) ; B (0) END CmpCl0 ; (* 80 F9 00 *)
  592. PROCEDURE MovBhBl ; BEGIN B (88H) ; B (0DFH) END MovBhBl ; (* BH:=BL *)
  593. PROCEDURE CmpCh0 ; BEGIN B (80H) ; B (0FDH) ; B (0) END CmpCh0 ;
  594. PROCEDURE AddAlCl ; BEGIN B (00H) ; B (0C8H) END AddAlCl ; (* AL:=AL+CL *)
  595. PROCEDURE AddAlImm (v : CARDINAL) ;
  596. BEGIN B (04H) ; B (v MOD 256) END AddAlImm ;
  597. PROCEDURE MovSiImm (v : CARDINAL) ;
  598. BEGIN B (0BEH) ; W (v) END MovSiImm ;
  599. PROCEDURE RcrCh1 ; BEGIN B (0D0H) ; B (0DDH) END RcrCh1 ; (* RCR CH,1 *)
  600. PROCEDURE RclCh1 ; BEGIN B (0D0H) ; B (0D5H) END RclCh1 ; (* RCL CH,1 *)
  601. PROCEDURE Lahf ; BEGIN B (9FH) END Lahf ;
  602. PROCEDURE Sahf ; BEGIN B (9EH) END Sahf ;
  603. PROCEDURE XorCxDi ; BEGIN B (31H) ; B (0F9H) END XorCxDi ; (* CX:=CX XOR DI *)
  604. PROCEDURE XorDxDi ; BEGIN B (31H) ; B (0FAH) END XorDxDi ; (* DX:=DX XOR DI *)
  605. PROCEDURE NotCh ; BEGIN B (0F6H) ; B (0D5H) END NotCh ; (* NOT CH *)
  606. PROCEDURE AndDxDi ; BEGIN B (21H) ; B (0FAH) END AndDxDi ; (* AND DX,DI *)
  607. PROCEDURE AndBxSi ; BEGIN B (21H) ; B (0F3H) END AndBxSi ; (* AND BX,SI *)
  608. PROCEDURE AndAhCh ; BEGIN B (20H) ; B (0ECH) END AndAhCh ; (* AND AH,CH *)
  609. PROCEDURE CmpDxDi ; BEGIN B (39H) ; B (0FAH) END CmpDxDi ; (* CMP DX,DI *)
  610. PROCEDURE CmpBxSi ; BEGIN B (39H) ; B (0F3H) END CmpBxSi ; (* CMP BX,SI *)
  611. PROCEDURE CmpAhCh ; BEGIN B (38H) ; B (0ECH) END CmpAhCh ; (* CMP AH,CH *)
  612. PROCEDURE MovDhImm (v : CARDINAL) ;
  613. BEGIN B (0B6H) ; B (v MOD 256) END MovDhImm ; (* MOV DH,imm8 *)
  614. PROCEDURE AndChImm (v : CARDINAL) ;
  615. BEGIN B (80H) ; B (0E5H) ; B (v MOD 256) END AndChImm ;
  616. PROCEDURE MovDiData (delta : CARDINAL) ;
  617. BEGIN B (0BFH) ; Dd (delta) END MovDiData ;
  618. PROCEDURE LdChDiOff ; BEGIN B (8AH) ; B (2DH) END LdChDiOff ; (* CH:=[DI] *)
  619. PROCEDURE AddAhData (delta : CARDINAL) ;
  620. BEGIN B (02H) ; B (26H) ; Dd (delta) END AddAhData ; (* ADD AH,[Vx] *)
  621. PROCEDURE AdcBxData (delta : CARDINAL) ;
  622. BEGIN B (13H) ; B (1EH) ; Dd (delta) END AdcBxData ; (* ADC BX,[Vx] *)
  623. PROCEDURE AdcDxData (delta : CARDINAL) ;
  624. BEGIN B (13H) ; B (16H) ; Dd (delta) END AdcDxData ; (* ADC DX,[Vx] *)
  625. PROCEDURE StDataCh (delta : CARDINAL) ;
  626. BEGIN B (88H) ; B (2EH) ; Dd (delta) END StDataCh ;
  627. PROCEDURE StDataCx (delta : CARDINAL) ;
  628. BEGIN B (89H) ; B (0EH) ; Dd (delta) END StDataCx ;
  629. PROCEDURE StDataSi (delta : CARDINAL) ;
  630. BEGIN B (89H) ; B (36H) ; Dd (delta) END StDataSi ;
  631. PROCEDURE StDataAx (delta : CARDINAL) ;
  632. BEGIN B (0A3H) ; Dd (delta) END StDataAx ;
  633. PROCEDURE StDataBx (delta : CARDINAL) ;
  634. BEGIN B (89H) ; B (1EH) ; Dd (delta) END StDataBx ;
  635. PROCEDURE StDataDx (delta : CARDINAL) ;
  636. BEGIN B (89H) ; B (16H) ; Dd (delta) END StDataDx ;
  637. PROCEDURE StDataAl (delta : CARDINAL) ;
  638. BEGIN B (0A2H) ; Dd (delta) END StDataAl ;
  639. PROCEDURE MovCxData (delta : CARDINAL) ;
  640. BEGIN B (8BH) ; B (0EH) ; Dd (delta) END MovCxData ;
  641. PROCEDURE MovBxData (delta : CARDINAL) ;
  642. BEGIN B (8BH) ; B (1EH) ; Dd (delta) END MovBxData ;
  643. PROCEDURE MovDxData (delta : CARDINAL) ;
  644. BEGIN B (8BH) ; B (16H) ; Dd (delta) END MovDxData ;
  645. PROCEDURE CmpDxData (delta : CARDINAL) ;
  646. BEGIN B (3BH) ; B (16H) ; Dd (delta) END CmpDxData ;
  647. PROCEDURE CmpBxData (delta : CARDINAL) ;
  648. BEGIN B (3BH) ; B (1EH) ; Dd (delta) END CmpBxData ;
  649. PROCEDURE CmpAhData (delta : CARDINAL) ;
  650. BEGIN B (3AH) ; B (26H) ; Dd (delta) END CmpAhData ;
  651. PROCEDURE SubAhData (delta : CARDINAL) ;
  652. BEGIN B (2AH) ; B (26H) ; Dd (delta) END SubAhData ; (* SUB.B AH,remul21a: ignores the carry *)
  653. (* --- the real formatter/parser's instruction set (fmtreal/ascreal) ------ *)
  654. PROCEDURE Jl8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (7CH, nm) END Jl8 ;
  655. PROCEDURE Jg8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (7FH, nm) END Jg8 ;
  656. PROCEDURE MovAxBx ; BEGIN B (89H) ; B (0D8H) END MovAxBx ; (* AX:=BX *)
  657. PROCEDURE MovAxSi ; BEGIN B (89H) ; B (0F0H) END MovAxSi ; (* AX:=SI *)
  658. PROCEDURE MovSiDi ; BEGIN B (89H) ; B (0FEH) END MovSiDi ; (* SI:=DI *)
  659. PROCEDURE MovBlCl ; BEGIN B (88H) ; B (0CBH) END MovBlCl ; (* BL:=CL *)
  660. PROCEDURE MovChDh ; BEGIN B (88H) ; B (0F5H) END MovChDh ; (* CH:=DH *)
  661. PROCEDURE MovChCl ; BEGIN B (88H) ; B (0CDH) END MovChCl ; (* CH:=CL *)
  662. PROCEDURE MovDlCh ; BEGIN B (88H) ; B (0EAH) END MovDlCh ; (* DL:=CH *)
  663. PROCEDURE MovDhAl ; BEGIN B (88H) ; B (0C6H) END MovDhAl ; (* DH:=AL *)
  664. PROCEDURE MovClAh ; BEGIN B (88H) ; B (0E1H) END MovClAh ; (* CL:=AH *)
  665. PROCEDURE MovClAl ; BEGIN B (88H) ; B (0C1H) END MovClAl ; (* CL:=AL *)
  666. PROCEDURE MovAhAl ; BEGIN B (88H) ; B (0C4H) END MovAhAl ; (* AH:=AL *)
  667. PROCEDURE MovAlCl ; BEGIN B (88H) ; B (0C8H) END MovAlCl ; (* AL:=CL *)
  668. PROCEDURE MovChImm (v : CARDINAL) ;
  669. BEGIN B (0B5H) ; B (v MOD 256) END MovChImm ; (* MOV CH,imm8 *)
  670. PROCEDURE CmpDxImm (v : CARDINAL) ;
  671. BEGIN B (81H) ; B (0FAH) ; W (v) END CmpDxImm ; (* CMP DX,imm16 *)
  672. PROCEDURE CmpAxImm (v : CARDINAL) ;
  673. BEGIN B (3DH) ; W (v) END CmpAxImm ; (* CMP AX,imm16 *)
  674. PROCEDURE CmpDl0 ; BEGIN B (80H) ; B (0FAH) ; B (0) END CmpDl0 ;
  675. PROCEDURE TestChImm (v : CARDINAL) ;
  676. BEGIN B (0F6H) ; B (0C5H) ; B (v MOD 256) END TestChImm ; (* TEST CH,imm8 *)
  677. PROCEDURE TestDiOffImm (off, v : CARDINAL) ;
  678. BEGIN B (0F6H) ; B (45H) ; B (off MOD 256) ; B (v MOD 256) END TestDiOffImm ;
  679. PROCEDURE AndClImm (v : CARDINAL) ;
  680. BEGIN B (80H) ; B (0E1H) ; B (v MOD 256) END AndClImm ; (* AND CL,imm8 *)
  681. PROCEDURE AndBlImm (v : CARDINAL) ;
  682. BEGIN B (80H) ; B (0E3H) ; B (v MOD 256) END AndBlImm ; (* AND BL,imm8 *)
  683. PROCEDURE OrChImm (v : CARDINAL) ;
  684. BEGIN B (80H) ; B (0CDH) ; B (v MOD 256) END OrChImm ; (* OR CH,imm8 *)
  685. PROCEDURE AddChImm (v : CARDINAL) ;
  686. BEGIN B (80H) ; B (0C5H) ; B (v MOD 256) END AddChImm ; (* ADD CH,imm8 *)
  687. PROCEDURE AddClImm (v : CARDINAL) ;
  688. BEGIN B (80H) ; B (0C1H) ; B (v MOD 256) END AddClImm ; (* ADD CL,imm8 *)
  689. PROCEDURE ShrChCl ; BEGIN B (0D2H) ; B (0EDH) END ShrChCl ; (* SHR CH,CL *)
  690. PROCEDURE ShrBl1 ; BEGIN B (0D0H) ; B (0EBH) END ShrBl1 ; (* SHR BL,1 *)
  691. PROCEDURE ShlAx1 ; BEGIN B (0D1H) ; B (0E0H) END ShlAx1 ; (* SHL AX,1 *)
  692. PROCEDURE ShlAl1 ; BEGIN B (0D0H) ; B (0E0H) END ShlAl1 ; (* SHL AL,1 *)
  693. PROCEDURE AddBlBh ; BEGIN B (00H) ; B (0FBH) END AddBlBh ; (* BL:=BL+BH *)
  694. PROCEDURE XorBhBh ; BEGIN B (30H) ; B (0FFH) END XorBhBh ; (* BH:=0 *)
  695. PROCEDURE AddAlAh ; BEGIN B (00H) ; B (0E0H) END AddAlAh ; (* AL:=AL+AH *)
  696. PROCEDURE AddAxCx ; BEGIN B (03H) ; B (0C1H) END AddAxCx ; (* AX:=AX+CX *)
  697. PROCEDURE AdcBxCx ; BEGIN B (13H) ; B (0D9H) END AdcBxCx ; (* BX:=BX+CX+CF *)
  698. PROCEDURE AdcDxCx ; BEGIN B (13H) ; B (0D1H) END AdcDxCx ; (* DX:=DX+CX+CF *)
  699. PROCEDURE AddAxDx ; BEGIN B (03H) ; B (0C2H) END AddAxDx ; (* AX:=AX+DX *)
  700. PROCEDURE AddBxAx ; BEGIN B (03H) ; B (0D8H) END AddBxAx ; (* BX:=BX+AX *)
  701. PROCEDURE SubAxDx ; BEGIN B (2BH) ; B (0C2H) END SubAxDx ; (* AX:=AX-DX *)
  702. PROCEDURE AddAxImm (v : CARDINAL) ;
  703. BEGIN B (05H) ; W (v) END AddAxImm ; (* ADD AX,imm16 *)
  704. PROCEDURE DecBx ; BEGIN B (4BH) END DecBx ; (* DEC BX *)
  705. PROCEDURE DecCh ; BEGIN B (0FEH) ; B (0CDH) END DecCh ; (* DEC CH *)
  706. PROCEDURE DecDl ; BEGIN B (0FEH) ; B (0CAH) END DecDl ; (* DEC DL *)
  707. PROCEDURE NegCl ; BEGIN B (0F6H) ; B (0D9H) END NegCl ; (* NEG CL *)
  708. PROCEDURE Cbw ; BEGIN B (98H) END Cbw ; (* AL -> AX *)
  709. PROCEDURE ImulDx ; BEGIN B (0F7H) ; B (0EAH) END ImulDx ; (* IMUL DX *)
  710. PROCEDURE StSiImm (v : CARDINAL) ;
  711. BEGIN B (0C7H) ; B (04H) ; W (v) END StSiImm ; (* [SI]:=imm16 *)
  712. PROCEDURE StSiAl ; BEGIN B (88H) ; B (04H) END StSiAl ; (* [SI]:=AL *)
  713. PROCEDURE StSiCh ; BEGIN B (88H) ; B (2CH) END StSiCh ; (* [SI]:=CH *)
  714. PROCEDURE StBxAl ; BEGIN B (88H) ; B (07H) END StBxAl ; (* [BX]:=AL *)
  715. PROCEDURE LdAlSi ; BEGIN B (8AH) ; B (04H) END LdAlSi ; (* AL:=[SI] *)
  716. PROCEDURE LdClBx ; BEGIN B (8AH) ; B (0FH) END LdClBx ; (* CL:=[BX] *)
  717. PROCEDURE LdAlDi ; BEGIN B (8AH) ; B (05H) END LdAlDi ; (* AL:=[DI] *)
  718. PROCEDURE XorDiOffImm (off, v : CARDINAL) ;
  719. BEGIN B (80H) ; B (75H) ; B (off MOD 256) ; B (v MOD 256) END XorDiOffImm ;
  720. PROCEDURE LdClSi ; BEGIN B (8AH) ; B (0CH) END LdClSi ; (* CL:=[SI] *)
  721. PROCEDURE LdAxDi ; BEGIN B (8BH) ; B (05H) END LdAxDi ; (* AX:=[DI] *)
  722. PROCEDURE LdBxDiOff (off : CARDINAL) ;
  723. BEGIN B (8BH) ; B (5DH) ; B (off MOD 256) END LdBxDiOff ; (* [DI+off] *)
  724. PROCEDURE LdDxDiOff (off : CARDINAL) ;
  725. BEGIN B (8BH) ; B (55H) ; B (off MOD 256) END LdDxDiOff ; (* [DI+off] *)
  726. PROCEDURE LeaDiBxData (delta : CARDINAL) ;
  727. BEGIN B (8DH) ; B (0BFH) ; Dd (delta) END LeaDiBxData ; (* LEA DI,[BX+data] *)
  728. PROCEDURE MovSiData (delta : CARDINAL) ;
  729. BEGIN B (0BEH) ; Dd (delta) END MovSiData ;
  730. PROCEDURE MovBxDataAddr (delta : CARDINAL) ;
  731. BEGIN B (0BBH) ; Dd (delta) END MovBxDataAddr ;
  732. PROCEDURE CmpSiData (delta : CARDINAL) ;
  733. BEGIN B (81H) ; B (0FEH) ; Dd (delta) END CmpSiData ;
  734. PROCEDURE MovAxData (delta : CARDINAL) ;
  735. BEGIN B (8BH) ; B (06H) ; Dd (delta) END MovAxData ;
  736. PROCEDURE LdClData (delta : CARDINAL) ;
  737. BEGIN B (8AH) ; B (0EH) ; Dd (delta) END LdClData ;
  738. PROCEDURE DecData (delta : CARDINAL) ;
  739. BEGIN B (0FEH) ; B (0EH) ; Dd (delta) END DecData ;
  740. PROCEDURE CmpBxByte (v : CARDINAL) ;
  741. BEGIN B (80H) ; B (3FH) ; B (v MOD 256) END CmpBxByte ; (* CMP [BX],imm8 *)
  742. PROCEDURE MovBxByte (v : CARDINAL) ;
  743. BEGIN B (0C6H) ; B (07H) ; B (v MOD 256) END MovBxByte ; (* [BX]:=imm8 *)
  744. PROCEDURE MovBxOff1Byte (v : CARDINAL) ;
  745. BEGIN B (0C6H) ; B (47H) ; B (1) ; B (v MOD 256) END MovBxOff1Byte ;
  746. PROCEDURE IncBxByte ; BEGIN B (0FEH) ; B (07H) END IncBxByte ; (* INC [BX] *)
  747. PROCEDURE MovDiBp6 ; BEGIN B (8BH) ; B (7EH) ; B (6) END MovDiBp6 ;
  748. PROCEDURE MovDxBp6 ; BEGIN B (8BH) ; B (56H) ; B (6) END MovDxBp6 ;
  749. PROCEDURE PushBp6 ; BEGIN B (0FFH) ; B (76H) ; B (6) END PushBp6 ;
  750. PROCEDURE MovCxBp6 ; BEGIN B (8BH) ; B (4EH) ; B (6) END MovCxBp6 ;
  751. PROCEDURE MovDxBp4 ; BEGIN B (8BH) ; B (56H) ; B (4) END MovDxBp4 ;
  752. PROCEDURE MovDiBp8 ; BEGIN B (8BH) ; B (7EH) ; B (8) END MovDiBp8 ;
  753. PROCEDURE SbbBxData (delta : CARDINAL) ;
  754. BEGIN B (1BH) ; B (1EH) ; Dd (delta) END SbbBxData ;
  755. PROCEDURE SbbDxData (delta : CARDINAL) ;
  756. BEGIN B (1BH) ; B (16H) ; Dd (delta) END SbbDxData ;
  757. PROCEDURE StDiCh ; BEGIN B (88H) ; B (2DH) END StDiCh ; (* [DI]:=CH *)
  758. PROCEDURE Cmc ; BEGIN B (0F5H) END Cmc ;
  759. PROCEDURE Clc ; BEGIN B (0F8H) END Clc ;
  760. PROCEDURE IncCl ; BEGIN B (0FEH) ; B (0C1H) END IncCl ;
  761. PROCEDURE DecDi ; BEGIN B (4FH) END DecDi ;
  762. PROCEDURE SubAhCh ; BEGIN B (28H) ; B (0ECH) END SubAhCh ; (* AH:=AH-CH *)
  763. PROCEDURE SubAlDl ; BEGIN B (2AH) ; B (0C2H) END SubAlDl ; (* AL:=AL-DL *)
  764. PROCEDURE IncCh ; BEGIN B (0FEH) ; B (0C5H) END IncCh ; (* INC CH *)
  765. PROCEDURE IncDl ; BEGIN B (0FEH) ; B (0C2H) END IncDl ; (* INC DL *)
  766. PROCEDURE MovClImm (v : CARDINAL) ;
  767. BEGIN B (0B1H) ; B (v MOD 256) END MovClImm ;
  768. PROCEDURE CmpClImm (v : CARDINAL) ;
  769. BEGIN B (80H) ; B (0F9H) ; B (v MOD 256) END CmpClImm ;
  770. PROCEDURE SubClAl ; BEGIN B (28H) ; B (0C1H) END SubClAl ; (* CL:=CL-AL *)
  771. PROCEDURE SubAlCl ; BEGIN B (28H) ; B (0C8H) END SubAlCl ; (* AL:=AL-CL *)
  772. PROCEDURE SubClImm (v : CARDINAL) ;
  773. BEGIN B (80H) ; B (0E9H) ; B (v MOD 256) END SubClImm ;
  774. PROCEDURE AddAhCh ; BEGIN B (00H) ; B (0ECH) END AddAhCh ; (* AH:=AH+CH *)
  775. PROCEDURE AddAhImm (v : CARDINAL) ;
  776. BEGIN B (80H) ; B (0C4H) ; B (v MOD 256) END AddAhImm ;
  777. PROCEDURE AdcBxSi ; BEGIN B (11H) ; B (0F3H) END AdcBxSi ;
  778. PROCEDURE AdcDxDi ; BEGIN B (11H) ; B (0FAH) END AdcDxDi ;
  779. PROCEDURE AdcBxImm (v : CARDINAL) ;
  780. BEGIN B (83H) ; B (0D3H) ; B (v MOD 256) END AdcBxImm ;
  781. PROCEDURE AdcDxImm (v : CARDINAL) ;
  782. BEGIN B (83H) ; B (0D2H) ; B (v MOD 256) END AdcDxImm ;
  783. PROCEDURE SbbBxSi ; BEGIN B (19H) ; B (0F3H) END SbbBxSi ;
  784. PROCEDURE SbbDxDi ; BEGIN B (19H) ; B (0FAH) END SbbDxDi ;
  785. PROCEDURE OrDiImm (v : CARDINAL) ;
  786. BEGIN B (81H) ; B (0CFH) ; W (v) END OrDiImm ; (* DI:=DI OR imm16 *)
  787. PROCEDURE OrDhImm (v : CARDINAL) ;
  788. BEGIN B (80H) ; B (0CEH) ; B (v MOD 256) END OrDhImm ;
  789. PROCEDURE TestDhImm (v : CARDINAL) ;
  790. BEGIN B (0F6H) ; B (0C6H) ; B (v MOD 256) END TestDhImm ;
  791. PROCEDURE StDataImm (delta, v : CARDINAL) ;
  792. BEGIN B (0C7H) ; B (06H) ; Dd (delta) ; W (v) END StDataImm ;
  793. PROCEDURE StDataByte (delta, v : CARDINAL) ;
  794. BEGIN B (0C6H) ; B (06H) ; Dd (delta) ; B (v MOD 256) END StDataByte ;
  795. PROCEDURE StDataCl (delta : CARDINAL) ;
  796. BEGIN B (88H) ; B (0EH) ; Dd (delta) END StDataCl ;
  797. PROCEDURE StDataDi (delta : CARDINAL) ;
  798. BEGIN B (89H) ; B (3EH) ; Dd (delta) END StDataDi ;
  799. PROCEDURE LdAlData (delta : CARDINAL) ;
  800. BEGIN B (0A0H) ; Dd (delta) END LdAlData ;
  801. PROCEDURE XorDiData (delta : CARDINAL) ;
  802. BEGIN B (33H) ; B (3EH) ; Dd (delta) END XorDiData ; (* XOR DI,[Vx] *)
  803. PROCEDURE XorDataDh (delta : CARDINAL) ;
  804. BEGIN B (30H) ; B (36H) ; Dd (delta) END XorDataDh ; (* XOR [Vx],DH *)
  805. PROCEDURE XorDhData (delta : CARDINAL) ;
  806. BEGIN B (32H) ; B (36H) ; Dd (delta) END XorDhData ; (* XOR DH,[Vx] *)
  807. PROCEDURE XorDataImm (delta, v : CARDINAL) ;
  808. BEGIN B (80H) ; B (36H) ; Dd (delta) ; B (v MOD 256) END XorDataImm ;
  809. PROCEDURE AndDataImm (delta, v : CARDINAL) ;
  810. BEGIN B (80H) ; B (26H) ; Dd (delta) ; B (v MOD 256) END AndDataImm ;
  811. PROCEDURE TestDataImm (delta, v : CARDINAL) ;
  812. BEGIN B (0F6H) ; B (06H) ; Dd (delta) ; B (v MOD 256) END TestDataImm ;
  813. PROCEDURE LdCxSiOff (off : CARDINAL) ;
  814. BEGIN B (8BH) ; B (4CH) ; B (off MOD 256) END LdCxSiOff ;
  815. PROCEDURE LdDxSiOff (off : CARDINAL) ;
  816. BEGIN B (8BH) ; B (54H) ; B (off MOD 256) END LdDxSiOff ;
  817. PROCEDURE LdCxDiOff (off : CARDINAL) ;
  818. BEGIN B (8BH) ; B (4DH) ; B (off MOD 256) END LdCxDiOff ;
  819. PROCEDURE LdSiDiOff (off : CARDINAL) ;
  820. BEGIN B (8BH) ; B (75H) ; B (off MOD 256) END LdSiDiOff ;
  821. PROCEDURE LdDiDiOff (off : CARDINAL) ;
  822. BEGIN B (8BH) ; B (7DH) ; B (off MOD 256) END LdDiDiOff ;
  823. PROCEDURE PushBp4 ; BEGIN B (0FFH) ; B (76H) ; B (4) END PushBp4 ; (* PUSH [BP+4] *)
  824. PROCEDURE JccNear (code : CARDINAL ; nm : ARRAY OF CHAR) ;
  825. (* A conditional near jump: the 8086 has no Jcc rel16, so an INVERSE Jcc_s
  826. +3 over an E9 rel16. The inverse is what makes the E9 run when `code'
  827. HOLDS; a raw Jcc here would jump when it does NOT (remult's zero test did
  828. exactly that and multiplied everything to zero). Use this when the target
  829. is more than 127 bytes away; Jcc's rel8 wraps silently otherwise (the
  830. FixUp guard below now refuses that). *)
  831. BEGIN
  832. IF (code MOD 2) = 0 THEN
  833. B (code + 1)
  834. ELSE
  835. B (code - 1)
  836. END ;
  837. B (3) ; B (0E9H) ; AddFix (1, rpos, nm, 0) ; W (0)
  838. END JccNear ;
  839. PROCEDURE JmpNear (nm : ARRAY OF CHAR) ;
  840. BEGIN B (0E9H) ; AddFix (1, rpos, nm, 0) ; W (0) END JmpNear ;
  841. PROCEDURE Call (nm : ARRAY OF CHAR) ;
  842. BEGIN B (0E8H) ; AddFix (1, rpos, nm, 0) ; W (0) END Call ;
  843. PROCEDURE StVxBx (delta : CARDINAL ) ;
  844. (* 89 1E lo hi: MOV [Vx],BX - the mirror of LdBxVx above, and it must use the
  845. same mod=00 / rm=110 and the same Dd fixup kind, or one of the two
  846. addresses a different place. *)
  847. BEGIN
  848. B (89H) ; B (01EH) ; Dd (delta)
  849. END StVxBx ;
  850. PROCEDURE JmpBx ; BEGIN B (0FFH) ; B (0E3H) END JmpBx ; (* FF E3: JMP BX *)
  851. (* JE 74 JNE 75 JB 72 JBE 76 JGE 7D *)
  852. PROCEDURE Je8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (74H, nm) END Je8 ;
  853. PROCEDURE Jne8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (75H, nm) END Jne8 ;
  854. PROCEDURE Jb8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (72H, nm) END Jb8 ;
  855. PROCEDURE Jbe8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (76H, nm) END Jbe8 ;
  856. PROCEDURE Jge8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (7DH, nm) END Jge8 ;
  857. PROCEDURE Jle8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (7EH, nm) END Jle8 ;
  858. PROCEDURE Ja8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (77H, nm) END Ja8 ;
  859. PROCEDURE Jae8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (73H, nm) END Jae8 ;
  860. PROCEDURE Jcxz8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (0E3H, nm) END Jcxz8 ;
  861. PROCEDURE Loop8 (nm : ARRAY OF CHAR ) ; BEGIN Jcc (0E2H, nm) END Loop8 ;
  862. (* ---------------------------------------------------------------- *)
  863. (* the entries *)
  864. (* ---------------------------------------------------------------- *)
  865. PROCEDURE EmitInitMem ;
  866. (* AX = offset of the program header. The header holds, at +4 the base of
  867. the program's data area and at +8 its end, so the globals can be zeroed -
  868. Pascal leaves them undefined, TP3's runtime clears them. Also makes
  869. ES = DS so that any string instruction in the library would work. *)
  870. BEGIN
  871. M ("initmem") ;
  872. MovSiAx ; (* SI = AX = the header offset the caller passed *)
  873. MovDxSi4 ; (* DX = [SI+4] = hdrDS = data base *)
  874. MovCxSi6 ; (* CX = [SI+6] = hdrHeap = data end *)
  875. CmpCxDx ;
  876. Jbe8 ("im_done") ;
  877. MovDiDx ; (* DI = data base *)
  878. M ("im_zero") ;
  879. XorAxAx ; (* AX = 0: the value written into every global.
  880. The caller passes the header offset in AX, so
  881. without this the "zeroing" loop would write the
  882. header offset into all of them. *)
  883. StDiAx ;
  884. AddDi2 ;
  885. CmpDiCx ;
  886. Jb8 ("im_zero") ;
  887. M ("im_done") ;
  888. PushDs ; PopEs ;
  889. RetR
  890. END EmitInitMem ;
  891. PROCEDURE EmitEnd ;
  892. (* progend and halt are the same code: the compiler already zeroes AX for
  893. progend, and a HALT argument is discarded at compile time, so both leave
  894. with exit code 0. *)
  895. BEGIN
  896. M ("progend") ;
  897. M ("halt") ;
  898. XorAxAx ;
  899. MovAh (4CH) ;
  900. Int21 ;
  901. RetR
  902. END EmitEnd ;
  903. PROCEDURE EmitStackChk ;
  904. (* called from every procedure prologue. Range and stack checking are not
  905. compiled in yet, so this must do nothing at all - in particular it must
  906. not touch a register, because the call site is in the middle of a
  907. partially evaluated expression. *)
  908. BEGIN
  909. M ("stackchk") ;
  910. RetR
  911. END EmitStackChk ;
  912. PROCEDURE EmitGetCh ;
  913. (* AL = next character, 1Ah at end of input. INT 21h AH=08h reads without
  914. echoing, so a redirected stdin behaves the same as a keyboard.
  915. This is TP3's `getbyte' (TPSRC4:94), not a bare INT 21h. getbyte serves a
  916. buffered character from the file record without advancing the buffer
  917. pointer when the "char pre-read" flag ($02) is set, and readnum sets that
  918. flag on the character that ENDS a number - the terminating blank is *not*
  919. consumed. xreadln is then called and it is the xreadln that consumes the
  920. newline.
  921. This matters because `readln(n)' is emitted as xrdint followed by xreadln
  922. (TPSRC8 prdrdln emits xreadln whenever rdlnflg is set). A getch that
  923. always consumed its character left xreadln starting on the *following*
  924. line, so `readln(n); readln(c)' read the char from line 3 of the input.
  925. One byte of pushback reproduces the flag. *)
  926. BEGIN
  927. M ("getch") ;
  928. LdBxVx (D_PUSH) ; (* BX := the slot's contents *)
  929. CmpBx0 ;
  930. Je8 ("gc_dos") ;
  931. MovAlBl ; (* AL := the pending character, i.e. the
  932. low byte OF BX - not the byte at [BX] *)
  933. XorBxBx ;
  934. StVxBx (D_PUSH) ; (* and empty the slot *)
  935. RetR ;
  936. M ("gc_dos") ;
  937. MovAh (08H) ;
  938. Int21 ;
  939. RetR
  940. END EmitGetCh ;
  941. PROCEDURE EmitUnGetCh ;
  942. (* put AL back, so the next getch returns it again. TP3 does this by
  943. rewinding the buffer pointer; a one-byte slot is the same thing. *)
  944. BEGIN
  945. M ("ungetch") ;
  946. MovDlAl ;
  947. MovBxImm (1) ; (* BH := 1 marks the slot occupied, so
  948. a pushed-back NUL is still a
  949. pushed-back NUL *)
  950. MovBlDl ;
  951. StVxBx (D_PUSH) ;
  952. RetR
  953. END EmitUnGetCh ;
  954. PROCEDURE EmitWrInt ;
  955. (* one signed 16-bit value on the stack. Div CX gives the remainder in DX,
  956. which is turned into a digit and stored backwards from the end of the
  957. scratch area, then printed forwards. *)
  958. BEGIN
  959. M ("wrint") ;
  960. PushBp ; MovBpSp ;
  961. MovAxBp4 ;
  962. CmpAx0 ;
  963. Jge8 ("wi_pos") ;
  964. PushAx ;
  965. MovAlD (ORD ("-")) ; MovAh (2) ; Int21 ;
  966. PopAx ;
  967. NegAx ;
  968. M ("wi_pos") ;
  969. MovCxImm (10) ;
  970. MovBxVx (D_NUM + 8) ; (* BX = one past the last digit *)
  971. MovSiBx ;
  972. M ("wi_dig") ;
  973. XorDxDx ;
  974. DivCx ;
  975. AddDl (ORD ("0")) ;
  976. DecSi ;
  977. StSiDl ;
  978. CmpAx0 ;
  979. Jne8 ("wi_dig") ;
  980. M ("wi_out") ;
  981. CmpSiBx ;
  982. Je8 ("wi_done") ;
  983. MovAlSi ; (* the digit: AH=02h wants it in AL *)
  984. MovAh (02H) ; Int21 ;
  985. IncSi ;
  986. J8 ("wi_out") ;
  987. M ("wi_done") ;
  988. MovSpBp ; PopBp ; RetR
  989. END EmitWrInt ;
  990. PROCEDURE EmitWrIntW ;
  991. (* `writeln(I:2)': an integer in a field of `width' columns, right-justified.
  992. The caller pushes the VALUE first and the WIDTH second, so the value is at
  993. [BP+6] and the width at [BP+4]. The number of characters the signed
  994. decimal will occupy is counted first (one extra when negative); the pad is
  995. max(width - chars, 0) spaces. The digit loop is wrint's, duplicated rather
  996. than shared: the two entries have different argument offsets, and a jump
  997. into the middle of the other entry's frame would be a convention nobody
  998. could see from either name. *)
  999. BEGIN
  1000. M ("wrintw") ;
  1001. PushBp ; MovBpSp ;
  1002. (* BX := number of characters: sign + digits *)
  1003. MovBxImm (0) ;
  1004. MovAxBp6 ;
  1005. CmpAx0 ;
  1006. Jge8 ("wiw_abs") ;
  1007. IncBx ; (* the '-' counts as one column *)
  1008. NegAx ;
  1009. M ("wiw_abs") ;
  1010. MovCxImm (10) ;
  1011. M ("wiw_cnt") ;
  1012. IncBx ;
  1013. XorDxDx ;
  1014. DivCx ;
  1015. CmpAx0 ;
  1016. Jne8 ("wiw_cnt") ;
  1017. (* pad := width - chars; only a positive pad prints *)
  1018. MovAxBp4 ;
  1019. SubAxBx ;
  1020. CmpAx0 ;
  1021. Jle8 ("wiw_body") ;
  1022. MovCxAx ;
  1023. M ("wiw_pad") ;
  1024. MovAlD (ORD (' ')) ; MovAh (02H) ; Int21 ;
  1025. Loop8 ("wiw_pad") ;
  1026. M ("wiw_body") ;
  1027. (* the signed decimal, exactly as wrint prints it *)
  1028. MovAxBp6 ;
  1029. CmpAx0 ;
  1030. Jge8 ("wiw_pos") ;
  1031. PushAx ;
  1032. MovAlD (ORD ('-')) ; MovAh (2) ; Int21 ;
  1033. PopAx ;
  1034. NegAx ;
  1035. M ("wiw_pos") ;
  1036. MovCxImm (10) ;
  1037. MovBxVx (D_NUM + 8) ;
  1038. MovSiBx ;
  1039. M ("wiw_dig") ;
  1040. XorDxDx ;
  1041. DivCx ;
  1042. AddDl (ORD ('0')) ;
  1043. DecSi ;
  1044. StSiDl ;
  1045. CmpAx0 ;
  1046. Jne8 ("wiw_dig") ;
  1047. M ("wiw_out") ;
  1048. CmpSiBx ;
  1049. Je8 ("wiw_done") ;
  1050. MovAlSi ;
  1051. MovAh (02H) ; Int21 ;
  1052. IncSi ;
  1053. J8 ("wiw_out") ;
  1054. M ("wiw_done") ;
  1055. MovSpBp ; PopBp ; RetR
  1056. END EmitWrIntW ;
  1057. PROCEDURE EmitWrChar ;
  1058. (* the low byte of the one 16-bit argument, which sits above the return
  1059. address. There is no [SP] addressing in 16-bit mode, so BP stands in for
  1060. the stack pointer -- and it has to be SAVED, because BP is the one register
  1061. a caller is entitled to expect back: the test driver keeps its cursor into
  1062. the case record there, so an entry that borrows BP without returning it
  1063. sends the next call to a garbage address. That is not hypothetical: wrchar
  1064. and wrbool both did it, and "writeln(42) writeln TRUE in one program" hung
  1065. the machine with the record header printed twice, because the driver had
  1066. been sent back to the top of its own loop. Load BP, use it, hand it back.
  1067. MovAlArg4, not MovAlArg2: BP is pushed first, so the argument is four bytes
  1068. up, not two. AL, not DL: see the AL twins above. *)
  1069. BEGIN
  1070. M ("wrchar") ;
  1071. PushBp ; MovBpSp ;
  1072. MovAlArg4 ;
  1073. MovSpBp ; PopBp ;
  1074. MovAh (02H) ;
  1075. Int21 ;
  1076. RetR
  1077. END EmitWrChar ;
  1078. PROCEDURE EmitWrInl ;
  1079. (* Write an inline string literal - TP3 TPSRC4 "xwrtinl".
  1080. The compiler emits
  1081. CALL wrtinl <length byte> <character>...
  1082. so the return address on the stack points at the length byte that follows
  1083. the call. POP BX takes that address, CX picks up the length, and the
  1084. routine finishes with JMP BX - returning to just past the last character.
  1085. That is the whole trick: the literal is self-delimiting, so it needs no
  1086. terminator, no length table and no space in the data segment, and it costs
  1087. the code stream only the characters themselves (TPSRC10 "estring" emits
  1088. exactly <length byte><chars> for the same reason).
  1089. Consequently this entry has NO stack argument, unlike WrInt/WrChar: the
  1090. return address has already been consumed by the POP. *)
  1091. BEGIN
  1092. M ("wrtinl") ;
  1093. PopBx ; (* BX := address of the length byte *)
  1094. XorCxCx ;
  1095. MovClBx ; (* CX := length *)
  1096. IncBx ; (* BX -> first character *)
  1097. MovAh (02H) ; (* INT 21h/02h: put character, AL *)
  1098. Jcxz8 ("wn_end") ; (* empty string -> nothing to do *)
  1099. M ("wn_loop") ;
  1100. LdAlBx ; (* AL := next character *)
  1101. Int21 ; (* (preserves every register but AL) *)
  1102. IncBx ;
  1103. Loop8 ("wn_loop") ;
  1104. M ("wn_end") ;
  1105. JmpBx (* resume past the string; no RET here,
  1106. the return address is already gone *)
  1107. END EmitWrInl ;
  1108. PROCEDURE EmitWrStr ;
  1109. (* Write a string VARIABLE - TP3 TPSRC4 "xwrtstr", minus the field-width
  1110. formatting this compiler does not compile yet.
  1111. The compiler emits
  1112. <PUSH the string's address> from EmPushVarAddr
  1113. CALL wrstr
  1114. so the address is the one 16-bit argument on the stack, read through BP
  1115. exactly as WrChar reads its value (there is no [SP] form). The first byte
  1116. at that address is the current LENGTH and the characters follow it, which
  1117. is the layout TPSRC8's `string' type gives a variable: TPSRC8:83 does
  1118. `INC BX / MOV varsize,BX' for `string [n]', so n+1 bytes, length byte
  1119. first.
  1120. The loop is wrtinl's, pointed at the caller's bytes instead of at the
  1121. bytes behind the call: CL := [BX] is the length, BX moves to the first
  1122. character, and one INT 21h/02h prints each one until CX runs out. A
  1123. length of 0 prints nothing, so an empty string is silent. *)
  1124. BEGIN
  1125. M ("wrstr") ;
  1126. PushBp ; MovBpSp ;
  1127. PushBx ; PushCx ; PushAx ;
  1128. MovBxBp4 ; (* BX := the caller's string address *)
  1129. XorCxCx ;
  1130. MovClBx ; (* CX := the length byte at [BX] *)
  1131. IncBx ; (* BX -> the first character *)
  1132. MovAh (02H) ; (* INT 21h/02h: put the character in AL *)
  1133. Jcxz8 ("ws_end") ; (* empty string -> nothing to do *)
  1134. M ("ws_loop") ;
  1135. LdAlBx ; (* AL := next character *)
  1136. Int21 ; (* preserves every register but AL *)
  1137. IncBx ;
  1138. Loop8 ("ws_loop") ;
  1139. M ("ws_end") ;
  1140. PopAx ; PopCx ; PopBx ;
  1141. MovSpBp ; PopBp ;
  1142. RetR
  1143. END EmitWrStr ;
  1144. PROCEDURE EmitWrBool ;
  1145. (* 4, not 2, for the reason given on CmpArg4W0: BP is pushed before it is
  1146. borrowed, so the argument is four bytes up. POP BP before the RET, and
  1147. note that CmpArg4W0 has already handed SP back, so the pop lands on the
  1148. saved BP and not on the argument. *)
  1149. BEGIN
  1150. M ("wrbool") ;
  1151. PushBp ;
  1152. CmpArg4W0 ;
  1153. Jne8 ("wb_t") ;
  1154. MovDxVx (D_FALSE) ;
  1155. J8 ("wb_o") ;
  1156. M ("wb_t") ;
  1157. MovDxVx (D_TRUE) ;
  1158. M ("wb_o") ;
  1159. MovAh (09H) ;
  1160. Int21 ;
  1161. PopBp ;
  1162. RetR
  1163. END EmitWrBool ;
  1164. PROCEDURE EmitWrLn ;
  1165. BEGIN
  1166. M ("wrln") ;
  1167. MovDxVx (D_CRLF) ;
  1168. MovAh (09H) ;
  1169. Int21 ;
  1170. RetR
  1171. END EmitWrLn ;
  1172. PROCEDURE EmitWrReal ;
  1173. (* the 6-byte real is on the stack but is not formatted: the compiler does
  1174. not yet load real operands into a form the runtime could read. A visible
  1175. marker beats printing the mantissa as an integer. *)
  1176. BEGIN
  1177. M ("wrreal") ;
  1178. MovDxVx (D_REAL) ;
  1179. MovAh (09H) ;
  1180. Int21 ;
  1181. RetR
  1182. END EmitWrReal ;
  1183. PROCEDURE EmitRdInt ;
  1184. (* Read a number into a 16-bit variable: the caller's address is on the
  1185. stack. The scan itself is EmitRdScan, shared with rdbyte; an entry owns
  1186. its frame, its address load and its store, nothing else. *)
  1187. BEGIN
  1188. M ("rdint") ;
  1189. PushBp ; MovBpSp ;
  1190. PushAx ; PushBx ; PushCx ; PushDx ; PushDi ;
  1191. C8 ("rdscan") ;
  1192. CmpCxV (2) ;
  1193. Je8 ("ri_skip") ; (* nothing entered: leave the variable
  1194. alone, as TP3's xrdint at rdierr does *)
  1195. MovDiBp4 ; (* ...the caller's address into DI... *)
  1196. StDiAx ; (* ...and store the word. MovDiBp4 and
  1197. not a [BX] form: there is none; see
  1198. the note on the store emitters. *)
  1199. M ("ri_skip") ;
  1200. PopDi ; PopDx ; PopCx ; PopBx ; PopAx ;
  1201. MovSpBp ; PopBp ; RetR
  1202. END EmitRdInt ;
  1203. PROCEDURE EmitRdScan ;
  1204. (* The number reader, called by rdint (word store) and by rdbyte (byte
  1205. store). A transcription of TP3 TPSRC4 xrdint/readnum, minus the store:
  1206. rnspace: getbyte ; ^Z ? -> rnend
  1207. consume ; <= 20h ? -> rnspace
  1208. rndig: [buf]=char ; getbyte ; <= 20h ? -> rnend (NOT consumed)
  1209. consume ; -> rndig
  1210. rnend: buf[0] := 0
  1211. It builds NO frame of its own and touches neither BP nor SP, so it is
  1212. called from the caller's frame and that frame's [BP+4] is still the
  1213. caller's argument when it returns. It clobbers AX BX CX DX DI, which the
  1214. caller has already saved.
  1215. Returns AX = the value, and CX = 0 positive, 1 negative, 2 nothing read.
  1216. CX = 2 is set HERE, on the ^Z path: it is the state both callers test to
  1217. decide not to store, and before this split the check existed only as a
  1218. jump to the exit, so the state itself was never produced. AX is
  1219. meaningless when CX = 2.
  1220. Two behaviours are load-bearing and neither is obvious:
  1221. - the character that ENDS the scan is left pending, because readnum tests
  1222. it *before* clearing getbyte's pre-read flag. xreadln, which the
  1223. compiler emits next for a readln, is what consumes the newline. See the
  1224. note on getch.
  1225. - ^Z before any digit returns without a value, and the caller then returns
  1226. WITHOUT touching the variable: TP3's xrdint reaches rdierr and RETs.
  1227. Sign in CX, value in DI, value in AX on the way out. *)
  1228. BEGIN
  1229. M ("rdscan") ;
  1230. M ("ri_lead") ;
  1231. C8 ("getch") ;
  1232. CmpAl (1AH) ; (* ^Z: end of input *)
  1233. Je8 ("ri_none") ;
  1234. CmpAl (20H) ; (* every control char and the space.
  1235. 20H, not 20: readnum skips
  1236. everything <= #$20, and a literal
  1237. that READS like hex but is
  1238. decimal compares AL against 14h,
  1239. so a leading space is never
  1240. skipped and the scan ends with
  1241. nothing read. Every other
  1242. literal in this module is
  1243. explicit for the same reason. *)
  1244. Jbe8 ("ri_lead") ;
  1245. XorCxCx ;
  1246. CmpAl (ORD ("-")) ;
  1247. Jne8 ("ri_nos") ;
  1248. IncCx ;
  1249. C8 ("getch") ;
  1250. J8 ("ri_dig0") ;
  1251. M ("ri_nos") ;
  1252. CmpAl (ORD ("+")) ;
  1253. Jne8 ("ri_dig0") ;
  1254. C8 ("getch") ;
  1255. M ("ri_dig0") ;
  1256. XorDiDi ;
  1257. M ("ri_dig") ;
  1258. CmpAl (ORD ("0")) ;
  1259. Jb8 ("ri_done") ;
  1260. CmpAl (ORD ("9")) ;
  1261. Ja8 ("ri_done") ;
  1262. SubAl (ORD ("0")) ;
  1263. MovAh (0) ; (* AX := the digit, 0..9 *)
  1264. XchgAxDi ; (* AX := the value so far, DI := the digit.
  1265. The digit has to survive MUL, and the 8-bit
  1266. reg field of 8A/88 has only AL/CL/DL/BL/AH/
  1267. CH/DH/BH - there is no SI or DI byte to park
  1268. it in. MUL r/m16 writes DX, so the previous
  1269. version's "keep the digit across the multiply"
  1270. comment was describing a register the multiply
  1271. owns. Every read of an integer therefore
  1272. returned 0: the digit was computed, moved to
  1273. DH, and multiplied away one instruction later. *)
  1274. MovBxImm (10) ;
  1275. MulBx ; (* DX:AX := value * 10 *)
  1276. AddAxDi ; (* AX := low word + digit. A carry out of bit 15
  1277. is dropped, which is what a 16-bit INTEGER
  1278. does anyway - the high word of the product is
  1279. never stored. *)
  1280. MovDiAx ;
  1281. C8 ("getch") ;
  1282. J8 ("ri_dig") ;
  1283. M ("ri_done") ;
  1284. C8 ("ungetch") ; (* the delimiter stays pending *)
  1285. CmpCx0 ;
  1286. Je8 ("ri_pos") ;
  1287. NegDi ;
  1288. M ("ri_pos") ;
  1289. MovAxDi ; (* the parsed value out of DI and into AX,
  1290. where both callers store it from *)
  1291. RetR ;
  1292. M ("ri_none") ; (* ^Z first: no value, and CX says so *)
  1293. MovCxImm (2) ;
  1294. RetR
  1295. END EmitRdScan ;
  1296. PROCEDURE EmitRdByte ;
  1297. (* Read a number into a BYTE variable: the same scan, a one-byte store.
  1298. TPSRC8 prdnum dispatches on the variable's size - `CMP varsize,#$01 /
  1299. JA prdemit' keeps xrdint for anything wider and falls through to
  1300. `MOV AX,#xrdbyte' for a one-byte variable - and TPSRC4's two entries
  1301. differ in exactly one store: xrdbyte is `CLC', xrdint is `STC', and rdi1
  1302. stores AX or AL on `JNB rdibyt'.
  1303. The flag form is not transcribed. It would put the STC at the head of the
  1304. rdint entry, and the BP contract in tests/rt_exec.py requires an entry
  1305. that borrows BP to *start* with `55 8B EC' and to hand BP back from inside
  1306. its own slice - which is what an entry owning its own frame does. Two
  1307. frames and two stores around one scan is the same idea, with that
  1308. constraint respected.
  1309. The store is the whole reason this entry exists: rdint writes two bytes,
  1310. and the second one is the byte that FOLLOWS the variable, so
  1311. `readln (b)' was reading into b and into whatever the compiler had put
  1312. next to it. *)
  1313. BEGIN
  1314. M ("rdbyte") ;
  1315. PushBp ; MovBpSp ;
  1316. PushAx ; PushBx ; PushCx ; PushDx ; PushDi ;
  1317. C8 ("rdscan") ;
  1318. CmpCxV (2) ;
  1319. Je8 ("rby_skip") ; (* nothing entered: leave it alone *)
  1320. MovDiBp4 ; (* the caller's address into DI *)
  1321. StDiAl ; (* ONE byte - 88 05, AL not AX *)
  1322. M ("rby_skip") ;
  1323. PopDi ; PopDx ; PopCx ; PopBx ; PopAx ;
  1324. MovSpBp ; PopBp ; RetR
  1325. END EmitRdByte ;
  1326. PROCEDURE EmitRdChar ;
  1327. BEGIN
  1328. M ("rdchar") ;
  1329. PushBp ; MovBpSp ;
  1330. PushAx ; PushBx ; PushDi ;
  1331. C8 ("getch") ;
  1332. MovDlAl ;
  1333. MovDiBp4 ; (* no [BX] form: the address goes in DI *)
  1334. StDiDl ;
  1335. PopDi ; PopBx ; PopAx ;
  1336. MovSpBp ; PopBp ; RetR
  1337. END EmitRdChar ;
  1338. PROCEDURE EmitRdBool ;
  1339. (* one character, classified the way TP3 does: T/t/Y/y/1 true, anything else
  1340. false. *)
  1341. BEGIN
  1342. M ("rdbool") ;
  1343. PushBp ; MovBpSp ;
  1344. PushAx ; PushBx ; PushCx ; PushDi ;
  1345. C8 ("getch") ;
  1346. XorCxCx ;
  1347. CmpAl (ORD ("T")) ; Je8 ("rb_t") ;
  1348. CmpAl (ORD ("t")) ; Je8 ("rb_t") ;
  1349. CmpAl (ORD ("Y")) ; Je8 ("rb_t") ;
  1350. CmpAl (ORD ("y")) ; Je8 ("rb_t") ;
  1351. CmpAl (ORD ("1")) ; Je8 ("rb_t") ;
  1352. J8 ("rb_s") ;
  1353. M ("rb_t") ;
  1354. IncCx ;
  1355. M ("rb_s") ;
  1356. MovDiBp4 ; (* DI = the caller's address, because there is
  1357. no [BX] to store through *)
  1358. StDiCx ;
  1359. PopDi ; PopCx ; PopBx ; PopAx ;
  1360. MovSpBp ; PopBp ; RetR
  1361. END EmitRdBool ;
  1362. PROCEDURE EmitRdLn ;
  1363. (* discard the rest of the line, including the terminator *)
  1364. BEGIN
  1365. M ("rdln") ;
  1366. PushAx ;
  1367. M ("rl_loop") ;
  1368. C8 ("getch") ;
  1369. CmpAl (0DH) ; Je8 ("rl_e") ;
  1370. CmpAl (0AH) ; Je8 ("rl_e") ;
  1371. CmpAl (1AH) ; Je8 ("rl_e") ; (* ^Z: end of input *)
  1372. J8 ("rl_loop") ;
  1373. M ("rl_e") ;
  1374. PopAx ;
  1375. RetR
  1376. END EmitRdLn ;
  1377. PROCEDURE EmitRdStr ;
  1378. (* readln(s)/read(s) of a string variable: TPSRC4's xrdstr, the runtime entry
  1379. behind both prdstr and prdrdln. The caller pushes the variable's address
  1380. (one stack word, read here via BP), sets CX = max_len = varsize-1, and pops
  1381. the word after the call.
  1382. Keyed to ours rather than TP3's exactly: TP3 emits MOV CL,imm8 and clears
  1383. CH the length load; this runtime has no byte-register move into CH/CL, so
  1384. the compiler emits a full MOV CX,imm16 and CX *is* the limit (Loop8 / the
  1385. count arithmetic below rely on it).
  1386. Characters come from getch, which clobbers only AX and BX, so the running
  1387. count lives in BX behind a push/pop around each call. Reading stops at CR,
  1388. LF or ^Z - LF because our input files are LF-terminated - and the terminator
  1389. is left PENDING via ungetch, exactly as TP3's xrdstr leaves its CR pending
  1390. through the pre-read flag: the readln wrapper then consumes the newline,
  1391. while a bare read(s) leaves it for the next read. The LOOP truncates a line
  1392. longer than max_len. dest[0] := length; dest[1..] := chars. *)
  1393. BEGIN
  1394. M ("rdstr") ;
  1395. PushBp ; MovBpSp ;
  1396. PushAx ; PushBx ; PushCx ; PushDx ; PushDi ;
  1397. MovDiBp4 ; (* DI := dest base, for the length byte *)
  1398. MovAxBp4 ; MovSiAx ; (* SI := dest, walking *)
  1399. IncSi ; (* SI := dest + 1: chars live at dest[1..], the
  1400. length byte at dest[0] is stored at the end *)
  1401. XorBxBx ; (* BX := 0: count of characters read *)
  1402. M ("rs_loop") ;
  1403. PushBx ; (* getch clobbers BX (and AX) *)
  1404. C8 ("getch") ;
  1405. PopBx ;
  1406. CmpAl (0DH) ; Je8 ("rs_term") ; (* CR *)
  1407. CmpAl (0AH) ; Je8 ("rs_term") ; (* LF *)
  1408. CmpAl (1AH) ; Je8 ("rs_term") ; (* ^Z *)
  1409. MovDlAl ; (* DL := char *)
  1410. IncBx ; (* count := count + 1 *)
  1411. StSiDl ; (* dest[i] := char *)
  1412. IncSi ;
  1413. Loop8 ("rs_loop") ; (* CX--; loop while CX # 0: truncation *)
  1414. M ("rs_end") ;
  1415. MovAlBl ; (* AL := count *)
  1416. StDiAl ; (* dest[0] := length *)
  1417. PopDi ; PopDx ; PopCx ; PopBx ; PopAx ;
  1418. MovSpBp ; PopBp ; RetR ;
  1419. M ("rs_term") ;
  1420. PushBx ; (* ungetch clobbers BX too *)
  1421. C8 ("ungetch") ;
  1422. PopBx ;
  1423. J8 ("rs_end")
  1424. END EmitRdStr ;
  1425. PROCEDURE EmitStrStore ;
  1426. (* s := t for string variables: TPSRC2's strstore with TPSRC8 asnvar's stack
  1427. shape. The caller pushes BOTH addresses - destination first, source second,
  1428. so the LAST push (dest) sits at [BP+4] and the source at [BP+6] - sets
  1429. CX = max_len = varsize-1 of the DESTINATION, calls, and pops both words
  1430. (this runtime is caller-pops, unlike TP3's strstore which pops itself).
  1431. TP3's strstore rewrites the length byte of the stack copy of the source to
  1432. min(src[0], max), then copies length byte + chars, truncating at max, with
  1433. the destination TAIL past the new length left untouched. Here, with both
  1434. variables in memory and no stack copy, that becomes: count := min(src[0],
  1435. max); dest[0] := count; copy count chars src[1..] -> dest[1..]. The source
  1436. variable is never written - truncation is expressed in the destination's own
  1437. length byte only. A source longer than max is truncated; a shorter one is
  1438. copied whole (CX := src length, count including the empty case where LOOP
  1439. simply never runs). *)
  1440. BEGIN
  1441. M ("strstore") ;
  1442. PushBp ; MovBpSp ;
  1443. PushAx ; PushBx ; PushCx ; PushDx ;
  1444. MovAxBp4 ; MovSiAx ; (* SI := dest, walking *)
  1445. MovBxBp6 ; (* BX := source *)
  1446. XorDxDx ;
  1447. LdAlBx ; MovDlAl ; (* DL := src[0], the source length *)
  1448. IncBx ; (* BX -> first source character *)
  1449. CmpCxDx ; (* CMP CX (max), DX (src length) *)
  1450. Jb8 ("ss_trunc") ;
  1451. MovCxDx ; (* not truncated: CX := src length *)
  1452. J8 ("ss_len") ;
  1453. M ("ss_trunc") ;
  1454. MovDlCl ; (* truncated: DL := max, for the length store *)
  1455. M ("ss_len") ;
  1456. StSiDl ; (* dest[0] := length *)
  1457. IncSi ; (* dest -> first character *)
  1458. Jcxz8 ("ss_done") ; (* an empty source copies nothing; LOOP with
  1459. CX = 0 would run 65536 times, not zero *)
  1460. M ("ss_loop") ;
  1461. LdAlBx ; (* AL := src char *)
  1462. MovDlAl ; (* DL := char *)
  1463. StSiDl ; (* dest char := src char *)
  1464. IncBx ; IncSi ;
  1465. Loop8 ("ss_loop") ; (* CX--; loop while CX # 0 *)
  1466. M ("ss_done") ;
  1467. PopDx ; PopCx ; PopBx ; PopAx ;
  1468. MovSpBp ; PopBp ; RetR
  1469. END EmitStrStore ;
  1470. PROCEDURE EmitCmpStr ;
  1471. (* cmpstr: lexicographic comparison of two strings, TPSRC2's cmpstr with the
  1472. addresses taken as arguments rather than the strings pushed by value.
  1473. The caller pushes BOTH addresses - the FIRST (left) operand first, so it
  1474. sits at [BP+6], the second (right) at [BP+4], the same order strstore takes
  1475. its two - calls, and pops both words (caller-pops, unlike TP3's cmpstr,
  1476. which pops itself). It returns AX = -1, 0 or +1 for left < right, left =
  1477. right, left > right.
  1478. Why AX and not the flags, when TP3's cmpstr leaves them set? Because TP3's
  1479. cmpstr also REMOVES its own arguments (`MOV SP,BX', TPSRC2 960), so its six
  1480. wrappers (csteq/cstne/cstge/cstle/cstg/cstl, TPSRC2 890-925) can branch on
  1481. the flags immediately after the CALL. Here the caller pushes the two
  1482. addresses, and dropping them with ADD SP,4 - the one-instruction way - is
  1483. in the CPU's arithmetic group and DESTROYS the flags. (POP does not, but
  1484. then the flags would have to survive the caller's next emission, which is
  1485. exactly the invisible coupling the notes in this file keep warning about.)
  1486. A signed value in AX costs a few instructions inside the entry and keeps the
  1487. flag convention the compiler's own business, where the six-arm table already
  1488. lives: the caller runs OR AX,AX and the same EmSetcc it runs after every
  1489. numeric comparison, so the two paths cannot drift apart.
  1490. That tail is not invented. A TP3 wrapper is `MOV AX,#1 / Jcc / DEC AX /
  1491. OR AX,AX' - the same EmSetcc our numeric arm emits, followed by the same
  1492. OR AX,AX - and the only thing this design moves is WHERE it happens: out of
  1493. six one-per-operator runtime entries and into the one shared table. TP3
  1494. needs the six because its cmpstr is reached through vcompare's vector table
  1495. (TPSRC9 133) by type; we already have the operator as a number.
  1496. The comparison is TPSRC2's, translated to an explicit byte loop because
  1497. this runtime has no REPZ CMPS: min(lenL,lenR) bytes are compared in order,
  1498. the first difference decides, and if the common prefix is equal the LENGTHS
  1499. decide (a prefix is less than the string that continues it). The byte
  1500. compare is unsigned - CF is the borrow out of AL-DL - which is what
  1501. "lexicographic" means for characters. The tiebreak re-reads the lengths
  1502. from the arguments rather than keeping them in registers across the loop,
  1503. so CX is free to be LOOP's count, and the empty string is the case where
  1504. that count is 0 and LOOP would run 65536 times instead of none. *)
  1505. BEGIN
  1506. M ("cmpstr") ;
  1507. PushBp ; MovBpSp ;
  1508. PushBx ; PushCx ; PushDx ; (* AX is NOT saved: it is the result *)
  1509. XorCxCx ; (* CX := 0, to make the CL load below a length *)
  1510. XorDxDx ;
  1511. MovBxBp6 ; (* BX := the first (left) string's address *)
  1512. MovClBx ; (* CL := lenL *)
  1513. MovAxBp4 ; MovSiAx ; (* SI := the second (right) string's address *)
  1514. MovDlSi ; (* DL := lenR *)
  1515. CmpCxDx ; (* CMP lenL, lenR *)
  1516. Jbe8 ("cs_min") ; (* lenL <= lenR: CX is already the shorter *)
  1517. MovCxDx ; (* CX := lenR *)
  1518. M ("cs_min") ; (* CX = min (lenL,lenR) *)
  1519. Jcxz8 ("cs_len") ; (* an empty operand: no bytes to compare *)
  1520. IncBx ; IncSi ; (* past the length byte, to the characters *)
  1521. M ("cs_loop") ;
  1522. LdAlBx ; (* AL := left[i] *)
  1523. MovDlSi ; (* DL := right[i] *)
  1524. CmpAlDl ; (* CMP AL,DL - unsigned, so CF is the order *)
  1525. Jne8 ("cs_flag") ; (* the first difference decides *)
  1526. IncBx ; IncSi ;
  1527. Loop8 ("cs_loop") ;
  1528. M ("cs_len") ; (* equal over the common prefix: lengths decide *)
  1529. MovBxBp6 ; MovClBx ; (* CX := lenL again - BX walked past it *)
  1530. MovAxBp4 ; MovSiAx ;
  1531. XorDxDx ; MovDlSi ; (* DX := lenR *)
  1532. CmpCxDx ;
  1533. M ("cs_flag") ; (* flags are (left - right): CF=1 less, ZF=1 equal *)
  1534. MovAlD (0) ; MovAh (0) ; (* AX := 0. MOV leaves the flags alone, which
  1535. is what lets the two tests below still read
  1536. the comparison. *)
  1537. Jb8 ("cs_less") ;
  1538. Je8 ("cs_done") ;
  1539. MovAlD (1) ; (* greater *)
  1540. J8 ("cs_done") ;
  1541. M ("cs_less") ;
  1542. MovAlD (0FFH) ; MovAh (0FFH) ; (* AX := -1 *)
  1543. M ("cs_done") ;
  1544. PopDx ; PopCx ; PopBx ; (* but NOT AX: AX is the RESULT. The first
  1545. version pushed AX with the others and
  1546. popped it back here, which restored the
  1547. CALLER's AX - for `a < b' that is the
  1548. address of the RIGHT operand, still in AX
  1549. from its LEA - and every ordering test read
  1550. a positive number. `<', `<=', `=', `<>',
  1551. `>', `>=' over any left<right string came
  1552. back FALSE TRUE FALSE FALSE TRUE TRUE,
  1553. independently confirmed on qemu. Saving AX
  1554. at all is wrong: there is nothing to keep. *)
  1555. MovSpBp ; PopBp ; RetR
  1556. END EmitCmpStr ;
  1557. PROCEDURE EmitStrCat ;
  1558. (* strcat: dest := dest + src, TPSRC2's xconcat (963) with the two strings
  1559. taken as ADDRESSES rather than pushed by value.
  1560. The caller pushes the SOURCE first and the DESTINATION last, sets CX = the
  1561. destination's capacity in characters, calls, and pops both words (caller
  1562. pops). That is exactly strstore's argument shape, so the compiler's two
  1563. emitted calls for `a + b' differ only in which entry they name - see
  1564. Compiler.StrConcat.
  1565. TP3's xconcat adds the two length bytes (`ADD.B DL,CL') and takes the CARRY
  1566. as "string too long" (`JB concerr'), then concerr loads runerr's code 16;
  1567. the copy itself is the same append this does. So an overflowing
  1568. concatenation is a RUNTIME ERROR, not a silent truncation: if dest[0] +
  1569. src[0] exceeds CX the process leaves through INT 21h AH=4Ch with exit code
  1570. 16 (10H), which is where TP3's error 16 ends up. (strstore truncates
  1571. instead, which is also what TP3 does there - the two entries deliberately
  1572. do not agree, because TP3's two routines do not.)
  1573. AX, BX, CX, DX, SI and DI are all preserved: the compiler may hold a string
  1574. element's address in DI across the call that pushes the operand AFTER this
  1575. one (see Compiler.EmPushStrOperand), so strcat cannot be the entry that
  1576. quietly clobbers it. *)
  1577. BEGIN
  1578. M ("strcat") ;
  1579. PushBp ; MovBpSp ;
  1580. PushAx ; PushBx ; PushCx ; PushDx ; PushSi ; PushDi ;
  1581. MovAxBp4 ; MovSiAx ; (* SI := dest *)
  1582. MovBxBp6 ; (* BX := src *)
  1583. XorDxDx ;
  1584. MovDlSi ; (* DL := dest[0], the destination length *)
  1585. XorAxAx ;
  1586. LdAlBx ; (* AL := src[0]; AX is now the source length *)
  1587. AddDxAx ; (* DX := dest length + source length *)
  1588. CmpCxDx ; (* CMP CX (max), DX (total) *)
  1589. Jae8 ("sc_ok") ; (* max >= total: it fits *)
  1590. MovAlD (10H) ; (* 10H = 16: TP3's `String too long' error code *)
  1591. MovAh (4CH) ; (* AH = 4Ch: terminate *)
  1592. Int21 ; (* leaving now; the caller's stack is abandoned *)
  1593. M ("sc_ok") ;
  1594. MovCxDx ; (* CX := the new length *)
  1595. StSiCl ; (* dest[0] := new length (CL) *)
  1596. SubDxAx ; (* DX := total - source length = old dest length *)
  1597. MovDiSi ; (* DI := dest *)
  1598. IncDi ; (* -> first character *)
  1599. AddDiDx ; (* -> the append position, dest+1+old length *)
  1600. IncBx ; (* BX -> the source's first character *)
  1601. MovCxAx ; (* CX := source length, LOOP's count *)
  1602. Jcxz8 ("sc_done") ; (* an empty source appends nothing *)
  1603. M ("sc_loop") ;
  1604. LdAlBx ; (* AL := src char *)
  1605. StDiAl ; (* dest char := AL *)
  1606. IncBx ; IncDi ;
  1607. Loop8 ("sc_loop") ;
  1608. M ("sc_done") ;
  1609. PopDi ; PopSi ; PopDx ; PopCx ; PopBx ; PopAx ;
  1610. MovSpBp ; PopBp ; RetR
  1611. END EmitStrCat ;
  1612. PROCEDURE Str5 (s : ARRAY OF CHAR ; n : CARDINAL) ;
  1613. (* emit n characters, so the strings below are visibly the same length as the
  1614. D_ constants claim and an off-by-one in either place is a compile-time
  1615. mismatch rather than a wrong pointer nobody notices *)
  1616. VAR i : CARDINAL ;
  1617. BEGIN
  1618. i := 0 ;
  1619. WHILE i < n DO
  1620. B (ORD (s [i])) ;
  1621. INC (i)
  1622. END
  1623. END Str5 ;
  1624. PROCEDURE EmitRealFromInt ;
  1625. (* intreal (TPSRC3 130): an INTEGER -> the 6-byte MBF real.
  1626. ABI: the source word's ADDRESS is at [BP+6] and the destination's at
  1627. [BP+4]; the caller pushes the source first, so [BP+6] is the source. (The
  1628. reference keeps the real in AX/BX/DX, where AL is the exponent byte, AH
  1629. and BX the low 39 mantissa bits and DX the high 16 with the sign in DH's
  1630. bit 7. Packing is therefore just the two stores.)
  1631. The arithmetic is the reference's, instruction for instruction:
  1632. zero -> all six bytes zero
  1633. sign -> BH, DX := |value|
  1634. AX := 0090h (exponent 144, clear mantissa)
  1635. small value: exponent 88h and shift the mantissa 8
  1636. normalise: shift DX left and decrement AL until DX's sign bit is set
  1637. positive: clear DH bit 7; negative: leave it as the sign
  1638. clear the rest of the mantissa (BX)
  1639. The sign convention here is the packed one: bit 7 of the last byte is the
  1640. sign, and the implicit 1 is not stored. That is why intreal leaves DX's
  1641. top bit alone for negatives (it becomes the sign) and clears it for
  1642. positives. *)
  1643. BEGIN
  1644. M ("real_from_int") ;
  1645. PushBp ; MovBpSp ;
  1646. PushBx ; PushDx ; PushSi ; PushDi ;
  1647. MovSiBp6 ; (* SI := source word *)
  1648. LdAxSi ; (* AX := the integer *)
  1649. Call ("rf_core") ;
  1650. M ("rf_store") ;
  1651. MovDiBp4 ; (* DI := the destination *)
  1652. StDiAx ; (* bytes 0..1 *)
  1653. StDiOffBx (2) ; (* bytes 2..3 *)
  1654. StDiOffDx (4) ; (* bytes 4..5, sign in DH bit 7 *)
  1655. PopDi ; PopSi ; PopDx ; PopBx ;
  1656. MovSpBp ; PopBp ; RetR ;
  1657. (* ---- the callable integer -> real core: AX in, AX/BX/DX out ---- *)
  1658. M ("rf_core") ;
  1659. CmpAx0 ;
  1660. Jne8 ("rf_nz") ;
  1661. XorBxBx ;
  1662. XorDxDx ;
  1663. Jmp8 ("rf_core_ret") ;
  1664. M ("rf_nz") ;
  1665. MovBhAh ; (* BH := the integer's sign byte *)
  1666. MovDxAx ;
  1667. CmpDx0 ;
  1668. Jns8 ("rf_pos") ;
  1669. NegDx ;
  1670. M ("rf_pos") ;
  1671. MovAxImm (0090H) ; (* AL = exponent 144, AH = 0 *)
  1672. CmpDh0 ;
  1673. Jne8 ("rf_16") ;
  1674. MovAlD (088H) ; (* 16-bit value in the top byte: bias 88 *)
  1675. XchgDlDh ;
  1676. M ("rf_16") ;
  1677. CmpDx0 ;
  1678. Js8 ("rf_done") ;
  1679. M ("rf_nrm") ;
  1680. DecAl ;
  1681. ShlDx1 ;
  1682. Jns8 ("rf_nrm") ;
  1683. M ("rf_done") ;
  1684. CmpBh0 ;
  1685. Js8 ("rf_pos2") ;
  1686. AndDhImm (7FH) ; (* positive: bit 7 of DH is not a sign *)
  1687. M ("rf_pos2") ;
  1688. XorBxBx ; (* only DX's 16 bits carry the value now *)
  1689. M ("rf_core_ret") ;
  1690. RetR
  1691. END EmitRealFromInt ;
  1692. PROCEDURE EmitRealAddSub ;
  1693. (* readd/resub, TPSRC2 1421-1530, transcribed.
  1694. ABI: [BP+4] is the address of a 12-byte block holding a (6 bytes) then b
  1695. (6 bytes); the result overwrites a. Both operands are read before the
  1696. result is written, so the in-place destination is safe. The reference
  1697. keeps a in AX/BX/DX and b in CX/SI/DI, with AL the exponent, AH+BX the low
  1698. mantissa and DX the high 16 carrying the sign in DH bit 7. The scratch
  1699. bytes are the reference's own (resign/resave, remant/remant1, cvdecexp).
  1700. The sign of b is toggled for subtraction with XOR DI,resign, exactly as
  1701. resub does, and the rest is one shared core. An exponent overflow leaves
  1702. through the reference's STC + RET; no caller consumes that yet. *)
  1703. BEGIN
  1704. M ("real_add") ;
  1705. PushBp ; MovBpSp ;
  1706. PushBx ; PushDx ; PushSi ; PushDi ;
  1707. PushBp4 ; (* keep the block address for the store *)
  1708. MovSiBp4 ;
  1709. LdAxSi ;
  1710. LdCxSiOff (2) ; LdDxSiOff (4) ;
  1711. MovBxCx ;
  1712. MovDiSi ;
  1713. LdCxDiOff (6) ; LdSiDiOff (8) ; LdDiDiOff (10) ;
  1714. StDataImm (D_RSIGN, 0) ;
  1715. Call ("rd_core") ;
  1716. PopDi ;
  1717. StDiAx ; StDiOffBx (2) ; StDiOffDx (4) ;
  1718. PopDi ; PopSi ; PopDx ; PopBx ;
  1719. MovSpBp ; PopBp ; RetR ;
  1720. M ("real_sub") ;
  1721. PushBp ; MovBpSp ;
  1722. PushBx ; PushDx ; PushSi ; PushDi ;
  1723. PushBp4 ;
  1724. MovSiBp4 ;
  1725. LdAxSi ;
  1726. LdCxSiOff (2) ; LdDxSiOff (4) ;
  1727. MovBxCx ;
  1728. MovDiSi ;
  1729. LdCxDiOff (6) ; LdSiDiOff (8) ; LdDiDiOff (10) ;
  1730. StDataImm (D_RSIGN, 8000H) ;
  1731. Call ("rd_core") ;
  1732. PopDi ;
  1733. StDiAx ; StDiOffBx (2) ; StDiOffDx (4) ;
  1734. PopDi ; PopSi ; PopDx ; PopBx ;
  1735. MovSpBp ; PopBp ; RetR ;
  1736. M ("rd_core") ;
  1737. CmpCl0 ;
  1738. Je8 ("raret") ;
  1739. XorDiData (D_RSIGN) ;
  1740. CmpAl0 ;
  1741. Jne8 ("ranotriv") ;
  1742. M ("raretn2") ;
  1743. MovAxCx ; MovBxSi ; MovDxDi ;
  1744. M ("raret") ;
  1745. RetR ;
  1746. M ("ranotriv") ;
  1747. CmpAlCl ;
  1748. Jbe8 ("ranoswap") ;
  1749. XchgAxCx ; XchgBxSi ; XchgDxDi ;
  1750. M ("ranoswap") ;
  1751. StDataCl (D_CVDEC) ;
  1752. SubClAl ;
  1753. CmpClImm (28H) ;
  1754. Jb8 ("ranoundr") ;
  1755. LdClData (D_CVDEC) ;
  1756. Jmp8 ("raretn2") ;
  1757. M ("ranoundr") ;
  1758. StDataDi (D_RSIGN) ; (* resign; D_RESAVE is its high byte *)
  1759. AndDataImm (D_RESAVE, 80H) ;
  1760. StDataDi (D_RMANT) ;
  1761. XorDataDh (D_RMANT1) ; (* sign1 XOR sign2 in the high byte *)
  1762. OrDiImm (8000H) ; (* b's hidden 1 *)
  1763. OrDhImm (80H) ; (* a's hidden 1 *)
  1764. M ("raadj16l") ;
  1765. CmpClImm (10H) ;
  1766. Jb8 ("raadj8") ;
  1767. MovAhBh ; MovBxDx ; XorDxDx ; SubClImm (10H) ;
  1768. Jmp8 ("raadj16l") ;
  1769. M ("raadj8") ;
  1770. CmpClImm (08H) ;
  1771. Jb8 ("raadj8l") ;
  1772. MovAhBl ; MovBlBh ; MovBhDl ; MovDlDh ; XorDhDh ; SubClImm (08H) ;
  1773. M ("raadj8l") ;
  1774. CmpCl0 ;
  1775. Je8 ("raadjend") ;
  1776. M ("raadj1l") ;
  1777. ShrDx1 ; RcrBx1 ; RcrAh1 ; DecCl ; Jne8 ("raadj1l") ;
  1778. M ("raadjend") ;
  1779. LdAlData (D_CVDEC) ;
  1780. TestDataImm (D_RMANT1, 80H) ;
  1781. Jne8 ("radosub") ;
  1782. AddAhCh ; AdcBxSi ; AdcDxDi ;
  1783. Jae8 ("rasign") ;
  1784. RcrDx1 ; RcrBx1 ; RcrAh1 ; IncAl ; Jne8 ("rasign") ;
  1785. Stc ; RetR ;
  1786. M ("radosub") ;
  1787. XchgAhCh ; XchgBxSi ; XchgDxDi ;
  1788. SubAhCh ; SbbBxSi ; SbbDxDi ;
  1789. Jae8 ("ranoneg") ;
  1790. XorDataImm (D_RESAVE, 80H) ;
  1791. NotAh ; NotBx ; NotDx ;
  1792. AddAhImm (1) ; AdcBxImm (0) ; AdcDxImm (0) ;
  1793. M ("ranoneg") ;
  1794. MovClImm (5) ;
  1795. M ("ranrm8l") ;
  1796. CmpDh0 ;
  1797. Jne8 ("ranrm1l") ;
  1798. MovDhDl ; MovDlBh ; MovBhBl ; MovBlAh ; XorAhAh ;
  1799. SubAl (08H) ;
  1800. Jb8 ("razero") ;
  1801. DecCl ;
  1802. Jne8 ("ranrm8l") ;
  1803. Jmp8 ("razero") ;
  1804. M ("ranrm1l") ;
  1805. TestDhImm (80H) ;
  1806. Jne8 ("rasign") ;
  1807. ShlAh1 ; RclBx1 ; RclDx1 ; DecAl ; Jne8 ("ranrm1l") ;
  1808. M ("razero") ;
  1809. XorAxAx ; XorBxBx ; XorDxDx ;
  1810. RetR ;
  1811. M ("rasign") ;
  1812. AndDhImm (7FH) ;
  1813. XorDhData (D_RESAVE) ;
  1814. RetR
  1815. END EmitRealAddSub ;
  1816. PROCEDURE EmitRealMul ;
  1817. (* remult, TPSRC2 1532-1587, with testexp (TPSRC3 50-78) inlined.
  1818. ABI and register convention are real_add's: [BP+4] is a 12-byte block
  1819. holding a then b; the result overwrites a; a is AX/BX/DX and b CX/SI/DI.
  1820. testexp is inlined because the reference's version is called and, on
  1821. underflow or overflow, POPS ITS RETURN ADDRESS to leave remult entirely.
  1822. That trick cannot survive being called from our wrapper, so the two
  1823. exponent fault paths jump to this core's zero/error exits instead.
  1824. Multiplication is the reference's shift-and-add over the five mantissa
  1825. bytes: for each byte, if it is zero the 40-bit result is shifted right
  1826. eight; otherwise each of its eight bits conditionally adds a's mantissa
  1827. and shifts the result one. LAHF/SAHF carry the guard bit across the
  1828. normalisation TEST. Overflow leaves through TP3's error 1 (exit code 1);
  1829. underflow returns zero. *)
  1830. BEGIN
  1831. M ("real_mul") ;
  1832. PushBp ; MovBpSp ;
  1833. PushBx ; PushDx ; PushSi ; PushDi ;
  1834. PushBp4 ;
  1835. MovSiBp4 ;
  1836. LdAxSi ;
  1837. LdCxSiOff (2) ; LdDxSiOff (4) ;
  1838. MovBxCx ;
  1839. MovDiSi ;
  1840. LdCxDiOff (6) ; LdSiDiOff (8) ; LdDiDiOff (10) ;
  1841. Call ("rm_core") ;
  1842. PopDi ;
  1843. StDiAx ; StDiOffBx (2) ; StDiOffDx (4) ;
  1844. PopDi ; PopSi ; PopDx ; PopBx ;
  1845. MovSpBp ; PopBp ; RetR ;
  1846. M ("rm_core") ;
  1847. CmpCl0 ; JccNear (74H, "rm_zero") ;
  1848. CmpAl0 ; JccNear (74H, "rm_ret") ;
  1849. AddAlCl ;
  1850. Jb8 ("rm_ovr1") ;
  1851. AddAlImm (80H) ;
  1852. Jb8 ("rm_ok") ;
  1853. JmpNear ("rm_zero") ; (* underflow *)
  1854. M ("rm_ovr1") ;
  1855. AddAlImm (80H) ;
  1856. Jae8 ("rm_ok") ;
  1857. (* overflow: TP3 error 1 *)
  1858. MovAlD (1) ; MovAh (4CH) ; Int21 ;
  1859. M ("rm_ok") ;
  1860. (* testexp's tail *)
  1861. StDataCx (D_RMUL21) ;
  1862. MovCxDx ; XorCxDi ; NotCh ; AndChImm (80H) ; StDataCh (D_RESAVE) ;
  1863. OrDhImm (80H) ; OrDiImm (8000H) ;
  1864. StDataSi (D_RMUL22) ; StDataDi (D_RMUL23) ;
  1865. (* remult *)
  1866. StDataAx (D_RMUL11) ; StDataBx (D_RMUL12) ; StDataDx (D_RMUL13) ;
  1867. XorAhAh ; XorBxBx ; XorDxDx ;
  1868. MovDiData (D_RMUL21) ;
  1869. MovClImm (5) ;
  1870. M ("rm_byt") ;
  1871. IncDi ;
  1872. LdChDiOff ;
  1873. CmpCh0 ; Jne8 ("rm_domul") ;
  1874. MovAhBl ; MovBlBh ; MovBhDl ; MovDlDh ; XorDhDh ;
  1875. Jmp8 ("rm_nxt") ;
  1876. M ("rm_domul") ;
  1877. MovSiImm (8) ;
  1878. M ("rm_mul") ;
  1879. RcrCh1 ;
  1880. Jae8 ("rm_bit0") ;
  1881. AddAhData (D_RMUL11 + 1) ;
  1882. AdcBxData (D_RMUL12) ;
  1883. AdcDxData (D_RMUL13) ;
  1884. M ("rm_bit0") ;
  1885. RcrDx1 ; RcrBx1 ; RcrAh1 ;
  1886. DecSi ; Jne8 ("rm_mul") ;
  1887. M ("rm_nxt") ;
  1888. DecCl ; Jne8 ("rm_byt") ;
  1889. XchgAxCx ;
  1890. Lahf ;
  1891. TestDhImm (80H) ; Jne8 ("rm_noadj") ;
  1892. Sahf ;
  1893. RclCh1 ; RclBx1 ; RclDx1 ;
  1894. CmpCl0 ; Je8 ("rm_noadj") ;
  1895. DecCl ;
  1896. M ("rm_noadj") ;
  1897. XchgAxCx ;
  1898. XorDhData (D_RESAVE) ;
  1899. CmpAl0 ; Jne8 ("rm_ret") ;
  1900. M ("rm_zero") ;
  1901. XorAxAx ; XorBxBx ; XorDxDx ;
  1902. M ("rm_ret") ;
  1903. RetR ;
  1904. M ("real_div") ;
  1905. PushBp ; MovBpSp ;
  1906. PushBx ; PushDx ; PushSi ; PushDi ;
  1907. PushBp4 ;
  1908. MovSiBp4 ;
  1909. LdAxSi ;
  1910. LdCxSiOff (2) ; LdDxSiOff (4) ;
  1911. MovBxCx ;
  1912. MovDiSi ;
  1913. LdCxDiOff (6) ; LdSiDiOff (8) ; LdDiDiOff (10) ;
  1914. Call ("rv_core") ;
  1915. PopDi ;
  1916. StDiAx ; StDiOffBx (2) ; StDiOffDx (4) ;
  1917. PopDi ; PopSi ; PopDx ; PopBx ;
  1918. MovSpBp ; PopBp ; RetR ;
  1919. M ("rv_core") ;
  1920. CmpAl0 ; JccNear (74H, "rm_ret") ;
  1921. SubAlCl ;
  1922. Cmc ;
  1923. Jb8 ("rd_ovr1") ;
  1924. AddAlImm (80H) ;
  1925. Jb8 ("rd_okx") ;
  1926. JmpNear ("rm_zero") ; (* underflow *)
  1927. M ("rd_ovr1") ;
  1928. AddAlImm (80H) ;
  1929. Jae8 ("rd_okx") ;
  1930. MovAlD (1) ; MovAh (4CH) ; Int21 ;
  1931. M ("rd_okx") ;
  1932. (* testexp's tail *)
  1933. StDataCx (D_RMUL21) ;
  1934. MovCxDx ; XorCxDi ; NotCh ; AndChImm (80H) ; StDataCh (D_RESAVE) ;
  1935. OrDhImm (80H) ; OrDiImm (8000H) ;
  1936. StDataSi (D_RMUL22) ; StDataDi (D_RMUL23) ;
  1937. (* rediv *)
  1938. StDataAl (D_RMUL11) ;
  1939. MovDiData (D_RMUL21 - 1) ; (* remul21b: one before the mantissa *)
  1940. MovClImm (5) ;
  1941. MovSiImm (8) ;
  1942. M ("rd_loop") ;
  1943. CmpDxData (D_RMUL23) ; Jne8 ("rd_cmp") ;
  1944. CmpBxData (D_RMUL22) ; Jne8 ("rd_cmp") ;
  1945. CmpAhData (D_RMUL21 + 1) ;
  1946. M ("rd_cmp") ;
  1947. Jb8 ("rd_shft") ;
  1948. SubAhData (D_RMUL21 + 1) ; SbbBxData (D_RMUL22) ; SbbDxData (D_RMUL23) ;
  1949. M ("rd_shft") ;
  1950. Cmc ;
  1951. RclCh1 ;
  1952. DecSi ; Jne8 ("rd_bit") ;
  1953. StDiCh ;
  1954. DecCl ; Je8 ("rd_last") ;
  1955. DecDi ;
  1956. MovSiImm (8) ;
  1957. M ("rd_bit") ;
  1958. ShlAh1 ; RclBx1 ; RclDx1 ;
  1959. Jae8 ("rd_loop") ;
  1960. SubAhData (D_RMUL21 + 1) ; SbbBxData (D_RMUL22) ; SbbDxData (D_RMUL23) ;
  1961. Clc ;
  1962. Jmp8 ("rd_shft") ;
  1963. M ("rd_last") ;
  1964. ShlAh1 ; RclBx1 ; RclDx1 ;
  1965. Jb8 ("rd_shft2") ;
  1966. CmpDxData (D_RMUL23) ; Jne8 ("rd_cmp2") ;
  1967. CmpBxData (D_RMUL22) ; Jne8 ("rd_cmp2") ;
  1968. CmpAhData (D_RMUL21 + 1) ;
  1969. M ("rd_cmp2") ;
  1970. Cmc ;
  1971. M ("rd_shft2") ;
  1972. MovCxData (D_RMUL11) ; MovBxData (D_RMUL12) ; MovDxData (D_RMUL13) ;
  1973. Lahf ;
  1974. TestDhImm (80H) ; Jne8 ("rd_nrm") ;
  1975. Sahf ;
  1976. RclCh1 ; RclBx1 ; RclDx1 ;
  1977. Jmp8 ("rd_ok") ;
  1978. M ("rd_nrm") ;
  1979. IncCl ;
  1980. Jne8 ("rd_ok") ;
  1981. MovAlD (1) ; MovAh (4CH) ; Int21 ;
  1982. M ("rd_ok") ;
  1983. JmpNear ("rm_noadj")
  1984. END EmitRealMul ;
  1985. PROCEDURE EmitRealCmp ;
  1986. (* recmp: compare two packed 6-byte MBF reals.
  1987. ABI: [BP+4] points at a 12-byte block holding a then b (the same block the
  1988. four arithmetic entries take). Nothing is written; the result is AX =
  1989. -1/0/+1 for a<b, a=b, a>b.
  1990. TP3's recmp leaves the flags set and its six wrappers branch on them. Here
  1991. the caller is our compiler, which already has one comparison arm for reals
  1992. and strings: EmSetcc over AX. cmpstr returns the same -1/0/+1, so the two
  1993. cannot drift. The reference's core is kept intact; only the final
  1994. flag-to-AX step is new, and it is cmpstr's: MOV AX,0 leaves the flags alone,
  1995. then JB/JE/MOV decide.
  1996. The comparison is on the packed representation: the sign is bit 7 of each
  1997. high byte, the magnitude is exponent then DX/BX/AH (TP3's 40-bit order), and
  1998. a negative pair inverts the magnitude result. Different signs are decided
  1999. by RCL on a's high word, which shifts the sign into CF - exactly TPSRC's
  2000. trick, and it leaves ZF clear because the XOR found the signs different. *)
  2001. BEGIN
  2002. M ("real_cmp") ;
  2003. PushBp ; MovBpSp ;
  2004. PushBx ; PushDx ; PushSi ; PushDi ;
  2005. MovSiBp4 ;
  2006. LdAxSi ;
  2007. LdCxSiOff (2) ; LdDxSiOff (4) ;
  2008. MovBxCx ;
  2009. MovDiSi ;
  2010. LdCxDiOff (6) ; LdSiDiOff (8) ; LdDiDiOff (10) ;
  2011. (* recmp core *)
  2012. PushDx ; XorDxDi ; PopDx ;
  2013. Jns8 ("rc_sign") ;
  2014. PushDx ; RclDx1 ; PopDx ;
  2015. Jmp8 ("rc_flags") ;
  2016. M ("rc_sign") ;
  2017. TestDhImm (80H) ;
  2018. Je8 ("rc_pos") ;
  2019. Call ("rc_num") ;
  2020. Je8 ("rc_flags") ;
  2021. Cmc ;
  2022. Jmp8 ("rc_flags") ;
  2023. M ("rc_pos") ;
  2024. Call ("rc_num") ;
  2025. M ("rc_flags") ;
  2026. MovAlD (0) ; MovAh (0) ; (* AX := 0; MOV leaves the flags alone *)
  2027. Jb8 ("rc_less") ;
  2028. Je8 ("rc_done") ;
  2029. MovAlD (1) ;
  2030. Jmp8 ("rc_done") ;
  2031. M ("rc_less") ;
  2032. MovAlD (0FFH) ; MovAh (0FFH) ;
  2033. M ("rc_done") ;
  2034. PopDi ; PopSi ; PopDx ; PopBx ;
  2035. MovSpBp ; PopBp ; RetR ;
  2036. (* the shared magnitude compare; CALL/RET do not touch the flags *)
  2037. M ("rc_num") ;
  2038. CmpAlCl ; Jne8 ("rc_num_ret") ;
  2039. CmpAl0 ; Je8 ("rc_num_ret") ;
  2040. CmpDxDi ; Jne8 ("rc_num_ret") ;
  2041. CmpBxSi ; Jne8 ("rc_num_ret") ;
  2042. CmpAhCh ;
  2043. M ("rc_num_ret") ;
  2044. RetR
  2045. END EmitRealCmp ;
  2046. PROCEDURE EmitRealInt ;
  2047. (* reint: Int(real), the real-valued integer part, in place.
  2048. ABI: [BP+4] is the address of a 6-byte packed MBF real; the result
  2049. overwrites it. TPSRC3's reint works directly on the packed bytes: it builds
  2050. a mask by shifting 1s right by (exponent - 129) bits and ANDs it with the
  2051. mantissa, which clears the fraction while keeping the sign bit and the
  2052. implicit leading 1. Exponents >= 0A8h are already integral and returned
  2053. unchanged; exponents <= 80h give zero.
  2054. The 16/8-bit fast paths are the reference's, including the byte moves that
  2055. shift the mask instead of running the bit loop. Only the wrapper is ours
  2056. (the caller passes an address rather than the value in registers). *)
  2057. BEGIN
  2058. M ("real_int") ;
  2059. PushBp ; MovBpSp ;
  2060. PushBx ; PushDx ; PushSi ; PushDi ;
  2061. PushBp4 ;
  2062. MovSiBp4 ;
  2063. LdAxSi ;
  2064. LdCxSiOff (2) ; LdDxSiOff (4) ;
  2065. MovBxCx ;
  2066. (* reint core *)
  2067. CmpAl (0A8H) ;
  2068. Jae8 ("ri_store") ; (* already integral: return as-is *)
  2069. MovCxAx ; MovSiBx ; MovDiDx ; (* save the packed mantissa for the mask *)
  2070. XorAhAh ; XorBxBx ; XorDxDx ; (* build the mask in AH/BX/DX *)
  2071. SubClImm (80H) ; (* CL := exponent - 129 + 1 *)
  2072. Jbe8 ("ri_zero") ;
  2073. M ("ri_16") ;
  2074. CmpClImm (10H) ; Jb8 ("ri_8") ;
  2075. MovAhBh ; MovBxDx ; MovDxImm (0FFFFH) ;
  2076. SubClImm (10H) ; Jmp8 ("ri_16") ;
  2077. M ("ri_8") ;
  2078. CmpClImm (08H) ; Jb8 ("ri_1") ;
  2079. MovAhBl ; MovBlBh ; MovBhDl ; MovDlDh ; MovDhImm (0FFH) ;
  2080. SubClImm (08H) ; Jmp8 ("ri_8") ;
  2081. M ("ri_1") ;
  2082. CmpCl0 ; Je8 ("ri_and") ;
  2083. M ("ri_1l") ;
  2084. Stc ; RcrDx1 ; RcrBx1 ; RcrAh1 ; DecCl ; Jne8 ("ri_1l") ;
  2085. M ("ri_and") ;
  2086. AndDxDi ; AndBxSi ; AndAhCh ;
  2087. M ("ri_store") ;
  2088. PopDi ;
  2089. StDiAx ; StDiOffBx (2) ; StDiOffDx (4) ;
  2090. PopDi ; PopSi ; PopDx ; PopBx ;
  2091. MovSpBp ; PopBp ; RetR ;
  2092. M ("ri_zero") ;
  2093. MovAlD (0) ;
  2094. Jmp8 ("ri_store")
  2095. END EmitRealInt ;
  2096. PROCEDURE EmitRealConv ;
  2097. (* fmtreal / ascreal: the decimal<->binary conversions, TPSRC3 880-1250.
  2098. The cores are transcribed with their reference register conventions:
  2099. m10 multiplies AX/BX/DX by ten in place; rrange multiplies by 10**CL
  2100. through the fltdec table and the remult/rediv cores; rdec turns a packed
  2101. real into twelve decimal digits in D_CVOUT and returns the sign in CH and
  2102. the decimal exponent in CL; round and fdig* are the reference's helpers.
  2103. They all use CALL/RET internally, so a caller's flags survive where the
  2104. reference relies on that (the digit loop's CH flags are pushed explicitly
  2105. because rd_core uses CX for its second operand).
  2106. Two entries:
  2107. real_fmt (28): [BP+4] real ptr, [BP+6] destination; CX = width in CL
  2108. and decimals in CH. Writes a length-prefixed string
  2109. (dest[0] = length) padded on the LEFT to width, and
  2110. returns the length in AX. DEC >= 25 selects the
  2111. reference's scientific mode and derives the number of
  2112. fraction digits from the width.
  2113. real_val (29): [BP+4] ASCIIZ source, [BP+6] destination real. Returns
  2114. AX = the offset of the first character not consumed, or
  2115. FFFFh on a hard error (in which case the destination is
  2116. untouched). A trailing non-digit is NOT an error; the
  2117. offset tells Val where to report it, as TPSRC's ascreal
  2118. returns BX = end. *)
  2119. BEGIN
  2120. (* ---------------------------------------------------------- real_fmt *)
  2121. M ("real_fmt") ;
  2122. PushBp ; MovBpSp ;
  2123. PushBx ; PushDx ; PushSi ; PushDi ;
  2124. MovDiBp4 ; (* DI := the real *)
  2125. MovDlCh ; (* DL := decimals *)
  2126. MovChImm (0) ; (* CX := width *)
  2127. MovDhImm (0) ;
  2128. PushCx ; (* keep the width across rdec *)
  2129. MovBxDataAddr (D_FMTBUF) ;
  2130. Call ("rc_fmt") ;
  2131. PopCx ;
  2132. MovAxBx ; (* AX := end of the formatted text *)
  2133. MovBxDataAddr (D_FMTBUF) ;
  2134. SubAxBx ; (* AX := its length *)
  2135. StDataAx (D_FMTLEN) ;
  2136. StDataCx (D_FMTW) ;
  2137. (* pad := max (width - length, 0) *)
  2138. MovAxData (D_FMTW) ;
  2139. MovDxData (D_FMTLEN) ;
  2140. SubAxDx ;
  2141. CmpAx0 ;
  2142. Jge8 ("rc_fpadok") ;
  2143. XorAxAx ;
  2144. M ("rc_fpadok") ;
  2145. StDataAx (D_FMTPAD) ;
  2146. MovDxData (D_FMTLEN) ;
  2147. AddAxDx ; (* AX := pad + length *)
  2148. CmpAxImm (0FFH) ; (* a Pascal string's length is one byte *)
  2149. JccNear (76H, "rc_ftotok") ;
  2150. MovAxImm (0FFH) ;
  2151. M ("rc_ftotok") ;
  2152. StDataAx (D_FMTTOT) ;
  2153. (* dest[0] := length; then pad spaces, then the number *)
  2154. MovDxBp6 ; MovDiDx ;
  2155. StDiAl ;
  2156. IncDi ;
  2157. MovCxData (D_FMTPAD) ;
  2158. Jcxz8 ("rc_fpad_done") ;
  2159. M ("rc_fpad_loop") ;
  2160. MovAlD (20H) ; StDiAl ; IncDi ; Loop8 ("rc_fpad_loop") ;
  2161. M ("rc_fpad_done") ;
  2162. MovSiData (D_FMTBUF) ;
  2163. MovCxData (D_FMTLEN) ;
  2164. Jcxz8 ("rc_fcopy_done") ;
  2165. M ("rc_fcopy_loop") ;
  2166. LdAlSi ; StDiAl ; IncSi ; IncDi ; Loop8 ("rc_fcopy_loop") ;
  2167. M ("rc_fcopy_done") ;
  2168. MovAxData (D_FMTTOT) ;
  2169. PopDi ; PopSi ; PopDx ; PopBx ;
  2170. MovSpBp ; PopBp ; RetR ;
  2171. (* ---------------------------------------------------------- real_val *)
  2172. M ("real_val") ;
  2173. PushBp ; MovBpSp ;
  2174. PushBx ; PushDx ; PushSi ; PushDi ;
  2175. MovBxBp4 ;
  2176. MovDiBp6 ;
  2177. StDataBx (D_ASCSRC) ;
  2178. Call ("rc_asc") ;
  2179. JccNear (72H, "rc_averr") ;
  2180. MovAxBx ; (* AX := end pointer *)
  2181. MovDxData (D_ASCSRC) ;
  2182. SubAxDx ; (* AX := consumed length *)
  2183. JmpNear ("rc_avdone") ;
  2184. M ("rc_averr") ;
  2185. MovAxImm (0FFFFH) ;
  2186. M ("rc_avdone") ;
  2187. PopDi ; PopSi ; PopDx ; PopBx ;
  2188. MovSpBp ; PopBp ; RetR ;
  2189. (* ---------------------------------------------------------- m10 *)
  2190. (* AX/BX/DX := value * 10; CF = overflow; CX is preserved. The reference
  2191. multiplies by 1.25 (add the /4 value) and then by 8 via the exponent. *)
  2192. M ("rc_m10") ;
  2193. CmpAl0 ;
  2194. JccNear (75H, "rc_m10nz") ;
  2195. RetR ;
  2196. M ("rc_m10nz") ;
  2197. OrDhImm (80H) ;
  2198. PushCx ; PushDx ; PushBx ; PushAx ;
  2199. ShrDx1 ; RcrBx1 ; RcrAh1 ;
  2200. ShrDx1 ; RcrBx1 ; RcrAh1 ;
  2201. PopCx ; AddAhCh ;
  2202. PopCx ; AdcBxCx ;
  2203. PopCx ; AdcDxCx ;
  2204. PopCx ;
  2205. JccNear (73H, "rc_m10nrm") ;
  2206. RcrDx1 ; RcrBx1 ; RcrAh1 ;
  2207. IncAl ;
  2208. JccNear (75H, "rc_m10nrm") ;
  2209. Stc ; RetR ;
  2210. M ("rc_m10nrm") ;
  2211. AndDhImm (7FH) ;
  2212. AddAlImm (3) ;
  2213. RetR ;
  2214. (* -------------------------------------------------------- rrange *)
  2215. (* AX/BX/DX := value * 10**CL, signed CL, via the fltdec table. *)
  2216. M ("rc_range") ;
  2217. PushDx ; PushBx ; PushAx ;
  2218. StDataCl (D_CVDECEXP) ;
  2219. CmpCl0 ;
  2220. JccNear (79H, "rc_rpos") ;
  2221. NegCl ;
  2222. M ("rc_rpos") ;
  2223. MovBlCl ;
  2224. AndBlImm (0FCH) ;
  2225. MovBhBl ;
  2226. ShrBl1 ;
  2227. AddBlBh ;
  2228. XorBhBh ;
  2229. LeaDiBxData (D_FLTDEC) ;
  2230. LdAxDi ; LdBxDiOff (2) ; LdDxDiOff (4) ;
  2231. AndClImm (3) ;
  2232. JccNear (74H, "rc_rdone") ;
  2233. M ("rc_rmul") ;
  2234. Call ("rc_m10") ;
  2235. DecCl ;
  2236. JccNear (75H, "rc_rmul") ;
  2237. M ("rc_rdone") ;
  2238. MovCxAx ; MovSiBx ; MovDiDx ;
  2239. PopAx ; PopBx ; PopDx ;
  2240. TestDataImm (D_CVDECEXP, 80H) ;
  2241. JccNear (75H, "rc_rdiv") ;
  2242. JmpNear ("rm_core") ;
  2243. M ("rc_rdiv") ;
  2244. JmpNear ("rv_core") ;
  2245. (* ------------------------------------------------------- rangem *)
  2246. (* the signed range check around rrange: |CL| <= 38 *)
  2247. M ("rc_rangem") ;
  2248. CmpClImm (0DAH) ; JccNear (7CH, "rc_rerr") ;
  2249. CmpClImm (26H) ; JccNear (7FH, "rc_rerr") ;
  2250. PushCx ; PushSi ; PushDi ;
  2251. Call ("rc_range") ;
  2252. PopDi ; PopSi ; PopCx ;
  2253. RetR ;
  2254. M ("rc_rerr") ;
  2255. Stc ; RetR ;
  2256. (* ---------------------------------------------------------- rdec *)
  2257. (* DI -> real. D_CVOUT gets twelve digits; CH = sign, CL = decimal
  2258. exponent. The estimate is 77/256 * (exponent - 128), TPSRC's
  2259. log10(2) approximation. *)
  2260. M ("rc_rdec") ;
  2261. LdAxDi ; LdBxDiOff (2) ; LdDxDiOff (4) ;
  2262. CmpAl0 ;
  2263. JccNear (75H, "rc_rdno0") ;
  2264. MovSiData (D_CVOUT) ;
  2265. M ("rc_rdfill") ;
  2266. StSiImm (3030H) ;
  2267. IncSi ; IncSi ;
  2268. CmpSiData (D_CVOUT + 12) ;
  2269. JccNear (75H, "rc_rdfill") ;
  2270. MovCxImm (0) ;
  2271. RetR ;
  2272. M ("rc_rdno0") ;
  2273. MovChDh ;
  2274. AndDhImm (7FH) ;
  2275. PushAx ; PushDx ;
  2276. SubAl (80H) ; Cbw ; MovDxImm (004DH) ; ImulDx ; AddAxImm (5) ;
  2277. MovClAh ;
  2278. PopDx ; PopAx ;
  2279. CmpClImm (0D9H) ;
  2280. JccNear (75H, "rc_rdnocomp") ;
  2281. IncCl ;
  2282. M ("rc_rdnocomp") ;
  2283. PushCx ;
  2284. NegCl ;
  2285. Call ("rc_range") ;
  2286. PopCx ;
  2287. CmpAl (81H) ;
  2288. JccNear (73H, "rc_rdnoadj") ;
  2289. Call ("rc_m10") ;
  2290. DecCl ;
  2291. M ("rc_rdnoadj") ;
  2292. PushCx ;
  2293. OrDhImm (80H) ;
  2294. MovClImm (84H) ;
  2295. SubClAl ;
  2296. MovAlD (0) ;
  2297. CmpCl0 ;
  2298. JccNear (74H, "rc_rdnosh") ;
  2299. M ("rc_rdshift") ;
  2300. ShrDx1 ; RcrBx1 ; RcrAh1 ;
  2301. DecCl ;
  2302. JccNear (75H, "rc_rdshift") ;
  2303. M ("rc_rdnosh") ;
  2304. MovSiData (D_CVOUT) ;
  2305. M ("rc_rdconv") ;
  2306. MovChDh ; MovClImm (4) ; ShrChCl ; AddChImm (30H) ;
  2307. StSiCh ;
  2308. AndDhImm (0FH) ;
  2309. PushDx ; PushBx ; PushAx ;
  2310. ShlAx1 ; RclBx1 ; RclDx1 ;
  2311. ShlAx1 ; RclBx1 ; RclDx1 ;
  2312. PopCx ; AddAxCx ;
  2313. PopCx ; AdcBxCx ;
  2314. PopCx ; AdcDxCx ;
  2315. ShlAx1 ; RclBx1 ; RclDx1 ;
  2316. IncSi ;
  2317. CmpSiData (D_CVOUT + 12) ;
  2318. JccNear (75H, "rc_rdconv") ;
  2319. PopCx ;
  2320. RetR ;
  2321. (* --------------------------------------------------------- round *)
  2322. (* round the digits at index AL: look at the next digit, and carry if it is
  2323. 5 or more. CL is the decimal exponent and is incremented on carry-out. *)
  2324. M ("rc_round") ;
  2325. XorAhAh ;
  2326. MovBxDataAddr (D_CVOUT) ;
  2327. AddBxAx ;
  2328. CmpBxByte (35H) ;
  2329. MovBxByte (0) ;
  2330. JccNear (72H, "rc_rounddone") ;
  2331. M ("rc_roundloop") ;
  2332. DecAl ;
  2333. JccNear (78H, "rc_roundexp") ;
  2334. DecBx ;
  2335. IncBxByte ;
  2336. CmpBxByte (3AH) ;
  2337. JccNear (72H, "rc_rounddone") ;
  2338. MovBxByte (0) ;
  2339. JmpNear ("rc_roundloop") ;
  2340. M ("rc_roundexp") ;
  2341. MovBxByte (31H) ;
  2342. MovBxOff1Byte (0) ;
  2343. IncCl ;
  2344. M ("rc_rounddone") ;
  2345. RetR ;
  2346. (* ------------------------------------------------------------ fdig *)
  2347. M ("rc_fdig") ;
  2348. LdAlSi ;
  2349. CmpAl0 ;
  2350. JccNear (74H, "rc_fdig0") ;
  2351. IncSi ;
  2352. JmpNear ("rc_fdigsto") ;
  2353. M ("rc_fdig0") ;
  2354. MovAlD (30H) ;
  2355. M ("rc_fdigsto") ;
  2356. StBxAl ;
  2357. IncBx ;
  2358. RetR ;
  2359. (* ---------------------------------------------------------- fcore *)
  2360. (* DI = real, BX = destination, CX = width, DX = decimals. Writes raw
  2361. characters (no padding) and leaves BX one past the end. *)
  2362. M ("rc_fmt") ;
  2363. PushBx ;
  2364. CmpDxImm (19H) ;
  2365. JccNear (72H, "rc_fnlim2") ;
  2366. MovAxCx ;
  2367. CmpCh0 ;
  2368. JccNear (74H, "rc_flimok") ;
  2369. TestChImm (80H) ;
  2370. JccNear (74H, "rc_flimpos") ;
  2371. XorAxAx ;
  2372. JmpNear ("rc_flimok") ;
  2373. M ("rc_flimpos") ;
  2374. MovAxImm (0FFH) ;
  2375. M ("rc_flimok") ;
  2376. MovDl (7) ;
  2377. TestDiOffImm (5, 80H) ;
  2378. JccNear (74H, "rc_frpos") ;
  2379. IncDl ;
  2380. M ("rc_frpos") ;
  2381. SubAlDl ;
  2382. JccNear (73H, "rc_frnound") ;
  2383. MovAlD (0) ;
  2384. M ("rc_frnound") ;
  2385. CmpAl (9) ;
  2386. JccNear (72H, "rc_frnolim") ;
  2387. MovAlD (9) ;
  2388. M ("rc_frnolim") ;
  2389. IncAl ;
  2390. MovDlAl ;
  2391. MovDhAl ;
  2392. M ("rc_fnlim2") ;
  2393. PushDx ;
  2394. Call ("rc_rdec") ;
  2395. PopDx ;
  2396. MovAlDl ;
  2397. IncAl ;
  2398. CmpDh0 ;
  2399. JccNear (75H, "rc_frround") ;
  2400. AddAlCl ;
  2401. JccNear (79H, "rc_frlimprc") ;
  2402. StDataByte (D_CVOUT, 0) ;
  2403. JmpNear ("rc_frfmtit") ;
  2404. M ("rc_frlimprc") ;
  2405. CmpAl (0CH) ;
  2406. JccNear (72H, "rc_frround") ;
  2407. MovAlD (0BH) ;
  2408. M ("rc_frround") ;
  2409. Call ("rc_round") ;
  2410. M ("rc_frfmtit") ;
  2411. PopBx ;
  2412. MovSiData (D_CVOUT) ;
  2413. TestChImm (80H) ;
  2414. JccNear (74H, "rc_frpos2") ;
  2415. MovAlD (2DH) ;
  2416. Call ("rc_fdigsto") ;
  2417. M ("rc_frpos2") ;
  2418. MovChCl ;
  2419. CmpDh0 ;
  2420. JccNear (74H, "rc_frzer") ;
  2421. MovChImm (0) ;
  2422. M ("rc_frzer") ;
  2423. CmpCh0 ;
  2424. JccNear (79H, "rc_frint") ;
  2425. Call ("rc_fdig0") ;
  2426. JmpNear ("rc_frfrac0") ;
  2427. M ("rc_frint") ;
  2428. Call ("rc_fdig") ;
  2429. DecCh ;
  2430. JccNear (79H, "rc_frint") ;
  2431. M ("rc_frfrac0") ;
  2432. CmpDl0 ;
  2433. JccNear (74H, "rc_frexp") ;
  2434. MovAlD (2EH) ;
  2435. Call ("rc_fdigsto") ;
  2436. M ("rc_frfill0") ;
  2437. IncCh ;
  2438. JccNear (74H, "rc_frfrac") ;
  2439. Call ("rc_fdig0") ;
  2440. DecDl ;
  2441. JccNear (75H, "rc_frfill0") ;
  2442. M ("rc_frfrac") ;
  2443. DecDl ;
  2444. JccNear (78H, "rc_frexp") ;
  2445. Call ("rc_fdig") ;
  2446. JmpNear ("rc_frfrac") ;
  2447. M ("rc_frexp") ;
  2448. CmpDh0 ;
  2449. JccNear (75H, "rc_frputexp") ;
  2450. RetR ;
  2451. M ("rc_frputexp") ;
  2452. MovAlD (45H) ;
  2453. Call ("rc_fdigsto") ;
  2454. MovAlD (2BH) ;
  2455. CmpCl0 ;
  2456. JccNear (79H, "rc_frposexp") ;
  2457. NegCl ;
  2458. MovAlD (2DH) ;
  2459. M ("rc_frposexp") ;
  2460. Call ("rc_fdigsto") ;
  2461. MovAlD (2FH) ;
  2462. M ("rc_frexpsub") ;
  2463. IncAl ;
  2464. SubClImm (0AH) ;
  2465. JccNear (73H, "rc_frexpsub") ;
  2466. Call ("rc_fdigsto") ;
  2467. AddClImm (3AH) ;
  2468. MovAlCl ;
  2469. JmpNear ("rc_fdigsto") ;
  2470. (* ------------------------------------------------------------ asc *)
  2471. (* BX = ASCIIZ source, DI = destination real. Returns BX = end, CF = 1 on
  2472. a hard error. The digit add keeps the reference's stack dance: the
  2473. digit's real goes in a and the accumulated value in b (addition is
  2474. commutative), while the pointers and the CH flags are saved around the
  2475. call because rd_core uses CX/SI/DI. *)
  2476. M ("rc_asc") ;
  2477. LdClBx ;
  2478. CmpClImm (2DH) ;
  2479. JccNear (75H, "rc_asign") ;
  2480. IncBx ;
  2481. M ("rc_asign") ;
  2482. PushCx ;
  2483. Call ("rc_asc2") ;
  2484. PopCx ;
  2485. JccNear (72H, "rc_aouterr") ;
  2486. CmpClImm (2DH) ;
  2487. JccNear (75H, "rc_aok") ;
  2488. LdAlDi ;
  2489. CmpAl0 ;
  2490. JccNear (74H, "rc_aok") ;
  2491. XorDiOffImm (5, 80H) ;
  2492. M ("rc_aok") ;
  2493. Clc ;
  2494. RetR ;
  2495. M ("rc_aouterr") ;
  2496. Stc ;
  2497. RetR ;
  2498. M ("rc_asc2") ;
  2499. MovSiBx ;
  2500. XorAxAx ; XorBxBx ; XorDxDx ; XorCxCx ;
  2501. StDataByte (D_CVEXPCNT, 0) ;
  2502. M ("rc_aloop") ;
  2503. LdClSi ;
  2504. CmpClImm (61H) ; JccNear (72H, "rc_aup") ;
  2505. CmpClImm (7AH) ; Ja8 ("rc_aup") ;
  2506. SubClImm (20H) ;
  2507. M ("rc_aup") ;
  2508. Call ("rc_adigit2") ;
  2509. JccNear (72H, "rc_anodig") ;
  2510. Call ("rc_m10") ;
  2511. JccNear (72H, "rc_aerr") ;
  2512. PushDi ; PushSi ; PushCx ;
  2513. PushDx ; PushBx ; PushAx ;
  2514. MovAlCl ;
  2515. XorAhAh ;
  2516. Call ("rf_core") ;
  2517. MovCxAx ; MovSiBx ; MovDiDx ;
  2518. PopAx ; PopBx ; PopDx ;
  2519. StDataImm (D_RSIGN, 0) ; (* readd, not resub: resign := 0 *)
  2520. Call ("rd_core") ;
  2521. PopCx ; PopSi ; PopDi ;
  2522. TestChImm (40H) ;
  2523. JccNear (74H, "rc_anext") ;
  2524. DecData (D_CVEXPCNT) ;
  2525. M ("rc_anext") ;
  2526. IncSi ;
  2527. JmpNear ("rc_aloop") ;
  2528. M ("rc_anodig") ;
  2529. CmpClImm (2EH) ;
  2530. JccNear (75H, "rc_aexp") ;
  2531. TestChImm (40H) ;
  2532. JccNear (75H, "rc_aerr") ;
  2533. OrChImm (40H) ;
  2534. JmpNear ("rc_anext") ;
  2535. M ("rc_aexp") ;
  2536. CmpClImm (45H) ;
  2537. LdClData (D_CVEXPCNT) ;
  2538. JccNear (75H, "rc_aexp3") ;
  2539. Call ("rc_rangem") ;
  2540. JccNear (72H, "rc_aerr") ;
  2541. IncSi ;
  2542. LdClSi ;
  2543. CmpClImm (2BH) ;
  2544. JccNear (74H, "rc_aposexp") ;
  2545. CmpClImm (2DH) ;
  2546. JccNear (75H, "rc_aexp2") ;
  2547. OrChImm (20H) ;
  2548. M ("rc_aposexp") ;
  2549. IncSi ;
  2550. M ("rc_aexp2") ;
  2551. Call ("rc_adigit") ;
  2552. JccNear (72H, "rc_aerr") ;
  2553. PushAx ;
  2554. MovAlCl ;
  2555. IncSi ;
  2556. Call ("rc_adigit") ;
  2557. JccNear (72H, "rc_aonedig") ;
  2558. MovAhAl ;
  2559. ShlAl1 ; ShlAl1 ; AddAlAh ; ShlAl1 ; AddAlCl ;
  2560. IncSi ;
  2561. M ("rc_aonedig") ;
  2562. MovClAl ;
  2563. PopAx ;
  2564. TestChImm (20H) ;
  2565. JccNear (74H, "rc_aexp3") ;
  2566. NegCl ;
  2567. M ("rc_aexp3") ;
  2568. Call ("rc_rangem") ;
  2569. JccNear (72H, "rc_aerr") ;
  2570. StDiAx ; StDiOffBx (2) ; StDiOffDx (4) ;
  2571. Clc ;
  2572. JmpNear ("rc_aret") ;
  2573. M ("rc_aerr") ;
  2574. Stc ;
  2575. M ("rc_aret") ;
  2576. MovBxSi ;
  2577. RetR ;
  2578. (* -------------------------------------------------------- adigit *)
  2579. M ("rc_adigit") ;
  2580. LdClSi ;
  2581. M ("rc_adigit2") ;
  2582. CmpClImm (30H) ;
  2583. JccNear (72H, "rc_adret") ;
  2584. CmpClImm (3AH) ;
  2585. Cmc ;
  2586. JccNear (72H, "rc_adret") ;
  2587. SubClImm (30H) ;
  2588. M ("rc_adret") ;
  2589. RetR ;
  2590. (* --------------------------------------------------------- real_write *)
  2591. (* [BP+4] = decimals, [BP+6] = width, [BP+8] = real. Formats into the
  2592. internal buffer, then pads and prints: the same field real_fmt builds
  2593. as a string, sent straight to the console. `Write(Value:FW:DEC)' uses
  2594. this; the default widths are the compiler's (18 columns, decimals
  2595. FFFFh, which fmtreal turns into its scientific mode). *)
  2596. M ("real_write") ;
  2597. PushBp ; MovBpSp ;
  2598. PushBx ; PushDx ; PushSi ; PushDi ;
  2599. MovAxBp6 ; StDataAx (D_FMTW) ; (* width *)
  2600. MovDxBp4 ; (* DX := decimals *)
  2601. MovCxBp6 ; (* CX := width *)
  2602. MovDiBp8 ; (* DI := the real *)
  2603. MovBxDataAddr (D_FMTBUF) ;
  2604. Call ("rc_fmt") ; (* BX := end of the formatted text *)
  2605. MovAxBx ;
  2606. MovBxDataAddr (D_FMTBUF) ;
  2607. SubAxBx ; (* AX := length *)
  2608. StDataAx (D_FMTLEN) ;
  2609. MovAxData (D_FMTW) ;
  2610. MovDxData (D_FMTLEN) ;
  2611. SubAxDx ;
  2612. CmpAx0 ;
  2613. Jge8 ("rc_wpadok") ;
  2614. XorAxAx ;
  2615. M ("rc_wpadok") ;
  2616. StDataAx (D_FMTPAD) ;
  2617. MovCxData (D_FMTPAD) ;
  2618. Jcxz8 ("rc_wbody") ;
  2619. M ("rc_wpad") ;
  2620. MovAlD (20H) ; MovAh (02H) ; Int21 ; Loop8 ("rc_wpad") ;
  2621. M ("rc_wbody") ;
  2622. MovSiData (D_FMTBUF) ;
  2623. MovCxData (D_FMTLEN) ;
  2624. Jcxz8 ("rc_wdone") ;
  2625. M ("rc_wout") ;
  2626. LdAlSi ; MovAh (02H) ; Int21 ; IncSi ; Loop8 ("rc_wout") ;
  2627. M ("rc_wdone") ;
  2628. PopDi ; PopSi ; PopDx ; PopBx ;
  2629. MovSpBp ; PopBp ; RetR
  2630. END EmitRealConv ;
  2631. PROCEDURE EmitData ;
  2632. (* The runtime's data block. The D_ constants name the offsets in it and are
  2633. ASSERTED against what is emitted here, three ways: the starting offset of
  2634. each string, the offset just past it, and the block's total length. A D_
  2635. constant is a 16-bit immediate inside a MOV, so a wrong one is a
  2636. perfectly well-formed instruction that reads the wrong memory - see the
  2637. note on the D_ block. *)
  2638. BEGIN
  2639. dataAt := rpos ;
  2640. (* Each D_ is checked BEFORE the string it names is emitted, because it
  2641. names that string's START. *)
  2642. IF dataAt + D_NUM # rpos THEN
  2643. HALT
  2644. END ;
  2645. (* 8 bytes of scratch, never read before written *)
  2646. B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ;
  2647. IF dataAt + D_TRUE # rpos THEN
  2648. HALT
  2649. END ;
  2650. Str5 ("TRUE$", 5) ;
  2651. IF dataAt + D_FALSE # rpos THEN
  2652. HALT
  2653. END ;
  2654. Str5 ("FALSE$", 6) ;
  2655. IF dataAt + D_CRLF # rpos THEN
  2656. HALT
  2657. END ;
  2658. B (13) ; B (10) ; B (ORD ("$")) ;
  2659. IF dataAt + D_REAL # rpos THEN
  2660. HALT
  2661. END ;
  2662. Str5 ("?REAL?", 6) ;
  2663. IF dataAt + D_PUSH # rpos THEN
  2664. HALT
  2665. END ;
  2666. B (0) ; B (0) ; (* the pushback slot, empty to begin with *)
  2667. IF dataAt + D_RSIGN # rpos THEN
  2668. HALT
  2669. END ;
  2670. B (0) ; B (0) ; (* resign/resave *)
  2671. IF dataAt + D_RMANT # rpos THEN
  2672. HALT
  2673. END ;
  2674. B (0) ; B (0) ; (* remant/remant1 *)
  2675. IF dataAt + D_CVDEC # rpos THEN
  2676. HALT
  2677. END ;
  2678. B (0) ;
  2679. IF dataAt + D_RMUL11 # rpos THEN
  2680. HALT
  2681. END ;
  2682. B (0) ; B (0) ;
  2683. IF dataAt + D_RMUL12 # rpos THEN
  2684. HALT
  2685. END ;
  2686. B (0) ; B (0) ;
  2687. IF dataAt + D_RMUL13 # rpos THEN
  2688. HALT
  2689. END ;
  2690. B (0) ; B (0) ;
  2691. IF dataAt + D_RMUL21 # rpos THEN
  2692. HALT
  2693. END ;
  2694. B (0) ; B (0) ;
  2695. IF dataAt + D_RMUL22 # rpos THEN
  2696. HALT
  2697. END ;
  2698. B (0) ; B (0) ;
  2699. IF dataAt + D_RMUL23 # rpos THEN
  2700. HALT
  2701. END ;
  2702. B (0) ; B (0) ;
  2703. IF dataAt + D_CVOUT # rpos THEN
  2704. HALT
  2705. END ;
  2706. (* twelve decimal digit slots; rdconv fills all of them *)
  2707. B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ;
  2708. B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ;
  2709. IF dataAt + D_CVDECEXP # rpos THEN
  2710. HALT
  2711. END ;
  2712. B (0) ;
  2713. IF dataAt + D_CVEXPCNT # rpos THEN
  2714. HALT
  2715. END ;
  2716. B (0) ;
  2717. IF dataAt + D_FLTDEC # rpos THEN
  2718. HALT
  2719. END ;
  2720. (* 1e0, 1e4, ... 1e36 as the reference's AX:BX:DX triples, low byte first *)
  2721. B (081H) ; B (000H) ; B (000H) ; B (000H) ; B (000H) ; B (000H) ;
  2722. B (08EH) ; B (000H) ; B (000H) ; B (000H) ; B (040H) ; B (01CH) ;
  2723. B (09BH) ; B (000H) ; B (000H) ; B (020H) ; B (0BCH) ; B (03EH) ;
  2724. B (0A8H) ; B (000H) ; B (010H) ; B (0A5H) ; B (0D4H) ; B (068H) ;
  2725. B (0B6H) ; B (004H) ; B (0BFH) ; B (0C9H) ; B (01BH) ; B (00EH) ;
  2726. B (0C3H) ; B (0ACH) ; B (0C5H) ; B (0EBH) ; B (078H) ; B (02DH) ;
  2727. B (0D0H) ; B (0CDH) ; B (0CEH) ; B (01BH) ; B (0C2H) ; B (053H) ;
  2728. B (0DEH) ; B (0F9H) ; B (078H) ; B (039H) ; B (03FH) ; B (001H) ;
  2729. B (0EBH) ; B (02BH) ; B (0A8H) ; B (0ADH) ; B (0C5H) ; B (01DH) ;
  2730. B (0F8H) ; B (0C9H) ; B (07BH) ; B (0CEH) ; B (097H) ; B (040H) ;
  2731. IF dataAt + D_FMTBUF # rpos THEN
  2732. HALT
  2733. END ;
  2734. (* 64 bytes of formatted output; a fixed-format 1e38 with 10 decimals is
  2735. about 50 characters, so this has room *)
  2736. B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ;
  2737. B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ;
  2738. B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ;
  2739. B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ;
  2740. B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ;
  2741. B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ;
  2742. B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ;
  2743. B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ; B (0) ;
  2744. IF dataAt + D_FMTLEN # rpos THEN
  2745. HALT
  2746. END ;
  2747. B (0) ; B (0) ;
  2748. IF dataAt + D_FMTW # rpos THEN
  2749. HALT
  2750. END ;
  2751. B (0) ; B (0) ;
  2752. IF dataAt + D_FMTPAD # rpos THEN
  2753. HALT
  2754. END ;
  2755. B (0) ; B (0) ;
  2756. IF dataAt + D_FMTTOT # rpos THEN
  2757. HALT
  2758. END ;
  2759. B (0) ; B (0) ;
  2760. IF dataAt + D_ASCSRC # rpos THEN
  2761. HALT
  2762. END ;
  2763. B (0) ; B (0) ;
  2764. WHILE rpos < dataAt + D_END DO
  2765. B (0)
  2766. END ;
  2767. (* the total, so D_END is checked too and not just used as a pad target *)
  2768. IF rpos - dataAt # D_END THEN
  2769. HALT
  2770. END
  2771. END EmitData ;
  2772. PROCEDURE FixUp ;
  2773. VAR i, t, rel : CARDINAL ;
  2774. BEGIN
  2775. i := 0 ;
  2776. WHILE i < nfix DO
  2777. IF fix [i].kind = 2 THEN
  2778. (* A data address, and the ONE kind that is absolute rather than
  2779. relative: rtBase says where the blob sits in the image, val says
  2780. where inside the blob, and LoadBias says where the image itself
  2781. sits once DOS has loaded it. All three are needed; omitting any
  2782. one produces a readable-looking address into the wrong bytes. *)
  2783. t := (dataAt + rtBase + fix [i].val + LoadBias) MOD 10000H ;
  2784. rt [fix [i].place] := VAL (BYTE, t MOD 100H) ;
  2785. rt [fix [i].place + 1] := VAL (BYTE, (t DIV 100H) MOD 100H)
  2786. ELSE
  2787. t := LblOff (fix [i].nm) ;
  2788. IF fix [i].kind = 0 THEN
  2789. (* rel8 is measured from the end of the instruction, i.e. one
  2790. byte past the displacement field. A target beyond +127/-128
  2791. would wrap silently and send the machine into the middle of
  2792. an instruction; that happened in remult's early zero/return
  2793. jumps. Refuse it. *)
  2794. IF (VAL (LONGINT, t) - VAL (LONGINT, fix [i].place + 1) < -128)
  2795. OR (VAL (LONGINT, t) - VAL (LONGINT, fix [i].place + 1) > 127)
  2796. THEN
  2797. HALT
  2798. END ;
  2799. rel := (t + 100H - (fix [i].place + 1)) MOD 100H ;
  2800. rt [fix [i].place] := VAL (BYTE, rel)
  2801. ELSE
  2802. rel := (t + 10000H - (fix [i].place + 2)) MOD 10000H ;
  2803. rt [fix [i].place] := VAL (BYTE, rel MOD 100H) ;
  2804. rt [fix [i].place + 1] := VAL (BYTE, (rel DIV 100H) MOD 100H)
  2805. END
  2806. END ;
  2807. INC (i)
  2808. END
  2809. END FixUp ;
  2810. (* ---------------------------------------------------------------- *)
  2811. (* public interface *)
  2812. (* ---------------------------------------------------------------- *)
  2813. PROCEDURE RT_Build (base : CARDINAL) ;
  2814. BEGIN
  2815. IF built THEN
  2816. RETURN
  2817. END ;
  2818. rtBase := base ; (* must precede every Emit*, they read it *)
  2819. rpos := 0 ; ltop := 0 ; nfix := 0 ; dataAt := 0 ;
  2820. EmitInitMem ;
  2821. EmitEnd ;
  2822. EmitStackChk ;
  2823. EmitWrInt ; EmitWrIntW ; EmitWrChar ; EmitWrBool ; EmitWrReal ; EmitWrLn ;
  2824. EmitWrInl ; EmitWrStr ;
  2825. EmitRdInt ; EmitRdScan ; EmitRdByte ;
  2826. EmitRdChar ; EmitRdBool ; EmitRdLn ;
  2827. EmitGetCh ; EmitUnGetCh ;
  2828. EmitRdStr ; EmitStrStore ; EmitCmpStr ; EmitStrCat ;
  2829. EmitRealFromInt ; EmitRealAddSub ; EmitRealMul ;
  2830. EmitRealCmp ; EmitRealInt ; EmitRealConv ;
  2831. EmitData ;
  2832. FixUp ;
  2833. SetStr (entNm [0], "initmem") ;
  2834. SetStr (entNm [1], "progend") ;
  2835. SetStr (entNm [2], "stackchk") ;
  2836. SetStr (entNm [3], "wrint") ;
  2837. SetStr (entNm [4], "wrchar") ;
  2838. SetStr (entNm [5], "wrbool") ;
  2839. SetStr (entNm [6], "wrreal") ;
  2840. SetStr (entNm [7], "wrln") ;
  2841. SetStr (entNm [8], "rdint") ;
  2842. SetStr (entNm [9], "rdchar") ;
  2843. SetStr (entNm [10], "rdbool") ;
  2844. SetStr (entNm [11], "rdln") ;
  2845. SetStr (entNm [12], "halt") ;
  2846. SetStr (entNm [13], "wrtinl") ;
  2847. SetStr (entNm [14], "rdbyte") ;
  2848. SetStr (entNm [15], "wrstr") ;
  2849. SetStr (entNm [16], "rdstr") ;
  2850. SetStr (entNm [17], "strstore") ;
  2851. SetStr (entNm [18], "cmpstr") ;
  2852. SetStr (entNm [19], "strcat") ;
  2853. SetStr (entNm [20], "wrintw") ;
  2854. SetStr (entNm [21], "real_from_int") ;
  2855. SetStr (entNm [22], "real_add") ;
  2856. SetStr (entNm [23], "real_sub") ;
  2857. SetStr (entNm [24], "real_mul") ;
  2858. SetStr (entNm [25], "real_div") ;
  2859. SetStr (entNm [26], "real_cmp") ;
  2860. SetStr (entNm [27], "real_int") ;
  2861. SetStr (entNm [28], "real_fmt") ;
  2862. SetStr (entNm [29], "real_val") ;
  2863. SetStr (entNm [30], "real_write") ;
  2864. built := TRUE
  2865. END RT_Build ;
  2866. PROCEDURE RT_Size () : CARDINAL ;
  2867. BEGIN
  2868. IF NOT built THEN
  2869. RT_Build (0)
  2870. END ;
  2871. RETURN rpos
  2872. END RT_Size ;
  2873. PROCEDURE RT_Byte (i : CARDINAL ) : BYTE ;
  2874. BEGIN
  2875. IF NOT built THEN
  2876. RT_Build (0)
  2877. END ;
  2878. IF i >= rpos THEN
  2879. RETURN 0
  2880. END ;
  2881. RETURN rt [i]
  2882. END RT_Byte ;
  2883. PROCEDURE RT_Entry (i : CARDINAL ) : CARDINAL ;
  2884. BEGIN
  2885. IF NOT built THEN
  2886. RT_Build (0)
  2887. END ;
  2888. IF i > 30 THEN
  2889. RETURN 0
  2890. END ;
  2891. RETURN rtBase + LblOff (entNm [i])
  2892. END RT_Entry ;
  2893. PROCEDURE RT_CodeEnd () : CARDINAL ;
  2894. BEGIN
  2895. IF NOT built THEN
  2896. RT_Build (0)
  2897. END ;
  2898. RETURN dataAt
  2899. END RT_CodeEnd ;
  2900. END Runtime.