defining memory space

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

Post Reply
DedGzus
Posts: 11
Joined: Sat Jan 07, 2023 10:11 am

defining memory space

Post by DedGzus »

I'm trying to define my memory space for all of my variables in the following way currently.

RAM8_0 = $0300
RAM8_1 = $0300
RAM16_0 = $0310
...ettc

later in code I'll do something like

offset_counter = RAM8_0
byte_count = RAM8_1
index_ptr = RAM16_0

My problem is when I want to define a 2 byte variable for use as an indirect pointer I get an overflow error because the instruction doesn't know to treat the variable as 2 bytes. If i define the variable in the following way it works.

index_ptr: .res 2

My question is how do I assign a specific address for index_ptr AND let the compiler know to treat it as a two byte address. Thank you.
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: defining memory space

Post by lidnariq »

On 6502, pointers can only be in zeropage.

(Exception: pointers for indirect jumps)
unregistered
Posts: 1318
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Re: defining memory space

Post by unregistered »

ummm… well,

(I’m using asm6; whatever I write should be translatable, I think, into a form for your assembler. Though, you are using a compiler; so, I’m honestly unsure how 6502 math works in a compiler.)


Pointers can be used to point to any location; though, they usually must sit in zero page so that:

Code: Select all

lda (ptr1), y
works; that instruction is always assembled into 2 bytes. The first byte becomes $B1 and the second byte always addresses a zero page address bc it’s just 1 byte.


Symbols, assigned with =, can hold 16bit values, like you’ve written. However, most all variables on the 6502 are byte size. The symbols aren’t variables; and so, they CAN’T be written to during the game. They are sections of memory in your CPU that you’ve assigned for use only during your game’s assembly. i.e.:

Code: Select all

RAM8_0 = $0300

lda #18
sta RAM8_0 ;<Error

The assembler, for the most part, uses 8bit addresses exclusively. Therefore, both bytes of a 16bit variable need to be accessed during an addition. i.e.:

Code: Select all

clc  ;clears carry
lda low_byte
adc #49  ;this instruction may set the carry
sta low_byte
lda high_byte
adc #00 ;is added to the accumulator with value of carry
sta high_byte

6502 assemblers’ variables have to be accessed in a byte-manner, like above. And since a nes compiler translates written high level language into 6502 assembly, I believe there isn’t a way to use actual 16bit variables, in a 16bit form, when writing high level language for the NES.

I’m clearly not certain of that; I’ve always used assembly bc a compiler’s translation looses speed reduces the speed of your code and it’s not a direct way to access the NES’es 2A03 (or 6502). :)
Post Reply