如题所诉,今天被问到,一时竟无言以对,只是了解有些东西好像只能由汇编来操作,但不知道细节,望大侠指点!
------解决方案--------------------
汇编负责启动,堆栈初始化,系统运行段划分等关键代码,c语言用在算法,逻辑等于硬件关联不大的地方
------解决方案--------------------
CPU上电后会从IO空间的某地址取第一条指令。但此时:PLL没有启动,CPU工作频率为外部输入晶振频率,非常低;CPU工作模式、中断设置等不确定;存储空间的各个BANK(包括内存)都没有驱动,内存不能使用。在这种情况下必须在第一条指令处做一些初始化工作,这段初始化程序与操作系统独立分开,称之为bootloader。
一个嵌入式Bootloader最初始部分的代码几乎必须是用汇编语言写成的,因为开发板刚上电后没有准备好C程序运行环境,比如堆栈指针SP没有指到正确的位置。汇编代码应该完成最原始的硬件设备初始化,并准备好C运行环境,这样后面的功能就可以用C语言来写了。
------解决方案--------------------
因為有些動作, 像 Coprocessor 及 status register 的操作, User Mode/Supervisor Mode 的切換, SVC/IRQ/FIRQ/USER Stack 的設定, C language 都沒有相對應的 keyword, 所以只能用 Assembly 來實現.
Paul, Chao @ Techware
------解决方案--------------------
刚启动后还没有C语言环境,例如堆栈没有设置好,CPU工作模式、时钟频率什么的都得先初始化好
------解决方案--------------------
C 也可以,只是要有些技巧。很多 C 编译器在实现函数的时候,在进入函数时会自动的压栈,这样如果栈指针没设置好的话会有问题,如果这一步可以解决,那就没什么问题。但是内联汇编是不可少的。
有兴趣的话可以看看 Codewarrior 里面 ColdFire 的启动代码。
------解决方案--------------------
For bootloaders, the first line of code to be executed has to be written in assembly. The correspondent file name is usually called start.s(or crt0.s). start.s is the first program to run after power up. It does some initialization, then jumps to your main function in C language. In the lingo of C language, start.s is called run time library.
The C run time library has to be written in assembly language, and takes the name like crt0.s or start.s. As its name implies, it needs to set up the run time environment before the code in main() function can be executed (That is why it can not be written in C!), which includes:
(1) Initialize the hardware, such as CPU cache and MMU, if necessary
(2) Setup the Stack
(3) Initialize the .bss section to all zero in the memory
(4) Calling the main() function
For application developers, the compiler would provide a default run time library during link stage, which is good enough in most cases. However, for programs like the bootloader, the developer has to provide its own run time library in order to set up the environment correctly. It will be the first code to run after power reset.
------解决方案--------------------
------解决方案--------------------
MSDN的How develop a boot loader文档里有详细的说明,可以去看看。不过好像楼上已经给出来了。可以去看看wince自带的几个BSP包里的文件,明白了。呵呵
------解决方案--------------------
开始还没有C语言环境,相关初始化要通过直接读写寄存器来完成