Author Topic: How compute size of masm procedure?  (Read 611 times)

morgot

  • Member
  • **
  • Posts: 62
How compute size of masm procedure?
« on: January 26, 2020, 01:33:08 AM »
Hello, i forgot, how to compute size of procedure. Something of $ - ..

example, i have

Code: [Select]
some proc uses ebx p1:dword
....
some code here..

ret
some endp

How i can programmly compute the size of this code?
Sorry for the bad English

jimg

  • Member
  • ***
  • Posts: 462
Re: How compute size of masm procedure?
« Reply #1 on: January 26, 2020, 02:09:18 AM »
startofproc = $
some proc uses ebx p1:dword
....
some code here..

ret
some endp

sizeofproc=$-startofproc

or

sizeofproc=$-some

daydreamer

  • Member
  • *****
  • Posts: 1313
  • building nextdoor
Re: How compute size of masm procedure?
« Reply #2 on: January 26, 2020, 02:16:46 AM »
Hello, i forgot, how to compute size of procedure. Something of $ - ..

example, i have

Code: [Select]
some proc uses ebx p1:dword
....
some code here..

ret
some endp

How i can programmly compute the size of this code?
Call from beginning of proc,that put eip on stack
Push that adress from stack that and save in variable1
Now that puts that adress on the stack
Call from end of proc,that puts eip on stack
Push that adress from stack and subtract with variable 1 and you get size

Quote from Flashdance
Nick  :  When you give up your dream, you die
*wears a flameproof asbestos suit*
Gone serverside programming p:  :D
I love assembly,because its legal to write
princess:lea eax,luke
:)

morgot

  • Member
  • **
  • Posts: 62
Re: How compute size of masm procedure?
« Reply #3 on: January 26, 2020, 02:48:21 AM »
Thank you friends , all works!
Sorry for the bad English

Vortex

  • Member
  • *****
  • Posts: 2266
Re: How compute size of masm procedure?
« Reply #4 on: January 26, 2020, 06:50:30 AM »
Obtaining the size of a procedure :

Code: [Select]
include     \masm32\include\masm32rt.inc

testproc    PROTO :DWORD,:DWORD,:DWORD

.data

string1     db 'The size of the procedure is %u bytes.'
            db 13,10,0

.data?

ProcSize    dd ?

.code

start:

    invoke  testproc,10,20,ADDR ProcSize

    invoke  crt_printf,ADDR string1,ProcSize

    invoke  ExitProcess,0

testproc PROC x:DWORD,y:DWORD,pSize:DWORD

LOCAL temp:DWORD

    mov     eax,OFFSET label1
    sub     eax,OFFSET testproc

    mov     edx,pSize
    mov     DWORD PTR [edx],eax

    mov     eax,x
    add     eax,y
    ret

testproc ENDP

label1:

    nop

END start

jj2007

  • Member
  • *****
  • Posts: 10439
  • Assembler is fun ;-)
    • MasmBasic
Re: How compute size of masm procedure?
« Reply #5 on: January 26, 2020, 07:49:59 AM »
Simplified:
Code: [Select]
include \masm32\include\masm32rt.inc

.code
testproc PROC x:DWORD,y:DWORD,pSize:DWORD
LOCAL temp:DWORD, whatever, buffer[120]:BYTE ; try buffer[121]:BYTE
  nops 90
  ret
testproc ENDP
testproc_endp:

ProcSize    dd testproc_endp-testproc

start:
print str$(ProcSize), " bytes long"
invoke  ExitProcess,0

END start

ML and UAsm yield slightly different sizes. ML uses good ol' leave+ret.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7423
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: How compute size of masm procedure?
« Reply #6 on: January 26, 2020, 09:51:24 AM »
Yep, 2 global labels, subtract the offset of the first from the second and you have the length in bytes of the code between the two labels.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 10439
  • Assembler is fun ;-)
    • MasmBasic
Re: How compute size of masm procedure?
« Reply #7 on: January 26, 2020, 02:01:59 PM »
It could even be simpler :badgrin:

include \Masm32\MasmBasic\Res\JBasic.inc ; ## builds in 32- or 64-bit mode with UAsm, ML, AsmC ##
.code

SayHi proc <cb> arg:SIZE_P
  Print Chr$("Assembled with ", @AsmUsed$(1), " in ", jbit$, "-bit format")
  PrintLine ComCtl32$(", using common controls version %i.%i")  ; see EndOfCode below
  MsgBox 0, arg, "Hi", MB_OK or MB_SETFOREGROUND
  ret
SayHi endp

Init           ; OPT_64 1      ; put 0 for 32 bit, 1 for 64 bit assembly
  Print Str$("The proc 'SayHi' is %i bytes long\n", ProcSize(SayHi))
  jinvoke SayHi, Chr$("Yep, it works!!!!")
EndOfCode XP                   ; option xp adds a manifest


Code: [Select]
The proc 'SayHi' is 376 bytes long
Assembled with ML64 in 64-bit format, using common controls version 6.16

Code: [Select]
The proc 'SayHi' is 249 bytes long
Assembled with UAsm64 in 32-bit format, using common controls version 6.16

daydreamer

  • Member
  • *****
  • Posts: 1313
  • building nextdoor
Re: How compute size of masm procedure?
« Reply #8 on: February 01, 2020, 07:55:45 PM »
Obtaining the size of a procedure :

Code: [Select]
include     \masm32\include\masm32rt.inc

testproc    PROTO :DWORD,:DWORD,:DWORD

.data

string1     db 'The size of the procedure is %u bytes.'
            db 13,10,0

.data?

ProcSize    dd ?

.code

start:

    invoke  testproc,10,20,ADDR ProcSize

    invoke  crt_printf,ADDR string1,ProcSize

    invoke  ExitProcess,0

testproc PROC x:DWORD,y:DWORD,pSize:DWORD

LOCAL temp:DWORD

    mov     eax,OFFSET label1
    sub     eax,OFFSET testproc

    mov     edx,pSize
    mov     DWORD PTR [edx],eax

    mov     eax,x
    add     eax,y
    ret

testproc ENDP

label1:

    nop

END start
thanks,could also be used to store start adresses and sizes in .data section,to be used in a better way for creating jumptables which jumps to section of different sizes,than run it in debugger and check codesize manually,for example my calculator simple has fadd/ret,fsub/ret,fmul/ret,fdiv but functions like fdiv needs to be bigger to check for divide by zero before and sqrt needs to check for not trying to calculate square root of minus
 
Quote from Flashdance
Nick  :  When you give up your dream, you die
*wears a flameproof asbestos suit*
Gone serverside programming p:  :D
I love assembly,because its legal to write
princess:lea eax,luke
:)