This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

Noob question about lda and lda #

Noob question about lda and lda #
by on (#225851)
Hello to everyone,
I've another noob question on ASM 6052. I can't really understand the difference between LDA VARIABLE and LDA #VARIABLE. To be more precise:

I've a variable:

EM_ENEMY_1_STATE .rs 1

and a constant:

PLAYER_STATUS_WALK = $0001

I know that if I do LDA #EM_ENEMY_1_STATE it load the ADDRESS into accumulator, if I do LDA EM_ENEMY_1_STATE it load the VALUE. And that's ok, but, I i try to load a Constant and put the value into a variable, the variable is always 0000.

lda PLAYER_STATUS_WALK
sta EM_ENEMY_1_STATE


EM_ENEMY_1_STATE == 00

lda #PLAYER_STATUS_WALK
sta EM_ENEMY_1_STATE

EM_ENEMY_1_STATE == 01

But, that's the value or the address? I can't understand why in the first case is 0 and in the second is 01.

Thanks a lot for any response! :)
Re: Noob question about lda and lda #
by on (#225852)
When you .rs 1, you are naming a number. When you "= $0001" you are also naming a number.

EM_ENEMY_1_STATE is a number. PLAYER_STATUS_WALK is a number. They are the same type of thing, even if you have defined them differently.

The 6502 can use numbers 2 ways. As an address, or as an "immediate" value. If you want to use the number as an immediate value, you precede it with a '#' symbol. If you want to use the number as an address, you don't.

Code:
lda PLAYER_STATUS_WALK;Use $0001 as an address. So A gets the value from address $0001
lda #PLAYER_STATUS_WALK;Use $0001 as an immediate value. So A gets the value 1.

lda EM_ENEMY_1_STATE;Use... whatever number this ends up getting as an address. So A gets the value from that address.
lda #EM_ENEMY_1_STATE;Use... whatever number this ends up getting as an immediate value. So A gets whatever value that number is.

The assembler doesn't know that when you use ".rs" you want variables and when you use "= $????" you want constants, so if you want "PLAYER_STATUS_WALK" to be used as a constant you have to precede it with a '#'.

If you want EM_ENEMY_1_STATE to be used as a variable you must NOT precede it with a '#'.

The '#' is the only thing that makes a difference. (At least for what you've posted.)

Edit: So the reason EM_ENEMY_1_STATE ends up as zero in the first example is because at RAM location PLAYER_STATUS_WALK ($0001) was the value #$00 when you loaded it, and then this value (#$00) was stored to EM_ENEMY_1_STATE
Re: Noob question about lda and lda #
by on (#225855)
Thanks a lot!
Re: Noob question about lda and lda #
by on (#225862)
Code:
    SMALL_NUMBER = $12
    BIG_NUMBER = $3456

SOME_ADDRESS: .res 1

    lda #SMALL_NUMBER       ; SMALL_NUMBER as number
    lda SMALL_NUMBER        ; SMALL_NUMBER as address (in zero page)
    lda (SMALL_NUMBER, x)   ; \ SMALL_NUMBER as address of an address
    lda (SMALL_NUMBER), y   ; / (only possible in zero page)

    lda #<BIG_NUMBER        ; Least significant byte of BIG_NUMBER (A = $56)
    lda #>BIG_NUMBER        ; Most significant byte of BIG_NUMBER (A = $34)
    lda #<SOME_ADDRESS      ; Least significant byte of SOME_ADDRESS (as number)
    lda #>SOME_ADDRESS      ; Most significant byte of SOME_ADDRESS (as number)
                            ; Note: the > and < syntax is assembler specific
    lda SOME_ADDRESS        ; SOME_ADDRESS as address