Beginners ASM - accessing variables

Just starting out? Need help? Post your questions and find answers here.
zoot33
User
User
Posts: 69
Joined: Sat Jul 11, 2009 7:43 pm
Location: Oxford, UK

Beginners ASM - accessing variables

Post by zoot33 »

How to access variables direct and inline ASM. It confused me for a bit, I hope this helps someone.

Please correct anything if necessary. Thanks

Code: Select all

Global orange

Procedure abc()
  Protected apple
  Static strawberry
  Shared pear

  orange = 11
  pear = 21
  apple = 31
  strawberry = 41
  
  EnableASM
  
  MOV edx, orange
  INC edx
  MOV orange, edx
  
  MOV edx, pear
  INC edx
  MOV pear, edx
  
  MOV edx, apple
  INC edx
  MOV apple, edx
  
  MOV edx, strawberry
  INC edx
  MOV strawberry, edx
  
  PrintN("Inside Procedure abc()")
  PrintN("-------------------------")
  PrintN("Orange = " + Str(orange))
  PrintN("Pear = " + Str(pear))
  PrintN("Apple = " + Str(apple))
  PrintN("Strawberry = " + Str(strawberry))
  PrintN("-------------------------")
  PrintN("")
  PrintN("")
  DisableASM

EndProcedure

Procedure def()
  Protected apple
  Static strawberry
  Shared pear
  
  orange = 11
  pear = 21
  apple = 31
  strawberry = 41
  
  !MOV edx, dword [v_orange]
  !INC edx
  !MOV dword [v_orange], edx

  !MOV edx, dword [v_pear]
  !INC edx
  !MOV dword [v_pear], edx
  
  !MOV edx, dword [p.v_apple]
  !INC edx
  !MOV dword [p.v_apple], edx
  
  !MOV edx, dword [s_def.v_strawberry]
  !INC edx
  !MOV dword [s_def.v_strawberry], edx
  
  PrintN("Inside Procedure def()")
  PrintN("-------------------------")
  PrintN("Orange = " + Str(orange))
  PrintN("Pear = " + Str(pear))
  PrintN("Apple = " + Str(apple))
  PrintN("Strawberry = " + Str(strawberry))
  PrintN("-------------------------")
  
EndProcedure

orange = 11
OpenConsole()
abc()
def()
Repeat: Until Inkey()
CloseConsole()
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Post by idle »

I'm sure it will help but where's the banana. :wink:
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

perhaps a mention should be made as to the difference in accessing variables when using

Code: Select all

enableasm
....
disableasm
and using !
you should also show how to access parameters to a procedure.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

use a ProcedureReturn without a parameter to use the contents of EAX as the return value
just a minor addition blueznl, the above statement is true for returning a 32-bit integer, for Double or Float leave the result in st0
for quad, eax = low-part, edx = high-part

Code: Select all

Procedure.q t()
  eax.l
  edx.l
  x.q = $1111111122222222
  ! mov eax,[p.v_x]
  ! mov edx,[p.v_x+4]
  ! mov [p.v_eax],eax
  ! mov [p.v_edx],edx
  MessageRequester("", "eax (low)= $"+Hex(eax)+"   edx (high)= $"+Hex(edx), 0)
  ! mov eax,[p.v_x]
  ! mov edx,[p.v_x+4]
  ProcedureReturn
EndProcedure

MessageRequester("", "Quad = $"+Hex(t()), 0)
Last edited by jack on Tue Aug 18, 2009 3:36 am, edited 2 times in total.
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post by Ollivier »

@BlueZnl

Your link is excellent.

I found this:

Code: Select all

    $01         ; open fridge
    $02 03      ; take 3 bottles of beer
    $03         ; consume
    $03         ; consume
    $03         ; consume
I correct it:

Code: Select all

AtHome:
    $01         ; open fridge
    $05 08      ; is there more than 8 bottles?
    $06 08      ; no, go out
    $02 03      ; yes, take 3 bottles of beer
    $04         ; close fridge
    $03         ; consume
    $03         ; consume
    $03         ; consume
    $07         ; go to bed
Out:
    $08 24    ; buy 24 bottles
    $09 F1    ; go at home
Post Reply