Runtime.mod 129 KB

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