Noob questions about 6502 Assembly

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

User avatar
tokumaru
Posts: 11996
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Noob questions about 6502 Assembly

Post by tokumaru » Wed Feb 03, 2021 9:12 am

Esns68 wrote:
Wed Feb 03, 2021 6:08 am
I also had some follow up questions.
I'm not at all familiar with the assembler you're using, so I may not be able to answer everything, but I'll do my best.
Okay. So first just curious, how do you define variables?
That can vary a lot from assembler to assembler, and even in the same assembler there are different ways to go about it, but at the most basic level, a variable is exactly the same thing as a label, just a nickname for an address. Most assemblers will let you do MyVariable = $05F0 to define a variable, and many people like this simple approach.
As for the .db stuff, I'm sorry to say I'm still having trouble understanding what it's used for. What is raw data?
Any data you may need to use in your program... e.g. the positions of enemies in a level, a list of sprite entries, the map of a level... In some cases you can hardcode some of that information in the logic itself, but you don't want to write 1000 different sprite drawing subroutines just because you have 1000 different sprites in your game, you write only one subroutine and point it to one of a 1000 sprite definitions (data) stored in the ROM.
And what are address tables and all those?
An address table is literally that, a list of addresses pointing to different things. For example, say you have a maze game with 20 levels, each with a different maze layout. Will you write 20 different functions to draw those maps on the screen? No, you'll write one function, and pass the number of the level you want to draw as an argument. The function will then use this number as an index to read one of 20 addresses from a table, and use that address to read the data it needs to draw the requested level.
Is there any examples on when you want to use .db (and the others) to do something specific?
Say I want to make a program to tell everyone how many cookies I ate each day in January. I need a function that will receive a day of the month as an argument, and will return the number of cookies I ate that day:

Code: Select all

;Subroutine: returns number of cookies eaten in a day of January.
;Input: A = day in January (1 to 31);
;Output: A = number of cookies;
GetCookiesForDay:

  ;subtracts 1 because table starts from 0, not 1
  sec
  sbc #1

  ;uses this number as an index
  tax

  ;gets the number of cookies from the table
  lda CookiesPerDay, x

  ;returns
  rts

;Table: lists cookies eaten for each day in January.
CookiesPerDay:
  .db 4, 3, 2, 2, 6, 5, 5, 2, 0, 2, 4, 3, 2, 2, 6, 4, 0, 2, 2, 2, 4, 3, 2, 3, 6, 5, 6, 2, 0, 2, 3
Now I can call this function like this:

Code: Select all

  ;gets the number of cookies eaten in January 7
  lda #7
  jsr GetCookiesForDay
Would there be any difference if you wrote a row like that and used either hex addresses like $F1, or value ones like #$F1?
The purpose of # is to define an addressing mode for instructions (immediate addressing), so you normally shouldn't use it for anything other than the operand of a CPU instruction.
Okay I kind of catch you there. I just wondered, is it supposed to specific "data", or just all the "data"?
Normally you need a name (label) before every block of data in your program, so you have a way of referencing that data, but it is possible that you don't need a name if you're calculating the address of a data table in some other way.
Okay. Is there any chart with all the symbols and their uses in 6502?
At its core level, assembly doesn't have many symbols at all... It's mainly the ones we already talked about: # for immediate, $ for hex, % for binary, () and/or [] for indirection. Anything beyond that is assembler-specific, so you should refer to your assembler's documentation.

EDIT: fixed CLC/SEC mix-up.
Last edited by tokumaru on Thu Feb 04, 2021 10:49 am, edited 1 time in total.

Pokun
Posts: 1746
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Noob questions about 6502 Assembly

Post by Pokun » Wed Feb 03, 2021 1:49 pm

It might help to do a test assemble of a really short source file. Just type ".db $01,$02,$03" for example then assemble it and open the assembled file in a hex editor. You will see that it just contains the values $01, $02 and $03. If you add an instruction like "lda $AB" the corresponding machine code values will show up and so on.
It's just too bad that WLA-DX requires you to add a bunch of crap before it allows you to assemble. Also I've noticed this assembler doesn't always output correctly in 65816 mode (and even worse in SPC700 mode).

unregistered
Posts: 1126
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Re: Noob questions about 6502 Assembly

Post by unregistered » Wed Feb 03, 2021 3:05 pm

This is perhaps the most crucial :P reply in noob questions about 6502 assembly:

I realize it’s just a great example, but make sure to never eat 6 cookies in a single day. That’s an insane amount of sugar for a human body to process in one day. A maximum of 2 cookies per day should be good to keep, IMHO. :)

Esns68
Posts: 31
Joined: Tue Jan 07, 2020 2:03 pm

Re: Noob questions about 6502 Assembly

Post by Esns68 » Wed Feb 03, 2021 5:54 pm

tokumaru wrote:
Wed Feb 03, 2021 9:12 am
Say I want to make a program to tell everyone how many cookies I ate each day in January. I need a function that will receive a day of the month as an argument, and will return the number of cookies I ate that day:
Wow, that was a very good example. That example couldn't have been anymore perfect in helping me understand the concept.
I get the idea now. Thank you so much again!

