如何通过Watcom编译器从C生成16位可执行BINARY RAW格式?

时间:2012-05-18 16:53:40

标签: c assembly compilation watcom

我想通过Watcom C compiler生成16位可执行BINARY RAW格式。类似于EXE文件,没有任何标题在实模式下运行。

我使用Large内存模型,因此代码段和数据段可能不同,并且可以增加超过64K字节。

我喜欢这个:

// kernel.c
void kernel(void)
{
    /* Print Hello! */
    __asm
    {
        mov ah, 0x0E;
        mov bl, 7

        mov al, 'H'
        int 0x10

        mov al, 'e'
        int 0x10

        mov al, 'l'
        int 0x10

        mov al, 'l'
        int 0x10

        mov al, 'o'
        int 0x10

        mov al, '!'
        int 0x10
    }
    return;
}

为了编译上面的代码,我在批处理文件下面运行:

@rem build.bat

@rem Cleaning.
del *.obj
del *.bin

cls

@rem Compiling.
@rem 0:     8088 and 8086 instructions.
@rem d0:    No debugging information.
@rem ml:    The "large" memory model (big code, big data) is selected.
@rem s:     Remove stack overflow checks.
@rem wx:    Set the warning level to its maximum setting.
@rem zl:    Suppress generation of library file names and references in object file.
wcc -0 -d0 -ml -s -wx -zl kernel.c

@rem Linking.
@rem FILE:      Specify the object files.
@rem FORMAT:    Specify the format of the executable file.
@rem NAME:      Name for the executable file.
@rem OPTION:    Specify options.
@rem Note startup function (kernel_) implemented in kernel.c.
wlink FILE kernel.obj FORMAT RAW BIN NAME kernel.bin OPTION NODEFAULTLIBS, START=kernel_

del *.obj

运行build.bat Watcom编译器和链接器生成的以下消息后:

D:\Amir-OS\ckernel>wcc -0 -d0 -ml -s -wx -zl kernel.c
Open Watcom C16 Optimizing Compiler Version 1.9
Portions Copyright (c) 1984-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
kernel.c: 28 lines, included 35, 0 warnings, 0 errors
Code size: 39

D:\Amir-OS\ckernel>wlink FILE kernel.obj FORMAT RAW BIN NAME kernel.bin OPTION N
ODEFAULTLIBS, START=kernel_
Open Watcom Linker Version 1.9
Portions Copyright (c) 1985-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
loading object files
Warning! W1014: stack segment not found
creating a RAW Binary Image executable

成功生成输出文件。

但我的问题是:

如何解决W1014警告?

有没有办法指定初始CS值?

1 个答案:

答案 0 :(得分:1)

堆栈段信息通常在cstartup模块中初始化,最终调用main。链接器将对链接中包含的模块的堆栈要求求和,这将反映在sp中的值cstartup位置。堆栈段将成为ds和es分组的一部分,除非您具有多线程标志(当ss将是独立的时)。您还可以尝试在链接器命令文件中设置堆栈大小。

我建议你编写一个.asm模块,它在调用内核之前定义大内存模型所需的段和组。大量的试验和错误,但您学到的内容将适用于大多数环境的启动代码。

查看Watcom可以生成的(反汇编).lst文件,您应该获得有关如何编写.asm模块的足够指导。