22.10.2013 Views

4 - Forth Interest Group

4 - Forth Interest Group

4 - Forth Interest Group

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

smoothly. This EXE file was<br />

not created by LINK, how-<br />

ever, and has no way to speak<br />

to anything it doesn't compile<br />

or metacompile for itself.<br />

When the linker links OBJ<br />

and LIB files to create the EXE,<br />

it creates an almost execut-<br />

able image that could run if it<br />

could be loaded at the very<br />

bottom of memory, without a<br />

PSP or a memory allocation<br />

block below it. In otherwords,<br />

the linker makes all segment<br />

references in the load module<br />

relative to the beginning of<br />

the load module. When DOS<br />

loads the load module into<br />

memory it always does so on<br />

a segment boundary. All that<br />

is necessary, then, to "fur up"<br />

the file for execution is to take<br />

the load address as a segment<br />

value and add it to all segment<br />

references in the loaded im-<br />

age.<br />

To facilitate this futing up,<br />

the EXE header contains a<br />

"relocation pointer table."This<br />

table is poorly named: noth-<br />

ing is actually relocated. What<br />

it actually contains is just a list<br />

of pointers to segment refer-<br />

ences in the load module,<br />

represented as segment:offset<br />

pairs, relative to the beginning<br />

of the load module. This is<br />

exactly enough information<br />

to find the locations,in the file<br />

that need to have their con-<br />

tents adjusted. In fact, this is all<br />

very simple: the hard work is<br />

done by the linker and, before<br />

that, by the language com-<br />

piler/translator that creates the<br />

OBJ files.<br />

3. External References<br />

Functions written in C are<br />

accessed by calls. The basic<br />

format of these calls is de-<br />

scribed in the mixed-language<br />

programming sections of C<br />

and assembler manuals and<br />

books, where you can also<br />

find details of calling conven-<br />

tions for Pascal, Fortran, BA-<br />

SIC, etc. Parameters are passed<br />

on the stack and (unlike most<br />

VARIABLE LAST-XLINK \ pointer to last extern in linked list<br />

VARIABLE 1ST-XLINK \ pointer to first extern<br />

0 DUP 1ST-XLINK ! LAST-XLINK ! \ mark list empty<br />

: EXT-LINK, \ install the link<br />

LAST-XLINK @ ?DUP \ is there already at least 1 extern?<br />

IF HERE SWAP ! \ store fwd link in previous extern<br />

ELSE HERE 1ST-XLINK ! \ or in linked list base pointer<br />

THEN HERE LAST-XLINK ! \ store current link in head pointer<br />

0 , ; \ terminate list<br />

--><br />

Screen 10<br />

\ EXTERN: LINKED LIST TRAVERSAL RA 03JAN93<br />

: XLINK>FIXADR ( ext-link --- patch-adr ) 2- @ ;<br />

: XLINK>XNAME ( ext-name --- xname-adr ) 2+ ;<br />

VARIABLE CUR-XLINK VARIABLE XREF#<br />

: >1ST-XLINK 1ST-XLINK CUR-XLINK @ ! 1 XREF# ! ;<br />

: >NXT-XLINK CUR-XLINK @ CUR-XLINK @ ! 1 XREF# +! ;<br />

: NEXT,<br />

AD CI \ LODSW<br />

93 C, \ XCHG AX, BX<br />

FF C, 27 C, ; \ JMP [BXI<br />

--><br />

Screen 11<br />

\ EXTERN: SAVE-REGS & RESTORE-REGS 04FEB93 RA 03JAN93<br />

HEX CREATE RG-SV 8 ALLOT<br />

HERE<br />

89 C, 2E C, RG-SV , \ BP -> RG-SV FORTH RP<br />

89 C, 1E C, RG-SV 2+ , \ BX -> RG-SV+2 FORTH W<br />

89 C, 36 C, RG-SV 4 + , \ SI -> RG-SV+4 FORTH IP<br />

8C C, 06 C, RG-SV 6 + , \ ES -> RG-SV+6<br />

C3 C, \ RET<br />

CONSTANT SAVE-REGS<br />

HERE<br />

8B C, 2E C, RG-SV \ BP<br />

8B C, 1E C, RG-SV 2+ , \ BX<br />

8B C, 36 C, RG-SV 4 + , \ SI<br />

8E C, 06 C, RG-SV 6 + , \ ES<br />

C3 C, \ RET<br />

CONSTANT RESTORE-REGS --><br />

Screen 12<br />

\ EXTERN: CODE-EXTERN 15FEB93 RA 03JAN93<br />

HEX<br />

: CODE-EXTERN ( #out #in --- cfa patch-address )<br />

HERE \ -- #out #in cfa<br />

E8 C, SAVE-REGS HERE 2+ - , \ CALL SAVE-REGS<br />

9~ C, HERE 0 , 0 , \ -- #out #in cfa patch \ CALL 0000:0000<br />

ROT ?DUP<br />

IF \ -- #out cfa patch-addr #params<br />

\ clean up the stack in C fashion<br />

83 C, C4 C, 2* C, \ ADD SP,2*#params<br />

THEN \ -- #out cfa patch-addr<br />

E8 C, RESTORE-REGS HERE 2+ - , \ CALL RESTORE-REGS<br />

ROT ?DUP \ -- cfa patch 0 I cfa patch n n<br />

IF 50 C, 1- \ PUSH AX<br />

November 1993 December 14 <strong>Forth</strong> Dimensions

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!