我正在使用平面二进制文件作为我操作系统的外部程序。当我编译它们时,像这样:
gcc -Wall ctest.c -o ctest.bin -nostdlib -Wl,-Ttext=0x8000,-nostdlib -masm=intel
objcopy -O binary -j .text ctest.bin ctest
但是这样做,字符数组的内容并没有放入文件中。这是我的代码:
static volatile char string[4] = "Hi!\0";
static volatile char string2[15] = "Hello, World!\n\0";
int _start()
{
asm("mov eax, [string]");
asm("mov ebx, 0x00");
asm("int 0x01");
asm("mov eax, [string2]");
asm("mov ebx, 0x00");
asm("int 0x01");
return 0;
}
当我运行 objdump 时(我在 elf 上运行它,但我验证它具有与此相同的代码):
00008000 <_start>:
8000: 55 push ebp
8001: 89 e5 mov ebp,esp
8003: a1 70 90 00 00 mov eax,ds:0x9070
8008: bb 00 00 00 00 mov ebx,0x0
800d: cd 01 int 0x1
800f: a1 74 90 00 00 mov eax,ds:0x9074
8014: bb 00 00 00 00 mov ebx,0x0
8019: cd 01 int 0x1
801b: b8 00 00 00 00 mov eax,0x0
8020: 5d pop ebp
8021: c3 ret
如您所见,找不到文本。我希望它会做这样的事情:string db "Hi!", 0
我会用 nasm 做。
我应该怎么做才能在输出 bin 文件中包含字符而不用汇编编码?
提前致谢。
最佳答案
二进制可执行文件通常分为几个部分。您的字符串只是被放置在与代码不同的部分。这是有道理的,因为代码应该是只读的,但字符串已被声明为非 const 和 volatile。
关于c - 在平面二进制文件中包含 char 数组的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14065566/