简体   繁体   English

如何告诉 GCC 为实模式生成 16 位代码

[英]How to tell GCC to generate 16-bit code for real mode

I am writing real mode function, which should be normal function with stackframes and so, but it should use %sp instead of %esp.我正在编写实模式函数,它应该是带有堆栈帧等的普通函数,但它应该使用 %sp 而不是 %esp。 Is there some way to do it?有什么方法可以做到吗?

GCC 5.2.0 (and possible earlier versions) support 16-bit code generation with the -m16 flag. GCC 5.2.0(以及可能的早期版本)支持使用-m16标志生成 16 位代码。 However, the code will almost certainly rely on 32-bit processor features (such as 32-bit wide registers), so you should check the generated assembly carefully.但是,代码几乎肯定会依赖于 32 位处理器功能(例如 32 位宽寄存器),因此您应该仔细检查生成的程序集。

From the man pages:man页:

The -m16 option is the same as -m32, except for that it outputs the ".code16gcc" assembly directive at the beginning of the assembly output so that the binary can run in 16-bit mode. -m16 选项与 -m32 相同,不同之处在于它在汇编输出的开头输出“.code16gcc”汇编指令,以便二进制文件可以在 16 位模式下运行。

Firstly, gcc could build 16bit code, because the linux kernel is go through realmode to protectmode, so it could even build 16bit c code.首先gcc可以编译16位代码,因为linux内核是经过实模式到保护模式的,所以它甚至可以编译16位c代码。

Then, -m16 option is supported by GCC >= 4.9 and clang >= 3.5然后,GCC >= 4.9 和 clang >= 3.5 支持 -m16 选项

gcc will ignore asm(".code16"),you can see it by -S output the assembly code surround by #APP #NO_APP gcc 会忽略 asm(".code16"),你可以通过 -S 看到它输出 #APP #NO_APP 包围的汇编代码

the linux kernel do the trick to compile 16bit c with a code16gcc.h(only have .code16gcc) pass to gcc compile params directly. linux内核使用code16gcc.h(只有.code16gcc)直接传递给gcc编译参数来编译16位c。 see Build 16-bit code with -m16 where possible , also see the linux kernel build Makefile请参阅尽可能使用 -m16 构建 16 位代码,另请参阅 linux 内核构建Makefile

if you direct put the asm(".code16gcc"), see Writing 16-bit Code , it's not real 16bit code, call , ret , enter , leave , push , pop , pusha , popa , pushf , and popf instructions default to 32-bit size如果你直接把 asm(".code16gcc"),参见编写 16 位代码,它不是真正的 16 位代码, call , ret , enter , leave , push , pop , pusha , popa , pushfpopf指令默认为 32 -位大小

GCC does not produce 8086 code. GCC 不产生 8086 代码。 The GNU AS directive .code16gcc can be used to assemble the output of GCC to run in a 16-bit mode, put asm(".code16gcc") at the start of your C source, your program will be limited to 64Kibytes. GNU AS 指令.code16gcc可用于汇编 GCC 的输出以在 16 位模式下运行,将asm(".code16gcc")放在您的 C 源代码的开头,您的程序将被限制为 64 KB。

On modern GCC versions you can pass the -m16 argument to gcc which will produce code to run in a 16-bit mode.在现代 GCC 版本中,您可以将-m16参数传递给gcc ,这将生成以 16 位模式运行的代码。 It still requires a 386 or later.它仍然需要 386 或更高版本。

As far as I know, GCC does not support generation of code for 16-bit x86.据我所知,GCC 不支持为 16 位 x86 生成代码。 For legacy bootloaders and similar purposes, you should write a small stub in assembly language to put the cpu in 32-bit mode and pass off execution to 32-bit code.对于传统引导加载程序和类似目的,您应该用汇编语言编写一个小存根,将 cpu 置于 32 位模式并将执行传递给 32 位代码。 For other purposes you really shouldn't be writing 16-bit code.出于其他目的,您真的不应该编写 16 位代码。

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

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