tags:

views:

288

answers:

10

Hello everybody.. My question is simple because I am a beginner, so Can I program a full computer system with only using C++ language? Thanks.

+1  A: 

Simply, yes you can.

Goz
And how do you make direct access to hardware register in C++ or even C?
drhirsch
Well that depends on how you define "system" then doesn't it? I was assuming he meant a full _software_ system ...
Goz
drhirsch, also depends on the hardware. If the hardware register is memory mapped, no problem at all.
Amigable Clark Kant
@drhirsch: You assume that you need to? That knowledge is in the compiler.
Martin York
For accessing hardware registers that are memory mapped, just assign its address to a pointer then dereference the pointer; always declare the pointer as pointing to something `volatile` so the compiler won't cache the value. As for registers mapped as ports, one has to use assembly because C has no notion of ports.
Thomas Matthews
+10  A: 

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++.

unwind
And also AROS. (http://www.aros.org)
Amigable Clark Kant
A: 

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"

MSalters
Of course, this only holds for a specific definition of "possible program", and Turing completeness assumes a pretty abstract model. You really can't write a PC operating system kernel in Perl, simply because it won't run straight on the hardware.
Thomas
On the other hand, there's no fundamental law that perl can not be compiled to native code.
jalf
Technically you can code an OS in any Turing complete language. You could always use Perl to write a compiler which outputs the necessary assembly.
Charles Salvia
A: 

Hi,I think may be you can

Visetor
Hi,I think may be you don't know
ur
A: 

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).

Adam Luchjenbroers
Just because something "compiles to machine code" doesn't mean you can write a full OS in it. How are you going to write a context switch in C++? How are you going to save register values?
tgamblin
C++ provides the same facilities for inline assembly code that you find in C. You could be pedantic and argue that that isn't writing in C++, but the bulk of the code would be C++.
Adam Luchjenbroers
A complete OS does not take a decade to write. Many people, myself included, have written minimal OSes in a day or so. All depends on the complexity of the OS. A machine may not require an OS.
Thomas Matthews
+2  A: 

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.

Pavel Radzivilovsky
You are on to something, but the post reads a little like FUD. Hardware may be different, but a surprising amount can be done in C or C++. Just look at how little of the Linux kernel is written in assembler. And much of what is written in assembler is not because it was impossible to do in C (or C++, the same principle applies) but for performance reasons.
Amigable Clark Kant
I agree. It's important to understand that only a few non-C++ code is required. However, it's also important that a programming language itself is toothless without proper link to the real world.
Pavel Radzivilovsky
Actually, C++ does have an offical Technical Report that DOES specify exactly such an low-level interface to HW.
MSalters
A: 

Thanks a lot for your answers, so if I want to program a computer system(with a good performance and full access to the hardware) I have to learn assembly language too, but I had to be a C++ pro first. Best regards.

x86
You can get by with very little assembly. Of course, it helps remove a lot of the mystery, to know it. Ideally, you would know a fair bit of assembly, but code in C or C++ anyway. Also, far more than assembly, you need to know about the hardware platform. If we are talking PC hardware, you need to know about interrupts, DMA, the keyboard controller, maybe PCI, BIOS calls and so on.
Amigable Clark Kant
A: 

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!

ya23
+1  A: 

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

doron
Depends on the OS. Remember the good old days of MS-DOS, Windows 3.1/95/98, MacOS up to version 9? They were limited, but I got a lot of good work done on some of those.
David Thornley
+1  A: 

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

larsm