Page 1 of 1

Beginners ASM - accessing variables

Posted: Mon Aug 17, 2009 4:18 pm
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()

Posted: Mon Aug 17, 2009 9:41 pm
by idle
I'm sure it will help but where's the banana. :wink:

Posted: Mon Aug 17, 2009 9:45 pm
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.

Posted: Mon Aug 17, 2009 10:18 pm
by blueznl

Posted: Mon Aug 17, 2009 11:38 pm
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)

Posted: Tue Aug 18, 2009 12:15 am
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