News:

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

Main Menu

masm 10 vs 11

Started by bomz, January 22, 2013, 09:45:06 AM

Previous topic - Next topic

bomz

http://www.c-jump.com/CIS77/ASM/Procedures/lecture.html#P77_0130_fastcall_call_summary
Quote13. FASTCALL Caller and Callee Summary

    *

      The following sample illustrates the code generated in the calling function and in the called function to support __fastcall, the fastcall calling convention:

         int __fastcall FastFunc( int a, int b );

            calling function    called function
            -------------------------------------------
            mov edx, b          @FastFunc@8 PROC NEAR
            mov ecx, a                       .
            call @FastFunc@8                 .
             .                               .
             .                              RET 8
             .                  @FastFunc@8 ENDP
RET 8 - so stack not destroyed but parametres need ecx edx

dedndave

that doesn't seem correct
perhaps it's a misprint
see if you can find another source of information to verify

bomz

I make code with syscall proc it's work like stdcall function because use ret [N], but I can't export my proc from it to see it's end @8 or @12

mineiro

Hello bomz;
from my readings about calling convention, I have see that fastcall differs from one operating system to another, and maybe from different compilers. The fastcall to win64 is not the same as fastcall to linux64, both use fastcall, linux64 use 6 registers to pass parameters, while windows64 use 4 registers. A good read about this is the paper of agner. Well, these calling conventions are a big zoo, it's hard to create portable code because these things, and exceptions handlers change too from win32 to win64, ... .
http://www.agner.org/optimize/calling_conventions.pdf

bomz




The very last version of Alex script here. support masm32, protogen and protoize includes. syscal
awk here
http://gnuwin32.sourceforge.net/downlinks/gawk-bin-zip.php

Vortex


MichaelW


#include <conio.h>
#include <stdio.h>
int __fastcall fastcall( int a, int b )
{
    return a+b;               // line 5
}
// This necessary to prevent compiler from
// optimizing the call by eliminating it:
#pragma optimize( "", off )
void main( void )
{
    int i, a=1, b=2;
    i = fastcall( a, b );     // line 13
    getch();
}


. . .
@fastcall@8 PROC NEAR ; COMDAT
; _a$ = ecx
; _b$ = edx
; File c:\program files\microsoft visual c++ toolkit 2003\my\newcounter\fastcall.c
; Line 5
lea eax, DWORD PTR [ecx+edx]
; Line 6
ret 0
@fastcall@8 ENDP
. . .
_main PROC NEAR ; COMDAT
; Line 11
push ebp
mov ebp, esp
sub esp, 12 ; 0000000cH
; Line 12
mov DWORD PTR _a$[ebp], 1
mov DWORD PTR _b$[ebp], 2
; Line 13
mov edx, DWORD PTR _b$[ebp]
mov ecx, DWORD PTR _a$[ebp]
call @fastcall@8
mov DWORD PTR _i$[ebp], eax
; Line 14
call _getch
; Line 15
xor eax, eax
mov esp, ebp
pop ebp
ret 0
_main ENDP
. . .


In optimized code, and in procedures that have no more than 2 parameters, using the fastcall convention saves a few cycles beyond what can be saved by eliminating the stack frame (as in a naked function). For the 32-bit compilers the Microsoft fastcall convention passed the first 2 parameters in registers. Borland at one time implemented a fastcall convention that passed the first 3 parameters in registers.
Well Microsoft, here's another nice mess you've gotten us into.

bomz

I don't know C absolutely. I learn at school basic for Hewlett Pacard

Quoteexterndef syscall @procname@16:proc = procname@16 proto syscall param1:dword,param2:dword,param3:dword,param4:dword
???


Quote from: Vortex on January 31, 2013, 05:30:47 AM
Hi bomz,

Nice work :t
Thanks for Alex. compatible with three ml.exe v8 and two ml.exe v10 + link.exe two version. new non-batch (awk) algorithm much quickly

bomz

Four-F KMDKits author send me a letter. He about 10 year not practice with masm and don't remember any details

bomz

selling the purchase
http://exelab.ru/f/index.php?action=vthread&forum=6&topic=15165&page=0

MASM compiller not support FASTCALL(SYSCALL)  (?no errors occurs?), this functions decorated with @
the decision by Four-F
QuoteEXTERNDEF SYSCALL @InterlockedCompareExchange@12:PROC
InterlockedCompareExchange EQU <@InterlockedCompareExchange@12>
fastcall macro included in ntddk.inc from KMDKit last version (?)
fastcall macro finvoke.asm

fcinv MACRO algo, a1, a2, a3, a4, a5, a6
LOCAL invAlgo$, out$, Description, movreg1, movreg2, movreg3
  invAlgo$ CATSTR <invoke >, <algo>
  ifidn <?>, <algo>
include \masm32\macros\macrohlp.inc
showhelp fcinv
invAlgo$ equ <>
  endif
  movreg1 equ <>
  movreg2 equ <>
  movreg3 equ <>
  ifnb <a1>
movreg1 equ CrtMovReg(a1)
ifb movreg1
invAlgo$ CATSTR invAlgo$, <, >, <a1>
endif
  ifnb <a2>
movreg2 equ CrtMovReg(a2)
ifb movreg2
invAlgo$ CATSTR invAlgo$, <, >, <a2>
endif
  ifnb <a3>
movreg3 equ CrtMovReg(a3)
ifb movreg3
invAlgo$ CATSTR invAlgo$, <, >, <a3>
endif
  ifnb <a4>
invAlgo$ CATSTR invAlgo$, <, >, <a4>
  ifnb <a5>
invAlgo$ CATSTR invAlgo$, <, >, <a5>
  endif
  endif
  endif
  endif
  endif
  movreg1 ;; mov eax, offset arg1 (for example)
  movreg2 ;; mov ecx, offset arg2
  movreg3 ;; mov edx, offset arg3
  ; .err
;;   % echo invAlgo$ ; uncomment in case of problems
  invAlgo$ ;; invoke MyAlgo, arg4, arg5, arg6
ENDM

; Usage:
; showcycles InString, 1, addr MainString, addr SubString
; showcycles FcDemo2Regs, ecx:82, edx:offset MainString, addr SubString ; the FASTCALL variant

CrtMovReg MACRO arg
LOCAL src$, ado$, var$, sc
  if @InStr(1, <arg>, <:>) eq 4
echo A=<arg>
src$ SUBSTR <arg>, 5
% echo S=src$
  % sc SIZESTR <src$>
if sc gt 5
; echo GREATER
ado$ SUBSTR src$, 1, 4
; % echo AD=<ado$>
ifidni ado$, <addr>
var$ SUBSTR src$, 6
; % echo Local=var$
oa = (opattr var$) AND 127
if oa eq 98
MovReg$ CATSTR <lea >, @SubStr(<arg>, 1, 3), <, >, var$
; % echo CM=MovReg$
else
MovReg$ CATSTR <mov >, @SubStr(<arg>, 1, 3), <, offset >, var$
endif
else
MovReg$ CATSTR <mov >, @SubStr(<arg>, 1, 3), <, >, @SubStr(<arg>, 5)
endif
else
; echo smaller
MovReg$ CATSTR <mov >, @SubStr(<arg>, 1, 3), <, >, @SubStr(<arg>, 5)
endif
; % echo MR=MovReg$
; .err
EXITM <MovReg$>
  else
EXITM <>
  endif
ENDM

showcycles MACRO algo, a1, a2, a3, a4, a5, a6
LOCAL invAlgo$, out$, Description, movreg1, movreg2, movreg3
  invAlgo$ CATSTR <invoke >, <algo>
  ifidn <?>, <algo>