Code: Select all

  ;subtracts 1 because table starts from 0, not 1
  clc
  sbc #1
Just a quick question about that. I heard that sbc automatically subtracts 1 if the carry is clear (and opposite for Add With Carry).
So since the carry is cleared right before sbc #1, wouldn't it already subtract 1.
So does placing #1 right there do?

So besides that, I just have one more question about all of this.

So all the labels and stuff are "reference to a memory address location".
I just wondered, since they're declared alone only as "label:", how do the labels specify an address location in memory?
Does it automatically pick a spot?
My other guess is that the first code line involving an address is the memory location for the label.
unregistered wrote:
Wed Feb 03, 2021 3:05 pm
This is perhaps the most crucial :P reply in noob questions about 6502 assembly:

I realize it’s just a great example, but make sure to never eat 6 cookies in a single day. That’s an insane amount of sugar for a human body to process in one day. A maximum of 2 cookies per day should be good to keep, IMHO. :)
Very well said. Unfortunately I struggle with that whenever someone bakes fresh warm oven cookies. XD

User avatar
tokumaru
Posts: 11996
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Noob questions about 6502 Assembly

Post by tokumaru » Wed Feb 03, 2021 7:12 pm

unregistered wrote:
Wed Feb 03, 2021 3:05 pm
I realize it’s just a great example, but make sure to never eat 6 cookies in a single day. That’s an insane amount of sugar for a human body to process in one day. A maximum of 2 cookies per day should be good to keep, IMHO. :)
I actually don't even like cookies, I just needed a quick example so I went with the first thing that came to mind! :mrgreen:
Esns68 wrote:
Wed Feb 03, 2021 5:54 pm

Code: Select all

  ;subtracts 1 because table starts from 0, not 1
  clc
  sbc #1
Just a quick question about that. I heard that sbc automatically subtracts 1 if the carry is clear (and opposite for Add With Carry).
So since the carry is cleared right before sbc #1, wouldn't it already subtract 1.
So does placing #1 right there do?
You're correct, that was my mistake! I should have used SEC instead of CLC there. This means that the code in the example is actually subtracting 2, which's a bug.
So all the labels and stuff are "reference to a memory address location".
I just wondered, since they're declared alone only as "label:", how do the labels specify an address location in memory?
Does it automatically pick a spot?
When you start writing a program, you somehow have to tell your assembler where that code will be loaded in memory (how you do this varies from assembler to assembler, but one of the most common ways to do this is using the .ORG directive), so once it knows the start address, everything you write from then on will consume memory, and the assembler automatically increment the address as it assembles your code. When it finds a label definition, it simply assigns the current address to that label.

Garth
Posts: 206
Joined: Wed Nov 30, 2016 4:45 pm
Location: Southern California
Contact:

Re: Noob questions about 6502 Assembly

Post by Garth » Wed Feb 03, 2021 7:15 pm

Esns68 wrote:
Wed Feb 03, 2021 5:54 pm
Just a quick question about that. I heard that sbc automatically subtracts 1 if the carry is clear (and opposite for Add With Carry).
So since the carry is cleared right before sbc #1, wouldn't it already subtract 1.
So does placing #1 right there do?
Yes, simple mistake. Normally you'll use SEC before a subtraction.
So besides that, I just have one more question about all of this.

So all the labels and stuff are "reference to a memory address location".
I just wondered, since they're declared alone only as "label:", how do the labels specify an address location in memory?
Does it automatically pick a spot?
My other guess is that the first code line involving an address is the memory location for the label.
The label takes on the address that's being assembled for at the moment. You start with the ORG (ORiGin) assembler directive to tell it where to start; then each instruction and every piece of data you lay down after that advances that address by the appropriate amount. So if you tell it ORG $300, that's where it will start. (You can have as many occurrences of ORG in your code as you want; but most applications will probably only have two or three.) Let's say you put a routine there and give it the label FOOBAR. So FOOBAR will take on the value $300, and then doing a JSR FOOBAR will lay down the machine code 4C 00 03 wherever such a line is encountered.

Then let's say you give it a dozen assembly-language instructions taking 27 (1B in hex) bytes, and then have a string constant there and call it STRING. So STRING will take on the value $031B (because the assembler already put something in addresses $300 to 31A) , and that number $031B will be substituted in anywhere you have an operand referring to STRING, for example LDA STRING,X which will give you BD 1B 03 in the resulting machine code. Both STRING and FOOBAR are labels, even though one is for data and one is for code.

The same thing goes for variables (including variable arrays). If you proceed to have some variable space, you'll give that a label too, and tell the assembler how many bytes to allot for your variable, so subsequent assembly skips over that many bytes so that subsequent additions don't step on those bytes you wanted to keep available for your variable. (Obviously if you're assembling for ROM, variables will have to go in a separate space, in RAM which the program can write to.) Let's say you call it ARRAY1, and then at some point in the code you want to store to the fourth byte in the array, so you write STA ARRAY1+3 (or, more likely, use one of the ways to index).

My 6502 primer has a chapter called "Program-Writing: Where Do I Start?", at http://wilsonminesco.com/6502primer/PgmWrite.html . It's there because of a forum post many years ago that said, "and no one really ever tells you in what program do you put the code." Let me know if something there is unclear, so I can improve it.
http://WilsonMinesCo.com/ lots of 6502 resources

User avatar
Controllerhead
Posts: 214
Joined: Tue Nov 13, 2018 4:58 am
Location: $4016
Contact:

Re: Noob questions about 6502 Assembly

Post by Controllerhead » Wed Feb 03, 2021 7:24 pm

Garth wrote:
Wed Feb 03, 2021 7:15 pm
ORG (ORiGin)
Interesting. I assumed it was "ORGanize" :roll:
Image

Pokun
Posts: 1746
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Noob questions about 6502 Assembly

Post by Pokun » Thu Feb 04, 2021 7:40 am

unregistered wrote:
Wed Feb 03, 2021 3:05 pm
This is perhaps the most crucial :P reply in noob questions about 6502 assembly:

I realize it’s just a great example, but make sure to never eat 6 cookies in a single day. That’s an insane amount of sugar for a human body to process in one day. A maximum of 2 cookies per day should be good to keep, IMHO. :)
2 cookies? There's no way that would fill my stomach!
Ahem, while I understand this was well meant and isn't baseless advice, I can't agree with the number here (unless they are 2 exceedingly large cookies) and it is in direct conflict with traditional Swedish fika-etiquette. According to that, the hostess should ideally have 7 different types of hand-made cookies to offer her guests, in order to neither appear stingy nor overabundant. The polite guest should of course try at least one of every type of cookie. 7 cookies is excluding the mandatory sweet bun (which is consumed first) or other soft fika-bread such as sweet rolls or sponge cake.
This of course should be accompanied with exercise. At least about 1 hour of active exercise a day is needed.

unregistered
Posts: 1126
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Re: Noob questions about 6502 Assembly

Post by unregistered » Thu Feb 04, 2021 10:34 am

Pokun, perhaps you misunderstand what a cookie is? :)

It’s a dessert. Therefore, cookies aren’t supposed to fill you up. Fill up with mostly normal, and healthy (IMHO), food... and then eat dessert if that would satisfy. :)


tokumaru, it was a great example. :D

unregistered
Posts: 1126
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Re: Noob questions about 6502 Assembly

Post by unregistered » Thu Feb 04, 2021 10:58 am

Pokun, I came back to confess that, obviously, I’m not familiar with traditional Swedish fika-etiquette. Though, that hour of daily exercise makes great sense. :)

My way does not require an hour of daily exercise bc my body is not having to constantly process the huge amount of sugar that would come with 7 cookies and cake too. But, one of my jobs is spent at a vineyard and deals with construction, so I guess I already get more than that hour of exercise. :)

(I sometimes go for days without eating dessert. :))

Sry, back to nes dev.

Pokun
Posts: 1746
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Noob questions about 6502 Assembly

Post by Pokun » Thu Feb 04, 2021 12:39 pm

Healthy and normal food fills up the healthy and normal stomach. Cookies and other deserts fills up the desert stomach.

The 1 hour exercise isn't just if you eat 7 cookies though, it's an everyday thing. If you have a physical job of course you already fulfill that. Maybe you need to eat more cookies instead. Balance is key.

unregistered
Posts: 1126
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Re: Noob questions about 6502 Assembly

Post by unregistered » Thu Feb 04, 2021 1:55 pm

Pokun, we all have a stomach. If you supply your stomach with a constant excess amount of sugar, then yes your body will feel it needs the daily excess amount of sugar.

However, that sugar excess may cause you to feel terrible. And flabby weight gain is more of a possibility.

If one, under your conditions, forces themself to eat healthy, you will definitely feel awful in the beginning. Though, if these efforts are kept persistently, your stomach will change AND you’ll feel much better. :)

More energy and a more pleasant life are definitely possible for anyone. :)

I agree with you, balance is the key! :D A balance of the different food groups is necessary. However, dessert is not a food group. :) Plus, 7 cookies and cake daily is a big unbalance of food, to me at least. :)

Pokun
Posts: 1746
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Noob questions about 6502 Assembly

Post by Pokun » Fri Feb 05, 2021 6:37 am

I never said I don't eat healthy. I eat lots of meat, lots of fish, lots of seafood, lots of vegetables and lots of cookies. I eat about anything, balanced diet all around.
Also I never said to eat 7 cookies a day. It's just when you are eating fika (afternoon tea) at someone's place and they offer you 7 types of cookies according to tradition. 7 cookies a day is probably bad for you.

My job isn't very physically active, but I do karate, gym and occasionally go out for a jog in my free time, exercising body, soul and spirit.

Post Reply