Address of a variable: Difference between revisions

Content added Content deleted
No edit summary
No edit summary
Line 22: Line 22:
MYDSECT DSECT
MYDSECT DSECT
J DS F</lang>
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}}==
=={{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.
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. Therefore you pretty much always know a variable's address at all times.


<lang 68000devpac>UserRam equ $100000
<lang 68000devpac>UserRam equ $100000
Line 49: Line 75:


=={{header|8086 Assembly}}==
=={{header|8086 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. However there is a little more work that needs to be done to get the address in 8086 Assembly compared to assembly for other machines.
When programming in assembly language yourself, generally a variable's address is determined in advance by the programmer. The 8086 differs compared to other machines of its era, where the actual numeric value of the address is not needed to get or set the address of a desired variable. In exchange, a little more work needs to be done to store or retrieve values to/from memory.
{{works with|https://www.dosbox.com DOSBox}}
{{works with|https://www.dosbox.com DOSBox}}
===Get The Address===

<lang asm>.model small
<lang asm>.model small
.stack 1024
.stack 1024
Line 64: Line 90:
mov ax, offset UserRam ;load into AX the address of UserRam
mov ax, offset UserRam ;load into AX the address of UserRam
mov di, ax ;load that value into the destination index register</lang>
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}}==
=={{header|AArch64 Assembly}}==