如何启动此代码?
我是汇编和C语言编程的新手(在Linux中使用GCC)
这里有人可以告诉我如何将C代码编译成汇编语言并使用笔式驱动器从它启动
我使用命令(在Linux终端中):
gcc -S bootcode.c
代码给了我一个bootcode.S 文件
我该怎么办???
我只想编译以下代码并直接从 USB 记忆棒运行它,
#include<stdio.h>
void main()
{
printf ("hi");
}
这里有任何帮助吗???
i am a newbie to assembly and program in c (use GCC in Linux)
can anyone here tell me how to compile c code into assembly and boot from it using pen drive
i use the command (in linux terminal) :
gcc -S bootcode.c
the code gives me a bootcode.S file
what do i do with that ???
i just wanna compile the following code and run it directly from a USB stick
#include<stdio.h>
void main()
{
printf ("hi");
}
any help here ???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,
您应该意识到,当您编写引导加载程序代码时,您应该知道您正在创建自己的代码环境,这意味着,没有任何现成的 C 库可供您使用或类似的东西,唯一且唯一BIOS 服务(或中断例程)。
现在,如果你得到了这个,你可能会发现上面的代码将无法启动,因为你没有“stdio.h”标头,这意味着CPU在执行编译后的代码时不会找到这个标头,因此无法理解什么是“printf”(因为 printf 是 stdio.h 标头的一种方法)。
因此,如果您想打印任何字符串,您需要自己编写此函数,或者在单独的文件中作为标头,并在创建最终二进制文件时在编译时链接其目标文件,或者在同一文件中。这取决于你。可能还有其他方法,我不太熟悉,只是做一些研究。
您应该知道的另一件事是,BIOS 负责将此启动代码(在您的情况下为上述代码)加载到内存位置 0x07C00(段:偏移表示中的 0x0000h:0x7C00),因此您必须在代码中提及您正在此内存位置上编写此代码,方法是
1-使用 ORG 指令
2-或者通过加载相应的寄存器(cs,ds,es)
此外,您应该自己熟悉段:偏移内存表示方案,只需谷歌它或阅读英特尔手册。
最后,为了让 BIOS 将代码加载到 0x07C00,引导代码不得超过 512 字节(仅在可引导媒体的第一个扇区上,因为扇区是 512 字节)并且他必须找到第一个扇区的最后两个字节(代码的字节 510 和字节 511) 的启动签名 0x55AA,否则 BIOS 不会将此代码视为可启动。
通常编码为:
ORG 0x7C00
...
您的引导代码并加载更多代码,因为 512 字节是不够的。
...
乘以 510 - ($ - $$) db 0x00 ; Zerofill 高达 510 字节
只是为了让您知道,我不会在这里涵盖所有内容,因为如果是这样,我将编写有关它的页面,您需要在网上寻找更多资源,这里是一个开始的链接with(在汇编中编码):
http://www.brokenthorn.com/Resources/OSDevIndex.html
就是这样,希望这对您有帮助...^_^
Khilo - 阿尔及利亚
First of all,
You Should be aware that when you are writing bootloader codes , you should know that you are CREATING YOUR OWN ENVIRONMENT of CODE, that means, there is nothing such ready made C Library available to you or anything similar , ONLY and ONLY BIOS SERVICES (or INTERRUPT ROUTINES).
Now, if you got this, you will probably figure out that the above code won't boot since, you don't have the "stdio.h" header, this means that the CPU when executing your compiled code won't find this header and thereby won't understand what is "printf" (since printf is a method of the stdio.h header).
So if you want to print any string you need to write this function by YOUR OWN either in a separate file as a header and link its object file at compilation time when creating the final binary file or in the same file. it is up to you. There could be other ways, I'm not well familiar with them, just do some researches.
Another thing you should know, it is the BIOS who is responsible for loading this boot code (your above code in your case) into memory location 0x07C00 (0x0000h:0x7C00 in segment:offset representation), so you HAVE to mention in your code that you are writing this code on this memory location, either by
1-using the ORG instruction
2-Or by loading the appropriate registers for that (cs,ds,es)
Also, you should get yourself familiar with the segment:offset memory representation scheme, just google it or read intel manuals.
Finally, for the BIOS to load your code into the 0x07C00, the boot code must not exceed 512byte (ONLY ON FIRST SECTOR OF THE BOOTABLE MEDIA, since a sectore is 512byte) and he must find at the last two byte of this first sector (byte 510 & byte 511) of your code the boot signature 0x55AA, otherwise the BIOS won't consider this code AS BOOTABLE.
Usually this is coded as :
ORG 0x7C00
...
your boot code and to load more codes since 512byte won't be sufficient.
...
times 510 - ($ - $$) db 0x00 ; Zerofill up to 510 bytes
Just to let you know, I'm not covering everything here, because if so, I'll be writing pages about it, you need to look for more resources on the net, and here is a link to start with(coding in assembly):
http://www.brokenthorn.com/Resources/OSDevIndex.html
That's all, hopefully this was helpful to you...^_^
Khilo - ALGERIA
启动计算机并不那么容易。需要编写引导加载程序。 Bootloader必须遵守一定的规则并与ROM等硬件相对应。你还需要禁用中断,保留一些内存等。查找MikeOS,这是一个很棒的项目,可以更好地帮助你理解这个过程。
干杯
Booting a computer is not that easy. A bootloader needs to be written. The bootloader must obey certain rules and correspond with hardware such as ROM. You also need to disable interrupts, reserve some memory etc. Look up MikeOS, it's a great project that can better help you understand the process.
Cheers