6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Mon Jul 08, 2024 7:27 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: variables using labels
PostPosted: Sun Oct 01, 2006 11:48 pm 
Offline

Joined: Sun Oct 01, 2006 11:41 pm
Posts: 1
hello

i am trying to make variables using labels. i know labels can be used to poitn to certain areas of memory.

so if i write

label1 lda #60

lda is now represented by the label. thus if i write lda label1 then the numerical value of lda will be loaded into acc.

how can i do this for any numerical vaule and not just those of the opcodes??


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Oct 02, 2006 12:25 am 
Offline

Joined: Sat Jan 04, 2003 10:03 pm
Posts: 1706
This depends on your assembler of choice. For ACME, you can do this:

Code:
myLabel: !by 0, 1, 2, ...
anotherLabel:
    !16 $DEAD, $BEEF, $FEED, $FACE


For ca65, the syntax differs somewhat:

Code:
myLabel: .byte 0, 1, 2, ...
anotherLabel:
    .word $DEAD, $BEEF, $FEED, $FACE


Still other assemblers have other syntaxes.

There is another method as well. Assuming that addresses are located at fixed addresses in memory, then you can use their addresses directly:

Code:
sprite0x = $D000
sprite0y = $D001


And, finally, there are offsets from various pointers -- if you have multiple records of the same type, these are quite useful. The syntax is generally the same as above, but instead of addresses, you can use offsets from the base address:

Code:
VICBASE = $D000

sprite0x = 0
sprite0y = 1
sprite1x = 2
sprite1y = 3
...


You can use the latter method like so:

Code:
  lda #<VICBASE
  sta ptr
  lda #>VICBASE
  sta ptr+1

  ldy #sprite1y
  lda (ptr),y
  ...


Hopefully this will help.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Oct 02, 2006 4:35 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8464
Location: Southern California
cezalinho,

Suppose the assembler's address pointer is at 1483 when it encounters "label1" in your text source code. Then your list code showing exactly what got assembled would show this line, with the address and the two bytes
Code:
1483   A9 3C

at the left end, since A9 is the op code for LDA immediate, and 3C is hex for 60. (That's pretty normal so far.) Then
Code:
        LDA   label1

(not immediate, but absolute, like you had it) would assemble AD 83 14, and when you run it, would read the A9 byte from address 1483 and put that A9 into the accumulator. An accumulator-load instruction using this absolute addressing of a piece of code instead of something like data or I/O would not be very useful.

If you do
Code:
        LDA   #label1

(note that it's immediate, represented byt the "#") then the assembler would grab the address, $1483, (not the contents of that address at run time) and try to assemble that address as the operand; but since the 6502 is limited to one-byte operands for immediate addressing, the high byte would be lost and you get A9 83, which would just put $83 in the accumulator, without ever looking to see what's at address $1483. This would not be useful very often either.

There's no problem using a label for a piece of program that starts with LDA# of course; but labels for variables will be a different usage. As kc5tja alluded to, there are lots of ways to address a variable or table. You would do well to read WDC's programming manual which is available for free download at http://www.westerndesigncenter.com/wdc/downloads.cfm by clicking on "Assembly Language Programming Manual for the W65C02 and W65C816" in the box labeled "Systems".

To address the simplest form, variables may have names (ie, labels) like FLIGHT_TIME, COUNTER, or anything else that follows the normal rules for labels in your assembler, followed by how many bytes of memory to reserve for the particular variable. For an example right out of one of my applicatons,
Code:
T2_PERIODS: BLKB 2  ; Free-run counter for time ref.
KEY_TIME:   BLKB 2  ; Record when to beep or begin repeating.
PTT_TIME:   BLKB 1  ; To know when to warn of likely PTT-stuck condx.

The BLKB above is the 2500AD assembler directive for advancing the address pointer so many bytes, in this case to leave two bytes for T2_PERIODS, two for KEY_TIME, and one for PTT_TIME. If you put these in page zero for quicker, denser code, and suppose T2_PERIODS is put at $004B, then T2_PERIODS can be a two-byte variable using addresses $004B and $004C, KEY_TIME can be a two-byte variable using $004D and $004E, and PTT_TIME can be a 1-byte variable using $004F, leaving $0050 as the next available byte.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: