Hello, I'm back, still need assistences

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

DocWaluigean
Posts: 205
Joined: Sun Jun 17, 2018 6:41 pm

Re: Hello, I'm back, still need assistences

Post by DocWaluigean » Thu Jun 11, 2020 8:12 pm

Gilbert wrote:
Thu Jun 11, 2020 7:37 pm
In either case, they're copy, not move.

Example 1:
The code:

Code: Select all

LDA #5
STA mem1
where mem1 is a label corresponding to a certain memory location, is equivalent to, in higher level language terms:

Code: Select all

A = 5
mem1 = A
Which after storing the value of A (5) into mem1, A still carries the value 5, so STA just copies the value of A to a memory location.

Example 2:
The code:

Code: Select all

LDA #7
TAX
is equivalent to, in higher level language terms:

Code: Select all

A = 7
X = A
Which after transferring the value of A (7) into X, A still carries the value 7, so TAX just copies the value of A to a X.
Wow, that made SO much sense! My concern is, if the code A = 5 : mem1 = A is true, does mem1 number changes if A = something other than 5? Or it won't change?

So it's not copy, huh? Guess I have to do AXYS = 0 if I want to erase the contains of Registers every time I used the number once for it.

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

Re: Hello, I'm back, still need assistences

Post by tokumaru » Thu Jun 11, 2020 8:42 pm

DocWaluigean wrote:
Thu Jun 11, 2020 8:12 pm
My concern is, if the code A = 5 : mem1 = A is true, does mem1 number changes if A = something other than 5?
No. Once a value is copied, it doesn't matter what you do with the source of that value, the copy will not be affected.
So it's not copy, huh? Guess I have to do AXYS = 0 if I want to erase the contains of Registers every time I used the number once for it.
Why would you bother erasing the values? Just leave them there...

DocWaluigean
Posts: 205
Joined: Sun Jun 17, 2018 6:41 pm

Re: Hello, I'm back, still need assistences

Post by DocWaluigean » Thu Jun 11, 2020 8:51 pm

tokumaru wrote:
Thu Jun 11, 2020 8:42 pm
DocWaluigean wrote:
Thu Jun 11, 2020 8:12 pm
My concern is, if the code A = 5 : mem1 = A is true, does mem1 number changes if A = something other than 5?
No. Once a value is copied, it doesn't matter what you do with the source of that value, the copy will not be affected.
So it's not copy, huh? Guess I have to do AXYS = 0 if I want to erase the contains of Registers every time I used the number once for it.
Why would you bother erasing the values? Just leave them there...
Oh, okay. Still, better to question it since everyone deserve to know tiny details about it. A simple mistake like bug or glitch can lead to huge innovations (Street Fighter 2, "The before Grand Theft Auto", Smash Bros. Melee) or pure chaos and problems (Atari E.T.).

---

I mean erase the Registers of A, X, and Y if not using or at least replace it with new numbers, not the Address. I think why erase it is to set certain Flags on or off.

DocWaluigean
Posts: 205
Joined: Sun Jun 17, 2018 6:41 pm

Re: Hello, I'm back, still need assistences

Post by DocWaluigean » Thu Jun 11, 2020 9:02 pm

But anyway, I'm wondering which "most important, basic stuff first" am I missing out right now?

