2

我正在用 OpenWatcom V2 编译这个 C 源代码wcc

/* Writes a '\0'-terminated string to the file descriptor. */
static void fdputs(int fd, const char *s);
#pragma aux fdputs = \
"push si" \
"mov cx, -1" \
"repz scasb" \
"neg cx" \
"inc cx"  /* now cx is the number of bytes to write */ \
"pop dx"  /* now dx points to the buffer (s argument) */ \
"mov ah, 0x40" /* WRITE */ \
"int 0x21" \
parm [ bx si ] \
modify [ ax cx dx si ];  /* Also modifies cf */

int myfunc(void) {
  fdputs(1, "Hello!");
  return 0;
}

在 wcc 生成的 .obj 文件的 disassebly 中,6 pushes 和 5 pops 不平衡。(代码在运行时会因此而崩溃。)

$ wcc -bt=dos -ms -s -os -W -w4 -wx -we -wcd=202 -0 -fr -fo=t.obj t.c
$ wdis -a -fi -i=@ t.obj
.387
                PUBLIC  myfunc_
                EXTRN   _small_code_:BYTE
DGROUP          GROUP   CONST,CONST2,_DATA
_TEXT           SEGMENT BYTE PUBLIC USE16 'CODE'
                ASSUME CS:_TEXT, DS:DGROUP, SS:DGROUP
myfunc_:
        push            bx
        push            cx
        push            dx
        push            si
        mov             ax,offset DGROUP:@$1
        push            ax
        mov             bx,1
        xor             si,si
        push            si
        mov             cx,0ffffH
        repe scasb
        neg             cx
        inc             cx
        pop             dx
        mov             ah,40H
        int             21H
        xor             ax,ax
        pop             si
        pop             dx
        pop             cx
        pop             bx
        ret
_TEXT           ENDS
CONST           SEGMENT WORD PUBLIC USE16 'DATA'
@$1:
    DB  48H, 65H, 6cH, 6cH, 6fH, 21H, 0

CONST           ENDS
CONST2          SEGMENT WORD PUBLIC USE16 'DATA'
CONST2          ENDS
_DATA           SEGMENT WORD PUBLIC USE16 'DATA'
_DATA           ENDS
                END

我是否正确使用了 wcc 内联程序集?这可能是wcc中的错误吗?

4

1 回答 1

3

我相信这是 OpenWatcom C/C++ 的一个错误,因为我过去曾观察到这种行为。为了解决这个问题,我将每个参数分别列出在它们自己的[]. 尝试修改:

parm [ bx si ] \

成为:

parm [ bx ] [ si ] \

生成的代码应如下所示:

.387
                PUBLIC  myfunc_
                EXTRN   _small_code_:BYTE
DGROUP          GROUP   CONST,CONST2,_DATA
_TEXT           SEGMENT BYTE PUBLIC USE16 'CODE'
                ASSUME CS:_TEXT, DS:DGROUP, SS:DGROUP
myfunc_:
        push            bx
        push            cx
        push            dx
        push            si
        mov             si,offset DGROUP:@$1
        mov             bx,1
        push            si
        mov             cx,0ffffH
        repe scasb
        neg             cx
        inc             cx
        pop             dx
        mov             ah,40H
        int             21H
        xor             ax,ax
        pop             si
        pop             dx
        pop             cx
        pop             bx
        ret
_TEXT           ENDS
CONST           SEGMENT WORD PUBLIC USE16 'DATA'
@$1:
    DB  48H, 65H, 6cH, 6cH, 6fH, 21H, 0

CONST           ENDS
CONST2          SEGMENT WORD PUBLIC USE16 'DATA'
CONST2          ENDS
_DATA           SEGMENT WORD PUBLIC USE16 'DATA'
_DATA           ENDS
                END

删除了使用AX寄存器来传输地址const char *s以及push ax没有pop与之关联的相应内容的额外内容。

于 2020-07-12T22:49:42.693 回答