简体   繁体   English

OpenWatcom编译链接的小model DOS.exe崩溃

[英]Small model DOS .exe compiled and linked by OpenWatcom crashes

I'm trying to create a small DOS.exe program.我正在尝试创建一个小型 DOS.exe 程序。 I wrote the entry point in NASM assembly我在 NASM 汇编中写了入口点

; st.nasm
global _small_code_
global _printmsg_
extern _main0_

segment code
_small_code_:
..start:
mov ax, data
mov ds, ax
mov ax, stack
mov ss, ax
mov sp, stacktop

mov ah, 9  ; WRITE_STDOUT
mov dx, hello_msg
int 0x21
call _main0_
; call _printmsg_
; mov ax, 3
mov dx, ax
add dx, hello_msg
mov ah, 9  ; WRITE_STDOUT
int 0x21
mov ah, 0x4c  ; EXIT, exit code in al
int 0x21

_printmsg_:
ret
push dx
xchg ax, dx
mov ah, 9  ; WRITE_STDOUT
mov dx, hello_msg  ; !!
int 0x21
pop dx
ret  ; !! restore AX?

segment data
hello_msg: db 'Hello, World!', 13, 10, '$'

segment stack stack
resb 1024
stacktop:

Please note that I'm not sure how the ..start: code and the segments should look like, I copy-pasted that part from somewhere.请注意,我不确定..start:代码和段应该是什么样子,我从某处复制粘贴了该部分。

I wrote the main program in C:我在C写了主程序:

/* prog.c */
void _printmsg(const char *msg);
int add(int a, int b) {
  return a + b * 2;
}
void other() {
  _printmsg("Hello!\r\n$"); /*CRASH*/
  /*_printmsg(0);*/  /*OK*/
}
int _main0() {
  return 5;
}

I compile it with this:我用这个编译它:

$ nasm -f obj -o st.obj st.nasm
$ owcc -bdos -mcmodel=s -fno-stack-check -Os -s -march=i86 -o prog.exe prog.c st.obj

The resulting prog.exe is:生成的 prog.exe 是:

$ xxd prog.exe 
00000000: 4d5a 8b00 0100 0200 0300 4000 ffff 0100  MZ........@.....
00000010: 4b04 0000 0a00 0100 2000 0000 0000 0000  K....... .......
00000020: 0b00 0100 1000 0100 0000 0000 0000 0000  ................
00000030: d1e2 01d0 c3b8 0000 e934 00b8 0500 c300  .........4......
00000040: 4865 6c6c 6f21 0d0a 2400 b801 008e d8b8  Hello!..$.......
00000050: 0100 8ed0 bc4b 04b4 09ba 3b00 cd21 e8da  .....K....;..!..
00000060: ff89 c281 c23b 00b4 09cd 21b4 4ccd 21c3  .....;....!.L.!.
00000070: 5292 b409 ba3b 00cd 215a c348 656c 6c6f  R....;..!Z.Hello
00000080: 2c20 576f 726c 6421 0d0a 24              , World!..$

Disassembly of prog.exe: prog.exe反汇编:

$ ndisasm -e 0x20 -b 16 prog.exe 
00000000  0B00              or ax,[bx+si]
00000002  0100              add [bx+si],ax
00000004  1000              adc [bx+si],al
00000006  0100              add [bx+si],ax
00000008  0000              add [bx+si],al
0000000A  0000              add [bx+si],al
0000000C  0000              add [bx+si],al
0000000E  0000              add [bx+si],al
00000010  D1E2              shl dx,1
00000012  01D0              add ax,dx
00000014  C3                ret
00000015  B80000            mov ax,0x0
00000018  E93400            jmp 0x4f
0000001B  B80500            mov ax,0x5
0000001E  C3                ret
0000001F  004865            add [bx+si+0x65],cl
00000022  6C                insb
00000023  6C                insb
00000024  6F                outsw
00000025  210D              and [di],cx
00000027  0A24              or ah,[si]
00000029  00B80100          add [bx+si+0x1],bh
0000002D  8ED8              mov ds,ax
0000002F  B80100            mov ax,0x1
00000032  8ED0              mov ss,ax
00000034  BC4B04            mov sp,0x44b
00000037  B409              mov ah,0x9
00000039  BA3B00            mov dx,0x3b
0000003C  CD21              int 0x21
0000003E  E8DAFF            call 0x1b
00000041  89C2              mov dx,ax
00000043  81C23B00          add dx,0x3b
00000047  B409              mov ah,0x9
00000049  CD21              int 0x21
0000004B  B44C              mov ah,0x4c
0000004D  CD21              int 0x21
0000004F  C3                ret
00000050  52                push dx
00000051  92                xchg ax,dx
00000052  B409              mov ah,0x9
00000054  BA3B00            mov dx,0x3b
00000057  CD21              int 0x21
00000059  5A                pop dx
0000005A  C3                ret
0000005B  48                dec ax
0000005C  656C              gs insb
0000005E  6C                insb
0000005F  6F                outsw
00000060  2C20              sub al,0x20
00000062  57                push di
00000063  6F                outsw
00000064  726C              jc 0xd2
00000066  64210D            and [fs:di],cx
00000069  0A24              or ah,[si]

