开发者

Programming from scratch [closed]

开发者 https://www.devze.com 2023-01-28 15:28 出处:网络
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 8 years ago.

开发者_Go百科 Improve this question

What I would like to know is to start programming from scratch without any Operating System and anything like it.

As I know Windows and Mac and almost anything even the DOS is written in C, C++ Pascal etc., so I think I should know one of these languages, but for this I would need a program where I can write the code, and also to compile it but without an Operating System and programs how can be this done? How could they do it?

But this is not enough far, how was C written? in what? So when I mean scratch I mean, building everything from the basics. Maybe from 0,1,0,1 right now I think this is the exact start point. But how can I do it, what should I have and where should I start?

Thanks for every answer!


There's a great textbook called The Elements of Computing Systems: Building a Modern Computer from First Principles by Noam Nisan and Shimon Schocken, which serves as the basis for a university course taught by the authors called Workshop In Computer Construction - From NAND to Tetris in 12 Steps. This course is also taught at other universities under different names.

There's a 10 minute introduction on YouTube and a 1 hour Google TechTalk on Google Video, both by the author himself.

The official companion web site is http://nand2tetris.org/

Don't let the title "Computer Construction" fool you. By "computer", the author does not just mean the rectangular plastic box on your desk, he means the entire computing system, from the individual logic gates up to highest-level application programming.

The book/course teaches you

  • how to create your own individual logic gates
  • how to build your own logic circuits
  • how to build your own CPU
  • how to program in machine code directly
  • how to program in your own assembler (which you wrote in machine code)
  • how to write your own virtual machine
  • how to design, implement and program in your own high-level language (which you wrote in assembler)
  • how to write a bootloader
  • how to write an operating system
  • and ultimately how to write a simple game


You can acquire a microcontroller prototyping board such as Arduino, Beagleboard or MPLAB PIC board, and then create a machine monitor program that can load and execute your other programs in user space.

You can program those microcontrollers in C or their respective instruction sets.

Hook up an LCD display to that MCU board and you have a monitor. Hook up a keyboard and mouse to it and you have controls. (You can even write your own trap handlers for writing to LCD and reading inputs from keyboard and mouse to understand how interrupts and exceptions work at low level. If you want to go lower, you will have to look at MCU pin-outs for interrupt control lines and timing diagrams).

Voilà! You've designed your own computer from scratch!

To answer your question about C, compilers for the first programming languages were written in assembly, and compilers for assembly were written in machine code.


what you're talking about used to be called a 'monitor' program. you might get some interesting info at osdev.org and taking a look at colorforth.

[afterthought] google Rick Hohensee's ramblings on comp.lang.forth also.

