Apple2 Assembly Hello, World
Jan 17, 2020Every development example starts with the classic “Hello, world!” and this one is no different.
Here’s the code in all it’s glory…
WRVEC = $03D0 ; Warm Re-entry vector
;;;
.include "inc/asc.inc"
START: JMP MAIN
.include "subs/hyde.ca65"
MAIN: JSR PRINT
ASC "HELLO, WORLD!"
.BYT $8D,$00
JMP WRVEC
.END
Now, I’m cheating a little to make it short because the PRINT
subroutine
and the ASC
macro are pulled in from other files.
Note that START
and MAIN
have no special meaning here, they’re just labels.
Assemble and link in one step using CC65’s
cl65
frontend:
$ cl65 -v -t apple2enh -C apple2enh-asm.cfg \
-u __EXEHDR__ --start-addr 8192 hello.a65
or doing explicit assemble and link steps:
$ ca65 -v -t apple2enh --listing hello.listing hello.a65
$ ld65 -v -C apple2enh-asm.cfg --start-addr 8192 \
apple2enh.lib -u __EXEHDR__ hello.o -o hello
A few notes about this. I’m specifying a start address of 8192 ($2000)
since that’s a good location for a program under Prodos
(refer to Beneath Apple Prodos for details).
I’m also including the __EXEHDR__
which defines an
“AppleSingle File”
header. In the first cl65
includes it for me and in the second I
must be explict and include the CC65-supplied library. The header
carries, among other things, the start address and the program type
(BIN
).
Now that we have a binary, let’s get it onto a disk image using the CLI version of AppleCommander. The first step is to create an empty disk image.
$ java -jar ac.jar -pro140 disk.po hw
Now we can leverage the header from above and the built in support for it in AppleCommander.
$ java -jar ac.jar -as disk.po hello < ./hello
Had I decided not to use the CC65 library and corresponding AppleCommander support, I would place the file in the image like this:
$ java -jar ac.jar -p disk.po hello bin $2000 < ./hello
Firing up our emulator, we boot Prodos and run our binary. Behold!