RE: ASM Help

new topic     » goto parent     » topic index » view thread      » older message » newer message

> -----Original Message-----
> From: Robert Craig [mailto:rds at RapidEuphoria.com]
 
> Can't you use VC to disassemble, or generate an
> assembly listing of your .dll? Then you could
> poke the bytes of code into your Euphoria program,
> and eliminate the need for the .dll. Alternatively,
> you could get the address of the start of the dll routine,
> using '&' in C, and write the first 100 bytes or so to a file.

D'oh!  Thanks.  

I used the VC debugger and aped the machine code that VC was generating, and
now I can call functions by pointers (including Eu routines with a call_back
address!).  Here's the asm code (mostly generated by asm.e, but I hand
modified the 'call' instruction in order to call using a dword pointer as
address):

constant
fptr_asm = {
    #60,                    --    0: pusha
    #BB,#00,#00,#00,#00,    --    1: mov ebx, paramcount (2)
    #B9,#00,#00,#00,#00,    --    6: mov ecx, params (7)
                            --    B: start: (this pushes the params onto the
stack)
    #8B,#01,                --    B: mov eax, [ecx]
    #50,                    --    D: push eax
    #83,#C1,#04,            --    E: add ecx, 4
    #4B,                    --   11: dec ebx
    #75,#F7,                --   12: jnz start
    #FF,#15,#00,#00,#00,#00,--   14: call dword ptr [comfunc] (22)
    #89,#15,#00,#00,#00,#00,--   1A: mov [retpointer], edx (28) (puts the
return into a buffer)
    #61,                    --   20: popa
    #C3},                   --   21: ret


fptr_paramcount = 2,
fptr_params = 7,
fptr_funcptr = 22,
fptr_retptr = 28

Of course, call() doesn't return a value, so I had to allocate some memory
for a return value.  Here's the code to call a function:

constant
fptr_asm_addr = allocate( length( fptr_asm ) )

poke( fptr_asm_addr, fptr_asm )

function call_fptr( atom fptr, sequence params )
    atom ptraddr, paramptr, ret

    ptraddr = allocate(4)
    poke4( ptraddr, fptr )

    poke4( fptr_asm_addr + fptr_funcptr, ptraddr )

    params = reverse(params)
    paramptr = allocate( length( params )*4)

    poke4( paramptr, params )
    poke4( fptr_asm_addr + fptr_params, paramptr )
    poke4( fptr_asm_addr + fptr_paramcount, length(params) )
    poke4( fptr_asm_addr + fptr_retptr, retval )

    call( fptr_asm_addr )

    ret = peek4u( retval )
    free( ptraddr )
    free( paramptr )

    return ret
end function

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu