News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Macros for using the FASTCALL convention

Started by Vortex, September 30, 2018, 10:14:41 PM

Previous topic - Next topic

Vortex

Here are some macros to call FASTCALL functions in 32-bit Masm. fcall similar to the internal Masm macro invoke and FPROC designed for FASTCALL procedures.

.386
.model flat,stdcall
option casemap:none

include     \masm32\include\kernel32.inc
include     \masm32\include\user32.inc
include     \masm32\include\masm32.inc

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\user32.lib
includelib  \masm32\lib\masm32.lib

include     fcall.inc

.data

string1     db '%s 10 + 20 + 30 + 40 = %d',0
string2     db 'Addition operation :',13,10,0

.data?

buffer      db 64 dup(?)
string3     db 24 dup(?)

.code

start:

    fcall   strcpy,ADDR string3,ADDR string2

    fcall   sum,10,20,30,40
   
    invoke  wsprintf,ADDR buffer,\
            ADDR string1,ADDR string3,eax
           
    invoke  StdOut,ADDR buffer
    invoke  ExitProcess,0


FPROC strcpy,dest:DWORD,src:DWORD

; no need of a stack frame as the procedure
; takes only two parameters

    push    ebx
    push    ecx
    xor     ebx,ebx
@@:
    movzx   eax,BYTE PTR [edx+ebx]
    mov     BYTE PTR [ecx+ebx],al
    add     ebx,1
    test    eax,eax
    jnz     @b

    pop     eax
    pop     ebx
    ret

ENDFP strcpy    ;  = strcpy ENDP


FPROC sum,w:DWORD,x:DWORD,y:DWORD,z:DWORD

    mov     eax,w
    add     eax,x
    add     eax,y
    add     eax,z
    ret

ENDFP sum

   
END start

Vortex

- New FPROTO macro to declare functions.
- Correct decoration of the FASTCALL functions.

.386
.model flat,stdcall
option casemap:none

include     \masm32\include\kernel32.inc
include     \masm32\include\user32.inc
include     \masm32\include\masm32.inc

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\user32.lib
includelib  \masm32\lib\masm32.lib

include     fcall.inc

; FPROTO macro to declare FASTCALL functions

FPROTO      sum,:DWORD,:DWORD,:DWORD,:DWORD
FPROTO      strcpy,:DWORD,:DWORD

.data

string1     db '%s 10 + 20 + 30 + 40 = %d',0
string2     db 'Addition operation :',13,10,0

.data?

buffer      db 64 dup(?)
string3     db 24 dup(?)

.code

start:

    fcall   strcpy,ADDR string3,ADDR string2

    fcall   sum,10,20,30,40
   
    invoke  wsprintf,ADDR buffer,\
            ADDR string1,ADDR string3,eax
           
    invoke  StdOut,ADDR buffer
    invoke  ExitProcess,0
   
END start


.386
.model flat,syscall
option casemap:none

include fcall.inc

.code

FPROC strcpy,dest:DWORD,src:DWORD

; no need of a stack frame as the procedure
; takes only two parameters

    push    ebx
    push    ecx
    xor     ebx,ebx
@@:
    movzx   eax,BYTE PTR [edx+ebx]
    mov     BYTE PTR [ecx+ebx],al
    add     ebx,1
    test    eax,eax
    jnz     @b

    pop     eax
    pop     ebx
    ret

ENDFP strcpy


FPROC sum,w:DWORD,x:DWORD,y:DWORD,z:DWORD

    mov     eax,w
    add     eax,x
    add     eax,y
    add     eax,z

    fret    ; = ret 2*4
            ; = y and z removed from the stack
            ; fret calculates the number
            ; of the bytes to be removed
            ; from the stack

ENDFP sum

END