flat assembler
Message board for the users of flat assembler.

Index > Main > invoking functions

Author
Thread Post new topic Reply to topic
realcr



Joined: 02 Apr 2007
Posts: 39
realcr 30 Jun 2007, 14:06
Hey.

I'm trying to invoke a function I wrote:

Code:
start:

        push    31d
        push    0d
        push    3453445d
        call    get_bits
        add     esp,3*4         ; I entered 3 parameters
        cinvoke printf,mess,eax


        cinvoke  get_bits,0, 0,0
        cinvoke printf,mess,eax

        invoke  ExitProcess,0 ; exit and return zero


get_bits:
        ; take the value of the number given , from the a bit to the b bit.
        ; parameters:
        ; x - number  [ebp + 8]
        ; a - first bit [ebp + 12]
        ; b = last bit [ebp + 16]
        push    ebp
        mov     ebp,esp
        mov     esi,1b
        ror     esi,1           ; rotate the bit to be
                                ; the be an msb
        xor     eax,eax         ; init eax
        mov     ecx,[ebp + 12]  ; the first bit location
        shr     esi,cl          ; go the the first bit
.bitloop:
        or      eax,esi
        shr     esi,1h
        inc     ecx             ; add to the counter of bit locations
        cmp     ecx,[ebp + 16]
        jna     .bitloop
        
        ; now we have inside eax bits a to b marked as 1.
        ; The rest should be zeroes.
        mov     edx,[ebp + 8]   ; the number to be manipulated
        and     eax,edx         ; mask the number from a to b
        mov     ecx,[ebp + 12]  ; the first bit location
        shl     eax,cl
        ; result is in eax now

        pop     ebp             ; restore stack frame
        ; note that the caller should handle the stack frame
        ret                    
    


The first time I call the function normally everything works.
If I try to "invoke" it , then I get an error message:
"operand size not specified"
refering to line 45 in proc32.inc :
Code:
macro cinvoke proc,[arg]                ; indirectly call CDECL procedure
 { common
    size@ccall = 0
    if ~ arg eq
   reverse
    pushd arg
    size@ccall = size@ccall+4
   common
    end if
    call [proc]                   ; <------- this is line 45
    if size@ccall
    add esp,size@ccall
    end if }    
    


I'm probably missing some size definition , I can't find out where.

realcr.
Post 30 Jun 2007, 14:06
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 30 Jun 2007, 14:24
cinvoke is to call a procedure throught a pointer. Since you provided a label which holds directly the offset you must use ccall instead.
Post 30 Jun 2007, 14:24
View user's profile Send private message Reply with quote
realcr



Joined: 02 Apr 2007
Posts: 39
realcr 30 Jun 2007, 15:31
Thanks for your help Loco , it works now when I use ccall.
However I still don't understand what you said about function pointer..

How should I construct a function that can be called using invoke?

realcr.
Post 30 Jun 2007, 15:31
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 30 Jun 2007, 15:49
I took the liberty of rewriting your procedure, I hope I didn't make any mistake.

Code:
include 'win32axp.inc'

.code
start:
  cinvoke p_get_bits, 3453445d, 7, 20
  int3 ; Trap for debugging


p_get_bits dd get_bits ; Pointer to get_bits proc

get_bits:
; a <= b condition isn't checked so be careful Very Happy
  label .x at esp+4
  label .a at esp+8
  label .b at esp+12

; Calculate mask to start from a
  or      eax, -1
  mov     cl, byte [.a]
  shl     eax, cl

; Calculate mask to finish on b
  or      edx, -1
  mov     cl, 31
  sub     cl, byte [.b]
  shr     edx, cl

; Combine masks
  and     edx, eax

; Return bits
  mov     eax, [.x]
  and     eax, edx

  ret

.end start     


I used cinvoke to show you when it could be used but in this particular case a pointer is not needed at all and ccall should be used instead.
Post 30 Jun 2007, 15:49
View user's profile Send private message Reply with quote
realcr



Joined: 02 Apr 2007
Posts: 39
realcr 30 Jun 2007, 16:06
Cool , I understand it now.
Great thanks for your help
[and also making my function more efficient lol..]

realcr.
Post 30 Jun 2007, 16:06
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.