Jump to content

Variables: Difference between revisions

Line 64:
===Initialization===
This mostly depends on the hardware you are programming for. If the code runs in RAM, you can initialize an entire block of RAM to the value of choice with <code>ds <count>,<value></code> (replace count and value with numbers of your choice, no arrow brackets.) On systems like the NES which have dedicated RAM areas on the CPU and the whole program is in ROM, you're best off setting all RAM to zero on startup and initializing the ram to the desired values at runtime.
 
===Assignment===
Assignment is done by storing values into memory. Variables larger than 8 bits will need to be loaded in pieces.
<lang 6502asm>LDA #$05
STA Player1_Lives ;equivalent C code: Player1_Lives = 5;
 
LDA #$05
STA pointer+1
LDA #$40
STA pointer
;loads the variable pointer with the value $0540</lang>
 
===Datatypes===
The 6502 can only handle one data type natively: 8 bit. Anything larger will need to be represented by multiple memory locations. Since the CPU is little-endian the low byte should be "first," i.e. if you have a variable declared such as <code>pointer dsb 2</code> then the low byte must be stored in <code>pointer</code> and the high byte in <code>pointer+1</code>. (You do not declare <code>pointer+1</code> separately; the assembler will take <code>pointer+1</code> to mean the memory address directly after <code>pointer</code>. The 6502 uses consecutive zero-page memory locations as a means of indirect addressing.)
 
===Scope===
The 6502 has no concept of scope; every variable is global. Assemblers can enforce scope rules to allow the reuse of labels you would want to use frequently, like "skip," "continue," "done," etc., but normally each label can't be defined more than once in the same program. You can trick the assembler into (effectively although not technically) letting you re-use the same label with bank switching but for the most part each label must be unique.
 
===Referencing===
Labels are always defined at compile time. The assembler converts each label into the value or address it represents as part of the assembling process. The CPU doesn't know the labels even exist; labels are simply a convenience for the programmer. Once defined, a label can be used in place of an address or value.
 
<lang 6502asm>NametableBase equ $20 ;label referring to a constant
VRAM_ADDR equ $2006 ;label referring to a memory address
VRAM_DATA equ $2007 ;label referring to a memory address
 
LDA #NametableBase ;without a # this is interpreted as a memory address like any other number would be.
STA VRAM_ADDR
LDA #$00
STA VRAM_ADDR
 
LDA #$03
STA VRAM_DATA</lang>
 
=={{header|360 Assembly}}==
1,489

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.