Lukas
Lukas

Reputation: 5

Incrementing the first two bytes of an address assembly MOS6502

here i have a problem since the registers are only 8 bits i cannot store a 16 bit address so i have to divide it in two bytes es:

addr: %4300 will be divided as little endian

high byte: 43

low byte : 00

the problem is that i can't increment the high byte of the address but only the low byte using a simple INC instruction.

for example:

LDA $4300
ADC #01
STA %4300

EDIT:

i want to increment the memory address that is $4300 but i just want to increment the first two bytes so the high byte, i don't care to write a value for that address

example:

LDA #$4300
ADC #1

; the result i want should be $4400 and so on..

how can i solve that?

thanks!

Upvotes: 0

Views: 1376

Answers (1)

Jeff Alyanak
Jeff Alyanak

Reputation: 334

If you want to increment or alter the value of an address - or any piece of data - then you need to know the address of that address.

That might sound a bit confusing at first glance, but keep in mind that everything that the CPU is working with is either in its memory space or inside a register.

That includes every instruction and value that your compiler spits out. So to increment the high byte of your address ($4300) you have to know where that data actually is.

One more thing to know, the 6502 is 'little-endian' so instructions read the low byte first and then the high byte. So in memory, your address $4300 will actually be a $00 followed by a $43.

Now, there are many different ways to accomplish what you're aiming to do, but here is a simple example:

cool_address:   .res 2  ; We reserve 2 bytes somewhere in RAM
                        ; and give it the label cool_address
                        ; so we can easily access it.
...

LDA #$00                ; Put $00 into the Accumulator
STA cool_address        ; Store the Accumulator in the 1st byte of address
LDA #$43                ; Put $43 into the Accumulator
STA cool_address+1      ; Store the Accumulator in the 2nd byte of address

                        ; At this point, the 2 bytes at cool_address are
                        ; 00 43

INC cool_address+1      ; Increment 2nd byte of address


                        ; At this point, the 2 bytes at cool_address are
                        ; 00 44

The label cool_address can now be given to any instruction that takes an address and the instruction will be operating on address $4400.

Upvotes: 1

Related Questions