How to make a bootable program?

一笑奈何 提交于 2020-01-01 03:30:10

问题


So, the question might seem strange, but let's say that I compile:

int main(void)
{
    int x = 3;
    int y = 4;

    int z = x + y;
}

Is it possible to make the CPU run just that? How? For example, would this allow me to write to the monitor? (If I remember it correctly, there are places in memory in which you can write stuff to be displayed.)


回答1:


In the case of your program, it does not rely on any operating system services other than getting it started. If it were to additionally do input or output, it would almost certainly rely on the operating system to perform the i/o.

If you want to replace the operating system with your own code, then you could well do it. Minimalistic operating systems and program loaders exist. But yes, you have a lot to learn.




回答2:


printf is about the worst C library call to use it reaches in so many directions, floating point, string stuff, division, etc, etc. avoid it for this kind of thing. I see that you have not but read the comments.

To SEE the program running you want to blink an led or pin on a parallel port or send characters out a serial port, something that is significantly easier than programming the registers in a graphics card and debugging that code without anything to see if you are making progress.

Now with x86 you have a layer, not really an OS, but a layer that has this kind of code behind it that you can make simple calls to to print characters on the screen in a text mode and SEE your program running. I have stripped that out of pcemu_samples but the full pcemu and dosbox and other x86 emulation environments as well as the actual computer, have these things available to you. one of the sim environments (dosbox, pcemu, etC) are a better place to start (if you feel you have to start with x86, notice the theme here, I am actually discouraging starting with x86). If you did x86 you could go from dosbox or something like that and then work your way probably into a bootable usb stick and work your way from there.

embedded processors of which there are many, many eval boards to be had for under $50, some good ones at or under $20. But many many simulators that are free that you can learn from. I have a few you can try, thumbulator, amber_samples, pcemu_samples are probably the most stable (less likely to have to debug my stuff, mostly debugging your stuff), then others that are less stable, meecoate, msp430sim, etc. $5 will get you an msp430 board, $10 will get you an ARM cortex-m3 board, $20 will get you a too powerful to be called a microcontroller, microcontroller board (cortex-m4 based). And more money doesnt get you faster or better but more choices. boards that can run linux can/will make more pain for what you are trying to learn, stay under that level.

your simple example requires very little, outside the code the compiler creates for that function you need as little as one instruction to branch from the reset vector to the entry point of the main function. Ideally you want to setup a stack and eventually maybe zero .bss or copy .data, etc. Just learning to use the tools (compiler, assembler, linker) to build an embedded binary (even if you have no hardware/simulator to run on) will show you (disassemble) how to connect the dots between reset and main() (you dont have to call it main, you can name it anything you want, some compilers you DONT want to call it main they will throw extra junk into your binary).




回答3:


It's just a long story ,you need to be familiar with assembly and you have to know what scratch is the proper for your CPU kind generally , assembly for x86 series is very famous and you can go through learning it .

you will be seeing some thing like that :

Start:

mov AX ,0xff ;example 1
mov BX ,[CX] ;example 2
myString db 'Hi This is my First scratch',0 ;Print Example
call printFunc ;example 3 
jmp $

printFunc:
 mov ah, 0Eh ;This how you print on screen in Assembly
 loop
 lodsb
 cmp AX,BX
 je loop

etc etc ..... ... .



来源:https://stackoverflow.com/questions/9983353/how-to-make-a-bootable-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!