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

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

Noob questions about 6502 Assembly

Post by Esns68 » Sun Jan 31, 2021 10:30 pm

I am completely new to SNES assembly, though I have attempted a couple times in the past. I had more experience with C++.
I always found assembly too complicated to even start but now for the first time I'm actually starting to learn everything.

There's a few things here and there I still don't understand so I thought I'd ask them here. I find them to be small questions so I did decide to make them in one past rather than post one for each, if that's okay. If that is against forum rules or anything, please let me know and I will fix it.

1. Any systems I need to be familiar with (Besides the instructions and memory map)? Like the data system, which I learned in C++ (Int, long, signed etc), is all that exactly the same in assembly?
And in assembly (6502) What all operators are used and how are they used ( "/" ":" etc). Looking at source code I see things like
ldx #\2
lda #:\1
What would all those operators be doing in that code for example?

2. What is the difference between all the different kinds of functions, like labels, segments etc? What all of these are there, and what is the difference between each, how is each made and used, and how are they called? Also just wondering, which one of those can you use most similar to C++ function where you can pass values/arguments down?

So far, I think label are written as their own name alone and a semicolon. "LABEL:". Are "Labels" created without any predefining or anything like that, you can just go ahead and write them?

3. I'm also having trouble understanding what .db does even after research it. So I understand it means "Define Byte". At first I got the impression
it creates variables like in C++ like "int a, int b", but I got confused when I saw it being used with hex numbers, both in address ($) and value (#$) what does that do?
I've seen it like:
.DB $F1 $F2 $F3 $F4
I do think I recall in C++ that you can use numbers as variables too, but what does this do in both address and value forms?

Also I see "db" in cases like:
var1 db
var2 db
is that the same thing as ".db", or is that a completely different thing? Any difference in use?

4. Still learning how the Addressing modes work. From what I understand, certain instructions can be used in different ways.
So at this point I know what LDA $X and STA $X do, but what does those instructions do in other modes?
Like LDA $X, X, does that refer to the x register? And what does it do with it in this case?
In addressing modes with brackets and parenthesis, LDA ($X) LDA [$X], do those make any difference to the operation?

5. When using directives, do you have to write them in capital? Will they work the same in lowercase?

6. Does spacing matter much in 6502 assembly?

7. About the flags. I think it was Carry (maybe Overflow) where if the number is over the byte value limit, the remainder of the number of the number gets stored somewhere, where does it get stored at? And does the pre-remainder just remain at the max in whatever destination it was writing to?

Thank you so much for your time. I'm very excited about getting in to this!
I'll probably have more questions like these in the future.
Last edited by Esns68 on Mon Feb 01, 2021 12:48 am, edited 1 time in total.

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

Re: Noob questions about 6502 Assembly

Post by tokumaru » Mon Feb 01, 2021 12:32 am

Esns68 wrote:
Sun Jan 31, 2021 10:30 pm
1. Any systems I need to be familiar with (Besides the instructions and memory map)?
Assembly is a pretty barebones way of manipulating data... you're basically doing mathematical operations and moving numbers around in memory. What constitutes the system is the rest of the machine where the CPU is installed, and each console will vary radically in the way the CPU communicates with all the other components. In 6502 systems, communications happens via memory mapped ports (special addresses are "connected" to hardware that's not memory, and you communicate with those pieces of hardware by writing to and reading from those special addresses).
Like the data system, which I learned in C++ (Int, long, signed etc), is all that exactly the same in assembly?
Those are data types. Assembly has no concept of data types, everything is an integer. The programmer is responsible for giving meaning to those numbers by using them for different purposes.
And in assembly (6502) What all operators are used and how are they used ( "/" ":" etc).
Operators in assembly are instructions, not symbols. You don't add with "+", you add with "ADD" or "ADC". You will find operators like the ones in high level languages in assembly source code, but those are processed at assembly time, they're only used to automate some calculations of addresses, offsets, data tables, and so on. For example:

