Address of a variable: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 22:
MYDSECT DSECT
J DS F</lang>
=={{header|6502 Assembly}}==
When programming in assembly language yourself, generally the addresses of variables are known in advance and chosen by the programmer rather than dynamically determined by the compiler. Loading values from RAM into memory requires the programmer to specify the address as the operand. This is true for zero-page memory, absolute memory addresses, and memory-mapped ports whose location is defined by the hardware manufacturer. Typically, the assembly programmer will use labels to ease this process, which get converted to memory addresses automatically when the program is assembled.
 
<lang 6502asm>;;;;;;; zero page RAM memory addresses
CursorX equ $00
CursorY equ $01
temp equ $02
;;;;;;; memory-mapped hardware registers
BorderColor equ $D020
 
Joystick1 equ $DC01
Joystick2 equ $DC00</lang>
 
Rather than setting the address of a variable, in assembly the programmer stores a calculated quantity in an address. The terms "memory address" and "variable" are often used interchangably.
 
<lang 6502asm>LDA #$20 ; load a constant into the accumulator
 
CLC
ADC #$50
ASL
SBC #$30 ; do some math
 
STA temp ; store the result in a temp variable (really a zero-page memory address).
</lang>
 
 
 
=={{header|68000 Assembly}}==
When programming in assembly language yourself (i.e. not having a compiler do it for you), generally the addresses of variables are known in advance and chosen by the programmer rather than dynamically determined by the compiler. Therefore you pretty much always know a variable's address at all times.
 
<lang 68000devpac>UserRam equ $100000
Line 49 ⟶ 75:
 
=={{header|8086 Assembly}}==
When programming in assembly language yourself, (i.e.generally nota havingvariable's aaddress compileris dodetermined itin foradvance you), generallyby the addressesprogrammer. ofThe variables8086 arediffers knowncompared into advanceother andmachines chosenof byits theera, programmerwhere ratherthe thanactual dynamicallynumeric determinedvalue byof the compiler.address Thereforeis younot prettyneeded muchto alwaysget knowor aset variable'sthe address atof alla timesdesired variable. HoweverIn there isexchange, a little more work that needs to be done to getstore theor addressretrieve in 8086 Assembly comparedvalues to/from assembly for other machinesmemory.
{{works with|https://www.dosbox.com DOSBox}}
===Get The Address===
 
<lang asm>.model small
.stack 1024
Line 64 ⟶ 90:
mov ax, offset UserRam ;load into AX the address of UserRam
mov di, ax ;load that value into the destination index register</lang>
 
The actual numeric value of the address isn't needed, as the label system takes care of that for us. A memory dump routine can show the contents of <code>es:di</code>, displaying the actual memory location.
 
===Set The Address===
Like with other assembly languages, the terms "variable" and "memory address" are often interchangeable. Setting a variable to an address is just storing a value into memory. After the setup above has taken place, the programmer can store numbers into memory.
 
<lang asm>mov ax, 0FFFFh
mov [es:di],ax ;store 0xFFFF into the base address of UserRam.</lang>
 
Alternatively, this would also work:
<lang asm>.model small
.stack 1024
.data
UserRam 256 DUP (0) ;define the next 256 bytes as user RAM, initialize them all to zero.
 
.code
mov ax, @data ; load the data segment into ax
mov ds,ax ; point the data segment register to the data segment.
; (The 8086 can only load segment registers from ax, bx, cx, dx, or the pop command)
 
 
mov ax, 0FFFFh ; load the number 65535 (or -1 if you prefer) into ax.
mov word ptr [ds:UserRam] ; store this quantity in user ram.</lang>
 
 
=={{header|AArch64 Assembly}}==
1,489

edits