home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CP/M
/
CPM_CDROM.iso
/
cpm
/
utils
/
asmutl
/
reloc23.lbr
/
RELOC.DQC
/
RELOC.DOC
Wrap
Text File
|
1986-05-06
|
30KB
|
639 lines
A SIMPLE METHOD FOR GENERATING
SELF-RELOCATING PROGRAMS
or
To every problem, no matter how trivial, someone can
always find a complicated solution.
The "What"
┴ self-relocatinτ prograφ is¼á iε CP/═ terminology¼ onσ whicΦ wheε loadeΣ int∩ ì
thσ bottoφ oµ thσ transien⌠ prograφ areß move≤ itselµ u≡ t∩ thσ to≡ oµ thσ TP┴ ì
beforσá doinτ anythinτ useful«á Thσ effec⌠ i≤ t∩ freσ u≡ thσ lowe≥ enΣ oµ thσ ì
TPA for use by other programs. This is how program monitors such as DDT work.
The "Why"
Oεá anΣá ofµ ove≥ thσ pas⌠ yea≥ ╔ havσ beeε writinτ ß supe≥á Z8░á disassemble≥ ì
whicΦá include≤ thσ morσ usefu∞ feature≤ oµ WarΣ Christensen'≤á RESOURC┼á (seσ ì
CPMU╟á vo∞ 42⌐ alonτ witΦ ß fe≈ feature≤ oµ m∙ owε inventioε anΣ ß bette≥ use≥ ì
interface«á I⌠á occureΣá t∩ mσ ho≈ convenien⌠ i⌠ woulΣ bσ t∩á havσá thσá dis-ì
assemble≥á residσ iε higΦ memor∙ s∩ tha⌠ thσ prograφ beinτ disassembleΣá coulΣ ì
occup∙ it≤ norma∞ positioε iε thσ TPA« Iε thσ documentatioε oε RESOURCE¼ WarΣ ì
says:
áááááááááá"..«á typicall∙á yo⌡á wan⌠ t∩ disassemblσ ß prograφá whicΦ ì
áááááááááárun≤á a⌠á 100H¼á whicΦ i≤ werσ RESOURC┼á runs«á BoΓá Vaε ì
ááááááááááValzaΦáá woulΣá havσá solveΣá tha⌠á b∙áá makinτáá resourcσ ì
áááááááááárelocatablσá anΣá movinτ itselµ u≡ unde≥ BDOS«á ╔á wasn'⌠ ì
áááááááááátha⌠ industrious."
Instead¼á hσá fiddle≤ arounΣ witΦ aε "invisiblσ offsetó whicΦ make≤ ßá prograφ ì
loadeΣá ABOV┼ thσ disassemble≥ looδ a≤ thougΦ i⌠ reside≤ a⌠ 100h«á Ward'≤á owε ì
word≤ describσ m∙ feeling≤ oε thσ matter║ ╔ wasn'⌠ tha⌠ industriousí Thu≤ wa≤ ì
╔ leΣ t∩ conside≥ thσ method≤ oµ generatinτ self-relocatinτ programs.
And the "How"
Thσá enΣ resul⌠ oµ al∞ SR╨ generatioε scheme≤ i≤ aε objec⌠ prograφá comprisinτ ì
threσá discretσá segments¼á normall∙ (bu⌠ no⌠ necessarily⌐á iεá thσá followinτ ì
order:
1. Relocator module:
ááááááááááááááá┴ shor⌠ sectioε oµ codσ whicΦ shift≤ thσ relocatablσ segmen⌠ t∩ ì
áááááááááááááááthe top of the TPA and transfers control to it.
2. Relocatable program segment:
áááááááááááááááThe "useful" part of the SRP.
3. Relocation bitmap:
áááááááááááááááUseΣá b∙á thσá relocato≥á modulσ t∩ fi°á u≡á addresse≤á iεá thσ ì
ááááááááááááááárelocatable program segment to reflect the new program origin.
┴á bi⌠ oµ sneaker∙ i≤ involveΣ iε thσ generatioε anΣ usσ oµá thσá bitmap«á I⌠ ì
contain≤á onσá BI╘ fo≥ eacΦ BYT┼ oµ codσ iε thσ relocatablσá prograφá segment« ì
Oncσá thσá relocatablσ codσ i≤ moveΣ int∩ placσ thσ bitma≡ i≤ scanneΣ anΣá fo≥ ì
eacΦá bi⌠á set¼á ß relocatioε bia≤ i≤ addeΣ t∩ thσ correspondinτ bytσá oµá thσ ì
relocateΣ code«á Thi≤ devilishl∙ simplσ schemσ relie≤ oε thσ wa∙ iε whicΦ thσ ì
bitma≡á i≤á constructed«á Tw∩á codσá file≤ arσ prepareΣá sucΦá tha⌠á al∞á thσ ì
addresse≤á iε onσ diffe≥ froφ thosσ iε thσ othe≥ b∙ aε exac⌠ multiplσá oµá 25╢ ì
anΣ thσ bitma≡ i≤ constructeΣ sucΦ tha⌠ ß bi⌠ i≤ se⌠ iµ thσ correspondinτ codσ ì
byte≤ arσ differen⌠ and¼á conversely¼ ß bi⌠ i≤ rese⌠ iµ thσ correspondinτ codσ ì
bytes are the same. The following example may help to explain the idea:
0000 3A 0010 LD A,(NUMBER+1) ;Get whatever junk is in NUMBER
0003 CD 0011 CALL HEXOUT ;Display high-order byte in hex
0006 3A 000F LD A,(NUMBER) ;Get rest of NUMBER
0009 CD 0011 CALL HEXOUT ;Display low-order byte in hex
000C C3 0000 JP 0 ;End
000F NUMBER:
000F 7A49 DEFW 7A49h ;Arbitrary 16-bit thing
0011 HEXOUT:
0011 F5 PUSH AF ;Copy for displaying low-order nibble
0012 0F RRCA ;Isolate the high-order nibble
0013 0F RRCA
0014 0F RRCA
0015 0F RRCA
0016 CD 001A CALL HEXDIG ;Display a hex digit
0019 F1 POP AF ;Recover low-order bits
001A HEXDIG:
001A E6 0F AND 0Fh ;Drop spurious bits
001C C6 90 ADD A,90h ;Convert hex digit to ASCII
001E 27 DAA
001F CE 40 ADC A,40h
0021 27 DAA
0022 5F LD E,A ;Move to E for CP/M console output
0023 0E 02 LD C,2 ;Function selector
0025 C3 0005 JP 5 ;BDOS will do the RETurn for us
With the code origin set at 0 the object code for the above program is:
3A 10 00 CD 11 00 3A 0F 00 CD 11 00 C3 00 00 49
7A F5 0F 0F 0F 0F CD 1A 00 F1 E6 0F C6 90 27 CE
40 27 5F 0E 02 C3 05 00
Setting the code origin at 100h would generate the following object code:
3A 10 01 CD 11 01 3A 0F 01 CD 11 01 C3 00 00 49
7A F5 0F 0F 0F 0F CD 1A 01 F1 E6 0F C6 90 27 CE
40 27 5F 0E 02 C3 05 00
The code is 40 bytes long so the bitmap would be 40 bits long, i.e. 5 bytes:
0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
which, in hex notation is 24 90 00 80 00.è
Supposσá wσ no≈ loaΣ thσ origin-░ codσ a⌠ A60░ t∩ A627«á Thσ bitma≡ tell≤á u≤ ì
tha⌠ wσ havσ t∩ adΣ A6Φ t∩ thσ codσ byte≤ a⌠ A602¼á A605¼ A608¼ A60┬ anΣ A618« ì
The properly-relocated code becomes:
3A 10 A6 CD 11 A6 3A 0F A6 CD 11 A6 C3 00 00 49
7A F5 0F 0F 0F 0F CD 1A A6 F1 E6 0F C6 90 27 CE
40 27 5F 0E 02 C3 05 00
So what's hard about that?
Nothing¼á onσ woulΣ think«á Unfortunately¼ thσ commonl∙ availablσ method≤ fo≥ ì
buildinτá bitmap≤ anΣ constructinτ self-relocatablσ program≤ arσ s∩ mess∙ tha⌠ ì
╔á coulΣ almos⌠ sympathisσ witΦ WarΣ Christensen'≤ vie≈ oµá thσá exercise«á ╔ ì
shal∞á describσá onσ sucΦ methoΣ anΣ briefl∙ mentioε tw∩ other≤ bu⌠á witΦá thσ ì
forewarninτá tha⌠ ╔ havσ neve≥ an∙ oµ theφ anΣ tha⌠ ╔ aφ relyinτ oε sucΦ scan⌠ ì
documentatioεá a≤ ╔ havσ availablσ anΣ oε wha⌠ ╔ coulΣ learε b∙á halfheartedl∙ ì
playinτá arounΣá witΦ them«á Halfheartedly┐á Yes¼á afte≥ al∞ i⌠á wa≤á fairl∙ ì
obviou≤ tha⌠ ╔ woulΣ neve≥ seriousl∙ emplo∙ eithe≥ methoΣ anΣ m∙ onl∙ interes⌠ ì
iε theφ wa≤ attemptinτ t∩ removσ thσ wildes⌠ inaccuracie≤ froφ thesσ notes.
The GENMOD method
Normall∙á distributeΣ witΦ MP/═ bu⌠ sometime≤ availablσ witΦ CP/═ i≤ ß utilit∙ ì
prograφá calleΣ GENMOD«á Thσ step≤ iε thσ GENMO─ techniquσ fo≥ generatinτá aε ì
SRP are something like this:
1 Assemblσ thσ relocatablσ codσ segmen⌠ iε thσ norma∞ wa∙ specifyinτ ß ì
áááááááááácode origin of 100h and generating a .HEX file.
2 Modify the source program changing the code origin to 0.
3á Re-assemblσá thσá relocatablσ codσ segmen⌠ int∩ ßá (different⌐á .HE╪ ì
ááááááááááoutput file.
4 Concatenatσá thσ tw∩ .HE╪ files«á PI╨ ma∙ bσ useΣ t∩ d∩ thi≤ fairl∙ ì
ááááááááááneatly, e.g.
B>PIP RP.HEX=ORG000.HEX,ORG100.HEX
5 Ruε GENMO─ agains⌠ thσ concatenateΣ .HE╪ file, e.g.:
B>GENMOD RP.HEX RP.COM
GENMOD responds with:
REL MOD END 002A
REL MOD SIZE 007F
ABS MOD END 097F
GENMO─á put≤á thσ relocatablσ codσ segmen⌠ a⌠ 200Φ followeΣá b∙á tw∩ ì
áááááááááábyte≤á whosσ purposσ ╔ canno⌠ fathom«á Immediatel∙ thereafte≥á come≤ ì
ááááááááááthσá bitmap«á Thσ importan⌠ piecσ oµ informatioε abovσ i≤ thσá "RE╠ ì
ááááááááááMO─ ENDó address«á AdΣ 200Φ t∩ tha⌠ valuσ anΣ yo⌡ havσ thσá addres≤ ì
ááááááááááof the start of the bitmap.è
6 áUsσá DDT¼á ZSI─á o≥ whateve≥ t∩ loaΣ thσ relocato≥ modulσ belo≈á thσ ì
áááááááááárelocatablσá codσá segmen⌠ anΣ bitmap«á Someho≈ tel∞ you≥á relocato≥ ì
áááááááááámodulσá thσ lengtΦ oµ you≥ relocatablσ codσ segmen⌠ and/o≥ wherσá t∩ ì
ááááááááááfinΣá thσá bitmap«á Exi⌠ froφ thσ debugge≥ anΣ SAV┼á thσá SR╨á codσ ì
ááááááááááimage.
B>ZSID RP.COM
IRELOC.COM
R
{set up relocator information}
G0
B>SAVE nn RP.COM
and there you have it!
The REL.UTL method
Anothe≥ methoΣ fo≥ generatinτ SRP≤ i≤ offereΣ b∙ Rober⌠ vaε ValzaΦ (CPMU╟á vo∞ ì
38⌐ wh∩ provide≤ ß .UT╠ prograφ whicΦ run≤ unde≥ thσ contro∞ oµ SI─ o≥ ZSI─ t∩ ì
handlσá somσá oµ thσ step≤ describeΣ above«á Whilσ differen⌠ iεá detail¼á thσ ì
methoΣá i≤ oµ thσ samσ degreσ oµ complexit∙ a≤ thσ GENMO─átechiquσ.
The macro method
Usinτá ß fairl∙ powerfu∞ macr∩ assemble≥ sucΦ a≤ Digita∞ Research'≤ MA├ i⌠á i≤ ì
possiblσ t∩ definσ macro≤ t∩ generatσ ß bitmap«á ┴ finσ examplσ oµ thσ usσ oµ ì
this technique is provided by Gary Novosielski's UNSPOOL program.
; GENERATE A LABEL OF THE FORM ??RNN TO TAG AN
; ADDRESS REQUIRING RELOCATION:
RTAG MACRO LBL,VAL
??R&LBL EQU VAL
ENDM
;
; FLAG <INST> AS A RELOCATABLE INSTRUCTION
; <INST> IS OF THE FORM: <MNE OP1[,OP2]>
R MACRO INST
@RLBL SET @RLBL+1
RTAG %@RLBL,%2+$-@BASE
INST-@BASE
ENDM
;
; DURING BIT MAP CONSTRUCTION, GET THE NEXT R-TAGGED
; ADDRESS VALUE:
NXTRLD MACRO NN
@RLD SET ??R&NN
@NXTRLD SET @NXTRLD + 1
ENDM
In the code to be relocated we see these macros applied to instructions:
@BASE: ;START OF CODE TO BE RELOCATED
. . .
R <JZ SYSREQ>
0038+= ??R2 EQU 56
0336+CA9801 JZ SYSREQ-@BASE
Ever∙ instructioε whicΦ wil∞ neeΣ t∩ bσ modifieΣ durinτ relocatioε i≤ codeΣ a≤ ì
ßá paramete≥á t∩ thσ "Ró macr∩ whicΦ generate≤ aε assembly-timσá symbo∞á whosσ ì
valuσá i≤á thσ offse⌠ froφ thσ beginninτ oµ thσ relocatablσ codσ oµ thσá thirΣ ì
instructioεá byte«á A⌠á thσ samσ timσ thσ operanΣ i≤ modifieΣá t∩á ßá simila≥ ì
offset«á A⌠á thσá enΣá oµ thσ relocatablσ codσ i≤á thσá assembly-timσá bitma≡ ì
generator:
; BUILD THE RELOCATION INFORMATION INTO A
; BIT MAP FOLLOWING THE CODE.
;
0000 # @X SET 0
0000 # @BITCNT SET 0
0002 # @RLD SET ??R1
0002 # @NXTRLD SET 2
RTAG %@RLBL+1,0FFFFH ;DEFINE ONE MORE SYMBOL
FFFF+= ??R89 EQU 0FFFFH
;
REPT SEGLEN+8
IF @BITCNT>@RLD
NXTRLD %@NXTRLD ;;NEXT VALUE
ENDIF
IF @BITCNT=@RLD
@X SET @X OR 1 ;;SET LOW BIT
ENDIF
@BITCNT SET @BITCNT + 1
IF @BITCNT MOD 8 = 0
DB @X ;;DEFINE THE BYTE
@X SET 0 ;;CLEAR HOLD VARIABLE FOR MORE
ELSE
@X SET @X SHL 1 ;;NOT 8 YET. MOVE OVER.
ENDIF
ENDM
Thσá outpu⌠á oµá thi≤ in-linσ macr∩ i≤ ß serie≤á oµá "DBóá pseudo-instruction≤ ì
defining the bitmap.
A≤ coded¼á thi≤ techniquσ wil∞ NO╘ worδ fo≥ an∙ oµ thσ Z8░ instruction≤á whicΦ ì
havσá 16-bi⌠á opcode≤ bu⌠ ß simplσ rearrangemen⌠ oµ thσ "Ró macr∩ woulΣá solvσ ì
thi≤ problem:
R MACRO INST
@RLBL SET @RLBL+1
INST-@BASE
RTAG %@RLBL,$-%1-@BASE
ENDMè
Thσá macr∩ techiquσ i≤ probabl∙ thσ easies⌠ oµ thσ threσ t∩ usσ provideΣá tha⌠ ì
onσá ha≤ acces≤ t∩ ß gooΣ macr∩ assembler«á ╔ usσ Microsoft'≤ MACRO-8░á (M80⌐ ì
fo≥á al∞á oµ m∙ assembly-languagσ programminτ anΣ (withou⌠ puttinτ i⌠á t∩á thσ ì
test⌐á ╔ doub⌠ tha⌠ M8░ coulΣ handlσ thσ task«á Nevertheles≤ thσ techniquσ i≤ ì
viable¼á elegan⌠á anΣ cleve≥ althougΦ thσ complexit∙ oµ thσ macro≤á make≤á thσ ì
actua∞á assembl∙á ver∙ lonτ anΣ ╔ suspec⌠ tha⌠ witΦ ß largσ prograφ onσá coulΣ ì
ruεá int∩á memory-spacσá problem≤á becausσ oµá thσá largσá numbe≥á oµá symbol≤ ì
generated«á Eveε iµ ╔ haΣ MA├ ╔ woulΣ no⌠ usσ thi≤ techniquσ becausσ ╔ d∩ al∞ ì
oµá m∙á assembly-languagσá programminτá iε Ziloτá mnemonics«á Thesσá arσá no⌠ ì
supporteΣ b∙ MAC«á Also¼á wheε ╔ firs⌠ wrotσ thi≤ articlσ ╔ wa≤ no⌠ awarσá oµ ì
an∙á implementatioε oµ thσ macr∩ techniquσ anΣ ╔ doub⌠ tha⌠ ╔ woulΣ havσá beeε ì
smart enough to devise those very sophisticated macros.
There has to be a Better Way
A⌠á thσá timσá oµá writinτá thi≤ articlσ ╔ havσá codeΣá anΣá testeΣá thσá maiε ì
disassembl∙ routinσ anΣ thσ symbo∞ tablσ handlinτ routine≤ fo≥ m∙ disassemble≥ ì
anΣ ╔ havσ severa∞ othe≥ pre-coded¼ testeΣ module≤ kickinτ arounΣ iε librarie≤ ì
read∙á t∩ bσ incorporateΣ int∩ thσ program«á Nevertheles≤ ╔ expec⌠ tha⌠ some- ì
wherσá betweeε 2░ anΣ 4╡ assembly/testinτ cycle≤ wil∞ bσ requireΣ t∩á completσ ì
thσá projec⌠á anΣ thσ though⌠ oµ goinτ througΦ thσ abovσ procedurσá morσá thaε ì
oncσ o≥ twicσ woulΣ bσ sufficien⌠ t∩ discouragσ mσ froφ eve≥ finishinτ it.
Wha⌠ ╔ wanteΣ wa≤ aε automatiπ methoΣ oµ generatinτ aε SRP« Ideally¼ ╔ shoulΣ ì
bσ ablσ t∩ d∩ somσ oµ thσ assembly/testinτ cycle≤ witΦ thσ prograφ runninτá iε ì
thσ low-memor∙ enΣ oµ thσ TP┴ anΣ theε withou⌠ changinτ thσ program¼á generatσ ì
aε SR╨ forφ witΦ somσ assurancσ tha⌠ i⌠ wil∞ stil∞ work«
The Better Way
Thσá onσá essentia∞á too∞á fo≥ thσ SR╨ generatioε schemσ tha⌠ ╔á aφá abou⌠á t∩ ì
describσá i≤á aεá assembler-linke≥ combinatioε whicΦá let≤á yo⌡á specif∙á loaΣ ì
addresse≤ a⌠ linδ time« ╔ havσ two¼ namel∙ MACRO-8░ anΣ LINK-8░ froφ Microsof⌠ ì
anΣ ASMBL/┌ anΣ LINK/┌ froφ Ithacß InterSystem≤ bu⌠ ╔ havσ n∩ doub⌠ tha⌠ therσ ì
arσ other≤ whicΦ wil∞ worδ jus⌠ a≤ well.
Thσá essencσ oµ thσ schemσ i≤ t∩ le⌠ thσ linke≥ d∩ thσ worδ oµ puttinτ thσ re-ì
locatablσ codσ iε thσ righ⌠ spo⌠ rathe≥ thaε doinτ tw∩ assemblies¼á anΣ t∩ usσ ì
a program to match the two code images and append a bitmap.
┴ certaiε disciplinσ i≤ requireΣ wheε codinτ ß prograφ whicΦ wil∞ late≥ becomσ ì
self-relocating«á Thi≤á disciplinσá ha≤á n∩ effec⌠ oε thσá executioεá oµá thσ ì
program but provides information to the relocator module.
▒ AvoiΣ thσ usσ oµ "absoluteó prograφ segments╗ the∙ arσ no⌠ necessar∙ ì
anΣá unles≤á yo⌡ arσ VER┘ careful¼á wil∞ interferσ witΦ thσá linkinτ ì
order.
2 Fo≥á thσ benefi⌠ oµ thσ relocato≥ modulσ thσ firs⌠ tw∩ item≤ iεá thσ ì
áááááááááárelocatable program must be:
áááááááááááááááa)áááßá worΣá containinτá thσ addres≤á oµá thσá firs⌠ ì
áááááááááááááááááááácharacter past the end of the program,
èáááááááááááááááb)áááßá jum≡á t∩ thσ entr∙ poin⌠ oµá thσá relocatablσ ì
ááááááááááááááááááááprogram segment.
MACRO-80 Example:
.Z80 ;I always use Zilog mnemonics
cr equ 0dh
lf equ 0ah ;These statements don't generate
ckpt equ 144 ;any code so they can go here
zap macro field,len
. . .
endm
dseg ;Normally the first loaded
$memry::
defs 2 ;Address of end of program
j𠠠start ;Used and modified by relocator
;Rest of program as it would normally be written.
. . .
Start:
. . .
end start
ááááááááááIteφá (a⌐ i≤ useΣ b∙ thσ relocato≥ modulσ t∩ calculatσ thσ lengtΦ oµ ì
ááááááááááthσ relocatablσ prograφ segmen⌠ anΣ thencσ t∩ determinσ thσá addres≤ ì
áááááááááát∩á whicΦ t∩ movσ thσ relocatablσ code«á Thσ worΣ itselµ wil∞ no⌠ bσ ì
áááááááááárelocated.
Iµá yo⌡ arσ usinτ thσ Microsof⌠ assemble≥ anΣ linke≥ theεá iteφá (a⌐ ì
áááááááááácan be a source statement of the form:
$MEMRY:: defs 2
ááááááááááa≤á iεá thσ abovσ examplσ anΣ LINK-8░ wil∞ kindl∙ fil∞ thσ worΣá in« ì
ááááááááááLINK/┌á doesn'⌠ providσ thi≤ conveniencσ s∩ usσ ßá sourcσá statemen⌠ ì
áááááááááálike:
dw $END$
wherσá $ENDñá i≤ ß labe∞ declareΣ jus⌠ beyonΣ thσ las⌠ bytσá iεá thσ ì
ááááááááááprogram«á (Makσá surσ tha⌠ i⌠ i≤ iε thσ last-loadeΣ segmen⌠ oµá thσ ì
áááááááááálast-linked module!)
Thσá relocato≥á prograφ extract≤ thσ addres≤ fielΣ oµ iteφ (b⌐á and¼ ì
ááááááááááwheεá i⌠á ha≤ completeΣ thσ relocation¼á transfer≤ contro∞á t∩á tha⌠ ì
ááááááááááaddres≤ iε thσ relocateΣ codσ segment«á Iε thσ meantime¼ thσ targe⌠ ì
ááááááááááoµá thσ jum≡ instructioε i≤ modifieΣ t∩ jum≡ t∩ thσ BDO╙á whilσá thσ ì
áááááááááájum≡á a⌠ addres≤ ╡ i≤ modifieΣ t∩ jum≡ t∩ thi≤ instructioε s∩ tha⌠ ß ì
ááááááááááprogram loaded into low memory sees a smaller TPA.
ááááááááááConfused┐á Perhap≤á anothe≥á examplσ wil∞ help«á Supposσá tha⌠á wσ ì
áááááááááárelocatσá ß prograφ whicΦ i≤ 42BΦ byte≤ lonτ anΣ thσ BDO╙ start≤á a⌠ ì
ááááááááááC200h« Beforσ relocation¼ thσ scenari∩ i≤ somethinτ likσ this:
è 0005 JP C200 Jump into BDOS
. . .
0100 Start of relocator module
. . .
01FE DEFW 062B Address of first byte past program
0200 JP NTRYPT Jump to entry point of relocatable
program segment
. . .
02A7 NTRYPT: Program start address
. . .
062A Last byte of program
062B Start of bitmap
. . .
C200 BDOS
After relocation things will be a little different:
0005 JP BD00 Indirect jump to BDOS
. . .
BD00 JP C200 Actual jump into BDOS
. . .
BDA7 NTRYPT: Relocator exits to this address
. . .
C12A End of program
. . .
C200 BDOS
Providinτá tha⌠ ß prograφ ha≤ thσ requireΣ item≤ coded¼á i⌠ caεá bσá converteΣ ì
into a self-relocating program without re-assembly. The steps are:
1ááááGeneratσ ß .CO═ filσ b∙ linkinτ thσ relocato≥ modulσ anΣ relocatablσ ì
áááááááááácodσá segment«á Specif∙ ß loaΣ addres≤ oµ 01F┼ fo≥ thσá relocatablσ ì
áááááááááácodσ segment«
2 Generatσá anothe≥á .CO═ filσ b∙ linkinτ onl∙á thσá relocatablσá codσ ì
áááááááááásegment specifying a load address of 02FE.
3 Run the RELOC program to generate the self-relocating program.
Ste≡á ▒á effectivel∙á concatenate≤ thσ relocato≥ modulσá anΣá thσá relocatablσ ì
prograφá segment«á Thσ linke≥ put≤ thσ pointe≥ t∩ thσ enΣ oµ thσá prograφá a⌠ ì
(01FF,01FE⌐á anΣ thσ jum≡ instructioε a⌠ 0200«á Thσ relocato≥ modulσ caε theε ì
finΣá thσ prograφ star⌠ addres≤ a⌠ (0202,0201)«á Notσ tha⌠ thσ end-of-prograφ ì
pointer does not become part of the relocated program.
Ste≡ ▓ create≤ ß seconΣ imagσ oµ thσ relocatablσ prograφ segmen⌠ sucΦ tha⌠ al∞ ì
addresses differ by 100h from those in the file created in step 1.
Step 3 adds a bitmap.
No⌠á onl∙ arσ therσ fewe≥ step≤ thaε iε thσ GENMO─ anΣ REL.UT╠ method≤ bu⌠á n∩ ì
modificatioε t∩ thσ prograφ sourcσ codσ i≤ necessar∙ anΣ thσ wholσ proces≤ caε ì
be performed without operator intervention with a .SUB file like this:
:Generate a self-relocating version of the $1 programè L80 reloc,/p:1fe,$1,Org200/n/e
L80 /p:2fe,$1,Org300/n/e
RELOC $1
ERA org?00.com
where $1 will be substituted by the name of the program. Something like:
SUBMIT RELOC RP
doe≤ i⌠ allíá Easy«á Oncσ ╔ decideΣ ho≈ i⌠ coulΣ bσ done¼á implementatioε oµ ì
thσá SRP-generatioεá schemσ tooδ ß hel∞ oµ ß lo⌠ les≤ timσ thaεá writinτá thi≤ ì
documentation!
And that's not all...
Giveεá ßá systeφ sucΦ a≤ thσ above¼á i⌠ caε bσ madσ t∩ d∩ othe≥á things«á Fo≥ ì
example¼á somσ program≤ returε contro∞ t∩ thσ CC╨ wheε the∙ finisΦ insteaΣá oµ ì
exitinτá t∩ CP/═ b∙ doinτ ß "warφ boot"╗á iε relocatinτ sucΦ ß prograφ t∩ higΦ ì
memory¼á i⌠á woulΣ neeΣ t∩ bσ placeΣ jus⌠ belo≈ thσ CC╨ insteaΣ oµ jus⌠á belo≈ ì
thσ BDOS«á Again¼á withou⌠ to∩ mucΦ effort¼á thσ systeφ caε als∩ providσá thσ ì
abilit∙á fo≥ ß use≥ t∩ explicitl∙ nominatσ wherσ thσ relocateΣ prograφ codσ i≤ ì
t∩á reside«á Anothe≥ worthwhilσ facilit∙ i≤ tha⌠ oµ havinτ ß prograφá overla∙ ì
par⌠ oµ CP/═ itself¼á particularl∙ thσ BIOS«á Therσ i≤ no⌠ ß lo⌠ oµ rooφ oε ß ì
standarΣ singlσ densit∙ flopp∙ disδ fo≥ ß BIO╙ oµ an∙ sophisticatioε bu⌠ i⌠ i≤ ì
certainl∙á possiblσ t∩ overla∙ ß minima∞ BIO╙ witΦ ß self-relocatinτ onσá froφ ì
thσ disk'≤ datß track≤ whicΦ caε contaiε ß hos⌠ additiona∞ features«á Perhap≤ ì
aεá eveεá morσá genera∞ applicatioε fo≥ thi≤ facilit∙ i≤ tha⌠á i⌠á offer≤á thσ ì
abilt∙á t∩á tes⌠ BIO╙ modification≤ withou⌠ goinτ througΦ ß serie≤á oµá systeφ ì
generations.
T∩á implemen⌠á thesσ extrß capabilities¼á thσ SR╨ generato≥ ha≤á t∩á recognisσ ì
command-linσá directive≤á froφá thσá use≥ anΣá pas≤á thσá informatioεá t∩á thσ ì
relocato≥ modulσ whicΦ iε turε mus⌠ bσ cleve≥ enougΦ t∩ ac⌠ oε it«á Thi≤ doe≤ ì
impl∙á ß degreσ oµ interdependencσ betweeε thσ SR╨ generato≥ anΣ thσ relocato≥ ì
modulσá iε tha⌠ thσ SR╨ generato≥ need≤ t∩ kno≈ somethinτ abou⌠ thσá structurσ ì
oµá thσ relocator¼á bu⌠ ╔ believσ tha⌠ thσ sligh⌠ los≤ oµ generalit∙á i≤á wel∞ ì
compensateΣ b∙ thσ extrß versatilit∙ oµ thσ package.
A≤áá supplied¼áá thσá SR╨á generato≥á recognise≤á thσá followinτá command-linσ ì
directives:
RELOC X á Generatσá X.CO═á whicΦá wil∞ movσ itselµ u≡á t∩á jus⌠ ì
ááááááááááááááááááááááááábelo≈ thσ BDOS¼ overlayinτ somσ o≥ al∞ thσ CCP« Thi≤ ì
ááááááááááááááááááááááááái≤ thσ mos⌠ commoε usagσ oµ thσ SR╨ generato≥ anΣ ha≤ ì
áááááááááááááááááááááááááthσ simples⌠ commanΣ string« Prograφ ╪ i≤ assumeΣ t∩ ì
áááááááááááááááááááááááááreturε contro∞ t∩ CP/═ b∙ doinτ ß "warφá boot"«á Thσ ì
áááááááááááááááááááááááááBDO╙á jum≡ a⌠ memor∙ location≤ ╡ througΦ ╖ i≤ altereΣ ì
ááááááááááááááááááááááááát∩ reflec⌠ ß smalle≥ TP┴ size.
RELOC X - ááááááááááááA≤á fo≥ thσ previou≤ commanΣ forφ excep⌠á tha⌠á X.CO═ ì
áááááááááááááááááááááááááwil∞á relocatσá itselµ t∩ jus⌠ belo≈ thσ CC╨ anΣá thσ ì
áááááááááááááááááááááááááBDO╙ jum≡ a⌠ locatioε ╡ wil∞ no⌠ bσá affected«á I'l∞ ì
áááááááááááááááááááááááááexplain why later.
RELOC X addr "Addróá i≤á ßá hexadecima∞ numbe≥ whicΦá mus⌠á bσá aε ìèááááááááááááááááááááááááámultiplσá oµá 100h«á X.CO═ wil∞ movσ itselµ t∩á tha⌠ ì
áááááááááááááááááááááááááaddress.
Thσá "Xó prograφ generateΣ b∙ thσ abovσ commanΣ form≤ wil∞ bσá executeΣá afte≥ ì
relocatioεá ha≤á beeεá donσ anΣ thσ codinτ rule≤ describeΣá iεá thσá precedinτ ì
sectioε must be obeyed for the relocation system to work.
Onl∙á thσá "normalóá relocatioε t∩ jus⌠ belo≈ thσ BDO╙ cause≤á thσá BDO╙á jum≡ ì
instructioεá a⌠á memor∙á location≤á 5-╖ t∩ bσ modifieΣ t∩á reflec⌠á ßá reduceΣ ì
transien⌠ prograφ area«á Thσ decisioε t∩ omi⌠ thσ modificatioε iε othe≥ case≤ ì
wa≤ fairl∙ arbitrar∙ bu⌠ wa≤ takeε iε thσ interes⌠ oµ generality«á ╔ diΣá no⌠ ì
wisΦá t∩ imposσ an∙ restrictioε oε thσ prograφ beinτ relocateΣ anΣá considereΣ ì
thσ possibilit∙ tha⌠ thσ prograφ migh⌠ rel∙ oε thσ jum≡ instructioε beinτ lef⌠ ì
unchangeΣá fo≥ an∙ oµ ß variet∙ oµ reasons«á Oµ course¼á therσ i≤ nothinτá t∩ ì
sto≡ thσ relocateΣ prograφ froφ modifyinτ thσ BDO╙ jum≡ itself«á Also¼á therσ ì
i≤á ßá public-domaiε utilit∙ provideΣ b∙ Johε Woolne≥ fo≥ protectinτá thσá CC╨ ì
froφá beinτ overlaiΣ b∙ transien⌠ program≤ s∩ tha⌠ therσ exist≤ ßá methoΣá fo≥ ì
achievinτá thσ samσ resul⌠ a≤ woulΣ bσ attaineΣ b∙ havinτ thσ relocato≥ adjus⌠ ì
the jump instruction:
1. Install John Woolner's CCP protection,
2. Use the command form RELOC X.COM (i.e. no second parameter).
Thσá SR╨ generato≥ als∩ recognise≤ threσ variant≤ oµ anothe≥ commanΣ forφá fo≥ ì
whicΦá thσ codinτ rule≤ arσ slightl∙ different«á Thσ worΣ pointinτ t∩á "freeó ì
memor∙á i≤á stil∞ requireΣ bu⌠ thσ extrß jum≡ instructioε a⌠ thσ heaΣá oµá thσ ì
relocatablσ codσ neeΣ not¼á anΣ generall∙ mus⌠ not¼á bσ supplied« Thσ commanΣ ì
form is
RELOC X CCP or RELOC X BDOS or RELOC X.COM BIOS
Thσá X.CO═ filσ generateΣ b∙ thesσ command≤ wil∞ relocatσ s∩ a≤á t∩á overwritσ ì
thσá CCP¼á BDO╙á o≥á BIO╙ respectively«á Contro∞ wil∞ NO╘ bσá passeΣá t∩á thσ ì
relocateΣá code«á Instead¼á thσ relocato≥ modulσ wil∞ exi⌠ t∩ thσ CC╨ withou⌠ ì
doinτ ß warφ boo⌠ s∩ tha⌠ thσ relocateΣ codσ wil∞ remaiε iε memory« Notσ tha⌠ ì
thσá destinatioε addres≤ fo≥ thσ relocateΣ codσ i≤ calculateΣ froφ thσá targe⌠ ì
oµá thσ jum≡ instructioε a⌠ memor∙ locatioε ╡ anΣ wil∞ bσ wronτ iµá tha⌠á jum≡ ì
instruction has been altered.
Copyright notice
Thesσ program≤ havσ beeε submitteΣ t∩ thσ publiπ domaiε viß Bil∞ Bolton'≤ RCP═ ì
systeφ anΣ comprisσ ß systeφ fo≥ simpl∙ generatinτ self-relocatinτ program≤ b∙ ì
ßá methoΣ whicΦ relie≤ oε ß linke≥ t∩ generatσ tw∩ objec⌠ codσ file≤ whicΦ arσ ì
theεá processeΣ t∩ yielΣ aε objec⌠ prograφ consistinτ oµá ßá relocator¼á user-ì
supplieΣá objec⌠á codσá anΣá ß relocatioεá bitmap«á Thesσá program≤á anΣá thσ ì
accompanyinτá documentatioε ma∙ bσ freel∙ distributeΣ iε origina∞ o≥á modifieΣ ì
forφ subjec⌠ t∩ thσ followinτ conditions:
1.áááAlthougΦá therσ i≤ n∩ restrictioε oε thσ salσ oµ self-relocatinτ program≤ ì
ááááágenerateΣá b∙á thσ methoΣ describeΣ herein¼á thesσ program≤á o≥á variant≤ ì
áááááthereoµá ma∙ no⌠ bσ solΣ a≤ par⌠ oµ an∙ prograφ packagσá withou⌠á writteε ì
ááááápermissioεá froφ thσ author«á Neithe≥ ma∙ an∙ prograφ o≥ prograφ packagσ ì
áááááwhicΦá i≤ dependen⌠ fo≥ it≤ operatioε oε thσ usσ oµ thi≤ methoΣá bσá solΣ ì
áááááwithou⌠á sucΦ permission«á Iε othe≥ word≤ yo⌡ ma∙á sel∞á self-relocatinτ ìèáááááprograms but not the method of generating them.
2.áááThσá author'≤á namσá mus⌠á bσ retaineΣ iε al∞ sourcσá codσá (origina∞á o≥ ì
ááááámodified⌐á anΣá a≤ aε acknowlegemen⌠ iε an∙ messagσá displayeΣá b∙á thesσ ì
áááááprogram≤ o≥ variant(s⌐ thereof« Aε acknowledgemen⌠ givinτ credi⌠ fo≥ thσ ì
ááááámethoΣá shal∞ contaiε thσ author'≤ namσ o≥ thσ word≤ "H-┬ methodó o≥á thσ ì
áááááwords "Ashby method".
3.áááThi≤ copyrigh⌠ noticσ mus⌠ bσ includeΣ iε anΣ retaineΣ iε al∞ sourcσ codσ ì
áááááand documentation pertaining to this system.
John Hastwell-Batten
38 Silvia Street
Hornsby NSW 2077
AUSTRALIA
(02) 477 4225
1st November, 1982
Acknowledgement
Iεá testinτ thosσ relocation≤ whicΦ overla∙ specifieΣ portion≤ oµ CP/═ ╔á havσ ì
madσá usσ oµ Johε Woolner'≤ CC╨ protectioε scheme obtaineΣ vißá Bil∞á Bolton'≤ ì
RCP═á system«á Withou⌠á thσ CC╨ protectioε thσ verificatioε oµ thσá relocato≥ ì
modulσá woulΣ havσ beeε exceedingl∙ difficul⌠ a≤ thσ standarΣ prograφá testinτ ì
tool≤á al∞á overla∙á thσ CC╨ which¼á iε thσ casσ oµá thσá CP/═á overlays¼á thσ ì
relocator expects to be intact.
èSYSTEM PROGRAMS AND FILES
Therσá arσ ß lo⌠ oµ thing≤ calleΣ "RELOCó iε thi≤ systeφ anΣ thσ variou≤ file≤ ì
arσá distinquisheΣá b∙ thσ filenamσ extension«á Thσ actua∞ SR╨á generato≥á i≤ ì
writteεá iεá BDS-├á bu⌠ i⌠ coulΣ equall∙ (o≥ almos⌠á equally⌐á bσá writteεá iε ì
anythinτá else«á Thσ relocato≥ modulσ i≤ writteε iε Z8░ assembl∙ languagσ anΣ ì
jus⌠ abou⌠ thσ onl∙ restrictioε upoε i⌠ i≤ tha⌠ i⌠ mus⌠ bσ a⌠ mos⌠ 0FEΦá byte≤ ì
long«á ╔á playeΣ arounΣ witΦ somσ funn∙ Z8░ instruction≤ whilσ ╔ wa≤á writinτ ì
thσá relocato≥á anΣá neve≥á bothereΣ t∩á re-prograφá it«á (Eveεá withou⌠á thσ ì
"undocumentedó Z8░ instructions¼á thσ supplieΣ codeΣ i≤ highl∙ Z8░á dependent« ì
For an example of an 8080-coded relocator, see Robert van Valzah's REL.UTL)
These are the files:
RELOC.MAC Source of relocator module.
RELOC.REL Assembled relocator module.
RELOC.C BDS-C source of program generator.
RELOC.COM The SRP generator.
RELOC.SUB Submit file to automate the process of generating a
self-relocating program.
RELOC.DOC What you have been reading all this time.
System also needs:
M80 and L80 Microsoft's MACRO-80 assembler and LINK-80 link/loader
or
ASMBL/Z Ithaca InterSystems' version of the same thing
and LINK/Z
or
any other assembler-linker combination which allows load
addresses to be specified at link time.
Thσá submi⌠ filσ RELOC.SU┬ i≤ se⌠ u≡ t∩ proces≤ ß prograφ comprisinτ ßá singlσ ì
module. For multi-module programs just expand the linker command lines.
RELOC.SU┬ wil∞ accep⌠ onσ o≥ tw∩ arguments«á Iµ supplied¼ thσ seconΣ argumen⌠ ì
i≤á passeΣá t∩á thσá SR╨ generato≥ prograφ alonτá witΦá thσá (required⌐á firs⌠ ì
argument.
Iµá thσá SR╨ generato≥ i≤ invokeΣ witΦ n∩ argument≤ i⌠ display≤ ßá summar∙á oµ ì
acceptable command forms.
Thσá name≤á oµá thσ tw∩ intermediatσ .CO═ file≤ generateΣ b∙á thσá linke≥á arσ ì
ORG200.CO═ anΣ ORG300.COM« Thesσ name≤ arσ hard-codeΣ int∩ thσ SR╨ generator.
John Hastwell-Batten
Original version and documentation: 13th October 1982
Relocation options added: 30th & 31st October 1982
Documentation updated: 1st November 1982