Code: Select all

;checks if the 16-bit variable is 0
lda Points+0
ora Points+1
beq ZeroPoints
Here I'm using the "+" symbol in 6502 code to have the assembler access the low byte ("+0", which is pointless but helps convey that this is a multi-byte variable) and the high byte ("+1") of a 16-bit variable. The CPU itself is not doing any additions though, the assembler is, in order to generate something like this (assuming that the variable "Points" is at address $0050 and the label "ZeroPoints" is at address $81a0):

Code: Select all

lda $50
ora $51
beq $81a0

Code: Select all

Looking at source code I see things like
	ldx #\2
	lda #:\1	
What would all those operators be doing in that code for example?
I'm not sure what assembler this code is for, but I'm pretty sure that this is just a way to reference macro arguments ("\1" is the first argument, "\2" is the second, and so on). I have no idea what that ":" is, though. Where did you get this code?
2. What is the difference between all the different kinds of functions, like labels, segments etc?
Functions, labels and segments are all different things. A function is similar to a function in a high level language: you call it, optionally passing some arguments, it does something and returns, optionally with a result. The different is that there are no rules regarding the details of how the arguments and return value(s) are passed around... you can use registers, the stack, global variables... whatever works best for your program. Just try to be consistent, or you'll quickly lose your mind.

Labels are just nicknames to memory addresses. CPUs use memory addresses all the time, since they're constantly reading and writing data, but human programmers aren't very good at remembering thousands of different numbers, so we give names to all the memory addresses we use, and that's what labels are.

