home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / utils / asmutl / reloc23.lbr / RELOC.DQC / RELOC.DOC
Encoding:
Text File  |  1986-05-06  |  29.8 KB  |  639 lines

  1.                         A SIMPLE METHOD FOR GENERATING
  2.                            SELF-RELOCATING PROGRAMS
  3.  
  4.                                       or
  5.  
  6.              To every problem, no matter how trivial, someone can
  7.                       always find a complicated solution.
  8.  
  9.  
  10. The "What"
  11.  
  12. ┴ self-relocatinτ prograφ is¼á iε CP/═ terminology¼ onσ whicΦ wheε loadeΣ int∩ ì
  13. thσ bottoφ oµ thσ transien⌠ prograφ areß move≤ itselµ u≡ t∩ thσ to≡ oµ thσ TP┴ ì
  14. beforσá doinτ anythinτ useful«á  Thσ effec⌠ i≤ t∩ freσ u≡ thσ lowe≥ enΣ oµ thσ ì
  15. TPA for use by other programs.  This is how program monitors such as DDT work.
  16.  
  17. The "Why"
  18.  
  19. Oεá anΣá ofµ ove≥ thσ pas⌠ yea≥ ╔ havσ beeε writinτ ß supe≥á Z8░á disassemble≥ ì
  20. whicΦá include≤ thσ morσ usefu∞ feature≤ oµ WarΣ Christensen'≤á RESOURC┼á (seσ ì
  21. CPMU╟á vo∞ 42⌐ alonτ witΦ ß fe≈ feature≤ oµ m∙ owε inventioε anΣ ß bette≥ use≥ ì
  22. interface«á  I⌠á occureΣá t∩ mσ ho≈ convenien⌠ i⌠ woulΣ bσ t∩á havσá thσá dis-ì
  23. assemble≥á residσ iε higΦ memor∙ s∩ tha⌠ thσ prograφ beinτ disassembleΣá coulΣ ì
  24. occup∙ it≤ norma∞ positioε iε thσ TPA«  Iε thσ documentatioε oε RESOURCE¼ WarΣ ì
  25. says:
  26.  
  27. áááááááááá"..«á typicall∙á yo⌡á wan⌠ t∩ disassemblσ ß prograφá whicΦ ì
  28. áááááááááárun≤á a⌠á 100H¼á whicΦ i≤ werσ RESOURC┼á runs«á   BoΓá Vaε ì
  29. ááááááááááValzaΦáá woulΣá havσá solveΣá tha⌠á b∙áá makinτáá resourcσ ì
  30. áááááááááárelocatablσá anΣá movinτ itselµ u≡ unde≥ BDOS«á  ╔á wasn'⌠ ì
  31. áááááááááátha⌠ industrious."
  32.  
  33. Instead¼á hσá fiddle≤ arounΣ witΦ aε "invisiblσ offsetó whicΦ make≤ ßá prograφ ì
  34. loadeΣá ABOV┼ thσ disassemble≥ looδ a≤ thougΦ i⌠ reside≤ a⌠ 100h«á Ward'≤á owε ì
  35. word≤ describσ m∙ feeling≤ oε thσ matter║ ╔ wasn'⌠ tha⌠ industriousí  Thu≤ wa≤ ì
  36. ╔ leΣ t∩ conside≥ thσ method≤ oµ generatinτ self-relocatinτ programs.
  37.  
  38. And the "How"
  39.  
  40. Thσá enΣ resul⌠ oµ al∞ SR╨ generatioε scheme≤ i≤ aε objec⌠ prograφá comprisinτ ì
  41. threσá discretσá segments¼á normall∙ (bu⌠ no⌠ necessarily⌐á iεá thσá followinτ ì
  42. order:
  43.  
  44.      1.   Relocator module:
  45. ááááááááááááááá┴ shor⌠ sectioε oµ codσ whicΦ shift≤ thσ relocatablσ segmen⌠ t∩ ì
  46. áááááááááááááááthe top of the TPA and transfers control to it.
  47.  
  48.      2.   Relocatable program segment:
  49. áááááááááááááááThe "useful" part of the SRP.
  50.  
  51.      3.   Relocation bitmap:
  52. áááááááááááááááUseΣá b∙á thσá relocato≥á modulσ t∩ fi°á u≡á addresse≤á iεá thσ ì
  53. ááááááááááááááárelocatable program segment to reflect the new program origin.
  54.  
  55. ┴á bi⌠ oµ sneaker∙ i≤ involveΣ iε thσ generatioε anΣ usσ oµá thσá bitmap«á  I⌠ ì
  56. contain≤á onσá BI╘ fo≥ eacΦ BYT┼ oµ codσ iε thσ relocatablσá prograφá segment« ì
  57. Oncσá thσá relocatablσ codσ i≤ moveΣ int∩ placσ thσ bitma≡ i≤ scanneΣ anΣá fo≥ ì
  58. eacΦá bi⌠á set¼á ß relocatioε bia≤ i≤ addeΣ t∩ thσ correspondinτ bytσá oµá thσ ì
  59. relocateΣ code«á  Thi≤ devilishl∙ simplσ schemσ relie≤ oε thσ wa∙ iε whicΦ thσ ì
  60. bitma≡á i≤á constructed«á  Tw∩á codσá file≤ arσ prepareΣá sucΦá tha⌠á al∞á thσ ì
  61. addresse≤á iε onσ diffe≥ froφ thosσ iε thσ othe≥ b∙ aε exac⌠ multiplσá oµá 25╢ ì
  62. anΣ thσ bitma≡ i≤ constructeΣ sucΦ tha⌠ ß bi⌠ i≤ se⌠ iµ thσ correspondinτ codσ ì
  63. byte≤ arσ differen⌠ and¼á conversely¼ ß bi⌠ i≤ rese⌠ iµ thσ correspondinτ codσ ì
  64. bytes are the same.  The following example may help to explain the idea:
  65.  
  66. 0000  3A 0010      LD   A,(NUMBER+1)   ;Get whatever junk is in NUMBER
  67. 0003  CD 0011      CALL HEXOUT         ;Display high-order byte in hex
  68. 0006  3A 000F      LD   A,(NUMBER)     ;Get rest of NUMBER
  69. 0009  CD 0011      CALL HEXOUT         ;Display low-order byte in hex
  70. 000C  C3 0000      JP   0              ;End
  71. 000F          NUMBER:
  72. 000F  7A49         DEFW 7A49h          ;Arbitrary 16-bit thing
  73. 0011          HEXOUT:
  74. 0011  F5           PUSH AF             ;Copy for displaying low-order nibble
  75. 0012  0F           RRCA                ;Isolate the high-order nibble
  76. 0013  0F           RRCA
  77. 0014  0F           RRCA
  78. 0015  0F           RRCA
  79. 0016  CD 001A      CALL HEXDIG         ;Display a hex digit
  80. 0019  F1           POP  AF             ;Recover low-order bits
  81. 001A          HEXDIG:
  82. 001A  E6 0F        AND  0Fh            ;Drop spurious bits
  83. 001C  C6 90        ADD  A,90h          ;Convert hex digit to ASCII
  84. 001E  27           DAA
  85. 001F  CE 40        ADC  A,40h
  86. 0021  27           DAA
  87. 0022  5F           LD   E,A            ;Move to E for CP/M console output
  88. 0023  0E 02        LD   C,2            ;Function selector
  89. 0025  C3 0005      JP   5              ;BDOS will do the RETurn for us
  90.  
  91. With the code origin set at 0 the object code for the above program is:
  92.  
  93.           3A 10 00 CD 11 00 3A 0F 00 CD 11 00 C3 00 00 49
  94.           7A F5 0F 0F 0F 0F CD 1A 00 F1 E6 0F C6 90 27 CE
  95.           40 27 5F 0E 02 C3 05 00
  96.  
  97. Setting the code origin at 100h would generate the following object code:
  98.  
  99.           3A 10 01 CD 11 01 3A 0F 01 CD 11 01 C3 00 00 49
  100.           7A F5 0F 0F 0F 0F CD 1A 01 F1 E6 0F C6 90 27 CE
  101.           40 27 5F 0E 02 C3 05 00
  102.  
  103. The code is 40 bytes long so the bitmap would be 40 bits long, i.e. 5 bytes:
  104.  
  105.            0  0  1  0  0  1  0  0  1  0  0  1  0  0  0  0
  106.            0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0
  107.            0  0  0  0  0  0  0  0
  108.  
  109. which, in hex notation is 24 90 00 80 00.
  110. è
  111. Supposσá wσ no≈ loaΣ thσ origin-░ codσ a⌠ A60░ t∩ A627«á  Thσ bitma≡ tell≤á u≤ ì
  112. tha⌠ wσ havσ t∩ adΣ A6Φ t∩ thσ codσ byte≤ a⌠ A602¼á A605¼ A608¼ A60┬ anΣ A618« ì
  113. The properly-relocated code becomes:
  114.  
  115.           3A 10 A6 CD 11 A6 3A 0F A6 CD 11 A6 C3 00 00 49
  116.           7A F5 0F 0F 0F 0F CD 1A A6 F1 E6 0F C6 90 27 CE
  117.           40 27 5F 0E 02 C3 05 00
  118.  
  119. So what's hard about that?
  120.  
  121. Nothing¼á onσ woulΣ think«á  Unfortunately¼ thσ commonl∙ availablσ method≤ fo≥ ì
  122. buildinτá bitmap≤ anΣ constructinτ self-relocatablσ program≤ arσ s∩ mess∙ tha⌠ ì
  123. ╔á coulΣ almos⌠ sympathisσ witΦ WarΣ Christensen'≤ vie≈ oµá thσá exercise«á  ╔ ì
  124. shal∞á describσá onσ sucΦ methoΣ anΣ briefl∙ mentioε tw∩ other≤ bu⌠á witΦá thσ ì
  125. forewarninτá tha⌠ ╔ havσ neve≥ an∙ oµ theφ anΣ tha⌠ ╔ aφ relyinτ oε sucΦ scan⌠ ì
  126. documentatioεá a≤ ╔ havσ availablσ anΣ oε wha⌠ ╔ coulΣ learε b∙á halfheartedl∙ ì
  127. playinτá arounΣá witΦ them«á  Halfheartedly┐á  Yes¼á afte≥ al∞ i⌠á wa≤á fairl∙ ì
  128. obviou≤ tha⌠ ╔ woulΣ neve≥ seriousl∙ emplo∙ eithe≥ methoΣ anΣ m∙ onl∙ interes⌠ ì
  129. iε theφ wa≤ attemptinτ t∩ removσ thσ wildes⌠ inaccuracie≤ froφ thesσ notes.
  130.  
  131. The GENMOD method
  132.  
  133. Normall∙á distributeΣ witΦ MP/═ bu⌠ sometime≤ availablσ witΦ CP/═ i≤ ß utilit∙ ì
  134. prograφá calleΣ GENMOD«á  Thσ step≤ iε thσ GENMO─ techniquσ fo≥ generatinτá aε ì
  135. SRP are something like this:
  136.  
  137.      1    Assemblσ thσ relocatablσ codσ segmen⌠ iε thσ norma∞ wa∙ specifyinτ ß ì
  138. áááááááááácode origin of 100h and generating a .HEX file.
  139.  
  140.      2    Modify the source program changing the code origin to 0.
  141.  
  142.      3á   Re-assemblσá thσá relocatablσ codσ segmen⌠ int∩ ßá (different⌐á .HE╪ ì
  143. ááááááááááoutput file.
  144.  
  145.      4    Concatenatσá thσ tw∩ .HE╪ files«á  PI╨ ma∙ bσ useΣ t∩ d∩ thi≤ fairl∙ ì
  146. ááááááááááneatly, e.g.
  147.  
  148.                     B>PIP RP.HEX=ORG000.HEX,ORG100.HEX
  149.  
  150.      5    Ruε GENMO─ agains⌠ thσ concatenateΣ .HE╪ file, e.g.:
  151.  
  152.                     B>GENMOD RP.HEX RP.COM
  153.  
  154.           GENMOD responds with:
  155.  
  156.                     REL MOD END  002A
  157.                     REL MOD SIZE 007F
  158.                     ABS MOD END  097F
  159.  
  160.           GENMO─á put≤á thσ relocatablσ codσ segmen⌠ a⌠ 200Φ followeΣá b∙á tw∩ ì
  161. áááááááááábyte≤á whosσ purposσ ╔ canno⌠ fathom«á Immediatel∙ thereafte≥á come≤ ì
  162. ááááááááááthσá bitmap«á  Thσ importan⌠ piecσ oµ informatioε abovσ i≤ thσá "RE╠ ì
  163. ááááááááááMO─ ENDó address«á  AdΣ 200Φ t∩ tha⌠ valuσ anΣ yo⌡ havσ thσá addres≤ ì
  164. ááááááááááof the start of the bitmap.
  165. è
  166.      6   áUsσá DDT¼á ZSI─á o≥ whateve≥ t∩ loaΣ thσ relocato≥ modulσ belo≈á thσ ì
  167. áááááááááárelocatablσá codσá segmen⌠ anΣ bitmap«á Someho≈ tel∞ you≥á relocato≥ ì
  168. áááááááááámodulσá thσ lengtΦ oµ you≥ relocatablσ codσ segmen⌠ and/o≥ wherσá t∩ ì
  169. ááááááááááfinΣá thσá bitmap«á  Exi⌠ froφ thσ debugge≥ anΣ SAV┼á thσá SR╨á codσ ì
  170. ááááááááááimage.
  171.  
  172.                     B>ZSID RP.COM
  173.                     IRELOC.COM
  174.                     R
  175.                     {set up relocator information}
  176.                     G0
  177.                     B>SAVE nn RP.COM
  178.  
  179.           and there you have it!
  180.  
  181. The REL.UTL method
  182.  
  183. Anothe≥ methoΣ fo≥ generatinτ SRP≤ i≤ offereΣ b∙ Rober⌠ vaε ValzaΦ (CPMU╟á vo∞ ì
  184. 38⌐ wh∩ provide≤ ß .UT╠ prograφ whicΦ run≤ unde≥ thσ contro∞ oµ SI─ o≥ ZSI─ t∩ ì
  185. handlσá somσá oµ thσ step≤ describeΣ above«á  Whilσ differen⌠ iεá detail¼á thσ ì
  186. methoΣá i≤ oµ thσ samσ degreσ oµ complexit∙ a≤ thσ GENMO─átechiquσ.
  187.  
  188. The macro method
  189.  
  190. Usinτá ß fairl∙ powerfu∞ macr∩ assemble≥ sucΦ a≤ Digita∞ Research'≤ MA├ i⌠á i≤ ì
  191. possiblσ t∩ definσ macro≤ t∩ generatσ ß bitmap«á  ┴ finσ examplσ oµ thσ usσ oµ ì
  192. this technique is provided by Gary Novosielski's UNSPOOL program.
  193.  
  194.                 ;       GENERATE A LABEL OF THE FORM ??RNN TO TAG AN
  195.                 ;       ADDRESS REQUIRING RELOCATION:
  196.                 RTAG    MACRO   LBL,VAL
  197.                 ??R&LBL EQU     VAL
  198.                         ENDM
  199.                 ;
  200.                 ;       FLAG <INST> AS A RELOCATABLE INSTRUCTION
  201.                 ;       <INST> IS OF THE FORM: <MNE     OP1[,OP2]>
  202.                 R       MACRO   INST
  203.                 @RLBL   SET     @RLBL+1
  204.                         RTAG    %@RLBL,%2+$-@BASE
  205.                         INST-@BASE
  206.                         ENDM
  207.                 ;
  208.                 ; DURING BIT MAP CONSTRUCTION, GET THE NEXT R-TAGGED
  209.                 ; ADDRESS VALUE:
  210.                 NXTRLD  MACRO   NN
  211.                 @RLD    SET     ??R&NN
  212.                 @NXTRLD SET     @NXTRLD + 1
  213.                         ENDM
  214.  
  215. In the code to be relocated we see these macros applied to instructions:
  216.  
  217. @BASE:                          ;START OF CODE TO BE RELOCATED
  218.                 .       .       .
  219.  
  220.                  R      <JZ     SYSREQ>
  221.  0038+=         ??R2 EQU        56
  222.  0336+CA9801            JZ      SYSREQ-@BASE
  223.  
  224. Ever∙ instructioε whicΦ wil∞ neeΣ t∩ bσ modifieΣ durinτ relocatioε i≤ codeΣ a≤ ì
  225. ßá paramete≥á t∩ thσ "Ró macr∩ whicΦ generate≤ aε assembly-timσá symbo∞á whosσ ì
  226. valuσá i≤á thσ offse⌠ froφ thσ beginninτ oµ thσ relocatablσ codσ oµ thσá thirΣ ì
  227. instructioεá byte«á  A⌠á thσ samσ timσ thσ operanΣ i≤ modifieΣá t∩á ßá simila≥ ì
  228. offset«á  A⌠á thσá enΣá oµ thσ relocatablσ codσ i≤á thσá assembly-timσá bitma≡ ì
  229. generator:
  230.  
  231.                 ;       BUILD THE RELOCATION INFORMATION INTO A
  232.                 ; BIT MAP FOLLOWING THE CODE.
  233.                 ;
  234.  0000 #         @X      SET     0
  235.  0000 #         @BITCNT SET     0
  236.  0002 #         @RLD    SET     ??R1
  237.  0002 #         @NXTRLD SET     2
  238.                         RTAG    %@RLBL+1,0FFFFH ;DEFINE ONE MORE SYMBOL
  239.  FFFF+=         ??R89 EQU       0FFFFH
  240.                 ;
  241.                         REPT    SEGLEN+8
  242.                         IF      @BITCNT>@RLD
  243.                         NXTRLD  %@NXTRLD        ;;NEXT VALUE
  244.                         ENDIF
  245.                         IF      @BITCNT=@RLD
  246.                 @X      SET     @X OR 1         ;;SET LOW BIT
  247.                         ENDIF
  248.                 @BITCNT SET     @BITCNT + 1
  249.                         IF      @BITCNT MOD 8 = 0
  250.                         DB      @X              ;;DEFINE THE BYTE
  251.                 @X      SET     0           ;;CLEAR HOLD VARIABLE FOR MORE
  252.                         ELSE
  253.                 @X      SET     @X SHL 1        ;;NOT 8 YET. MOVE OVER.
  254.                         ENDIF
  255.                         ENDM
  256.  
  257. Thσá outpu⌠á oµá thi≤ in-linσ macr∩ i≤ ß serie≤á oµá "DBóá pseudo-instruction≤ ì
  258. defining the bitmap.
  259.  
  260. A≤ coded¼á thi≤ techniquσ wil∞ NO╘ worδ fo≥ an∙ oµ thσ Z8░ instruction≤á whicΦ ì
  261. havσá 16-bi⌠á opcode≤ bu⌠ ß simplσ rearrangemen⌠ oµ thσ "Ró macr∩ woulΣá solvσ ì
  262. thi≤ problem:
  263.  
  264.                 R       MACRO   INST
  265.                 @RLBL   SET     @RLBL+1
  266.                         INST-@BASE
  267.                         RTAG    %@RLBL,$-%1-@BASE
  268.                         ENDM
  269. è
  270. Thσá macr∩ techiquσ i≤ probabl∙ thσ easies⌠ oµ thσ threσ t∩ usσ provideΣá tha⌠ ì
  271. onσá ha≤ acces≤ t∩ ß gooΣ macr∩ assembler«á  ╔ usσ Microsoft'≤ MACRO-8░á (M80⌐ ì
  272. fo≥á al∞á oµ m∙ assembly-languagσ programminτ anΣ (withou⌠ puttinτ i⌠á t∩á thσ ì
  273. test⌐á ╔ doub⌠ tha⌠ M8░ coulΣ handlσ thσ task«á  Nevertheles≤ thσ techniquσ i≤ ì
  274. viable¼á elegan⌠á anΣ cleve≥ althougΦ thσ complexit∙ oµ thσ macro≤á make≤á thσ ì
  275. actua∞á assembl∙á ver∙ lonτ anΣ ╔ suspec⌠ tha⌠ witΦ ß largσ prograφ onσá coulΣ ì
  276. ruεá int∩á memory-spacσá problem≤á becausσ oµá thσá largσá numbe≥á oµá symbol≤ ì
  277. generated«á  Eveε iµ ╔ haΣ MA├ ╔ woulΣ no⌠ usσ thi≤ techniquσ becausσ ╔ d∩ al∞ ì
  278. oµá m∙á assembly-languagσá programminτá iε Ziloτá mnemonics«á  Thesσá arσá no⌠ ì
  279. supporteΣ b∙ MAC«á  Also¼á wheε ╔ firs⌠ wrotσ thi≤ articlσ ╔ wa≤ no⌠ awarσá oµ ì
  280. an∙á implementatioε oµ thσ macr∩ techniquσ anΣ ╔ doub⌠ tha⌠ ╔ woulΣ havσá beeε ì
  281. smart enough to devise those very sophisticated macros.
  282.  
  283. There has to be a Better Way
  284.  
  285. A⌠á thσá timσá oµá writinτá thi≤ articlσ ╔ havσá codeΣá anΣá testeΣá thσá maiε ì
  286. disassembl∙ routinσ anΣ thσ symbo∞ tablσ handlinτ routine≤ fo≥ m∙ disassemble≥ ì
  287. anΣ ╔ havσ severa∞ othe≥ pre-coded¼ testeΣ module≤ kickinτ arounΣ iε librarie≤ ì
  288. read∙á t∩ bσ incorporateΣ int∩ thσ program«á  Nevertheles≤ ╔ expec⌠ tha⌠ some- ì
  289. wherσá betweeε 2░ anΣ 4╡ assembly/testinτ cycle≤ wil∞ bσ requireΣ t∩á completσ ì
  290. thσá projec⌠á anΣ thσ though⌠ oµ goinτ througΦ thσ abovσ procedurσá morσá thaε ì
  291. oncσ o≥ twicσ woulΣ bσ sufficien⌠ t∩ discouragσ mσ froφ eve≥ finishinτ it.
  292.  
  293. Wha⌠ ╔ wanteΣ wa≤ aε automatiπ methoΣ oµ generatinτ aε SRP«  Ideally¼ ╔ shoulΣ ì
  294. bσ ablσ t∩ d∩ somσ oµ thσ assembly/testinτ cycle≤ witΦ thσ prograφ runninτá iε ì
  295. thσ low-memor∙ enΣ oµ thσ TP┴ anΣ theε withou⌠ changinτ thσ program¼á generatσ ì
  296. aε SR╨ forφ witΦ somσ assurancσ tha⌠ i⌠ wil∞ stil∞ work«
  297.  
  298. The Better Way
  299.  
  300. Thσá onσá essentia∞á too∞á fo≥ thσ SR╨ generatioε schemσ tha⌠ ╔á aφá abou⌠á t∩ ì
  301. describσá i≤á aεá assembler-linke≥ combinatioε whicΦá let≤á yo⌡á specif∙á loaΣ ì
  302. addresse≤ a⌠ linδ time« ╔ havσ two¼ namel∙ MACRO-8░ anΣ LINK-8░ froφ Microsof⌠ ì
  303. anΣ ASMBL/┌ anΣ LINK/┌ froφ Ithacß InterSystem≤ bu⌠ ╔ havσ n∩ doub⌠ tha⌠ therσ ì
  304. arσ other≤ whicΦ wil∞ worδ jus⌠ a≤ well.
  305.  
  306. Thσá essencσ oµ thσ schemσ i≤ t∩ le⌠ thσ linke≥ d∩ thσ worδ oµ puttinτ thσ re-ì
  307. locatablσ codσ iε thσ righ⌠ spo⌠ rathe≥ thaε doinτ tw∩ assemblies¼á anΣ t∩ usσ ì
  308. a program to match the two code images and append a bitmap.
  309.  
  310. ┴ certaiε disciplinσ i≤ requireΣ wheε codinτ ß prograφ whicΦ wil∞ late≥ becomσ ì
  311. self-relocating«á  Thi≤á disciplinσá ha≤á n∩ effec⌠ oε thσá executioεá oµá thσ ì
  312. program but provides information to the relocator module.
  313.  
  314.      ▒    AvoiΣ thσ usσ oµ "absoluteó prograφ segments╗ the∙ arσ no⌠ necessar∙ ì
  315.           anΣá unles≤á yo⌡ arσ VER┘ careful¼á wil∞ interferσ witΦ thσá linkinτ ì
  316.           order.
  317.  
  318.      2    Fo≥á thσ benefi⌠ oµ thσ relocato≥ modulσ thσ firs⌠ tw∩ item≤ iεá thσ ì
  319. áááááááááárelocatable program must be:
  320.  
  321. áááááááááááááááa)áááßá worΣá containinτá thσ addres≤á oµá thσá firs⌠ ì
  322. áááááááááááááááááááácharacter past the end of the program,
  323.  
  324. èáááááááááááááááb)áááßá jum≡á t∩ thσ entr∙ poin⌠ oµá thσá relocatablσ ì
  325. ááááááááááááááááááááprogram segment.
  326.  
  327.           MACRO-80 Example:
  328.  
  329.                          .Z80           ;I always use Zilog mnemonics
  330.                 cr    equ     0dh
  331.                 lf    equ     0ah       ;These statements don't generate
  332.                 ckpt  equ     144       ;any code so they can go here
  333.                 zap   macro   field,len
  334.                       . . .
  335.                       endm
  336.  
  337.                       dseg              ;Normally the first loaded
  338.                 $memry::
  339.                       defs    2         ;Address of end of program
  340.                       j≡áá    start     ;Used and modified by relocator
  341.  
  342.                 ;Rest of program as it would normally be written.
  343.                       . . .
  344.                 Start:
  345.                       . . .
  346.                       end     start
  347.  
  348. ááááááááááIteφá (a⌐ i≤ useΣ b∙ thσ relocato≥ modulσ t∩ calculatσ thσ lengtΦ oµ ì
  349. ááááááááááthσ relocatablσ prograφ segmen⌠ anΣ thencσ t∩ determinσ thσá addres≤ ì
  350. áááááááááát∩á whicΦ t∩ movσ thσ relocatablσ code«á Thσ worΣ itselµ wil∞ no⌠ bσ ì
  351. áááááááááárelocated.
  352.  
  353.           Iµá yo⌡ arσ usinτ thσ Microsof⌠ assemble≥ anΣ linke≥ theεá iteφá (a⌐ ì
  354. áááááááááácan be a source statement of the form:
  355.  
  356.                     $MEMRY::  defs      2
  357.  
  358. ááááááááááa≤á iεá thσ abovσ examplσ anΣ LINK-8░ wil∞ kindl∙ fil∞ thσ worΣá in« ì
  359. ááááááááááLINK/┌á doesn'⌠ providσ thi≤ conveniencσ s∩ usσ ßá sourcσá statemen⌠ ì
  360. áááááááááálike:
  361.  
  362.                               dw        $END$
  363.  
  364.           wherσá $ENDñá i≤ ß labe∞ declareΣ jus⌠ beyonΣ thσ las⌠ bytσá iεá thσ ì
  365. ááááááááááprogram«á  (Makσá surσ tha⌠ i⌠ i≤ iε thσ last-loadeΣ segmen⌠ oµá thσ ì
  366. áááááááááálast-linked module!)
  367.  
  368.           Thσá relocato≥á prograφ extract≤ thσ addres≤ fielΣ oµ iteφ (b⌐á and¼ ì
  369. ááááááááááwheεá i⌠á ha≤ completeΣ thσ relocation¼á transfer≤ contro∞á t∩á tha⌠ ì
  370. ááááááááááaddres≤ iε thσ relocateΣ codσ segment«á  Iε thσ meantime¼ thσ targe⌠ ì
  371. ááááááááááoµá thσ jum≡ instructioε i≤ modifieΣ t∩ jum≡ t∩ thσ BDO╙á whilσá thσ ì
  372. áááááááááájum≡á a⌠ addres≤ ╡ i≤ modifieΣ t∩ jum≡ t∩ thi≤ instructioε s∩ tha⌠ ß ì
  373. ááááááááááprogram loaded into low memory sees a smaller TPA.
  374.  
  375. ááááááááááConfused┐á  Perhap≤á anothe≥á examplσ wil∞ help«á  Supposσá tha⌠á wσ ì
  376. áááááááááárelocatσá ß prograφ whicΦ i≤ 42BΦ byte≤ lonτ anΣ thσ BDO╙ start≤á a⌠ ì
  377. ááááááááááC200h«  Beforσ relocation¼ thσ scenari∩ i≤ somethinτ likσ this:
  378.  
  379. è               0005      JP   C200      Jump into BDOS
  380.                 .         .     .
  381.                0100                     Start of relocator module
  382.                 .         .     .
  383.                01FE      DEFW 062B      Address of first byte past program
  384.                0200      JP   NTRYPT    Jump to entry point of relocatable
  385.                                         program segment
  386.                 .         .     .
  387.                02A7 NTRYPT:             Program start address
  388.                 .         .     .
  389.                062A                     Last byte of program
  390.                062B                     Start of bitmap
  391.                 .         .     .
  392.                C200                     BDOS
  393.  
  394.           After relocation things will be a little different:
  395.  
  396.                0005      JP   BD00      Indirect jump to BDOS
  397.                 .        .      .
  398.                BD00      JP   C200      Actual jump into BDOS
  399.                 .        .      .
  400.                BDA7 NTRYPT:             Relocator exits to this address
  401.                 .        .      .
  402.                C12A                     End of program
  403.                 .        .      .
  404.                C200                     BDOS
  405.  
  406. Providinτá tha⌠ ß prograφ ha≤ thσ requireΣ item≤ coded¼á i⌠ caεá bσá converteΣ ì
  407. into a self-relocating program without re-assembly.  The steps are:
  408.  
  409.      1ááááGeneratσ ß .CO═ filσ b∙ linkinτ thσ relocato≥ modulσ anΣ relocatablσ ì
  410. áááááááááácodσá segment«á  Specif∙ ß loaΣ addres≤ oµ 01F┼ fo≥ thσá relocatablσ ì
  411. áááááááááácodσ segment«
  412.  
  413.      2    Generatσá anothe≥á .CO═ filσ b∙ linkinτ onl∙á thσá relocatablσá codσ ì
  414. áááááááááásegment specifying a load address of 02FE.
  415.  
  416.      3    Run the RELOC program to generate the self-relocating program.
  417.  
  418. Ste≡á ▒á effectivel∙á concatenate≤ thσ relocato≥ modulσá anΣá thσá relocatablσ ì
  419. prograφá segment«á  Thσ linke≥ put≤ thσ pointe≥ t∩ thσ enΣ oµ thσá prograφá a⌠ ì
  420. (01FF,01FE⌐á anΣ thσ jum≡ instructioε a⌠ 0200«á  Thσ relocato≥ modulσ caε theε ì
  421. finΣá thσ prograφ star⌠ addres≤ a⌠ (0202,0201)«á  Notσ tha⌠ thσ end-of-prograφ ì
  422. pointer does not become part of the relocated program.
  423.  
  424. Ste≡ ▓ create≤ ß seconΣ imagσ oµ thσ relocatablσ prograφ segmen⌠ sucΦ tha⌠ al∞ ì
  425. addresses differ by 100h from those in the file created in step 1.
  426.  
  427. Step 3 adds a bitmap.
  428.  
  429. No⌠á onl∙ arσ therσ fewe≥ step≤ thaε iε thσ GENMO─ anΣ REL.UT╠ method≤ bu⌠á n∩ ì
  430. modificatioε t∩ thσ prograφ sourcσ codσ i≤ necessar∙ anΣ thσ wholσ proces≤ caε ì
  431. be performed without operator intervention with a .SUB file like this:
  432.  
  433.           :Generate a self-relocating version of the $1 program
  434. è          L80 reloc,/p:1fe,$1,Org200/n/e
  435.           L80 /p:2fe,$1,Org300/n/e
  436.           RELOC $1
  437.           ERA org?00.com
  438.  
  439. where $1 will be substituted by the name of the program.  Something like:
  440.  
  441.           SUBMIT RELOC RP
  442.  
  443. doe≤ i⌠ allíá  Easy«á  Oncσ ╔ decideΣ ho≈ i⌠ coulΣ bσ done¼á implementatioε oµ ì
  444. thσá SRP-generatioεá schemσ tooδ ß hel∞ oµ ß lo⌠ les≤ timσ thaεá writinτá thi≤ ì
  445. documentation!
  446.  
  447. And that's not all...
  448.  
  449. Giveεá ßá systeφ sucΦ a≤ thσ above¼á i⌠ caε bσ madσ t∩ d∩ othe≥á things«á  Fo≥ ì
  450. example¼á somσ program≤ returε contro∞ t∩ thσ CC╨ wheε the∙ finisΦ insteaΣá oµ ì
  451. exitinτá t∩ CP/═ b∙ doinτ ß "warφ boot"╗á iε relocatinτ sucΦ ß prograφ t∩ higΦ ì
  452. memory¼á i⌠á woulΣ neeΣ t∩ bσ placeΣ jus⌠ belo≈ thσ CC╨ insteaΣ oµ jus⌠á belo≈ ì
  453. thσ BDOS«á  Again¼á withou⌠ to∩ mucΦ effort¼á thσ systeφ caε als∩ providσá thσ ì
  454. abilit∙á fo≥ ß use≥ t∩ explicitl∙ nominatσ wherσ thσ relocateΣ prograφ codσ i≤ ì
  455. t∩á reside«á  Anothe≥ worthwhilσ facilit∙ i≤ tha⌠ oµ havinτ ß prograφá overla∙ ì
  456. par⌠ oµ CP/═ itself¼á particularl∙ thσ BIOS«á  Therσ i≤ no⌠ ß lo⌠ oµ rooφ oε ß ì
  457. standarΣ singlσ densit∙ flopp∙ disδ fo≥ ß BIO╙ oµ an∙ sophisticatioε bu⌠ i⌠ i≤ ì
  458. certainl∙á possiblσ t∩ overla∙ ß minima∞ BIO╙ witΦ ß self-relocatinτ onσá froφ ì
  459. thσ disk'≤ datß track≤ whicΦ caε contaiε ß hos⌠ additiona∞ features«á  Perhap≤ ì
  460. aεá eveεá morσá genera∞ applicatioε fo≥ thi≤ facilit∙ i≤ tha⌠á i⌠á offer≤á thσ ì
  461. abilt∙á t∩á tes⌠ BIO╙ modification≤ withou⌠ goinτ througΦ ß serie≤á oµá systeφ ì
  462. generations.
  463.  
  464. T∩á implemen⌠á thesσ extrß capabilities¼á thσ SR╨ generato≥ ha≤á t∩á recognisσ ì
  465. command-linσá directive≤á froφá thσá use≥ anΣá pas≤á thσá informatioεá t∩á thσ ì
  466. relocato≥ modulσ whicΦ iε turε mus⌠ bσ cleve≥ enougΦ t∩ ac⌠ oε it«á  Thi≤ doe≤ ì
  467. impl∙á ß degreσ oµ interdependencσ betweeε thσ SR╨ generato≥ anΣ thσ relocato≥ ì
  468. modulσá iε tha⌠ thσ SR╨ generato≥ need≤ t∩ kno≈ somethinτ abou⌠ thσá structurσ ì
  469. oµá thσ relocator¼á bu⌠ ╔ believσ tha⌠ thσ sligh⌠ los≤ oµ generalit∙á i≤á wel∞ ì
  470. compensateΣ b∙ thσ extrß versatilit∙ oµ thσ package.
  471.  
  472. A≤áá supplied¼áá thσá SR╨á generato≥á recognise≤á thσá followinτá command-linσ ì
  473. directives:
  474.  
  475. RELOC X     á            Generatσá X.CO═á whicΦá wil∞ movσ itselµ u≡á t∩á jus⌠ ì
  476. ááááááááááááááááááááááááábelo≈ thσ BDOS¼ overlayinτ somσ o≥ al∞ thσ CCP«  Thi≤ ì
  477. ááááááááááááááááááááááááái≤ thσ mos⌠ commoε usagσ oµ thσ SR╨ generato≥ anΣ ha≤ ì
  478. áááááááááááááááááááááááááthσ simples⌠ commanΣ string«  Prograφ ╪ i≤ assumeΣ t∩ ì
  479. áááááááááááááááááááááááááreturε contro∞ t∩ CP/═ b∙ doinτ ß "warφá boot"«á  Thσ ì
  480. áááááááááááááááááááááááááBDO╙á jum≡ a⌠ memor∙ location≤ ╡ througΦ ╖ i≤ altereΣ ì
  481. ááááááááááááááááááááááááát∩ reflec⌠ ß smalle≥ TP┴ size.
  482.  
  483. RELOC X -    ááááááááááááA≤á fo≥ thσ previou≤ commanΣ forφ excep⌠á tha⌠á X.CO═ ì
  484. áááááááááááááááááááááááááwil∞á relocatσá itselµ t∩ jus⌠ belo≈ thσ CC╨ anΣá thσ ì
  485. áááááááááááááááááááááááááBDO╙ jum≡ a⌠ locatioε ╡ wil∞ no⌠ bσá affected«á  I'l∞ ì
  486. áááááááááááááááááááááááááexplain why later.
  487.  
  488. RELOC X addr             "Addróá i≤á ßá hexadecima∞ numbe≥ whicΦá mus⌠á bσá aε ìèááááááááááááááááááááááááámultiplσá oµá 100h«á  X.CO═ wil∞ movσ itselµ t∩á tha⌠ ì
  489. áááááááááááááááááááááááááaddress.
  490.  
  491. Thσá "Xó prograφ generateΣ b∙ thσ abovσ commanΣ form≤ wil∞ bσá executeΣá afte≥ ì
  492. relocatioεá ha≤á beeεá donσ anΣ thσ codinτ rule≤ describeΣá iεá thσá precedinτ ì
  493. sectioε must be obeyed for the relocation system to work.
  494.  
  495. Onl∙á thσá "normalóá relocatioε t∩ jus⌠ belo≈ thσ BDO╙ cause≤á thσá BDO╙á jum≡ ì
  496. instructioεá a⌠á memor∙á location≤á 5-╖ t∩ bσ modifieΣ t∩á reflec⌠á ßá reduceΣ ì
  497. transien⌠ prograφ area«á  Thσ decisioε t∩ omi⌠ thσ modificatioε iε othe≥ case≤ ì
  498. wa≤ fairl∙ arbitrar∙ bu⌠ wa≤ takeε iε thσ interes⌠ oµ generality«á  ╔ diΣá no⌠ ì
  499. wisΦá t∩ imposσ an∙ restrictioε oε thσ prograφ beinτ relocateΣ anΣá considereΣ ì
  500. thσ possibilit∙ tha⌠ thσ prograφ migh⌠ rel∙ oε thσ jum≡ instructioε beinτ lef⌠ ì
  501. unchangeΣá fo≥ an∙ oµ ß variet∙ oµ reasons«á  Oµ course¼á therσ i≤ nothinτá t∩ ì
  502. sto≡ thσ relocateΣ prograφ froφ modifyinτ thσ BDO╙ jum≡ itself«á  Also¼á therσ ì
  503. i≤á ßá public-domaiε utilit∙ provideΣ b∙ Johε Woolne≥ fo≥ protectinτá thσá CC╨ ì
  504. froφá beinτ overlaiΣ b∙ transien⌠ program≤ s∩ tha⌠ therσ exist≤ ßá methoΣá fo≥ ì
  505. achievinτá thσ samσ resul⌠ a≤ woulΣ bσ attaineΣ b∙ havinτ thσ relocato≥ adjus⌠ ì
  506. the jump instruction:
  507.  
  508.      1.   Install John Woolner's CCP protection,
  509.      2.   Use the command form RELOC X.COM (i.e. no second parameter).
  510.  
  511. Thσá SR╨ generato≥ als∩ recognise≤ threσ variant≤ oµ anothe≥ commanΣ forφá fo≥ ì
  512. whicΦá thσ codinτ rule≤ arσ slightl∙ different«á  Thσ worΣ pointinτ t∩á "freeó ì
  513. memor∙á i≤á stil∞ requireΣ bu⌠ thσ extrß jum≡ instructioε a⌠ thσ heaΣá oµá thσ ì
  514. relocatablσ codσ neeΣ not¼á anΣ generall∙ mus⌠ not¼á bσ supplied«  Thσ commanΣ ì
  515. form is
  516.  
  517. RELOC X CCP      or      RELOC X BDOS      or     RELOC X.COM BIOS
  518.  
  519. Thσá X.CO═ filσ generateΣ b∙ thesσ command≤ wil∞ relocatσ s∩ a≤á t∩á overwritσ ì
  520. thσá CCP¼á BDO╙á o≥á BIO╙ respectively«á  Contro∞ wil∞ NO╘ bσá passeΣá t∩á thσ ì
  521. relocateΣá code«á  Instead¼á thσ relocato≥ modulσ wil∞ exi⌠ t∩ thσ CC╨ withou⌠ ì
  522. doinτ ß warφ boo⌠ s∩ tha⌠ thσ relocateΣ codσ wil∞ remaiε iε memory«  Notσ tha⌠ ì
  523. thσá destinatioε addres≤ fo≥ thσ relocateΣ codσ i≤ calculateΣ froφ thσá targe⌠ ì
  524. oµá thσ jum≡ instructioε a⌠ memor∙ locatioε ╡ anΣ wil∞ bσ wronτ iµá tha⌠á jum≡ ì
  525. instruction has been altered.
  526.  
  527. Copyright notice
  528.  
  529. Thesσ program≤ havσ beeε submitteΣ t∩ thσ publiπ domaiε viß Bil∞ Bolton'≤ RCP═ ì
  530. systeφ anΣ comprisσ ß systeφ fo≥ simpl∙ generatinτ self-relocatinτ program≤ b∙ ì
  531. ßá methoΣ whicΦ relie≤ oε ß linke≥ t∩ generatσ tw∩ objec⌠ codσ file≤ whicΦ arσ ì
  532. theεá processeΣ t∩ yielΣ aε objec⌠ prograφ consistinτ oµá ßá relocator¼á user-ì
  533. supplieΣá objec⌠á codσá anΣá ß relocatioεá bitmap«á  Thesσá program≤á anΣá thσ ì
  534. accompanyinτá documentatioε ma∙ bσ freel∙ distributeΣ iε origina∞ o≥á modifieΣ ì
  535. forφ subjec⌠ t∩ thσ followinτ conditions:
  536.  
  537. 1.áááAlthougΦá therσ i≤ n∩ restrictioε oε thσ salσ oµ self-relocatinτ program≤ ì
  538. ááááágenerateΣá b∙á thσ methoΣ describeΣ herein¼á thesσ program≤á o≥á variant≤ ì
  539. áááááthereoµá ma∙ no⌠ bσ solΣ a≤ par⌠ oµ an∙ prograφ packagσá withou⌠á writteε ì
  540. ááááápermissioεá froφ thσ author«á  Neithe≥ ma∙ an∙ prograφ o≥ prograφ packagσ ì
  541. áááááwhicΦá i≤ dependen⌠ fo≥ it≤ operatioε oε thσ usσ oµ thi≤ methoΣá bσá solΣ ì
  542. áááááwithou⌠á sucΦ permission«á  Iε othe≥ word≤ yo⌡ ma∙á sel∞á self-relocatinτ ìèáááááprograms but not the method of generating them. 
  543.  
  544. 2.áááThσá author'≤á namσá mus⌠á bσ retaineΣ iε al∞ sourcσá codσá (origina∞á o≥ ì
  545. ááááámodified⌐á anΣá a≤ aε acknowlegemen⌠ iε an∙ messagσá displayeΣá b∙á thesσ ì
  546. áááááprogram≤ o≥ variant(s⌐ thereof«  Aε acknowledgemen⌠ givinτ credi⌠ fo≥ thσ ì
  547. ááááámethoΣá shal∞ contaiε thσ author'≤ namσ o≥ thσ word≤ "H-┬ methodó o≥á thσ ì
  548. áááááwords "Ashby method".
  549.  
  550. 3.áááThi≤ copyrigh⌠ noticσ mus⌠ bσ includeΣ iε anΣ retaineΣ iε al∞ sourcσ codσ ì
  551. áááááand documentation pertaining to this system.
  552.  
  553.                              John Hastwell-Batten
  554.                                38 Silvia Street
  555.                                Hornsby NSW 2077
  556.                                   AUSTRALIA
  557.                                 (02) 477 4225
  558.  
  559.                               1st November, 1982
  560.  
  561. Acknowledgement
  562.  
  563. Iεá testinτ thosσ relocation≤ whicΦ overla∙ specifieΣ portion≤ oµ CP/═ ╔á havσ ì
  564. madσá usσ oµ Johε Woolner'≤ CC╨ protectioε scheme obtaineΣ vißá Bil∞á Bolton'≤ ì
  565. RCP═á system«á  Withou⌠á thσ CC╨ protectioε thσ verificatioε oµ thσá relocato≥ ì
  566. modulσá woulΣ havσ beeε exceedingl∙ difficul⌠ a≤ thσ standarΣ prograφá testinτ ì
  567. tool≤á al∞á overla∙á thσ CC╨ which¼á iε thσ casσ oµá thσá CP/═á overlays¼á thσ ì
  568. relocator expects to be intact.
  569.  
  570. èSYSTEM PROGRAMS AND FILES
  571.  
  572. Therσá arσ ß lo⌠ oµ thing≤ calleΣ "RELOCó iε thi≤ systeφ anΣ thσ variou≤ file≤ ì
  573. arσá distinquisheΣá b∙ thσ filenamσ extension«á  Thσ actua∞ SR╨á generato≥á i≤ ì
  574. writteεá iεá BDS-├á bu⌠ i⌠ coulΣ equall∙ (o≥ almos⌠á equally⌐á bσá writteεá iε ì
  575. anythinτá else«á  Thσ relocato≥ modulσ i≤ writteε iε Z8░ assembl∙ languagσ anΣ ì
  576. jus⌠ abou⌠ thσ onl∙ restrictioε upoε i⌠ i≤ tha⌠ i⌠ mus⌠ bσ a⌠ mos⌠ 0FEΦá byte≤ ì
  577. long«á  ╔á playeΣ arounΣ witΦ somσ funn∙ Z8░ instruction≤ whilσ ╔ wa≤á writinτ ì
  578. thσá relocato≥á anΣá neve≥á bothereΣ t∩á re-prograφá it«á  (Eveεá withou⌠á thσ ì
  579. "undocumentedó Z8░ instructions¼á thσ supplieΣ codeΣ i≤ highl∙ Z8░á dependent« ì
  580. For an example of an 8080-coded relocator, see Robert van Valzah's REL.UTL) 
  581.  
  582. These are the files:
  583.  
  584.      RELOC.MAC      Source of relocator module.
  585.  
  586.      RELOC.REL      Assembled relocator module.
  587.  
  588.      RELOC.C        BDS-C source of program generator.
  589.  
  590.      RELOC.COM      The SRP generator.
  591.  
  592.      RELOC.SUB      Submit file to automate the process of generating a 
  593.                     self-relocating program.
  594.  
  595.      RELOC.DOC      What you have been reading all this time.
  596.  
  597. System also needs:
  598.  
  599.      M80 and L80    Microsoft's MACRO-80 assembler and LINK-80 link/loader
  600.                                            or
  601.      ASMBL/Z        Ithaca InterSystems' version of the same thing
  602.      and LINK/Z
  603.                                            or
  604.                     any other assembler-linker combination which allows load
  605.                     addresses to be specified at link time.
  606.  
  607. Thσá submi⌠ filσ RELOC.SU┬ i≤ se⌠ u≡ t∩ proces≤ ß prograφ comprisinτ ßá singlσ ì
  608. module.  For multi-module programs just expand the linker command lines.
  609.  
  610. RELOC.SU┬ wil∞ accep⌠ onσ o≥ tw∩ arguments«á  Iµ supplied¼ thσ seconΣ argumen⌠ ì
  611. i≤á passeΣá t∩á thσá SR╨ generato≥ prograφ alonτá witΦá thσá (required⌐á firs⌠ ì
  612. argument.
  613.  
  614. Iµá thσá SR╨ generato≥ i≤ invokeΣ witΦ n∩ argument≤ i⌠ display≤ ßá summar∙á oµ ì
  615. acceptable command forms.
  616.  
  617. Thσá name≤á oµá thσ tw∩ intermediatσ .CO═ file≤ generateΣ b∙á thσá linke≥á arσ ì
  618. ORG200.CO═ anΣ ORG300.COM«  Thesσ name≤ arσ hard-codeΣ int∩ thσ SR╨ generator.
  619.  
  620. John Hastwell-Batten
  621.  
  622. Original version and documentation:     13th October 1982
  623. Relocation options added:               30th & 31st October 1982
  624. Documentation updated:                  1st November 1982
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.