Address of variable

General FreeBASIC programming questions.
basic4fun
Posts: 3
Joined: Oct 26, 2015 18:34

Address of variable

Postby basic4fun » Oct 26, 2015 19:06

I should be very grateful if someone could explain to me why the addresses printed by the following code are not identical. On my machine the first address printed is 1244956 and the second is 1244968. I believe I'm making a foolish mistake but just can't see it.

Code: Select all

dim a as integer
dim b as byte

cls

print @b ' Print address of "b "

asm
 mov [a],b ' Move address of "b" into "a" then print outside ASM block ?
end asm

print ""
print a ' Print address of "b"

sleep
venom
Posts: 16
Joined: Apr 03, 2015 20:55
Location: Germany

Re: Address of variable

Postby venom » Oct 26, 2015 19:55

Are you sure your code even compiles?
r.asm: Assembler messages:
r.asm:31: Error: invalid use of register


Afaik the resulting "mov [ebp-8],ebp-12 " is not valid assembly.

/edit:
My assembly-skills are a bit rusty, but how about this:

Code: Select all

    dim a as integer
    dim b as byte

    cls

    print @b ' Print address of "b "

    asm
     lea ebx, [b]
     mov [a],ebx ' Move address of "b" into "a" then print outside ASM block ?
    end asm

    print ""
    print a ' Print address of "b"

    sleep
basic4fun
Posts: 3
Joined: Oct 26, 2015 18:34

Re: Address of variable

Postby basic4fun » Oct 26, 2015 20:18

Many thanks for taking the time to reply. That works and I can now see my mistake.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: Address of variable

Postby MichaelW » Oct 27, 2015 2:18

For local (stack based) variable references in inline assembly the compiler substitutes an expression of the form ebp-N, which unless it is enclosed in square brackets is not a valid memory operand. And for an instruction with two operands, only one can be a memory operand.

Code: Select all

dim a as integer
dim b as byte
print @b
asm
    nop
    lea eax, [b]
    mov [a], eax
    nop
end asm   
print a
sleep

And from the assembly output:

Code: Select all

nop
lea eax, [ebp-12]
mov [ebp-8], eax
nop
basic4fun
Posts: 3
Joined: Oct 26, 2015 18:34

Re: Address of variable

Postby basic4fun » Oct 28, 2015 9:12

Thanks MichaelW and again venom. Both very helpful. I can now see why venom was surprised that my example code compiled at all.

Return to “General”

Who is online

Users browsing this forum: No registered users and 4 guests