dn. Mon, 31 Jul 2023 17:28:58 +0200, Mateusz Viste napisał:
> It appears that the COM file is not being originated at offset 0x100,
> despite the "FORMAT DOS COM" wlink directive. It's also not
> 0-originated, so I am not sure how the offsets are calculated exactly.
> Once I fix them with a hex editor, the executable works.
>
> What am I missing here?
Hello all,
I talked with Bernd Böckmann today and I was surprised to learn that
he tackled this very same problem recently. He was, however, far more
successful than me and kindly shared the piece of information that I
have missed all along.
Bernd said:
"Because in tiny memory model the code is in the same segment as the
data, the linker must be told to merge these segments to a single one
while linking, otherwise the addresses are messed up. This is done by
the GROUP directive in startup.asm, which includes _TEXT (as opposed to
the .EXE version)."
The need of a custom startup code was already hinted in this thread by
Alexei A. Frounze, and I did attempt to create such startup back then,
but the necessity of grouping segments was lost on me.
Bernd provided me with a working example of his startup code. With this
new bit of information I was able to adapt my proof of concept project
- and this time, it works! The resulting executable size is 45 bytes.
I am pasting here below all the files for posterity.
Mateusz
--- HELLO.LNK ---------------------------------------------
name hello
system dos com
option map
option nodefaultlibs
file startup
file hello
--- HELLO.C -----------------------------------------------
void main(void) {
char *hello = "Hello$";
_asm {
mov ah, 9
mov dx, hello
int 0x21
}
}
--- STARTUP.ASM -------------------------------------------
.8086
dgroup group _TEXT,_DATA,CONST,CONST2,_BSS,
extrn "C",main : near
; public _cstart_, _small_code_, __STK
public _cstart_, _small_code_
_TEXT segment word public 'CODE'
org 100h
_small_code_ label near
_cstart_:
call main
mov ah, 4ch
int 21h
; Stack overflow checking routine is absent. Remember to compile your
; programs with the -s option to avoid referencing __STK
;__STK:
; ret
_DATA segment word public 'DATA'
_DATA ends
CONST segment word public 'DATA'
CONST ends
CONST2 segment word public 'DATA'
CONST2 ends
_BSS segment word public 'BSS'
_BSS ends
_TEXT ends
end _cstart_
--- BUILD.BAT ---------------------------------------------
wasm startup.asm
wcc -os -zl -ms -s -bt=dos hello.c
wlink @hello.lnk
-----------------------------------------------------------