There's A, X, Y Register
Address usages
Binary, HEX, and normal numbers
Carry Flag, Zero Flag, [and maybe Negative Flag but I still don't know how to activate it and use it yet.]

I'm guessing I'm missing the 4th Register, Stack Pointer? Or what else? Before ready to try first programming in easy-basic level.

turboxray
Posts: 104
Joined: Thu Oct 31, 2019 12:56 am

Re: Hello, I'm back, still need assistences

Post by turboxray » Fri Jun 12, 2020 4:28 pm

DocWaluigean wrote:
Thu Jun 11, 2020 8:51 pm
I mean erase the Registers of A, X, and Y if not using or at least replace it with new numbers, not the Address. I think why erase it is to set certain Flags on or off.
No. Because you'd just be wasting cycles. Any time you need to use one of those registers, you either want the remaining value in there already, or you load it with new value and use it. If you're not using them, then it doesn't matter what values are in them. I'm not sure what you mean by 'certain flags'. Do you mean processor flags or 'software' flags??

DocWaluigean
Posts: 205
Joined: Sun Jun 17, 2018 6:41 pm

Re: Hello, I'm back, still need assistences

Post by DocWaluigean » Fri Jun 12, 2020 4:58 pm

turboxray wrote:
Fri Jun 12, 2020 4:28 pm
DocWaluigean wrote:
Thu Jun 11, 2020 8:51 pm
I mean erase the Registers of A, X, and Y if not using or at least replace it with new numbers, not the Address. I think why erase it is to set certain Flags on or off.
No. Because you'd just be wasting cycles. Any time you need to use one of those registers, you either want the remaining value in there already, or you load it with new value and use it. If you're not using them, then it doesn't matter what values are in them. I'm not sure what you mean by 'certain flags'. Do you mean processor flags or 'software' flags??
I mean the flags like Zero Flag, V Flag, Carry Flag, etc. At least for NES.

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

Re: Hello, I'm back, still need assistences

Post by Garth » Fri Jun 12, 2020 8:38 pm

An automatic compare-to-zero instruction (which updates the N and Z flags) is built into the following 65c02 instructions: LDA, LDX, LDY, INC, INX, INY, DEC, DEX, DEY, AND, ORA, EOR, ASL, LSR, ROL, ROR, PLA, SBC, ADC, TAX, TXA, TAY, TYA, and TSX, and on the 65c02 (ie, CMOS), INA, DEA, PLX, and PLY. That's all the load, logic, and arithmetic instructions, but not storing, branching, jumping, and maybe others I'm forgetting. Whatever status is left over from the past can be left in place if you're not using it, without hurting anything. When you load new data into a processor register or do a logic or arithmetic operation on existing data, the status will be updated, and you can use that new status in subsequent instructions, regardless of what had been there before.
http://WilsonMinesCo.com/ lots of 6502 resources

DocWaluigean
Posts: 205
Joined: Sun Jun 17, 2018 6:41 pm

Re: Hello, I'm back, still need assistences

Post by DocWaluigean » Fri Jun 12, 2020 10:19 pm

Garth wrote:
Fri Jun 12, 2020 8:38 pm
An automatic compare-to-zero instruction (which updates the N and Z flags) is built into the following 65c02 instructions: LDA, LDX, LDY, INC, INX, INY, DEC, DEX, DEY, AND, ORA, EOR, ASL, LSR, ROL, ROR, PLA, SBC, ADC, TAX, TXA, TAY, TYA, and TSX, and on the 65c02 (ie, CMOS), INA, DEA, PLX, and PLY. That's all the load, logic, and arithmetic instructions, but not storing, branching, jumping, and maybe others I'm forgetting. Whatever status is left over from the past can be left in place if you're not using it, without hurting anything. When you load new data into a processor register or do a logic or arithmetic operation on existing data, the status will be updated, and you can use that new status in subsequent instructions, regardless of what had been there before.
This is going to confuse me a lot...
An automatic compare-to-zero instruction (which updates the N and Z flags) is built into the following 65c02 instructions: LDA, LDX, LDY, INC, INX, INY, DEC, DEX, DEY, AND, ORA, EOR, ASL, LSR, ROL, ROR, PLA, SBC, ADC, TAX, TXA, TAY, TYA, and TSX, and on the 65c02 (ie, CMOS), INA, DEA, PLX, and PLY.
Can you do examples about it???

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

Re: Hello, I'm back, still need assistences

Post by Garth » Fri Jun 12, 2020 11:30 pm

DocWaluigean wrote:
Fri Jun 12, 2020 10:19 pm
Can you do examples about it???
Ok, here's an example: Let's say you want to examine an 8-bit variable in memory to see if it's zero, and branch on whether it is or not. If you just load in into whichever register is free, the next instruction can be your branch instruction:

Code: Select all

        LDA  foobar       ; Examine variable "foobar."
        BEQ  label1       ; If it's 0, skip the next two lines and go down to label1.
        <do_stuff>        ; These two lines will get executed only if "foobar" was not 0.
        <do_stuff>
label1: <continue_here>   ; This part gets executed regardless.
Note that it was not necessary to do a CMP #0. It's an automatic, built-in part of the LDA instruction (and others I mentioned above). The same is true for things like decrementing a register or memory location, because that's an arithmetic operation. So you could have a loop that executes eight times, this way:

Code: Select all

        LDX  #7
label2: LDA  array1,X
        STA  array2,X
        DEX
        BPL  label2
(Tokumaru mentioned indexing earlier.) In this case, it copies eight bytes from array1 to array2. The first byte of each array is the array's base address plus 0, and the last byte is array plus 7. However we can start at the end instead of the beginning, and go backwards to take advantage of the automatic compare-to-0 built into DEX to decide whether to branch back up to the top of the loop for another go-around. It will execute for X=7, then 6, [...] and finally 0; but after the DEX DEcrements X from 0 to $FF, the N flag will be set, so BPL will drop through to the next instruction instead of branching back up to the top of the loop. There was no need for a CPX #0 instruction.


You need to go through a programming manual. There are quite a few good ones.

I think the best may be Programming the 65816-Including the 6502, 65C02 and 65802, by David Eyes and Ron Lichty. It has a thorough tutorial, writing applications, then very detailed and diagrammed information on all the 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 the op codes, plus more. It will tell you exactly how every single instruction affects or uses the flags, and a lot more too, as you wold expect from a well laid-out 469-page book.

"Programming the 6502" by Rodnay Zaks has a good reputation if you only intend to stay with the NMOS 6502 instruction set (which is what the NES uses). Another ones is "6502 Assembly Language Programming" by Lance A. Leventhal.
http://WilsonMinesCo.com/ lots of 6502 resources

turboxray
Posts: 104
Joined: Thu Oct 31, 2019 12:56 am

Re: Hello, I'm back, still need assistences

Post by turboxray » Sat Jun 13, 2020 10:17 am

DocWaluigean wrote:
Fri Jun 12, 2020 10:19 pm

An automatic compare-to-zero instruction (which updates the N and Z flags) is built into the following 65c02 instructions: LDA, LDX, LDY, INC, INX, INY, DEC, DEX, DEY, AND, ORA, EOR, ASL, LSR, ROL, ROR, PLA, SBC, ADC, TAX, TXA, TAY, TYA, and TSX, and on the 65c02 (ie, CMOS), INA, DEA, PLX, and PLY.
Can you do examples about it???
Although this is for PC-Engine, the flag logic is going to be the same for compatible 6502 instructions (nes version too).
http://turboxray.com/pce_docs/Otaku_no_ ... _page1.png

For the flag chart: "-" means it doesn't change, "r" means is renders the flag (set or clear), and 0 means it clears it (that's probably only PCE related though since the 6502 doesn't have a T flag).

But anyway, you can see what flags are modified based on the instruction group. For example, LDA automatically gives you a cmp #$00 like Garth mentioned. It also gives you a free sign comparison (is this value signed or not) but the value in bit #7 is going to be the value in the N flag - you could also simply treat it as a free "bit" check. Notice LDY, LDX, and the Txx transfer instructions that transfer between A, X, Y, SP also follow this pattern.

As for say the DEC.. imagine you have a counter, and when that counter expires.. you branch somewhere. So instead of doing something LDA counter, DEC counter, CMP #00, BEQ @someLabel, you could optimize it by simply doing removing the CMP #00, or even make the whole thing DEC counter, BEQ @someLabel (depending if you need the value in Acc at the time).

DocWaluigean
Posts: 205
Joined: Sun Jun 17, 2018 6:41 pm

Re: Hello, I'm back, still need assistences

Post by DocWaluigean » Sat Jun 13, 2020 12:13 pm

CODE: SELECT ALL

CODE: SELECT ALL

LDA foobar ; Examine variable "foobar."
BEQ label1 ; If it's 0, skip the next two lines and go down to label1.
<do_stuff> ; These two lines will get executed only if "foobar" was not 0.
<do_stuff>
label1: <continue_here> ; This part gets executed regardless.

LDX #7
label2: LDA array1,X
STA array2,X
DEX
BPL label2
(Tokumaru mentioned indexing earlier.) In this case, it copies eight bytes from array1 to array2. The first byte of each array is the array's base address plus 0, and the last byte is array plus 7. However we can start at the end instead of the beginning, and go backwards to take advantage of the automatic compare-to-0 built into DEX to decide whether to branch back up to the top of the loop for another go-around. It will execute for X=7, then 6, [...] and finally 0; but after the DEX DEcrements X from 0 to $FF, the N flag will be set, so BPL will drop through to the next instruction instead of branching back up to the top of the loop. There was no need for a CPX #0 instruction.
I don't understand at first sight.

LDA foobar - Load number into A Register from "foobar" label.
BEQ label1 - If Zero Flag is on, go to "label1".
[ ANY CODE ]
[ ANY CODE ]

label1: [ ANY CODE]
LDX #7 - Load number 7 into X Register.
LDA array1, X - Load numbers from "array1" label to Register A, and copy the "array1" number into Register X.
DEX - Decrement X Register
BPL label2 - If destination reached here while the Negative Flag is off, go to "label2"

You need to go through a programming manual. There are quite a few good ones.

I think the best may be Programming the 65816-Including the 6502, 65C02 and 65802, by David Eyes and Ron Lichty. It has a thorough tutorial, writing applications, then very detailed and diagrammed information on all the 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 the op codes, plus more. It will tell you exactly how every single instruction affects or uses the flags, and a lot more too, as you wold expect from a well laid-out 469-page book.

"Programming the 6502" by Rodnay Zaks has a good reputation if you only intend to stay with the NMOS 6502 instruction set (which is what the NES uses). Another ones is "6502 Assembly Language Programming" by Lance A. Leventhal.
I have the book by David Eyes and Ron Lichty. But I don't have Rodnay Zaks nor Lance A. Leventhal version.

Sadly, I also do not understand or read well with the books from it either. So I'm going to try pick something from Chapter 2 and read it:
The 6502 registers are:
· The accumulator, or A register, is the primary user register and generally holds one of the operands, as well
as the result, of any of the basic data-manipulation instructions.
· The X and Y index registers are used chiefly in forming effective addresses for memory accesses and as loop
counters.
· The processor status, or P, register contains bit-fields to indicate various conditions, modes, and results
within the processor.
· The stack pointer, or S register, is a pointer to the next available location on the system stack, a special area
of memory for temporary data storage. In addition to being available to the user, the stack pointer and stack
are also used automatically every time a subroutine is called or an interrupt occurs to store return information.
· Finally, the program counter, or PC, is a pointer to the memory location of the instruction to be executed
next.
I don't know if Processor status is used in NES, and program counter in NES. But I do know Stack Pointer is in NES in anyway. So it's a Temporary Data Storage, and the way to use it is...???

DocWaluigean
Posts: 205
Joined: Sun Jun 17, 2018 6:41 pm

Re: Hello, I'm back, still need assistences

Post by DocWaluigean » Sat Jun 13, 2020 12:50 pm

I'm going to just say that, I understand the basics of A, X, Y, and other things, but certain wording makes it difficult for me to understand. Although questions may be repeated, PLEASE simple answers, so it can help me:

The Zero Flag: It turns on if the A, X, Y, or S Register has zero? It turns on if ONLY The A Register is turned on? Or if the destination of Address is holding zero right now?

The Carry Flag: It turns on if ONLY the A Register got overflow [over 255] and revert back to zero, right? Or it can be also X, Y, and S? Or also the Address?

The Negative Flag: It turns on [or set] if the A Register got backward? Or it includes X, Y, and S? I don't know Stack Pointers yet. Does it include Address if the number stored is backward?

Jump:
LDA #0 - Load 0 into A.
STA $0050 - Store into Address $50.

LDA #$09 - Put HEX number 9 into A.
SBC #$0A - Subtract 10 [HEX number A] into Register A.



;;; The A Register is now carrying HEX number FF, and the Negative Flag is turned on.

STA $0100 - Put HEX number FF into A. - At ANY point the code goes to here, the Negative Flag is turned on.

Front:
LDA #10 - Since it's normal number, the Negative Flag is turned off.
STA $0101 - Store number 10 into Register. $101 ; The Negative Flag is STILL turned off.

.....No wait

Only if the A Register is carrying a backwards number, the Negative Flag will be turn on. BUT if the new number replaces it in A Register, the Negative Flag gets turned off? Or it stays on?
Does it affect with X, Y, and S Register also?


I have to explain it some how,
because if I was 15 years old right now, and learning about it now, I will never understand anything with many different words, as I have to know how to use the new words from the dictionary, and have to "translate" it so I could understand a sentences that means a very simple actions. I'm 24 now and I still struggle with the grammars and words like those as I don't use those in everyday life.

DocWaluigean
Posts: 205
Joined: Sun Jun 17, 2018 6:41 pm

Re: Hello, I'm back, still need assistences

Post by DocWaluigean » Sat Jun 13, 2020 12:56 pm

I'm going to try reading Nerdy Nights now, but I don't know if there's a better tool to use than ASM3 they provided. I know MESEN is a better choice than that.

User avatar
dougeff
Posts: 2739
Joined: Fri May 08, 2015 7:17 pm
Location: DIGDUG
Contact:

Re: Hello, I'm back, still need assistences

Post by dougeff » Sat Jun 13, 2020 1:05 pm

I personally don't like NESASM (or NESASM3), because of a few little things, like a strict $2000 byte bank system.

But it will be fine to use while you do the tutorial. It's not difficult to switch to another assembler later on.
nesdoug.com -- blog/tutorial on programming for the NES

DocWaluigean
Posts: 205
Joined: Sun Jun 17, 2018 6:41 pm

Re: Hello, I'm back, still need assistences

Post by DocWaluigean » Sat Jun 13, 2020 1:12 pm

Common Comparison opcodes
CMP #$01 ; CoMPare A to the value $01
; this actually does a subtract, but does not keep the result
; instead you check the status register to check for equal,
; less than, or greater than
This one is not true about subtract.
CMP - Compare
Z,C,N = A-M

This instruction compares the contents of the accumulator with another memory held value and sets the zero and carry flags as appropriate.
This one is correct to me. It's one of reasons why I don't trust Nerdy Nights about misinformation. How does this subtract, if SBC exist?


---
BEQ $FF00 ; Branch if EQual, contnue running code there
; first you would do a CMP, which clears or sets the zero flag
; then the BEQ will check the zero flag
; if zero is set (values were equal) the code jumps to $FF00 and runs there
; if zero is clear (values not equal) there is no jump, runs next instruction
I understand it now, but it needs more examples of this.
.inesprg 1 ; 1x 16KB bank of PRG code
.ineschr 1 ; 1x 8KB bank of CHR data
.inesmap 0 ; mapper 0 = NROM, no bank swapping
.inesmir 1 ; background mirroring (ignore for now)
Now this, I don't understand the usage of it. I don't think .ines is part of original codings, like official Nintendo games doesn't have .ines?

===========
I personally don't like NESASM (or NESASM3), because of a few little things, like a strict $2000 byte bank system.

But it will be fine to use while you do the tutorial. It's not difficult to switch to another assembler later on.
Indeed, like I want the best tools available, like NESASM6 or NESASM7 now?

I guess I'll stick with what they got before I'm ready for better tools.

Post Reply