 Hello everyone, this is a 6502 assembly language introduction by Tokyo EdTech. That is me and You see here it says I am live coding because I haven't really planned this out too well I got some things prepared, but I don't quite know what I'm doing And I'm just doing a lot of it from memory and some of the review I've done recently So yeah, it might be a little messy, but hopefully it'll be fun and informative. So real quick Just shout out to my oops wrong one shout out to my members here My snake members and my invader members who are paid supporters of the channel They make it possible for me to spend more time on the channel. So if you can join Click the join button down below much would much appreciate that. So 6502 assembly First what is assembly language assembly language is a very low-level? Representation of machine code machine code is the actual like binary numerical code that runs a computer's CPU That's a good definition, but it's hopefully understandable and There are of course many varieties of CPUs and many varieties of computer Hearts of the computers. So 6502 was an early microprocessor. It was launched here. You see here in 1975 and it basically launched the Like cheap computer ever revolution will say so you see here a list of computers like the Atari 2600 Apple II the NES and the Commodore 64 were probably the most popular machines that used this processor and I'm gonna talk to you a little bit about how it works and then try to give you an idea of what Yeah, basically how your computer works. So the way I'm gonna be doing this is By using something called easy 6502 and it was made by this person Skilled Rick skilled Rick on GitHub and I guess his name is Nick Morgan and this is a page And this has a lot more information About what I'm gonna be doing. It's it's more advanced. It knows more than I do. So I'm gonna keep it a little bit simpler I hope and What you need to do is to go to this person's github page and you'll see this page here It's 6502 JS and what? Nick has done is created a JavaScript version of a 6502 processor and given it a little screen and a way to do input So it's pretty cool. So I gotta do is go to code and click download zip and then you'll download the zip file I've already done this and you just open it and then See I'm sure what that means, you know, however your computer works open it Unpack it and on my computer. I have it here. I've already opened it up onto my desktop I'll show what that looks like real quick So in desktop, I've got that folder 6502 JS master and you'll see a file in there It says index dot HTML double-click that and it will open in your browser now. I'm using chromium I had I think it works better with chromium, but it does work on Firefox as well And what it does is it gives you this page and so we're gonna be typing our code here We can run it reset it do some different things There's a little bit of a display here and there's some very cryptic numbers and things over here I'll try to explain some of that. There's a little bit of output down here as well So, yeah, so this is what we're gonna be using to actually do the coding. Now just a real quick 6502 assembly language Uses I think it said over here. There are 56 instructions And I think there's some variants that have one or you know a few extra instructions and things like that But for now, we're just gonna stick to some of these now, of course, we're not gonna go over all of these I don't know what they all do to be honest I know what some of them do but not all of them and I'll just show you a little bit about how like said how this stuff works and Yeah, it's pretty fascinating stuff. Now one thing you may or may not know a lot of the examples that you'll see down here You can see how they have a dollar sign before the number That is hexadecimal that is base 16 So you see here, you know hex The dollar sign tells you this is a hexadecimal number So it's probably a little bit confusing to most people. So what I'm gonna do is I'm going to convert the hex over to decimal For people that aren't familiar with hex and it makes things a little bit easier. I think as well So I'll go ahead and get started So real quick, you'll see over here. You've got a we've got x and we've got why I'm gonna restart reload that Because I played around with it earlier. So you see a x y you see sp PC and then you see these, you know kind of cryptic letters here and I'm gonna talk about all of it, but the parts I'm gonna talk about are really important. So you got a which is called the accumulator x and Y so these are what's called registers and the accumulator is very very important that's where you do a lot of your mathematical operations and X is like kind of an auxiliary helper register and Y is another register now modern CPUs have way more registers and These are very fast memory locations that exist essentially inside the CPU and What assembly language lets us do and this is again. This is how your computer actually operates is that we can basically Get values out of memory put values in the memory we can add them we can I think we need to subtract them and That's about it. That's really all we do we can compare numbers and we can jump two different areas of code That's really all your computer does it just does it really really fast so Let me go ahead and show you basically what I'm talking about. So as I said earlier, there's three registers a X and Y so a is our main register. It's called the accumulator So if I want to load if I want to put a number into a so if you're thinking like I think like, you know, like for example like a equals one okay, so I Like to think of it that way at least for beginners X equals one Y equals one for example How we do that is we do a command called LDA and I want the number number signs really important and I'm just gonna put one there And I could say LDX Number two LDY number three So then to run this program, I'm gonna hit assemble You can see here indexing labels found zero labels Assembling code code assembled successfully six bytes. That's really important. The 6502 is an 8-bit Processor so see here the data width is 8 bits Which means that it can store values in one memory location from 0 to 255 or can process at least and it can address 16 bits which gives us 65,536 memory locations trust me on that one So what I've done over here Is I've basically loaded Well, actually, I haven't run it yet. I've assembled it and if I go to hex dump You can see here again. Everything's in hexadecimal. So a nine is LDA. Oops, I want that back Zero one of course is one a two is LDX Zero two is the number two a zero is LDY and the value Zero three so that's the actual numbers that is stored in memory. So if I run this You'll see over here now the the accumulator is zero one X is two and Y is three So we can load so it's load values into any one of those registers using these three commands Now I As I mentioned earlier, it's an 8-bit computer. So we can only store values up to 255 Oops 255. So let's go ahead and assemble that. I'm gonna run it and you can see over here F F F F F F. So if I go to hexadecimal to Decimal so F F is 255. So that is why you'll see these numbers over here Now, let's see what happens if I do 256. I'm kind of curious Okay, so we got a syntax error 256 is too big. Okay, so I cannot I can't use 256 because I Can't store that much information. Oops. That is not what would happen Back to where it was. Okay, so that is how we put values into one of the registers now a couple other things we can do is We can take those values. I'm gonna put I'm gonna go back to one two three And again, I don't have quite all this memorized, but I'm gonna do my best So if you click here where it says notes This is a little bit of information about this particular system Now if this was a Commodore 64 or Vic-20 or Atari something memory different memory locations control different things so in this particular program the creator Nick has created a little display for us which is which is very Nice and by changing a certain memory location value we can Put colors on the screen. This is again. This is how your computer works. It's far more complex now But this is the gist of it. So you see here where it says memory locations Dollars 200 to dollars 5 f f f map to screen pixels Again here the color code zero is black one is white all the way down to f which is light gray Now nine is nine a is 10 B is 11 C is 12 13 14 and 15 So we've got black White and red and now we won't be able to see black because the background is black. I'm so I'm gonna go ahead and change this to Zero is black. Okay, so one's white two is red and one is cyan. We'll go with that now as it says down here 200 to 5 f f map to the screen So this is 200 and this is gonna be 5 f f So what I'm gonna do is I'm gonna store some of these values Into these pixels So I'm gonna try I think it's stored accumulator now watch what I do here I want you to convert that so it was 200 in hex Convert that's 512. Okay, so Oops So I'm gonna store a in 512 notice. I did not put a number here I'm gonna store X. I think I can do this in 513. I'm gonna store Y in 514 so I'm gonna assemble it see what happens Okay code assembled successfully Took up 15 bytes and again if we look at the hex dump it looks like this Yeah, because everything's in hex. It's a little bit complicated Well, even it wasn't hacks are probably a little complicated. I'm gonna go ahead and run it and There we go. So we've got white Okay, the notes open again. So number one was white So I loaded number one into the accumulator. I stored it at 512 in the computer's memory now in this particular simulated system 512 represents this little Dot right here this little pixel right here Pretty cool Okay, so again, I did the same thing with store X and I did the same thing with store Y Okay, so now notice. I'm not limited to 255 here memory addresses Are 16 bits so as I mentioned earlier that gives you 65,535 536 memory locations 0 to 65 535 So you could see how using this we could start to draw some sort of shape Now let's see if we can Change some colors here Or change the background here. So notice how it's 200 to 5 ff. Okay, so Let's see. What is 300 it from hex to decimal 300 768 okay, so I'm gonna go back to here. I'm gonna do 768 and then I'm gonna go ahead and do And again, I know these numbers because I played around with this earlier. Okay, so There's there's no real secret here. I just spent some time working on this STA and then I want to go ahead and put STA into What's next 500 Converts 1280. All right So 1280 let's go ahead and assemble it. Okay, no errors. I'm gonna run it Okay, so you can see how this first block Starts here and it's gonna end like at this last little pixel here You think then here and here here and here here and here now because we can only do to you know I'm gonna put it So this is zero to 255 so it's 256 Bites of space in the memory and again, this is what this particular system does Okay, so I'm gonna keep those numbers and let me think about this for a second Okay, so watch what I do here. I'm gonna go ahead. I'm gonna just go ahead and get rid of this stuff now and And This is the start of memory So what I'm gonna do is I'm gonna go ahead and make a label and I'm gonna call my label just start So this is the start of the program and I'm gonna go ahead and LDA Yeah, I don't think it has to be capitalized, but it traditionally is number zero one or one because that's white and Then I'm gonna go ahead and say JMP start I'm gonna assemble Okay, I'm just gonna hit run Now it doesn't look like it's doing much But you can see over here. What's called the program counter is kind of going crazy. So the program counter Tells you where in the execution you are in your program. Now if you remember this hex dump And you see here the memory for the program starts at zero six hundred which is 1536 okay, so At six hundred it starts here and the command is LDA and then the next it's gonna load one into the accumulator Then it's gonna store What's in the accumulator in zero two hundred this is backwards I'm not sure if this is little Indian or big Indian But it's something the way just memory is stored the order that the bytes are stored in memory And I forget which one's which and then it jumps to six hundred back to the start I know that's probably a little complicated, but that's basically how it works But because the accumulator never changes It's just always going to store the same thing so what I want to try here Is I'm gonna do that Actually what I'm gonna do is I'm gonna try x instead ld x Store x I'm gonna assemble it and run it make sure it does the same thing. Okay, I'm gonna stop that And then there's a command called inx And what inx does and that's should be over here somewhere If I can find it Oh, there's increment. Is there increment? What does that do? Ah, no, it doesn't do what I think it does. Um, there is inx and inx should Increment x. Okay, so that's what I want to happen here. So let's go ahead and try that Assemble it I'm gonna run it Okay, I'm gonna stop that. Okay, so Because this is inside Basically, it's kind of a loop so I'm gonna assemble it and run it and now you can see how It's flashing different colors because what's happening is I start with one And the x I store it in the memory location that You know relates to this bit on the screen Then I increment x so x becomes you can see here how it's going really really fast So it's just adding One each time and storing it in that spot, which is pretty cool. Uh, so I'm gonna stop that now There's this other thing that we can do Uh is I'm gonna do ahead. I'm gonna do ldy Oops, and I'm gonna do number Zero And I think this is gonna work. Okay, so what should happen? Okay, is I'm gonna load I just make the zero as matters that'd be one but do that So let's assemble and see if it works. It's an x error. Okay. What did I do wrong? Um, okay, so Probably there is no store x uh or no, uh x like I said, I don't know this super well Ah Okay, cancel. Okay Again, I know what the problem is. It's just it's just super hard to explain So, um Sorry accumulator Okay, all right. Um, so it's just it's just the way six five or two assembly works I can't do what I'm trying to do here, but I can do this Uh load the accumulator Store the accumulator. I'll load x so I don't change a lot a lot around here and x Zero is black. So I'm gonna put put that back to white. Sorry. Um Assemble, okay. So what this does is I load one into the the accumulator I load zero into x and I've got my label start so that I can you know jump back to there Then I'm gonna store in five twelve Plus x this is called. I think I can put a comment like this. This is called the offset And I think that's how you do comments. Let me assemble. Yeah Um, so I'm offsetting 512 by whatever x is And then what I'm doing is I'm incrementing x each time. So it's going to start here at 512 plus zero is 512 increment x x is one 512 plus one is 513. So if I run this You something went very very wrong Is it this stuff? Get rid of that Reset Okay, so it is Locks I want to copy this and refresh. It's not happy with me And does that sometimes? assemble Run Okay, it is not working unknown opcode zero six zero seven Now I know I did this earlier So I did practice this I swear to god, uh comma x comma y All right, you should work Maybe there's no space there And again, it's like he's it locks up sometimes if you do that So I was it was moving along pretty well there, but uh, I made a mistake somewhere assemble That's really annoying Let me just try this once And see if that's working see if that's what I did wrong Okay, so that's working. So it's the offset thing is not working Um, it should work Don't know why that doesn't work assemble the symbols But doesn't run Okay Um, hmm Yeah, I did this really I did this earlier. It was actually really cool, but uh, I don't know why it's not working now reset All right, let me go ahead and check. This is why I said it was kind of live coding. Um Yeah, I don't actually know what that one means. Um It's really weird. Okay. It's probably something really obvious. Um You might just try See that makes a difference Yeah, that does make a difference. Uh, lovely Okay, so I think that's kind of a bug in the program because This doesn't work unless I use hexadecimal. Okay, so I'm gonna go ahead and just switch everything over To hex I apologize to people that don't read hex well, but I'll try and keep the numbers, you know, pretty much You know low end but 200 is 512 what's that spot there? Okay, so I'm gonna assemble it and run it And you know how it colored in the background with all white now what I can do Is I can also try things like Uh, actually, this will be kind of cool. I'm gonna stop that If you remember in the notes It says memory location fe Contains a new random byte on every instruction So what I'm gonna do Is in here dollars fe So what I'm doing this this is really important, you know, so so far we've put numbers into directly into the registers We've stored those numbers into a space in memory that that corresponds to a screen location And then we have yeah, it's all we've done So then what we want to do here Is we're gonna load into the accumulator The contents of this memory location, which says a new random byte every instruction Now if I did number dollars fe It would be fe all the time, which is Which is 254 So there's a space in memory Called you know has a number 254 and it's called the zero page. It's a little bit complicated. Anyway, uh, so But in this case, I'm actually loading The value that's in there and that value changes every instruction. So it's going to be random. So let's assemble that and run Pretty pretty cool Now as I said You know this notice how you know once we increment to 255 it rolls over back to To back to zero So if we want to do this with the whole screen What we'd have to do Is the following Now if you remember from the notes 200 to 5 f f So it's going to work out to 200 300 oops 400 and 500 And now if I assemble it and run it Oops something went wrong. Oh, that should be 300 Assemble and run it now we get this kind of cool pixelated I don't know if it's cool, but it's kind of cool Yeah, it's pretty nifty So you can see here We're changing the accumulator value. We're changing the x value Or the x registration say the x value of the x register And you can see kind of how it's working because we're storing it We're starting here. Then we do here then here then here Then we go to the next one here here here here the next one here here here and here I'm going to try a little experiment Now since there is a random number Coming out of fe I can do the same thing. So I'm going to try ldx You haven't tried this before this kind of experiment for me as well And I could do capital fe. That's usually the standard, but I could do fe And so that will give us a random offset Actually every time which is kind of cool Um Let's put some spaces in here and make it easier to read And I won't need inx anymore. So I'm going to go ahead and assemble it and run There we go That's pretty cool So basically what I did You gotta stop doing that assemble run So Now I don't need these anymore because I'm using lda and ldx here, but I I can leave them in there Let's take it out. Let's stop and Hopefully that'll be on the screen long enough that you guys can copy that if you want to try this out. So So I load the accumulator with the random number in fe I load x with a different random number in fe So again, it says every every command it changes So lda it gets a random number ldx it gets a random number and I store that Some so it's going to be some random offset and some random color Now some of you're probably wondering like why you know the colors are only zero to Basically 15 zero to f So the but this number could be like 200 or whatever it just looks at that lower end of the the bits for for that value That's why we're getting these the same colors Over and over again, which is what we want So let's go ahead and run that Pretty darn cool. I am pretty happy with that I hadn't done this one before I'm actually gonna copy that and I'll put that somewhere. So we have it There's my good friend genie and I should be able to save this as 6502 demo Dot asm That cattle's demo And I'm going to try and say that see what happens Okay, and you see how some of this is highlighted, but some of it is not. So I guess it doesn't really know 6502 assembly very well, but uh, we can cut some slack. Anyway, uh, so that was kind of a good overview Of some of the things that we can do and again, there's there's 56 instructions And I don't know what all of them do. I do want to try one other thing. I haven't tried before But I'm pretty sure it's possible Um, so just to review we have lda ldx and ldy Which allow us to either load in a number So if we want to load in a particular number, we use the number sign Or pound sign or hashtag where you kids might call it If we want to load the contents of a memory location, we don't put the number And then we can store the accumulator. We can store x we can store y In a particular memory location Now with store accumulator, we can do this offset thing To any area of memory. Let's I'll leave it at that And again, there was kind of a bug in this that if I was if this was not a decimal Um, if it wasn't hex decimal, like it doesn't the offset doesn't work. So that's something maybe We need to we should uh contact the developer about but you know, thanks to the person that made this because this is amazing and Yeah, so basically that's it. So we did a little bit offsetting and then we have jump which takes us back to the start of Our loop. This is kind of like while true Uh, it would just we're gonna repeat forever and ever now what I want to try is if you notice here Memory location ff contains the ASCII code of the last key pressed So I want to try and do a little bit of stuff with Uh keyboard commands Okay, so Uh, I'm gonna leave start So I'm gonna delete all this so I'm gonna keep that whole jump thing And then what I want to do is I'm gonna do lda And it says ff so dollars ff And that's all I want to do for now. So I'm gonna hit assemble I'm gonna hit run Now I'm gonna hit the a key Notice how it came up as dollars six one I'm gonna hit the d key, which is dollars six four So if I go over to this ASCII table, which I prepared ahead of time And I look for lower case a You can see the hex is 61 And the d is 64 Okay, so hopefully that makes a little bit of sense. So what I want to do here Is uh I want to hear Okay, I'm gonna try this ld. I'm gonna try ldx Number I'm just gonna put it like right in the middle Uh, let's see what what's the good? Well, I'll just make it zero doesn't matter ldx zero So I've put the value zero into the x-ribster And What I'm doing Is I want to figure out if I press the the d key we'll do d for now So compare Number dollars Six four Now watch what I do here So I've loaded the accumulator with the value in That keyboard memory location. So when I press the keyboard it changes the value in the memory location I'm comparing it to the number 64, which I know is d now New command branch if not equal to This is gonna be right So then I'm gonna make a label here called left I'm gonna do the same thing Uh compare to dollar number dollars 61 and Bne start. I don't know how to do that. Uh, okay. I'll do this inx Oh, I do I do actually how to do that. Uh Now, let me explain what I just did here. I think this is gonna let me assemble and see what happens. Yes, okay all right, and then No st a dollars C c c c 200 Oh x I'm gonna run this to see if it works if it works. I'll tell you what I did. Let's see branch is not equal Okay, jump to all right. I don't know if this is good good assembly language, but Interesting That's not what I wanted to happen, but it's pretty cool. Um, what I'm trying to do is move a little pixel left and right on the screen Um, so let me think this through Okay, I think this is gonna work. I know I'm not explaining it right now, but I will once I get it sorted Hmm Okay, well, let me try to explain what I'm doing here. Um, so I've loaded it into x one So I want the dot to start here um Okay, uh lda number Dollars zero one. All right, so let's try it again. Now nothing's happening. I'll run Okay, so what I want to do is I want to find out what keys bring pressed Okay, so I'm comparing it to 64 And that's d So if it's not d so branch if not equal to left it comes down here Now if it is d I increment x and then I jump to Display I think that's what the problem is Um, kind of works Stop there. I'm not sure why but uh All right, so let's that we'll call let's chalk this up to a win. Um Okay Yeah, I know what I'm trying to do. It's just it's just a little harder than I thought Anyway, let's let's let's let's get rid of left. Let's just do right Um, and if that works, okay, so we're gonna do we're gonna put left back in there Okay, assemble and run it kind of works x Okay, stop Ah, okay, um x it's probably it's possibly Yeah, this is like I said, this is live coding, right? Um Yeah, there we go. That was the problem Okay now Um, what was it? All right, I think that's it run Okay So I'm able to move this let's see what happens. It gets the end Probably a wrap around Okay, that's what I expected. Okay, so let me explain what happened. Okay, so I started out at one. I should I should just make that zero. I think um And then I started the program and then right so this loads the value that's in Memory location ff which contains the ask key code of the last key pressed not the current key pressed But the last key press so if I pressed it five minutes ago, it will still remember that I compare that to 64 dollar 64. This is this is hexadecimal if it's Not equal to 64 I want to skip all this stuff So I'm going to jump down To left so says branch if not equal So if 64 is not equal to the value that's in memory location ff. I'm going to jump down there Now if it does equal 64, it means I push the d key That's a lowercase d. I load the accumulator with the zero zero And I store that in the current spot that basically erases the dot that's there because the background's black That's why I chose zero there And here's the other thing that had to do I also put zero In back into ff Okay, because if I don't do that ff as soon as I hit d it's always going to be ff And I don't want it to be do that way. We could leave it that way. I guess but uh, I'm not going to do that way So then we increment x because I want to go right so we add One to the x and then we jump down to the display Okay, now I'm going to try and do the same thing. I'm just going to copy this Make this left And then the display is just the exact same thing we did in the previous program Where we load the accumulator with one which is white and then we store that at the offset of that particular thing now So ff I got to compare it to six one is a branch if not equal to display And then load a and then this is going to be the same but instead of incrementing x we're going to decrement Which means minus one. I think that'll work run d d d a a d d d a a So I can now move Something on the screen Okay, now again I'm skipping a couple things because I'm not quite sure how to do up and down and I it's kind of I'm not 100% sure How to I mean, I guess I know how to cross the boundaries, but it's a little bit complicated So I want to skip that for now But anyway, that was a super quick super simple introduction to 6502 assembly language. I'll put a link down to this code down below into my github and Yeah, just so just a kind of quick review Um ldx we have lda ldx and ldy and we can load numbers or From zero to 255 or the value of a memory location into those registers We can create labels they're just there for humans and then When we use jump or branch branching commands and then we load the accumulator with the value at That particular memory location again now if I'd put a number it would actually load ff into a but in this case it is The value in ff and again ff comes from the fact that this particular simulated system Puts the ASCII code in here now there's a Commodore 64 It would be a different memory location It was an Atari. It would be a different memory location now. They didn't have well sorry didn't have keywords But you get the idea Same thing would work with joysticks Then you compare it so compare the accumulator and that there is a I think there's a cpx let's just compare x I think there's a cpy I'll just compare y But cmp is compare this value To the value that's in the accumulator Now if it's not equal We want to jump down to the left. We don't want to do this Okay, now there is another one called beq Oops beq, which is branch if equal for using branch if not equal Skip down to this, but if it is a d load the accumulator with zero store it Here and that that will erase this the previous pixel Store the zero also in ff to erase the key the key press And then increment x in this case and that will move it to the to the right and then display just loads the one into There now I could I could use the fe thing Um instead of the number I could load the the random number and do it that way And I get to move like a little random randomly colored block which is kind of cool Yeah, so basically this is how your computer works um It's you know when you type python or when you type java you type whatever Um, I there's probably very good examples, but something like c or c++ that compiles down to actual machine code This is what it becomes. So your computer Is taking the you know the linkers and compilers and all kind of stuff take your code and they turn it into This kind of stuff And that's what a binary program is just a ton of numbers That interact with your system in a certain way now if you had a z80 processor or the new mac processor There's different commands new processors have many many more commands. They have many many more registers They can address larger numbers. They can address uh, not to go the more memory locations But this is a basic You know, this is basically how your computer works Again, there's a tons more Uh commands. There's a lot on this page Just even in this old, you know, 45 46 year old Assembly language has these commands But something a modern processor would have you know, probably hundreds. I don't actually know and then But like I said, this is the 6502. So again, I will leave Links to all these things down below Check it out. I hope you found that interesting and and more hope you found it more informative than Confusing I want to copy this and put that Also in my good hub and I'll I'll put a link down below again Many thanks to all my members snakes and invaders and You know subscribers and everybody Yeah, thanks for sticking around and uh hear what I have to say take care and keep on coding