簡體   English   中英

OpenWatcom編譯鏈接的小model DOS.exe崩潰

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

我正在嘗試創建一個小型 DOS.exe 程序。 我在 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:

請注意,我不確定..start:代碼和段應該是什么樣子,我從某處復制粘貼了該部分。

我在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;
}

我用這個編譯它:

$ 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

生成的 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!..$

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將 DOSBox 置於無限循環中。 奇怪的是,如果我從 C 源文件中刪除字符串文字(在other function 中,它甚至沒有被調用),它會成功返回。 匯編文件有什么問題?

請注意,這是我第一次使用 OpenWatcom,也是我第一次構建 DOS.exe 文件。

我不想寫一個main function,因為那樣會導致 OpenWatcom libc 鏈接到 output 可執行文件,使其變得不必要地大。

主要問題在於您如何定義代碼段。 當使用SMALL memory model 時,Watcom C/C++ 編譯器要求將代碼段稱為_TEXT ,代碼段為CODE 這種匯編代碼和C代碼不匹配導致代碼段在不同的物理段, call _main0_跳轉到memory錯誤的地方導致拋出異常,程序掛起或崩潰。

您還可以讓 Watcom linker 通過創建一個名為_STACK且屬性為STACK和 class STACK 如果以這種方式創建堆棧段,則無需在程序開始時初始化SS:SP

Watcom 在SMALL memory model 中使用的其他部分是:

  • _DATA段帶有 class 的DATA用於讀/寫數據
  • 包含 class DATACONST段用於字符串文字(預計不會被修改)
  • CONST2段帶有 class 的DATA用於其他只讀數據
  • _BSS段為 class 的BSS ,用於未初始化的數據。

Watcom 期望段CONSTCONST2_DATA_BSS都在同一個組中,稱為DGROUP 同一組中的所有數據都可以通過組名來引用。 當您以 Watcom 期望的方式設置DGROUP時,您所要做的就是將DS初始化為DGROUP段,而不是組內的各個段。

..開頭的特殊 label 充當應該開始執行的 DOS 入口點。 因此..start用於在DOS EXE header中生成一個入口點,告訴程序加載器當程序加載到memory時從哪里開始執行。

匯編代碼的修訂版本可能如下所示:

; 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

此代碼假定 SS,= DS,但是它必須使用 OWCC 的選項-Wc,-zu進行編譯,該選項將-zu傳遞給 WCC(Watcom 編譯器)。 -zu修改代碼生成,以便:

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

如果你想設置 SS==DS==DGROUP 有很多方法可以做到。 我可能建議的一個選項是將_STACK與所有其他程序數據一起放在DGROUP中。 resb 1024之后,您需要一個 label,例如stack_top:這樣您就可以在將SS設置為與DS相同的值后,在啟動時將該偏移量加載到SP中。 此更改將導致匯編代碼如下所示:

; 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