Hello World on the Apple II – 6502 Assembly Lesson H5

Hello World on the Apple II – 6502 Assembly Lesson H5


Hello, it’s Keith here, And today we’re going to be looking at how to create a hello world example on the Apple 2. Now, unlike some where the other systems the Apple 2 is gonna make things pretty easy for us because it’s got A nice firmware that’s gonna help us out with the basic commands. And in this hello world series, unlike my main series, we try and use the firmware where available to make things as simple as possible Anyway, let’s go over to the code and have a look at today’s example and see it’s in action So here’s today’s code we’re going to be running this. Let’s see it right away and just to check it really works So here it is you can see hello world on the screen on the command prompt here so it does seem to work quite nicely so How does this example code work? Well, first of all We need to think about the starting point of our code. You can see we’re starting at C00 here That’s the base memory address. This is a very low memory address. It’s as low as I think you can get safely So that’s what I’m using to make as much memory available as possible, and you can see we’re defining a symbol here FC62 is the new line command. This is a firmware function It’s helping us out doing the new line. Now when it comes to the print character routine We’re also using a firmware function, but we’re actually making a slight change to the calculations here. This VASM assembler outputs ASCII quite nicely, but unfortunately the Apple 2 doesn’t quite take that ASCII very well if I just Close this running version here. And I now REM out these two calculations here. I’m adding 128 and then I’ll run again You can see now we’ve got some quite confusing gibberish on the screen here so that’s not going to be what we’re gonna want So you have to add 128 in this case To get that working properly So, there we go We’re extending this print character into a print string routine here You can see we’re using Y as a counter and zero page entries 40 and 41 for a 16-bit address And of course, we’re passing it the hello world string here. Just 255 terminated We read in each character until we get to the 255 Then we pass them all to the print character routine and that will print a string to the screen Which is what you’ve seen. You can see it on the screen just up here now Okay. Now in my tutorials I always extend the basic print character routine into a more advanced routine which shows some monitoring functions To the screen. Essentially when I’m taking on a new system my first stage is get hello world onto the screen and then my next stage is get some simple debugging tool so I can make Some progress. And so I’ve decided to provide those debugging tools to you today because maybe that’s gonna help you out So the tools are very simple A monitor tool, which will show the registers to the screen and a memdump tool which will dump an area of the memory To the screen as well Now I’m not gonna go into how these actually work. A little bit complicated for today, but we are going to see them work So we can just call this monitor to see All of the registers or the mem dump to see a memory address and a number of lines on the screen You can see that’s exactly what it’s done We’ve got hello world as before. We’ve got the A, X and Y registers, the stack pointer The flags and the program counter and then we’ve dumped and memory address just here to the screen So that’s exactly what we might want if we’re just starting out and we’re needing to do a little bit of debugging Okay, then So we’ve seen now how we can just do some simple Showing of characters to the screen and how we can do a little bit of debugging but of course This is just the source code. To get our source code running on a real machine, or an emulator rather We’re going to need to convert that to some kind of binary and then get it onto a disk so we can run it on the machine and Here’s the source code for the batch file that I used to run today’s example So let’s actually see how this works. Now, the first thing you’ll see is we using VASM here in old style mode And here’s the command line that I use to compile today’s example Now because it’s a batch file, this percent one is actually the source file So in this case, it would be the hello world.ASM file You can see we’re defining a switch CO2. The Apple 2 actually uses a 65C02 processor and so we might want to use some of those extra commands and so to Enable those extra commands, we have to use the CO2 switch just here, that will turn them on. We’re specifying to check labels This is a handy little tool because it will check for common mistakes. Now in our code We might occasionally forget our tab, and the system will mistake that for a label. That’s going to cause a lot of mistakes So by putting that check labels command in there It will check if a label has Suspiciously the same name as a command and it will warn us and that will save some time for us I’m also disabling case sensitivity because I’m not used to case sensitivity. So it tends to catch me out. So for convenience there I’m also defining a few symbols the VASM symbol here isn’t really used I tend to define a symbol with the name of the current assembler just in case I’m supporting multiple assemblers at some point And also I’m defining one for the Apple 2, buildAP2 here. That one may be used by the debugger So I’d suggest you do have that one. If you’re using the debugger, you won’t need it for the basic one there I’m outputting a listing file This is quite handy. A listing file will show the assembly source and the byte code destination all in one text file So if you’re having weird trouble and you really can’t figure out what’s going on. It might be worth looking at that sometimes you’ll find your calculations with your labels might be getting confused, because the assembly uses multiple passes and sometimes you might have caught it out somehow or Sometimes you may not realize how the commands are compiling to byte code and they may not be doing the same thing as you Expecting, so it can help you if you’re really struggling Now we’re specifying to output a binary file that’s been here. And then we’re specifying the file name as Prog. here And that’s the file we’re going to need to put onto a disk in just a moment Now when it comes to creating the disk I’ve got a template blank disk just here so I copy that ready to attach the file and Then I’m using a2in here, which is part of the a2tools toolkit And we are specifying that we want to add that. We’re specifying a binary file we’re specifying the memory address to start it, which is the same as at the top of my code if you remember just here you can see that and We are attaching it to the disk file that we just copied up here, and we specifying the name of the program here So now we’ve got a program disk We can actually use, and then making a copy of a predefined safe state and then I’m just following it up with AppleWin Specifying this safe state just here, and specifying the disk image that we’ve created just here to drive one here The reason I’m using the safe state is just a saving of time really If you just see my example run here You can see we’ve already got BRUN PROG typed in just here and I mean it might seem silly to say You know save a state with that already typed in but if you’re having problems with your code, and it’s crashing You don’t know why you may have to compile and run 20, 30 times and so typing a few keys and especially if you miss type and Forget the command and type run prog and things it’s going to start eating up time So anything you can do to get the program assembled and run as fast as possible, I’d suggest you do. So with this assembler That’s just something we can do to save a little bit of time So, there we go We’ve learned how to do a very simple hello world example on the Apple 2. If you want to see graphical functions or wide variety Of other functions on the Apple 2. Please see my other tutorials. There’s a playlist on my youtube channel and my website It’s got a lot of Apple 2 content. So please take a look at that if that’s something that interests you I hope you’ll find this interesting. If you want to try these out please go to my website the source code for all of my examples are all available for download and The scripts and tools that I use are also on the website Anyway, I hope you found this interesting. I hope you’ll give it a go, and I hope you’ll enjoy programming the Apple 2 Please like and subscribe if you’ve enjoyed this cause I’ll be doing a lot more content coming soon. Thanks for watching today anyway, and goodbye

Posts created 29550

3 thoughts on “Hello World on the Apple II – 6502 Assembly Lesson H5

  1. 1. You can replace CLC/ADC#128 in line 40 with EOR#128 to save one byte and two cycles if you don't care about flags.
    2. Not all Apple II use 65c02, only later models did.

Leave a Reply

Your email address will not be published. Required fields are marked *

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top