Segments are a way to control how our code and data is organized in memory and in the final binary image (ROM). You define all the properties of the segments you have to use (address, type, etc.) in a configuration file and you select segments in the source code whenever you want to put something in specific segments. Not all assemblers make use of segments.
What all of these are there
The things you listed (functions, labels and segments) are not the same type of thing.
Also just wondering, which one of those can you use most similar to C++ function where you can pass values/arguments down?
Assembly has functions, like I said above, but they are more commonly called "subroutines".
So far, I think label are written as their own name alone and a semicolon. "LABEL:". Are "Labels" created without any predefining or anything like that, you can just go ahead and write them?
Yes. When you do that, the assembler will assign the current address to that label, and whenever you need to reference that point of the program (e.g. to jump there), you can use the name you wrote.
3. I'm also having trouble understanding what .db does even after research it. So I understand it means "Define Byte". At first I got the impression it creates variables like in C++ like "int a, int b"
No, that's not for variables. .db, .dw and their variations (some assemblers use .byte and .word, for example) are used for defining raw data. All programs/games need data in order to function... address tables, jump tables, etc. and you can write those using these directives.
.DB $F1 $F2 $F3 $F4
This will just output that exact sequence of 4 bytes to the output binary file.
Also I see "db" in cases like:
var1 db
var2 db
is that the same thing as ".db", or is that a completely different thing? Any difference in use?
Those are labels placed before the data, so you can use those names to access the data.
So at this point I know what LDA #X and STA #X do
"STA #" does not exist. "#" is used to indicate an immediate value, and you can't store something in a value, you can only store in an address.
Like LDA #X, X
Also doesn't exist. LDA $X, X will read the value at memory address $X + X and put it into A. X is an index register, useful for accessing tables, arrays. X is added to the base address of the table/array, so by changing access you change which element you're gonna access. In this case, X is like the index when you access an array in C/C++: LDA TABLE, X is equivalent to A = TABLE[X] in C.
In addressing modes with brackets and parenthesis, LDA (#X) LDA [#X]
Also doesn't exist, I think you're misusing "#". Anyway, when you put an address between parenthesis (or square brackets in some assemblers), that means that the address does not point to the data that you want to access, it points to the address of the data you want to access. Like pointers in C/C++. This is useful for when the address of something changes throughout the program (e.g. in a game, each level will point to a different level map), so instead of using a hardcoded address you store the address as a variable that you can change as the program runs.
5. When using directives, do you have to write them in capital? Will they work the same in lowercase?
They're normally case-insensitive.
6. Does spacing matter much in 6502 assembly?
Depends on the assembler. Some require that labels be in the very first column, or instructions not in the first column, things like that.
7. About the flags. I think it was Carry (maybe Overflow) where if the number is over the byte value limit, the remainder of the number of the number gets stored somewhere, where does it get stored at? And does the pre-remainder just remain at the max in whatever destination it was writing to?
The carry flag indicates when additions go over 255 (carry set), or when subtractions go below 0 (carry clear), in unsigned math. The overflow flag indicates signed overflows/underflows (more than 127 or less than -128). I don't really understand what you mean about remainders.

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

Re: Noob questions about 6502 Assembly

Post by Esns68 » Mon Feb 01, 2021 12:42 am

tokumaru wrote:
Mon Feb 01, 2021 12:32 am

So at this point I know what LDA #X and STA #X do
"STA #" does not exist. "#" is used to indicate an immediate value, and you can't store something in a value, you can only store in an address.
Oh my gosh I swear I meant to put $ for address in all of those spots not #, my bad!! Fixed it. Cause I know # means value of and $ means address.

But thank you so much for all your all your answers! That clears up things a lot.

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

Re: Noob questions about 6502 Assembly

Post by Garth » Mon Feb 01, 2021 1:20 am

Esns68 wrote:
Mon Feb 01, 2021 12:42 am
Oh my gosh I swear I meant to put $ for address in all of those spots not #, my bad!! Fixed it. Cause I know # means value of and $ means address.
$ means hexadecimal, like 0x___ in C. It can be an address or data. Tokumaru wrote an excellent response though.
http://WilsonMinesCo.com/ lots of 6502 resources

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

Re: Noob questions about 6502 Assembly

Post by Esns68 » Mon Feb 01, 2021 1:52 am

Garth wrote:
Mon Feb 01, 2021 1:20 am
$ means hexadecimal, like 0x___ in C. It can be an address or data.
Yeah, I didn't mean to say it that way. I just meant without a #, using $ alone accesses a (Hexadecimal) address/data, and with # added to it, "#$" means (Hexadecimal) Value. And I think % is for the binary form.
I apologize for my wording.

Oziphantom
Posts: 1080
Joined: Tue Feb 07, 2017 2:03 am

Re: Noob questions about 6502 Assembly

Post by Oziphantom » Mon Feb 01, 2021 4:19 am

couple of caveats as you are actually after SNES not NES.

You want 65816 not 6502.

On a 65816 you have 8bit and 16bit "ints" and it changes and depends on what you set on the status flags. So A can be 8 or 16 and X/Y can be 8 or 16 and they will change constantly on the fly. So you will need to be mindful of 8 or 16 bits. The assembler will only be able to help you so much. However Address can be Byte, Word or Long where Byte is 8bits, Word is 16bits and Long is 24bits and different addressing modes will use different sizes.

When asking about assembler directives, you need to mention which Assembler you are using, there are no standards and every assembler is different and what one uses ! for another will use it for something else entirely.

tepples
Posts: 22284
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Noob questions about 6502 Assembly

Post by tepples » Mon Feb 01, 2021 5:28 pm

tokumaru wrote:
Mon Feb 01, 2021 12:32 am
I'm not sure what assembler this code is for, but I'm pretty sure that this is just a way to reference macro arguments ("\1" is the first argument, "\2" is the second, and so on). I have no idea what that ":" is, though. Where did you get this code?
I'm guessing WLA DX, where :label means roughly the same as ca65 bank(label).
tokumaru wrote:
Mon Feb 01, 2021 12:32 am
Also I see "db" in cases like:
var1 db
var2 db
is that the same thing as ".db", or is that a completely different thing? Any difference in use?
Those are labels placed before the data, so you can use those names to access the data.
Those might be declarations of addresses in RAM, equivalent to var1: .res 1 in ca65.

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

Re: Noob questions about 6502 Assembly

Post by unregistered » Mon Feb 01, 2021 6:34 pm

Oziphantom wrote:
Mon Feb 01, 2021 4:19 am
what one uses ! for another will use it for something else entirely.
When reading this truth, it can be helpful if you realize basic Assembler facts. :)