include \masm32\macros\macrohlp.inc ;; [url=\masm32\include\macrohlp.inc]open[/url]
showhelp showcycles
invAlgo$ equ <>
  endif
  movreg1 equ <>
  movreg2 equ <>
  movreg3 equ <>
  ifndef LOOP_COUNT
LOOP_COUNT = 1000000/10 ;; default: one Million
  endif
  ifndef CurAlgo
  CurAlgo equ <>
  endif
  ifdif CurAlgo, <algo>
  CurAlgo equ <algo>
  endif

  Description equ <CurAlgo>
  ifnb <a1>
  movreg1 equ CrtMovReg(a1)
ifb movreg1
invAlgo$ CATSTR invAlgo$, <, >, <a1>
endif
Description CATSTR Description, <, >, <a1>

  ifnb <a2>
  movreg2 equ CrtMovReg(a2)
ifb movreg2
invAlgo$ CATSTR invAlgo$, <, >, <a2>
endif
Description CATSTR Description, <, >, <a2>
  ifnb <a3>
  movreg3 equ CrtMovReg(a3)
ifb movreg3
invAlgo$ CATSTR invAlgo$, <, >, <a3>
endif
Description CATSTR Description, <, >, <a3>
  ifnb <a4>
invAlgo$ CATSTR invAlgo$, <, >, <a4>
Description CATSTR Description, <, >, <a4>
  ifnb <a5>
invAlgo$ CATSTR invAlgo$, <, >, <a5>
Description CATSTR Description, <, >, <a5>
  endif
  endif
  endif
  endif
  endif
  ifndef globcycles ;; we might need to calculate a global average count
  .data?
  globcycles dd ?
  globcount dd ?
  .code
  endif
  ifndef REP_CT
REP_CT = 1
  endif
  ifndef REP_CTold
REP_CTold = REP_CT
  endif
  if REP_CT gt 1
  if REP_CTold eq 1
  pushad
print chr$(13, 10)
popad
endif
REP_CTold = REP_CT
  endif
  ;% echo invAlgo$ ;; uncomment to see what exactly you are invoking
  REPEAT REP_CT
pushad
invoke Sleep, 50
popad
counter_begin LOOP_COUNT, HIGH_PRIORITY_CLASS
movreg1
movreg2
movreg3
invAlgo$
counter_end
add globcycles, eax
inc globcount
out$ CATSTR <print str$(eax), 9, ">, Description, <", 13, 10>
out$
  ENDM
  if REP_CT gt 1
print chr$(13, 10)
  endif
ENDM

may be add it to masm32.lib (?), but it redefinition:
atol
isupper
islower
isalpha

such macros turn fastcall to slowcall
mov ecx, param1
sub esp, 4
call [fastcall]
????

dedndave


bomz

I leave first @ for mask function to prevent redefinition. fastcall=crazycall
Quote.386

.model flat, stdcall
option casemap :none

include \MASM32\INCLUDE\windows.inc
include user32.inc
include kernel32.inc
includelib user32.lib
includelib kernel32.lib
includelib ntdll.lib
include ntdll.inc
includelib ntoskrnl.lib
include ntoskrnl.inc

.data
mestitle   db "Bomz",0
form      db "EAX: %08hx", 0
form1      db "ECX: %04hx EDX: %04hx", 0
value      dd "A"

.data?
buffer      db 512 dup(?)

.code
start:
;mov eax, "a"
;mov eax, "A"
;invoke isupper, eax
mov ecx, 0ff00h
sub esp, 4
call @RtlUshortByteSwap
invoke wsprintf,ADDR buffer,ADDR form,eax
invoke MessageBox,0,ADDR buffer,ADDR mestitle,MB_ICONASTERISK
push 0ffh
push 0ffh
call @RtlUlonglongByteSwap
invoke wsprintf,ADDR buffer,ADDR form1,ecx, edx
invoke MessageBox,0,ADDR buffer,ADDR mestitle,MB_ICONASTERISK
invoke ExitProcess,0
end start