[much later afterthought] to really get to basics, get a plug-in circuit breadboard, some old EPROMs, a 5VDC power supply and a 25VDC power supply (24 ought to work, it's more common). Use pullup or pulldown resistors on the address and data lines, and switches to supply ground or power to the address and data lines. Step through the addresses using the switches, and enter the data as ones and zeros on the data bus, programming each with a pulse from the 25V. I've never actually done this manually, but I once wrote a program (http://jc.unternet.net/hacks/?1994-01-01-01) to do this from a Centronics port, and burned several programs that way. This was back in the day when Intel would send you free books for all their processors and auxiliary chips, complete with detailed instruction sets and timing.

When done, connect your processor to the EPROM, give it some juice, and watch what happens. Maybe make it strobe some LEDs or something before you start trying to integrate keyboards and monitors.


The problem you're describing is most commonly referred to as bootstrapping (and its' relationship to the chicken and egg problem). That specific link is for compilers, but you have the same problem in many areas - hardware, operating systems, etc., with similar solutions.


Beginning at the basics is 1 and 0. You need to know what instructions your processor supports and what registers and cache, etc. exist. Then it would be handy to write a little assembler compiler in machine code (for readable 1 and 0). If you have that, you can write the first real compiler for a simple higher language, and so on (each built using a lower-level "language"). You should know, though, that creating a simple operating system can take years of planning and even more time to develop, debug, etc. (and all this before you can even think of seeing something useful on the screen). And that operating system won't be able to run on any other processor than yours. Then you need drivers, etc for every single bit of hardware. And there are many steps to go from here. Beginning at the level of 1 and 0 is not very rewarding, since you won't see anything of it for a very long time. Simply don't.


Most C compilers can emit code that's not designed to run in the context of an OS (see the memtest86+ codebase for example), but that's going to severely limit the feature set provided by any of those compilers. (In particular, you're not going to get anything like malloc).


You can't do what you seek terribly conveniently on most modern PC-style computers, but there are many inexpensive (sub-$100, or even sub-$50) kits available for programming smaller single-chip micros. A typical micro will have a few dozen pins which can be driven high and low under program control, along with a processor, some RAM (typically 256 bytes-8K or so), and some flash memory for code (typically 8K-128K or so). The development kits will typically have a processor with some pins attached to LEDs or an LCD display, and with other wires attached to a few buttons. The boards will also include circuitry so you can develop your software on a PC, connect a USB cable, and transfer your code into the chip.

Embedded programming is fun. Gives some of the feel of 'old-school' programming, with some newer modern conveniences.


I suggest you get yourself a small microcontroller system to play around with. TI has a small system-on-a-usbstick based on the MSP430 for $20. You do not need a compiler or an OS to run code on this system. In fact, with only 128 bytes of ram it would be a challenge to get an OS onto it.

If you really want to start from scratch, you the write down your program in assembly code on a piece of paper and translate it by hand to machine code. Then you type in the machine code in a hex editor to get your ones and zeroes. This is really taking it to the extreme and you will not learn anything from doing the machine code by hand. Writing a program in assembly code will teach you a lot, though.

You do need a tool to get your program into flash memory for the cpu to be able to execute it, but once the flash has been programmed, there are no tools, os or anything like that involved.


I can't tell you about hardware but if you want to build your own operating system, you can do that. It's a lot of work, but very rewarding. jcomeau_itcx already mentioned osdev.org. There's also the alt.os.development newsgroup, although it's been extremely quiet over the past couple of years - but check out the archive if you can find it.

Sounds like you're not put off by all the "Don't do it!" warnings... that's good :-) You can learn about boot sectors and write your own in a few days, and then you'll see something on the screen and know that you're on your way.

Many people recommend that you do not write your own boot sector, because it's not strictly part of the operating system. They recommend that you boot using GRUB or similar. I disagree, partly because I wanted to learn how the computer booted, but also partly because I found it easier to write my own boot sector than to figure out how to use GRUB!

C is the best language for writing an OS, because it's high-level enough for you to be productive but low-level enough for you to work directly on the processor. Many people use gcc, but you can also use Visual C++ or whatever (but compile in C, not C++, and you do need a compiler that supports inline assembler and can generate functions with no prolog/epilog.) You can write your code on your regular PC and then run it in a virtual machine - I prefer bochs because it's got a built-in debugger, but there are others.

Some tips for getting started on your OS (after you've sorted out your boot sector)... Intel IA32 is the most readily available processor (because it's in almost every PC) and most OS devers prefer it, so you can get more advice about it than about other platforms. I don't know whether they do this any more, but Intel sent me hard copies of their IA32 Software Developer's Manual - free of cost, but invaluable. You could work in real mode (16-bit, segments, no protection between apps... think Windows 3.1) but I reckon it's worth the effort to get into 32-bit protected mode (rather tricky) and be a bit more modern. Virtual memory (paging) is optional, but I did it because my original motivation for writing an OS was to learn just how 32-bit protected mode provided protection between apps.

It will be a long journey. I started my OS in 2004, working in the evenings, but then I started my business in 2006 and haven't had any time for it since. So it's a long way from being finished!

If you're curious, do it. You'll really enjoy it. And if you don't, you can stop.


AFAIK, nothing you are describing exists in modern computers. Nowadays, you will always encounter layers of operating systems, bootstrapping, and possibly even virtualization between you and the hardware.

If you really want to understand the deep secrets of programming and how applications run on the hardware, I suggest you take an operating systems course or read up on how they work. For instance, the Andy Tanenbaum book Operating Systems Design and Implementation may be the ticket.


If you want to get down to the hardware, try checking out microcontrollers like PIC and AVR. I recently had the chance to work with some of those platforms, where the code you write (in C or assembler) actually runs on bare metal.

Of course, that only means that you have to implement some core OS services yourself, like interrupt routines to handle input. Much of it is available as example code, but still small enough to read and understand the whole system.

As far as computer archaeology goes, try taking the basic courses in digital circuits, computer architecture and compiler technology at you nearest university. That satisfied my curiosity, as well as my need to write programs with raw binary opcodes :-)


You can program without a Operating System (OS), but the OS does the heavy lifting for you. For example with C (which you want to know) the magic it's not the language itself, but the compiler. Which basically what it does is something like this:

C source code -> Non-optimized intermediate code -> Optimize intermediate code -> Machine code

Now, the Machine code goes to the OS, which itself uses it's own Kernel for translating that Machine code, into binary form so the Hardware itself can understand what you want to do.

So basically before you write a program that runs without OS, you need to create your own compiler and a kernel for your machine to run it.


I made the beginnings of an operating system with a computer I designed and cobbled together years ago (I think that would be about 1985 or so). It is decent fun and you learn a lot doing it. Good success on your project!

The answers people have been giving you about a platform you could use to develop your operating system have been of the hardware variety. You would have to spend a bit of $$. Assuming you have access to a computer you might consider using a virtual computer like QEMU, or Oracle's Vbox and develop an os for that. If you have a mac, I really like Q as the interface makes the virtual computer even look like one. I think other posts here have probably mentioned osdev.org. That is a great place to bounce ideas off other DIYers, and get some help getting started. Good success to you.

CHEERS

0

精彩评论

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