How to access a 'Static' variable with inline assembly?

Bare metal programming in PureBasic, for experienced users
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

How to access a 'Static' variable with inline assembly?

Post by Mijikai »

How can i change the content of 'Address' with inline assembly?

Code:

Code: Select all

;PB x64 v.5.62

Procedure.i TestFnc()
  Static Address.i
  !mov qword[p.v_Address],123;<- does not work for 'Static' !
  Debug Address
EndProcedure

TestFnc()
Fred
Administrator
Administrator
Posts: 16581
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: How to access a 'Static' variable with inline assembly?

Post by Fred »

If you want to look that, the best is to generate the assembly code with the commandline compiler (--commented switch) and look how it is named. You will save a lot of time when using inline ASM.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: How to access a 'Static' variable with inline assembly?

Post by Mijikai »

Fred wrote:If you want to look that, the best is to generate the assembly code with the commandline compiler (--commented switch) and look how it is named. You will save a lot of time when using inline ASM.
Thanks :)

Heres how it works:

Code: Select all

;-> so_PROCEDURENAME.v_VARIABLE
I found something im curious about:

If we have a Procedure that does not return a value PB does this:

Code: Select all

...
xor rax,rax
add rsp,28
ret
However if we return a value it looks like this:

Code: Select all

...
jmp @f
xor rax,rax
@@:
add rsp,28
ret 
why is the jmp & xor even in there ?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to access a 'Static' variable with inline assembly?

Post by wilbert »

Mijikai wrote:How can i change the content of 'Address' with inline assembly?
You could do it like this

Code: Select all

;PB x64 v.5.62

Procedure.i TestFnc()
  Static Address.i
  Protected *Address = @Address
  !mov rdx, [p.p_Address]
  !mov qword [rdx],123
  Debug Address
EndProcedure

TestFnc()
It does produce a bit of additional code and therefore is slightly slower but it does have the advantage that the reference to *Address (local variable) stays the same.
The reference to a static value changes when the procedure is included in a module.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: How to access a 'Static' variable with inline assembly?

Post by Mijikai »

wilbert wrote:...

Code: Select all

;PB x64 v.5.62
Procedure.i TestFnc()
  Static Address.i
  Protected *Address = @Address
  !mov rdx, [p.p_Address]
  !mov qword [rdx],123
  Debug Address
EndProcedure

TestFnc()
It does produce a bit of additional code and therefore is slightly slower but it does have the advantage that the reference to *Address (local variable) stays the same.
The reference to a static value changes when the procedure is included in a module.
Thanks, i didnt think of that way :)
User avatar
CELTIC88
Enthusiast
Enthusiast
Posts: 154
Joined: Thu Sep 17, 2015 3:39 pm

Re: How to access a 'Static' variable with inline assembly?

Post by CELTIC88 »

Mijikai wrote:...
I found something im curious about:

If we have a Procedure that does not return a value PB does this:

Code: Select all

...
xor rax,rax
add rsp,28
ret
However if we return a value it looks like this:

Code: Select all

...
jmp @f
xor rax,rax
@@:
add rsp,28
ret 
why is the jmp & xor even in there ?

so that "PB" can be adapted to any scenario
"is complex process!"
but you can optimize that.
interested in Cybersecurity..
Post Reply