Every computer runs with chips composed of many many transistors. Each transistor can hold one of two possible states. It can either be charged (1) or uncharged (0).

Geniuses used transistors to build computers. A way of interacting with these transistors needed to be created, so binary, a base2 number system, was invented. Base2 only has 2 digits, 0 and 1.

To make a lengthy story super short, all computers run on 1s and 0s; so binary is crucial.

Every computer needs to be programmed so it will perform properly. One way of programming is using a low level language, otherwise known as assembly. (All high level languages, languages such as C++, must be compiled before that program can run. A compiler translates its high level language into specific assembly code that runs on the machine it was compiled on.)

Assemblers can be written quite differently. But, the single goal of an assembler is to assemble its code. Assembling code is basically translating the assembly instructions into a bunch of 1s and 0s; obviously, these 1s and 0s are fed to the chips’ transistors.

Hope this helps. :)


Note: Hexadecimal is a base16 numbering system. Created so that eight 1s and 0s, a byte, can be cram packed into 2 characters.

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

Re: Noob questions about 6502 Assembly

Post by Esns68 » Tue Feb 02, 2021 3:21 am

Sorry, so yeah the assembler I'm using is WLA DX indeed.

Oziphantom
Posts: 1080
Joined: Tue Feb 07, 2017 2:03 am

Re: Noob questions about 6502 Assembly

Post by Oziphantom » Tue Feb 02, 2021 3:42 am

wla-6502 or wla-65816? They are different and have different uses/possible directives.

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

Re: Noob questions about 6502 Assembly

Post by Esns68 » Tue Feb 02, 2021 4:41 am

Oziphantom wrote:
Tue Feb 02, 2021 3:42 am
wla-6502 or wla-65816? They are different and have different uses/possible directives.
wla-65816, since I'm studying SNES programming.

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

Re: Noob questions about 6502 Assembly

Post by Garth » Tue Feb 02, 2021 12:49 pm

