The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: stbfish on September 12, 2009, 01:22:19 PM

Title: how to get a function size?
Post by: stbfish on September 12, 2009, 01:22:19 PM

stub proc
    ;
    pushfd
    pushad
    ;
    popad
    popfd
    ret
stub endp

like this function, how to count this function size in bytes.
i got idea is put two lables, on at top, one at end. then
mov eax, offset lab1
mov ebx, offset lab2
sub eax, ebx   :dazzled:

Title: Re: how to get a function size?
Post by: dedndave on September 12, 2009, 01:47:54 PM
there are a few ways to do it

        dd EndOfStub-stub

stub proc
    ;
    pushfd
    pushad
    ;
    popad
    popfd
    ret
stub endp

EndOfStub LABEL BYTE

        mov     eax,offset EndOfStub-stub
Title: Re: how to get a function size?
Post by: hutch-- on September 12, 2009, 03:40:05 PM
stbfish,


MyProc proc args etc ....  ; < this is actually a label.

  ; Your Code

    ret

MyProc endp

  end_label::   ; < notice :: for global label.

NextProc etc ....

; ============

; Later when length is required calculate length dynamically.
  mov eax, OFFSET end_label
  sub eax, OFFSET MyProc
Title: Re: how to get a function size?
Post by: Tedd on September 12, 2009, 06:28:00 PM
There is a simple way..

stub proc
    ;...
    ret
stub endp
len_stub equ $-OFFSET stub

"len_stub" can then be used as a constant wherever you need the size of the function (in bytes.)
Title: Re: how to get a function size?
Post by: BlackVortex on September 12, 2009, 06:47:23 PM
Wow, Tedd, that's awesome. So, that equate doesn't produce any actual code, right ?
Title: Re: how to get a function size?
Post by: Tedd on September 12, 2009, 07:01:23 PM
No code, the assembler just assigns the value to "len_stub"
Title: Re: how to get a function size?
Post by: qWord on September 12, 2009, 07:04:15 PM
however, forward-referencing isn't possible with this method ...
Title: Re: how to get a function size?
Post by: dedndave on September 12, 2009, 07:55:01 PM
qword is right - that is why i use a label (it can be a code or data label - no diff)
if you use an equate, it has to be defined in the file before it can be used
(some assemblers may allow it - not masm, though)
re: post #2 of the thread