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