Esns68 wrote:
Tue Feb 02, 2021 4:41 am
Oziphantom wrote:
Tue Feb 02, 2021 3:42 am
wla-6502 or wla-65816? They are different and have different uses/possible directives.
wla-65816, since I'm studying SNES programming.
Do yourself the favor (if you haven't already) of getting WDC's excellent 6502/65816 programming manual, "Programming the 65816—Including the 6502, 65C02 and 65802" by David Eyes and Ron Lichty. This is definitely the best 65xx programming manual available, and a must-have for every 65xx programmer! It starts with the basics, followed by architecture, the CMOS 65c02's many improvements over the original NMOS 6502 including added instructions and addressing modes and fixing the NMOS's bugs and quirks, and then the natural progression to the 65816; a thorough tutorial, writing applications, then very detailed and diagrammed information on all 34 addressing modes, at least a page of very detailed description for each instruction, with info on every addressing mode available for that instruction, then instruction lists, tables, and groups, of all 255 active op codes, plus more. 469 pages. From Western Design Center. (.pdf) Note: There were many problems with the earlier .pdf version that were not in the original paper manual; but in late March 2015, WDC scanned and OCR'ed the paper manual and posted the new, repaired .pdf.
http://WilsonMinesCo.com/ lots of 6502 resources

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

Re: Noob questions about 6502 Assembly

Post by Esns68 » Wed Feb 03, 2021 6:08 am

I also had some follow up questions.
tokumaru wrote:
Mon Feb 01, 2021 12:32 am
No, that's not for variables. .db, .dw and their variations (some assemblers use .byte and .word, for example) are used for defining raw data. All programs/games need data in order to function... address tables, jump tables, etc. and you can write those using these directives.
Okay. So first just curious, how do you define variables? .DEFINE? (As mentioned earlier, WLA DX, wla-65816) as long as creating variables is a thing in 6502 assembly.

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? And what are address tables and all those?
Is there any examples on when you want to use .db (and the others) to do something specific?
.DB $F1 $F2 $F3 $F4
This will just output that exact sequence of 4 bytes to the output binary file.
What would be the purpose of doing that in cases like that? Unless I miss-saw for another directive like .define or something, but I
definitely have seen a row similar to that in front of some kind of directive or label or something.

Would there be any difference if you wrote a row like that and used either hex addresses like $F1, or value ones like #$F1?
Also I see "db" in cases like:
var1 db
var2 db
-------------------
Those are labels placed before the data, so you can use those names to access the data
Okay I kind of catch you there. I just wondered, is it supposed to specific "data", or just all the "data"?
Cause I'm pretty sure when I saw it, I saw it just like that, nothing in front of the db to specify any specific data
Operators in assembly are instructions, not symbols. You don't add with "+", you add with "ADD" or "ADC". You will find operators like the ones in high level languages in assembly source code, but those are processed at assembly time, they're only used to automate some calculations of addresses, offsets, data tables, and so on.
Okay. Is there any chart with all the symbols and their uses in 6502?
Where did you get this source code?[/]
I think it was one of those source codes include in the SNES_Startkit, in Loadgraphics.asm or something.

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

Re: Noob questions about 6502 Assembly

Post by Esns68 » Wed Feb 03, 2021 6:16 am

Garth wrote:
Tue Feb 02, 2021 12:49 pm
Do yourself the favor (if you haven't already) of getting WDC's excellent 6502/65816 programming manual, "Programming the 65816—Including the 6502, 65C02 and 65802" by David Eyes and Ron Lichty. This is definitely the best 65xx programming manual available, and a must-have for every 65xx programmer! It starts with the basics, followed by architecture, the CMOS 65c02's many improvements over the original NMOS 6502 including added instructions and addressing modes and fixing the NMOS's bugs and quirks, and then the natural progression to the 65816; a thorough tutorial, writing applications, then very detailed and diagrammed information on all 34 addressing modes, at least a page of very detailed description for each instruction, with info on every addressing mode available for that instruction, then instruction lists, tables, and groups, of all 255 active op codes, plus more. 469 pages. From Western Design Center. (.pdf) Note: There were many problems with the earlier .pdf version that were not in the original paper manual; but in late March 2015, WDC scanned and OCR'ed the paper manual and posted the new, repaired .pdf.
Okay I was hoping to find something just like that, a book that "introduces" someone completely new like me to 6502 assembly.
Thank you so much!!

So it looks like you have to buy that PDF?

Oziphantom
Posts: 1080
Joined: Tue Feb 07, 2017 2:03 am

Re: Noob questions about 6502 Assembly

Post by Oziphantom » Wed Feb 03, 2021 6:48 am

There are lots of books on how to program 6502 for somebody who has never programmed 6502 before ; see the Machine language section of this site https://69.60.118.202/commodore/books.htm The Butterfield book "Machine Language on the Commodore 64, 128 and other Commodore Computers Revised and Expanded Edition" is considered the book to learn from but personally I prefer the "Commodore 64 Assembly Language" by Bruce Smith

The WDC book will cover 65816 which is a bit rarer to get a book upon but there are plenty 65816 for the 6502 guides and once you know 6502 65816 is not that much more. Although the Bantam Assembly Programming for the Apple IIgs book here https://69.60.118.202/apple/apple-books-iigs.htm may be of service.

Post Reply