不使用 MASM 5.0 将堆栈字节添加到 .EXE 文件

问题描述 投票:0回答:2

我正在为 DOS 编写 3 个汇编文件:

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 

我希望它们在由 Microsoft Macro Assembler 5.00 masm.exe 编译时是相同的,然后使用相应的 link.exe 链接到 .EXE 文件。但是,hm2b.exehm2.exehm3.exe 长约 256 == 100h 字节,因为它包含

STACK
的零字节。如何在不使用
.model
.stack
的情况下摆脱这些零字节,从而与 Microsoft Macro Assembler 4.00 及更早版本兼容?

assembly dos x86-16 masm stack-memory
2个回答
3
投票

我在 here 找到了 MASM 3.00 手册的副本。在第 3-2 页有一个看起来像这样的示例程序

 .8086

DATA segment ; Program Data Segment
STRING db "Hello .", 13, 10, "$"
DATA ends

CODE segment ; Program Code Segment
 assume cs :CODE, ds :DATA
START: ; Program Entry Point
 mov ax, seg DATA
 mov ds, ax
 mov dx, offset STRING
 mov ah, 9
 int 21h
 mov ah, 4ch
 int 21h
CODE ends

STACK segment stack ; Program Stack Segment
 assume ss :STACK
 dw 64 dup(?)
STACK ends

 end START

我能够将其组装并链接到 640 字节的 exe。当我增加堆栈大小(从 64 到 1024)时,exe 大小没有改变,据此我假设链接器没有填充图像中的单元化堆栈字节。 如果您将示例程序与您的示例程序进行比较,也许这会帮助您弄明白。

可能答案是删除 DGROUP 指令。


2
投票

对于旧式代码(没有

.model
指令和
dosseg
指令,例如:hm2b.asmhm2.asm),
STACK segment
必须是尾随 00 字节的最后一个被链接器省略。此外,_TEXT 和 _DATA 段的顺序决定了它们写入 .exe 程序输出文件的顺序。
DGROUP group
中的段顺序无关紧要。

对于新型代码(带有

.model
指令或
dosseg
指令,Microsoft Macro Assembler 5.00 及更高版本均支持;示例:hm3.asm),
.stack
.data
.code
指令无关紧要。汇编程序以正确的顺序(_TEXT、_DATA、STACK)生成 .obj 文件。

通常,.asm 文件中段的顺序传播到 .obj 文件,链接器使用它来决定最终 .exe 文件中的顺序。

© www.soinside.com 2019 - 2024. All rights reserved.