开发者

Program a system with C++

My question is simple because I am 开发者_运维百科a beginner, so Can I program a full computer system with only using C++ language? Thanks.


Well ... Typically you will need at least some assembly at the very lowest levels, for machine-dependent initialization and so on.

So "no" strictly speaking, but that can really be a very small proportion, making the real answer "yes".

BeOS is an example of an operating system written in C++.


No you cannot.

You need low-level services which are not standardized within programming languages. For example, you need system port and DMA IO, that basically look different on all platforms. This is usually done by inline assembly code on the lowest level, though some C++ compilers will provide you with special keywords to access CPU features such as registers and special opcodes. For instance, in MS VC++ you have _EAX pseudo-variable to access the EAX CPU register.


You will need some assembly. This is becaused there are some privileged instructions that are needed for operating system design that will not be generated by a C/C++ compiler.

One example is when userside code want to make use of an operating system service (like interprocess communication) it needs to switch from user mode into kernel mode. This is normally done by issuing a Software Interrupt (SWI). A C++ will never create the SWI instruction.

Similarly, when writing an arbitrary precision integer arithmatic library, one will need to find of the value of the Carry Bit. There is no C/C++ operator that can do this for you. You will have to use assembler.

Incidentation, writing directly to a device register can and often is done in C. the volatile keyword is placed into the language specifically for registers whose values can change unexpectedly


Yes. C++ is Turing Complete... So is Excel, which a clever dude discovered while implementing a realtime 3D engine.


Simply, yes you can.


You can write all possible programs in C++. In fact, you can write most programs in most languages, especially if you exclude performance concerns. This concept is known as "Turing completeness"


You may be interested in this book. It explains how computers work, going from the lowest hardware level to a code. Cannot recommend it enough!

http://ecx.images-amazon.com/images/I/31VTerGLfML._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA240_SH20_OU01_.jpg


You could program a complete OS in C++ if you were so inclined (and had a decade or so to spare....) since it does compile to machine code.

It's probably not a 'beginner' task though, and to be honest, plain old C would be a better choice for system level stuff (both the Windows and Linux kernels use C).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