Assembly For My Mac

Member
Posts: 72
Joined: 2004.06
Post: #31
Jones Wrote:I asked, because it's always been a dream of mine to make a simple OS. Just something that would boot and print out "Hello, World!".

Any tutorials you know of?

There aren't any I don't think. However, I've done what you say :-). After countless hours I have to say that you won't get anything more than the most basic input and and output in your OS so it's really not worth the trouble.

If you want to do it, look at BootX in the Darwin source. It's all written in C. It does stuff like putting characters on the screen by using OpenFirmware (The bios, as it were). Once you have stripped down the BootX code to a basic set of stuff you'll need to compile it like this:

gcc -arch ppc -O0 -Wmost -Wno-format -force_cpusubtype_ALL -fasm-blocks -fno-common -static -c -o all.o *.c

gcc -arch ppc -nostdlib -std=c99 -e _StartTVector -seg1addr 05600000 -o bootfile.macho all.o -lcc_kext

(Those two calls can be combined i think but they aren't combined in my build script for some reason and I can't be bothered to do it now)
That makes sure it doesn't use relative addressing amongst a lot of other stuff.

Once you've done that you need to do:
macho-to-xcoff bootfile.macho bootfile.xcoff
You'll find the macho-to-xcoff tool in the BootX source.

Once you've done that, write down the path of the .xcoff file (or move it to a place you can remember easily) and restart your computer while holding Apple-Alt-O-F (So that's hold Apple, Alt, O and F :-)) until a prompt looking thing appears. This is OpenFirmware and is what runs before the OS does. Then type:

boot hd:\path-to-xcoff-file

And it should boot your OS! (If you can call it an OS :-))

It will do what your OS does and then either give you an error or just stop (Depending on whether you put an infinite loop at the end or not) and you'll need to restart.

After I got a hello worldy sort of thing working I looked in to:
- Multitasking (At which point I realised I needed Memory Management First)
- Memory Management (Which I decided was so stupidly difficult it's untrue :-))
- Doing stuff without OpenFirmware (like graphics) which is again, so stupidely difficult...

When I was doing multitasking I needed to use Assembly so I could use the Decrementer. Assembly is by no means easy on the PPC, but it's not hard either. Just think what you want to do and look for the best instruction that does it.

Oh, an instruction list along with descriptions of the instructions and what parameters they have and stuff is in Shark.app which is somewhere in the Developer Applications folder. Look in the help(iirc) menu and there is an item called "PPC ISA Reference". And if you want to do something really complicated then write what you want to do in a C function and look at the assembly for it, it's much easier that way :-).

"So long and thanks for all the fish" - In memory of Douglas Adams.
My Site
Quote this message in a reply
Member
Posts: 198
Joined: 2005.01
Post: #32
Jones Wrote:If you wrote an os in c, how the heck would the OS run? An empty machine has no c frameworks installed... How would the computer understand "itself"?

On a standard machine, a C app links to dynamic libraries that have the basic "frameworks" (e.g. strcmp, printf, etc). When you're building an OS, you have to link in an embedded libc statically, like newlib. Check out the Linux kernel source sometime, it's kinda cool. There's a mini libc built into it. It doesn't have to link to any system libraries; the final step of the process is to take the standard OS executable file and strip out its data/code sections into a standalone binary with a fixed runtime address (which is put where it goes by the boot loader).

Something that was a real brain-f*** for me the first time I realized it is that OSes are just another app that runs on the raw hardware instead of under the main OS. It dynamically loads and unloads modules at runtime that have more functionality (user apps). Even multitask isn't that interesting, it's just swapping out the call stack when some piece of code calls into the OS.

ggadwa Wrote:C is not turned into assembly, it's turned into machine code. Assembly is machine code in human readable form, but is equivalent. When you ask XCode to show the assembly, it's just showing you the mnemonics (plus some convient labels which represent addresses.) There are not two steps.

GCC is very modular. A lot of compilers (like MSVC++) will go straight from multiple CPP files to multiple OBJ files, or at least that seems to be what it's doing. In GCC if you sit there with a very fast updating top or whatever, you can see it separately invoke cpp (pre-process the C), cc1 (compile from C to assembly), as (compile from assembly to .o) and ld (compile from .o to executable). C++ involves another step or two. So yeah, those flags on gcc like -S or -E are actually giving you something it really generates while compiling in Xcode, but you usually don't see them due to pipes between the processes or just really quick compilation.

Unless they changed something in GCC 4.x anyway.. that's how it's always been before that.

Cryptic Allusion Games / Cryptic Allusion, LLC
http://www.cagames.com/
Quote this message in a reply
Member
Posts: 21
Joined: 2003.01
Post: #33
While your mac has the hardwired firmware ( unless you find a way to overwrite it Rasp ), you can use an emulator like qemu if you want to write your own BIOS/Firmware/ROM/etc. You will have to recompile it though because it has its own which you can see at http://perso.magic.fr/l_indien/OpenHackWare/index.htm for reference.
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #34
truth is that programming itself lies on an incredibly complex structure you will never really know: in the end you are controlling electrons, you will never be able to do that yourself: we use instruments made by other instruments made by other instruments in a process 40 years long, with tons of geniuses working on the soulders of their predecessors.

Programmers are almost at the end of the chain as common users

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #35
Najdorf Wrote:truth is that programming itself lies on an incredibly complex structure you will never really know: in the end you are controlling electrons, you will never be able to do that yourself: we use instruments made by other instruments made by other instruments in a process 40 years long, with tons of geniuses working on the soulders of their predecessors.

Programmers are almost at the end of the chain as common users

[insert an Oasis' CD title/Newton's quote here] Grin
Quote this message in a reply
Apprentice
Posts: 8
Joined: 2007.06
Post: #36
Ingemar Wrote:...68000 quite OK. ...Go for 68000, pick up an old 68k Mac, find some assembler, or C compiler that supports inline assebly, and have fun! (Or if you like, run the 68k programs on a PPC Mac, even under Classic under OSX. After all, 68k emulation is built-in.) Brian Barnes wrote his first games in 68000 assembler, so he might have a comment on this.

I stumbled across this thread in my occasional search for resources to learn assembly. I picked up a Mac SE a few years ago, with the intent to learn how to write assembly code with it, and made some little progress, only of the altering example code sucessfully, and of reading books that I'd found, giving me some understanding of the concepts. I'm fairly stuck when it comes to that project, though, as I can't seem to find any documentation or forum/s that explain how to use the Mac OS resources. I think that two books out of the half dozen or so I found have any code that actually runs on my machine, and none of it is code that contains even one resource created with Rez, or ResEdit. I eventually picked up an Apple I replica, that I was more able to bend to my will and understanding (though I can't think of much that I want to do with it yet (I'd like to add some sensors, but that'll take more hardware understanding)).

So! Basically I'd just like to know if anyone knows of any resources that would get me started on using resources on old school Mac OS (6.0.8) on a Mac SE 68000 machine. Thanks for all/any help.
Quote this message in a reply
Post Reply