Page 1 of 1

ASM and parameters

Posted: Sun May 07, 2006 11:04 pm
by eriansa
From the manual :
The available volatile registers are: eax, edx and ecx. All others must be always preserved

I am not using inline asm, but direct asm.

In PB394 I always referenced parameters by [ESP+...]
In PB400 The new way is [p.v_parm]

I need to preserve EBP,EBX,EDI since I need to use those registers in my Asm procedure.

Example :

Code: Select all

Procedure.l MyProc(a.l,b.l,c.l,d.l,e.l,f.l)
  !MOV Eax, [p.v_a]
  !MOV Ecx, [p.v_b]
  !MOV Edx, [p.v_c]
  ;Now I want to move the other parms to registers which I have to reserve first
  ;How????
  ProcedureReturn
EndProcedure

a.l=1:b.l=1:c.l=1:d.l=1:e.l=1:f.l=1
a=MyProc(a,b,c,d,e,f)
Debug a
Is there any problem in the future if I do it this way :

Code: Select all

Procedure.l MyProc(a.l,b.l,c.l,d.l,e.l,f.l)
  !PUSH Ebp
  !PUSH Edi
  !PUSH Ebx
  
  !MOV Eax, [p.v_a+12]
  !MOV Ecx, [p.v_b+12]
  !MOV Edx, [p.v_c+12]
  ;...
  !POP Ebx
  !POP Edi
  !POP Ebp
  ProcedureReturn
EndProcedure

a.l=1:b.l=1:c.l=1:d.l=1:e.l=1:f.l=1
a=MyProc(a,b,c,d,e,f)
Debug a

Posted: Mon May 08, 2006 11:50 am
by Trond
No, but remember to put +12, and PB code using local variables won't work inside the block.

Posted: Mon May 08, 2006 1:20 pm
by Psychophanta
Or u can do this way:

Code: Select all

Procedure.l MyProc(a.l,b.l,c.l,d.l,e.l,f.l) 
  !PUSH Ebp Edi Ebx 
  !add esp,12
  ;...
  !MOV Eax, [p.v_a] 
  !MOV Ecx, [p.v_b] 
  !MOV Edx, [p.v_c] 
  Debug a+b+c+d+e+f
  ;... 
  !sub esp,12
  !POP Ebx Edi Ebp 
  ProcedureReturn a
EndProcedure 

a.l=1:b.l=2:c.l=3:d.l=4:e.l=5:f.l=6 
a=MyProc(a,b,c,d,e,f) 
Debug a

Posted: Mon May 08, 2006 1:46 pm
by Trond
That can be dangerous.

Code: Select all

Procedure.l MyProc(a.l,b.l,c.l,d.l,e.l,f.l) 
  !push ebp edi ebx 
  !add esp, 12

  I = 0 ; Oups, ebp, ebx and edi is gone!
  
  !sub esp,12 
  !pop ebx edi ebp 
  ProcedureReturn a 
EndProcedure 

Posted: Mon May 08, 2006 5:08 pm
by Fred
The '!MOV Eax, [p.v_a+12]' way is the prefered one and should work with any future version.

Posted: Mon May 08, 2006 5:32 pm
by josku_x
THanks for the info Fred

Posted: Sun Jun 03, 2007 12:48 pm
by gebe
This post might be stupid after the real specialist has spoken.
Please treat it more as a question than anything else...

For what it is worth: (I am far ,far from beeing a specialist).
I tried the following to be able to follow up the stack easily.
I cannot stop in the debugger with the < !code >
(correct ? Or have I missed something as usual ?).

Code: Select all

;This time I use the inline ASM
;do not forget to select in CONFIG compiler
;This way one can follow perfectly with the debugger
;I dont seem to stop in the debugger in pure ASM (with <!>)
;with <in line> OK ,debug stops on break points
Procedure StrL(l.l,p.l) 
;here p.l is a dummy as it is not used by the code
; but it was necessary to pass the variable on the stack
 
Debug "@st   "+Hex(p) 
Debug "l.l   "+Hex(l) 
;maybe there is another way ?
   :?: 
  
  MOV eax,l 
 ;in LIST.ASM p.v_p is declared as an EQUATE    
 ; esp+pso+4 (return pushed +4 ? what is pso ?)I dont know 
   ;PUSH edi;uncomment to save edi 
   MOV edi,[esp+8];dword[p.v_p+4];the pointer value    
  ;mov edi,[esp+12];uncomment if edi is pushed 
  ;to compensate for the push
  ;I traced the positions with the stack in the debugger
  
  
  
       MOV dl ,255     ;end mark 
      PUSH edx      ;save<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
      MOV ecx ,10   ;the divider 
! x0: 
      MOV edx ,0 
       DIV ecx       ; divide eax by 10 
        PUSH edx    ; save the rest <<<<<<<<<<<<<<<<<<<<<<< 
         CMP eax ,0; result of division 
          JA x0     ;  eax = 0 ? finished ? 
!x2: 
          POP eax   ;get results of divisions>>>>>>>>>>>> 
          CMP al ,255   ;until end mark (result order reversed ) 
                    JE x9 
          ADD al ,'0' 
          STOSB 
          JMP x2 
! x9:;! are needed for the labels 
          MOV al,0; 
          STOSB    ; put a zero for end string mark 
   ;uncomment if edi is save
   ; POP edi  ;   restore edi>>>>>>>>>>>>>>>>>>>>>>>>>  
EndProcedure 

;ALLOCATION of spce 
st.s="00000000000000" 
Debug "Before conversion   :  " +st 
b.l=19191919;here the  number to convert****************** 
 Debug @st    
Strl(b,@st); 

Debug " Conversion Ended   :  "+ st 
End 


; Reading from debug output
; Before conversion   :  00000000000000
; 9307752
; @st   8E0668
; l.l   124D86F
;  Conversion Ended   :  19191919


;gebe :) 
:?: