Variable size/Set: Difference between revisions

m (→‎{{header|ERRE}}: fixed missing lang opener)
Line 34:
* (24 bits => 16 MB of storage)
</lang>
 
=={{header|6502 Assembly}}==
 
Syntax will vary depending on the assembler and whether your program will run in RAM or ROM.
For programs that execute from RAM such as those that are loaded from a disk, you can use the same syntax that you would use to define constants. When defining bytes as variables or constants, you do not prefix them with a # sign. Only in actual CPU instructions do you need to use a # to prevent the assembler from treating the numeric value as a memory location.
 
Since these are variables, the value given to them (in this case, 0) is the initial value, and can be changed later at runtime. If you don't care what the initial value is, some assemblers allow you to use a "?" where the 0s are.
 
<lang 6502asm>
MyByte:
byte 0 ;most assemblers will also accept DB or DFB
MyWord:
word 0 ;most assemblers will also accept DW or DFW
MyDouble:
dd 0</lang>
 
For programs that are executed solely from ROM, such as video game console cartridges, you won't be able to use the above method. The assembler can often use an <code>enum<code> or <code>rsset</code> directive to sequentially assign labels to a series of consecutive memory locations in the system's RAM.
 
<lang 6502asm>.rsset $00 ;starting at $0400, the following labels represent sequential memory locations of length ".rs n"
VBlankFlag .rs 1 ;$00
soft_PPUCTRL .rs 1 ;$01
soft_PPUSTATUS .rs 1 ;$02
soft_SCROLL_X .rs 1 ;$03
soft_SCROLL_Y .rs 1 ;$04
temp_16 .rs 2 ;$05,$06
tempStack .rs 1 ;$07</lang>
 
Assemblers that don't have an <code>enum<code> or <code>rsset</code> directive can use the <code>equ</code> directive instead. This method lets you immediately see what each memory location actually is, but it makes it harder to insert a new one without having to redo all the numbering. Certain 6502 instructions rely on two memory addresses being consecutive.
 
<lang 6502asm>VBlankFlag equ $00
soft_PPUCTRL equ $01
soft_PPUSTATUS equ $02
soft_SCROLL_X equ $03
soft_SCROLL_Y equ $04
temp_16 equ $05 ;you have to keep track of spacing yourself in this method
tempStack equ $07</lang>
 
While setting a variable's size is easy, getting it isn't possible without knowing it in advance. The CPU does not (and cannot) know the intended size of a variable. There's no enforcement of types whatsoever on the 6502; anything is fair game.
 
=={{header|68000 Assembly}}==
{{trans|6502 Assembly}}
Syntax will vary depending on the assembler and whether your program will run in RAM or ROM.
For programs that execute from RAM such as those that are loaded from a disk, you can use the same syntax that you would use to define constants. When defining bytes as variables or constants, you do not prefix them with a # sign. Only in actual CPU instructions do you need to use a # to prevent the assembler from treating the numeric value as a memory location.
 
Since these are variables, the value given to them (in this case, 0) is the initial value, and can be changed later at runtime. If you don't care what the initial value is, some assemblers allow you to use a "?" where the 0s are.
 
<lang 68000devpac>MyByte:
DC.B 0
EVEN ;you need this to prevent alignment problems if you define an odd number of bytes.
MyWord:
DC.W 0 ;this takes up 2 bytes even though only one 0 was written
MyLong:
DC.L 0 ;this takes up 4 bytes even though only one 0 was written</lang>
 
For programs that are executed solely from ROM, such as video game console cartridges, you won't be able to use the above method. The assembler can often use an <code>enum<code> or <code>rsset</code> directive to sequentially assign labels to a series of consecutive memory locations in the system's RAM. Assemblers that don't have an <code>enum<code> or <code>rsset</code> directive can use the <code>equ</code> directive instead. This method lets you immediately see what each memory location actually is, but it makes it harder to insert a new one without having to redo all the numbering. These variables are located in the heap and thus there is no need to use <code>EVEN</code> directives to align this data.
 
<lang 68000devpac>
Cursor_X equ $100000 ;byte - only comments can tell you the intended variable size.
Cursor_Y equ $100001 ;byte
tempWord equ $100002 ;word - also occupies $100003
tempLong equ $100004 ;long - also occupies $100005,6,7</lang>
 
 
While setting a variable's size is easy, getting it isn't possible without knowing it in advance. The CPU does not (and cannot) know the intended size of a variable. There's no enforcement of types whatsoever on the 6502; anything is fair game.
 
=={{header|8086 Assembly}}==
1,489

edits