 What does a 10-year-old child see when they turn on a computer today? What are they? Oops, still working? OK, cool. What do they see? A bunch of folders, a desktop. Does the computer invite them to write code, to inspect memory locations, to run programs in machine language? The device I have here was purchased in 1984. I'm not going to lift it up because of the prodigious amount of cables and adapters attached to it, so that it can display its output in 2017. But ask me later, and I'll show it to you. It's an Apple 2C, the fancy portable model in the Apple 2 line. It has no hard drive, but it comes with a number of odds and ends in read-only memory, including an interpreter for Applesoft Basic, an early collaboration between Apple and Microsoft. I count myself extremely privileged to have had access to this computer when I was a kid, by virtue of my dad being an early telecomuter. And when I turned it on, this is what I saw. Control reset. This is a basic prompt. Oops, caps lock. Print. Hello. I think you can see where this is going. Print's hello world. To engage with the machine, I had to learn to program in basic. And before preparing for this talk, I'd forgotten that I'd had to learn a lot more than just basic because it turns out that basic is an extremely thin abstraction around low-level manipulation of the machine. The Motorola 6502 processor that Christian explained to us yesterday, the RAM, the IOPorts, and the 16K of software in the ROM, most of it machine language subroutines. To demonstrate all this, we'll write a drawing program in basic for this joystick here, where we clear the screen by pressing this button. And then we'll show how we can clear the screen a lot faster if we do that part in machine language. But first, I'd like to thank physicist Sherma Strafsky who wrote an article in the March 1981 issue of Compute Magazine, upon which a portion of the code for this talk is based. I found the article on the internet where you can find an unbelievable amount of stuff for the Apple II, including some great emulators. So you can try this at home. Now, let's get to it. So we're going to go home. So programs are stored in memory by line number. Right now, there's nothing there. We list it. There's nothing there. Start out with a simple for loop, 10, for i equals, oops. I haven't typed in Quarty in about three years. So this is going to be interesting. If I had chosen Dvorak instead of Colmack, there's a button here that switches to Dvorak, interesting one. But I did not. So 20 print i monkeys next i. Oh, you've got to often forget to type the actual line number. It's written as an immediate command. So now we can list it and then run it. Boom, 10 monkeys. What do you want? Thank you. So now we'll do some graphics. Going to lower as graphics mode with gr. Set a nice white as the color. Plot something in the middle of the screen. And plot something in the lower right. 39, 39 is the lower right. Then we'll go back to text mode. Maybe wondering what this is. The lower as graphics mode and text mode shares the same page in memory. So you can see the lower right-hand corner. There's that point that I plotted. All right, now for the serious stuff. I'm going to write a fairly long program here. It's going to be in three parts. First part is going to be one line, which is just to go into graphics mode. The next part is going to be the part that clears the screen. So we're going to start that line 100. Color equals 2. It's kind of a light gray. 110 for x equals 39 to 0, step minus 1. It's going to sweep from right to left. 120, draw a vertical line at every one of those x-coordinates all the way down the screen at the x-coordinate. 130, next x. And home list looks pretty good. Run, next without 4 error in 30. All right, a little debugging here on the fly. Next, oh yeah. Oh, I forgot to erase the lines from the earlier program by typing new. So how am I here? I forgot to do it. You can actually just get rid of individual lines by typing their numbers, though. 20, and 30. So now if I list, OK, now we're good. All right, boom, got it. All right, home list. Now we're going to write the part that does the main event loop for moving the thing and drawing the stuff and pressing the button. All right, so 200. Color equals that nice white we had earlier. 210, we're going to make a variable name. Joystick x equals PDL 0. Now, you're wondering why PDL? That's short for paddle. Why paddle? Because the joystick is a recent invention as of 1980. Before that, there were two paddles. And they were combined into the x and y component of the joystick. But they're still called paddles. Why are they called paddles? Because they were originally invented in 1972 by the Atari Corporation to play pong. So they're actually dials, but they're called paddles because of table tennis. We'll hit a three lesson. That should be good. Now we're going to read the button also. We're going to make a variable name button. We can't actually call it button, B-U-T-T-O-N, because the word two is in the middle of that, and it parses in the middle of words. Yeah, I discovered that yesterday. So and also in this one, button equals, and you'd think it would be like, you know, read the button, B-T-N, some function name, but no peak 49249, because they bothered to write a function name for paddle, but not for button. So and this is true of a lot of this stuff, the massive amount of this stuff, you just have to read directly from memory locations. This is kind of like reading directly from the machine, this IO port at location 49249. That gets us the button 240. This goes from 0 to 255, et cetera, so we've got to shrink it down to 39. So we're going to plot int Jx over 6.5, int Jy over 6.5. That should be pretty good. And then 250, we'll check the button, see if anyone's pressed it down, see if it's currently being pressed. If button is greater than 127, because we only care about the high bit, very machine-y once again, then go to 100, the screen clearing routine, otherwise just go back to the main event loop for the drawing, go to 200. Don't forget the go to, go to 200. So list, not that I can really eyeball that and see if it's going to work, but let's try it. Run. Yay. OK. I pressed the button. That also works, which is nice. But it's a little slow. We can make it faster. We can make it three times faster. Did a little benchmark yesterday with a simple machine language subroutine. So I don't have, I could have made this program to terminate when I pressed the other button, but that would have taken another 30 seconds of typing, wouldn't do that. So home, oops, you can do colons to do the next command, home colon list. So what we're going to do is we're going to erase everything in that clearing subroutine except the color equals 2. So we're going to just do 110, 120, 130, that erases those lines. And then we're going to replace it with 110, call 768. And 768 is a location memory in decimal notation here where we're going to write a machine language subroutine. We'll do that by going directly into what's called the monitor, running another machine language subroutine, call at location minus 151, which has a positive equivalent that's harder to type because it's longer. And now we're in the monitor. And we're going to at location 0300, that's hexadecimal for 768, we're going to enter some assembly language program. In earlier versions of this computer and later versions, you could actually type in assembly language. But for some reason, they got rid of it for a certain period of time. And you can only type in machine language. So this is the same machine language that Christian showed us yesterday. What? I think it's, oh, weird. Well, you can still see it on the side, yeah. Cool. All right, so where was I? We're at A900, OK. 2028F8, that's another command to jump to a different subroutine which draws, well, what this does is it's the same thing the other one does. Draws vertical lines from right to left using the color that it shares state with the basic program in. So you can set the same color and it should work. 888, 10F8 is the go back down to zero command. I mean, the branching command, the zero case. And 60 is RTS, return from subroutine. Now, we can look at these. 030E was the end of the range. Those are the numbers we just typed in. We can, interestingly, we can't type in assembly language, but we can disassemble. So we can see it. It shows you just the random next set of things on one page. Down below the RTS, on the right, it's all garbage. The other stuff is what we typed in, as Christian was explaining yesterday. So the column on the left is the memory locations. The sort of middle columns there are the numbers we just typed in. Some of them are commands and some of them are arguments. And then on the right is sort of the mnemonic version of that, so the commands and the arguments. So we're just going to pray now. And home, list, once again, and run. OK. And faster. And we could have made it even faster, nearly instantaneous, but that would have involved some serious bit twiddling. Direct, this program makes use of some subroutines to do the vertical lines. We could have made it really direct, but that would have been way too long to type. So thank you very much. Check out the emulators, but there's one thing that I have discovered during the course of doing research for this that I don't think any of the emulators can do. And that's closed apple, open apple, control, reset. Thank you very much.