Runtime.mod 130 KB

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