Search code examples
assemblydosx86-16masmstack-memory

Not adding the stack bytes to the .EXE file with MASM 5.0


I'm writing 3 assembly files for DOS:

hm2.asm:

.8086 
DGROUP group _DATA, STACK  ; Required by MASM 3.0 and 4.0. 
 
; MASM 4.0 doesn't support USE16 (but MASM >=5.0 does). 
_TEXT segment word public 'CODE' 
assume cs:_TEXT, ds:DGROUP, ss:STACK 
main proc 
mov ax, DGROUP  ; Initialize DS. 
mov ds, ax 
mov ah, 9  ; DOS Function call to print a message. 
mov dx, offset message 
int 21h 
mov ax, 4c00h  ; DOS Function call to exit back to DOS. 
int 21h 
main endp 
_TEXT ends 
 
_DATA segment word public 'DATA' 
message db "Hello, World!", 0dh, 0ah, '$' 
_DATA ends 
 
STACK segment para stack 'STACK' 
db 100h dup (?) 
STACK ends 
 
end main 

hm2b.asm:

DGROUP group _DATA, STACK  ; Required by MASM 3.0 and 4.0. 
 
STACK segment para stack 'STACK' 
db 100h dup (?) 
STACK ends 
 
_DATA segment word public 'DATA' 
message db "Hello, World!", 0dh, 0ah, '$' 
_DATA ends 
 
; MASM 4.0 doesn't support USE16 (but MASM >=5.0 does). 
_TEXT segment word public 'CODE' 
assume cs:_TEXT, ds:DGROUP, ss:STACK 
main proc 
mov ax, DGROUP  ; Initialize DS. `seg _DATA' also works. 
mov ds, ax 
mov ah, 9  ; DOS Function call to print a message. 
mov dx, offset message 
int 21h 
mov ax, 4c00h  ; DOS Function call to exit back to DOS. 
int 21h 
main endp 
_TEXT ends 
 
end main

hm3.asm:

.8086 
.model small 
.stack 100h 
.data 
message db "Hello, World!", 0dh, 0ah, '$' 
.code 
main proc 
mov ax, @data  ; Initialize DS. 
mov ds, ax 
mov ah, 9  ; DOS Function call to print a message. 
mov dx, offset message 
int 21h 
mov ax, 4c00h  ; DOS Function call to exit back to DOS. 
int 21h 
main endp 
end main 

I want these to be identical when compiled by Microsoft Macro Assembler 5.00 masm.exe and then linked to an .EXE file with the corresponding link.exe . However, hm2b.exe is about 256 == 100h bytes longer than hm2.exe and hm3.exe, because it contains zero-bytes for the STACK. How do I get rid of these zero-bytes without using .model and .stack, thus being compatible with Microsoft Macro Assembler 4.00 and earlier?


Solution

  • For old-style code (without a .model directive and without the dosseg directive, example: hm2b.asm and hm2.asm), the STACK segment must be the last for the trailing 00 bytes to be omitted by the linker. Also, the order of _TEXT and _DATA segment determines the order in which they are written to the .exe program output file. The order of segments within the DGROUP group doesn't matter.

    For new-style code (with a .model directive or the dosseg directive, both supported from Microsoft Macro Assembler 5.00 and later; example: hm3.asm), the order of .stack, .data and .code directives doesn't matter. the assembler generates the .obj file in the correct order (_TEXT, _DATA, STACK).

    In general, the order of segments within the .asm file propagates to the .obj file, and the linker uses that to decide the order within the final .exe file.