gpt4 book ai didi

c - NASM ORG 指令有 GCC 版本吗?

转载 作者:太空宇宙 更新时间:2023-11-04 01:22:24 24 4
gpt4 key购买 nike

我目前正在制作一个操作系统,当我尝试添加 C 支持时,我遇到了一点问题......在汇编中,我的操作系统上的每个程序都以 ORG 32768 开头(用于偏移代码来源的 NASM 编译器预处理器指令),但我似乎无法找到任何使用 C 的 GCC 编译器来执行此操作的方法。所以,我的问题是,如何实现这一点(偏移代码的来源)在 C 中使用 GCC? (是的,我在问之前已经查过了,甚至检查了 GNU's official GCC's C preprocessor documentation )

最佳答案

ORG 和 .ORG 可以追溯到您使用汇编语言编写程序并且不一定需要链接器的时代。

据我所知,gnu 工具不支持它。

开始.s

.globl _start
_start:
mov $0xA000,%rsp
callq fun
jmp .

函数.c

unsigned int fun ( void )
{
return(7);
}

有趣的.ld

MEMORY
{
ram : ORIGIN = 0x8000, LENGTH = 0x2000
}
SECTIONS
{
.text : { *(.text*) } > ram
.rodata : { *(.rodata*) } > ram
.data : { *(.data*) } > ram
.bss : { *(.bss*) } > ram
}

构建命令

as start.s -o start.o
gcc -O2 -nostdlib -nostartfiles -ffreestanding -c fun.c -o fun.o
ld -T fun.ld start.o fun.o -o fun

产生这个程序:

0000000000008000 <_start>:
8000: 48 c7 c4 00 a0 00 00 mov $0xa000,%rsp
8007: e8 04 00 00 00 callq 8010 <fun>
800c: eb fe jmp 800c <_start+0xc>
800e: 66 90 xchg %ax,%ax

0000000000008010 <fun>:
8010: b8 07 00 00 00 mov $0x7,%eax
8015: c3 retq

我使用了 0x8000 (32768) 的入口点。

如果您所说的 gcc 指的是 gnu 工具,并且只是想使用汇编语言,那么它会更简单一些,您只需要 binutils 包而不是 gcc。但是您仍然需要链接器并在上面的非常简单的链接器脚本示例中使用 ORIGIN,您可以在其中使用 .ORG 与程序集内联。

开始.s

.globl _start
_start:
mov $0xA000,%rsp
mov $0x7,%eax
add $0x1,%eax
jmp .

与上面相同的链接描述文件

as start.s -o start.o
ld -T fun.ld start.o -o fun

生产

0000000000008000 <_start>:
8000: 48 c7 c4 00 a0 00 00 mov $0xa000,%rsp
8007: b8 07 00 00 00 mov $0x7,%eax
800c: 83 c0 01 add $0x1,%eax
800f: eb fe jmp 800f <_start+0xf>

关于c - NASM ORG 指令有 GCC 版本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38966088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com