234 points · by iamnader · 12y ago · 70 comments
https://github.com/icebreaker/floppybird/blob/master/src/mai...
Nicely structured code! I'm almost tempted to port it to amd64, just for fun. After all we really don't need any more x86/16bit example code, we need more amd64 code! :-)
As mentioned by agumonkey, it feels a little bit like Forth.
In fact, I seem to recall a rather excellent tutorial on 68000 assembly featured very similar structure (even if I never finished it -- at the time I was defeated by my own bugs and the fact that those bugs, without an MMU or memory protection, could easily crash the entire machine. It was rather demotivating...).
For another example, see the original source of Apple II DOS, posted here a while back:
http://www.computerhistory.org/atchm/apple-ii-dos-source-cod...
Fundamentally I don't think the structure of the code is all that different? (Please correct me if I'm oversimplifying, or missing something).
Like a compiler? >:)
So I was playing with it and it has this weird linux on it that lets you set it up to very slowly browse the web and such, but it wasn't very interesting, all told.
When I saw this project come up, I grabbed the .img, wrote it to a spare usb stick and damn if the thing didn't just boot up to floppy bird. Nice work! Now I wonder what other dumb PCs I have sitting around that can boot it...
Same drive still boots to Floppy Bird only, unless you install some other OS or something which overwrites the Floppy Bird bootloader on sector 0, and give up the bird.
Partitioning and formatting alone do not overwrite the MBR, even if it is just a custom floppy boot sector at the time.
In other words it works on RAW METAL and doesn't require an Operating System."
The second statement doesn't quite follow the first. You can certainly write a 16 bit x86 assembly DOS application.
It's still awesome, though, and the code is really cleanly written and easy to follow.
Because the code is so nice, this would be a fun project to try to port to Protected Mode with its own HAL and maybe drivers for one set of hardware (perhaps QEMU's). Could be a fun experiment in why OSes are hard and x86 is painful.
In other words, it's using BIOS for input, but not drawing.
Also looks like boot.asm uses the BIOS to read the rest of the program...
Does it have a switch that lets the computer cheat?
Tiny nitpick: doesn't running asm still require an operating system? I haven't read all of the code but I assume that there are instructions for I/O or other traps that require the OS. Isn't the OS just the layer between the hardware resources and programs that want to access those resources?
The OS functions are provided here. The "game" is just an application in the OS (kinda). The BIOS provides whatever the OS needs to hook into to use the hardware. Here's a high-level overview of how this works http://duartes.org/gustavo/blog/post/how-computers-boot-up/
OS's don't need to be very complicated, just a few bytes in some cases depending on what you want to do. For example, you can just use the standard VGA modes (here, he's using 13h) to get something to display, and just write directly to the video memory. The BIOS helps handle some of the interface work to the various standard components.
This is the real old-school way of doing things and the code is really well done and commented. If you know x86 asm basics, you can read through most of the system-level stuff without too much trouble.
This can be done because ages ago, the PC hardware interface was almost completely standardised, and all PCs to this day implement that standard for backwards compatibility.
If he did this via 32-bit assembly he'd have a fair amount more code to get things done (Though he still uses very little hardware anyway so he could get away with not setting up most stuff).
Come to think of it, "bare metal" would really be modifying your BIOS to contain flappy bird. Then you could draw to the screen directly. Too bad it would only work on one model of motherboard. :)
For the latter, ASM isn't even strictly necessary outside possibly some narrow things that could (/should, if this was a "real" project) be inline assembly in a C program. Obviously, your C couldn't call any system calls and (depending on the runtime) might even need to forego malloc and similar.
The great 8-bit systems of the 80's - Apple II, C64 - booted their apps and games off of floppies all the time.
Although I admit this is more impressive than the usual "Floppy Bird only slightly different."
I really liked how fast my computer "booted" into the game. I would totally start collecting games like these if there were a scene.
edit: for this project specifically there is the small problem that you probably need to enter protected mode for "modern" audio.
This is a bit misleading, accesses to physical memory, disks and to other devices are usually protected by the operating system, not by the language. So you don't have direct hardware access because you program in assembly, but because you run a program without the control of an operating system. This can be done in assembly, but also in C or in any other language that can generate native code.
The only thing that you can do with assembly and that you can't really do with higher level language like C is accessing processor specific registers/instruction sets (e.g. SSE). And this is typically done using inline assembly snippets, as there is no reason to write a whole program in assembly when you only need to optimize a single function.
So I would say that having better hardware access is not a good reason to write an entire program in assembly.
On the other hand, building small, standalone executable file that use very little (stack) memory is still relevant for programs that run on very small (usually embedded) hardware. Especially when they need to run fast or to have low energy consumption. So speed and memory usage are still pretty good reasons to use assembly :)
First, you seem to think I was answering a slightly different question - "In what circumstances should I make the decision to write assembly?" The question I was answering was closer to "When people need to write assembly, why?"
The actual question, of course, was not quite either of those.
Also, addressing a point of ambiguity:
I did not mean "hardware access ([this is something that is] not exposed by other languages)" - that is, in general, wrong.
I meant "hardware access ([when the particular thing is] not exposed by other languages)". This includes SSE (though that is starting to be included as builtins in some compilers). Outside of SSE, I grant that there isn't much on x86 platforms that is relevant to a game, since games on x86 platforms typically run under an OS. It can be relevant if you're writing a driver, though. And this can vary substantially by platform.
And no, it's not a good reason to write an entire program in assembly unless a sufficient percentage of your program is calls to these things that the C inlines are just adding cruft.
"On the other hand, building small, standalone executable file that use very little (stack) memory is still relevant for programs that run on very small (usually embedded) hardware. Especially when they need to run fast or to have low energy consumption. So speed and memory usage are still pretty good reasons to use assembly :)"
No, that is substantially more misleading. Like I said, you cannot quickly do better than a modern compiler, and a compiler does it very, very quick.
The other three most important things you can do in assembly that you can't do in C are:
* Indirect jumps (aka the "labels as values" or "computed goto" extensions admitted available in GNU C)
* Directly accessing status flags like the carry flag (necessary for efficiently implementing bignum arithmetic and CPU emulators)
* and optimizing register allocation across complicated control flow (like language interpreters or emulators)
It can absolutely be worthwhile to write an appreciable portion of a program in assembly if these "features" make the code significantly faster or even easier to express.
http://spectrum.ieee.org/static/interactive-the-top-programm...
"Still used pretty much everywhere" is just not the case anymore, even in embedded. At least, that's what the chart seems to say and it matches my experience working alongside embedded developers, knowing some embedded developers I've never had the pleasure of working alongside, and doing a small amount of embedded work myself.
(Edited to remove: Right in that link, both C and C++ are used more than assembly in the embedded space, and Arduino (more C/C++) follows closely behind assembly. The chart is unclear, but the rankings seem to be based on numbers that don't change when different filters are applied, which would mean that it does not show this. It is still certainly consistent with "assembly no longer dominates embedded", though - some of the C and C++ will be embedded, and all of the Arduino will be embedded.)
I don't see it being called anywhere in the Makefile.
$apt-get install genisoimage $ln -s /usr/bin/genisoimage /usr/bin/mkisofs
builds fine after this.