prog.exe puts DOSBox to an infinite loop. prog.exe将 DOSBox 置于无限循环中。 Oddly enough, if I remove the string literal from the C source file (in the other function, which isn't even called), it successfully returns.奇怪的是,如果我从 C 源文件中删除字符串文字(在other function 中,它甚至没有被调用),它会成功返回。 What's wrong in the assembly file?汇编文件有什么问题?

Please note that this is the first time I'm using OpenWatcom, and this is the first time I build a DOS.exe file.请注意,这是我第一次使用 OpenWatcom,也是我第一次构建 DOS.exe 文件。

I don't want to write a main function, because that would cause the OpenWatcom libc to be linked to the output executable, making it unnecessarily large.我不想写一个main function,因为那样会导致 OpenWatcom libc 链接到 output 可执行文件,使其变得不必要地大。

The primary problem is in how you define the code segment.主要问题在于您如何定义代码段。 The Watcom C/C++ compiler when using the SMALL memory model requires the code segment to be called _TEXT with a class of CODE .当使用SMALL memory model 时,Watcom C/C++ 编译器要求将代码段称为_TEXT ,代码段为CODE This mismatch between the assembly code and the C code leads to the code segment being in different physical segments and the call _main0_ jumping to the wrong place in memory causing exceptions to be thrown and the program hanging or crashing.这种汇编代码和C代码不匹配导致代码段在不同的物理段, call _main0_跳转到memory错误的地方导致抛出异常,程序挂起或崩溃。

You can also get the Watcom linker to generate the required STACK in the DOS EXE by creating a segment called _STACK with attribute STACK and class STACK .您还可以让 Watcom linker 通过创建一个名为_STACK且属性为STACK和 class STACK If you create the stack segment this way, you won't need to initialize SS:SP at the beginning of your program.如果以这种方式创建堆栈段,则无需在程序开始时初始化SS:SP

The other sections that Watcom uses in the SMALL memory model are: Watcom 在SMALL memory model 中使用的其他部分是:

  • _DATA segment with a class of DATA for read/write data _DATA段带有 class 的DATA用于读/写数据
  • CONST segment with a class of DATA for string literals (that aren't expected to be modified)包含 class DATACONST段用于字符串文字(预计不会被修改)
  • CONST2 segment with a class of DATA for other read only data CONST2段带有 class 的DATA用于其他只读数据
  • _BSS segment with class of BSS for uninitialized data. _BSS段为 class 的BSS ,用于未初始化的数据。

Watcom expects that the segments CONST , CONST2 , _DATA and _BSS to all be in the same group called DGROUP . Watcom 期望段CONSTCONST2_DATA_BSS都在同一个组中,称为DGROUP All the data in the same group can be referenced by the name of the group.同一组中的所有数据都可以通过组名来引用。 When you set up DGROUP in the way Watcom expects then all you have to do is initialize DS to the DGROUP segment and not the individual segments within the group.当您以 Watcom 期望的方式设置DGROUP时,您所要做的就是将DS初始化为DGROUP段,而不是组内的各个段。

The special label that starts with .. acts as the DOS entry point where execution should start...开头的特殊 label 充当应该开始执行的 DOS 入口点。 Thus ..start is used to generate an entry point in the DOS EXE header telling the program loader where to start executing when the program is loaded into memory.因此..start用于在DOS EXE header中生成一个入口点,告诉程序加载器当程序加载到memory时从哪里开始执行。

A revised version of your assembly code could have looked like this:汇编代码的修订版本可能如下所示:

; st.nasm

; DGROUP in watcom C/C++ for small model is:
GROUP DGROUP CONST CONST2 _DATA _BSS

global _small_code_
global _printmsg_
extern _main0_

; Code Segment (16-bit code)
segment _TEXT use16 class=CODE
_small_code_:
; .. denotes the label to be used as the DOS entry point
..start:
    mov ax, DGROUP
    mov ds, ax

    mov ah, 9  ; WRITE_STDOUT
    mov dx, hello_msg
    int 0x21
    call _main0_
    ; call _printmsg_
    ; mov ax, 3
    mov dx, ax
    add dx, hello_msg
    mov ah, 9  ; WRITE_STDOUT
    int 0x21
    mov ah, 0x4c  ; EXIT, exit code in al
    int 0x21

_printmsg_:
    ret
    push dx
    xchg ax, dx
    mov ah, 9  ; WRITE_STDOUT
    mov dx, hello_msg  ; !!
    int 0x21
    pop dx
    ret  ; !! restore AX?

; Read only string literals here
segment CONST class=DATA
hello_msg: db 'Hello, World!', 13, 10, '$'

; Other read only data here
segment CONST2 class=DATA

; Read/Write data here
segment _DATA class=DATA

; Uninitialized data segment
segment _BSS class=BSS

; Stack segment 1k in size
segment _STACK STACK class=STACK
resb 1024

This code assumes that SS,= DS, however it would have to be compiled with OWCC's option -Wc,-zu that passes the -zu to WCC (Watcom Compiler).此代码假定 SS,= DS,但是它必须使用 OWCC 的选项-Wc,-zu进行编译,该选项将-zu传递给 WCC(Watcom 编译器)。 -zu modifies code generation so that: -zu修改代码生成,以便:

 -zu SS.= DGROUP (ie,, do not assume stack is in data segment)

If you wish to set SS==DS==DGROUP there are a number of ways to do it.如果你想设置 SS==DS==DGROUP 有很多方法可以做到。 One option I may suggest is putting _STACK in DGROUP with all the other program data.我可能建议的一个选项是将_STACK与所有其他程序数据一起放在DGROUP中。 You would need a label after resb 1024 like stack_top: so you can load that offset into SP at startup after you set SS to the same value as DS .resb 1024之后,您需要一个 label,例如stack_top:这样您就可以在将SS设置为与DS相同的值后,在启动时将该偏移量加载到SP中。 This change would result in assembly code that looks like:此更改将导致汇编代码如下所示:

; st.nasm

; DGROUP in watcom C/C++ for small model is:
GROUP DGROUP CONST CONST2 _DATA _BSS _STACK
; _STACK has been added to DGROUP so we can set SS==DS==DGROUP

global _small_code_
global _printmsg_
extern _main0_

; Code Segment (16-bit code)
segment _TEXT use16 class=CODE
_small_code_:
; .. denotes the label to be used as the DOS entry point
..start:
    mov ax, DGROUP
    mov ds, ax
    mov ss, ax             ; Set stack SS:SP to DGROUP:stack_top
    mov sp, stack_top

    mov ah, 9  ; WRITE_STDOUT
    mov dx, hello_msg
    int 0x21
    call _main0_
    ; call _printmsg_
    ; mov ax, 3
    mov dx, ax
    add dx, hello_msg
    mov ah, 9  ; WRITE_STDOUT
    int 0x21
    mov ah, 0x4c  ; EXIT, exit code in al
    int 0x21

_printmsg_:
    ret
    push dx
    xchg ax, dx
    mov ah, 9  ; WRITE_STDOUT
    mov dx, hello_msg  ; !!
    int 0x21
    pop dx
    ret  ; !! restore AX?

; Read/Write data here
segment _DATA class=DATA

; Read only string literals here
segment CONST class=DATA
hello_msg: db 'Hello, World!', 13, 10, '$'

; Other read only data here
segment CONST2 class=DATA

; Uninitialized data segment
segment _BSS class=BSS

; Stack segment 1k in size
segment _STACK STACK class=STACK
resb 1024
stack_top:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM