Results 1 to 5 of 5

Thread: call assembly function via VB

  1. #1
    lek_70
    Guest

    call assembly function via VB

    does anyone know how to call the assembly function(MASM ) via vb?? if so, how is the assembly function should be written?

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    you must write the assembly function to follow the pascal calling convention. Then you make a dll out of it and call it like a API function.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    lek_70
    Guest
    Can u give me a simple example for both vb and assembly code?? Thanks.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Well, I'm not good in ASM and have never done Win32 ASM, I also don't know much about VB, but here's some code, I can't guarantee it will work.
    Code:
    ; asm file, compile it and make a dll out of it
    ; I'll use AT&T syntax because I usually only code for linux (if I code at all)
    ; the most important things are that source and target operator are exchanged
    ; and opcodes have suffixes specifying the data length
    ; and registers and values have prefixes
    ; you can find information via google
    .section .data
    ErrStr:
    .ascii "An Error occured."
    
    .section .code
    .global ErrorMB
    
    ; this must follow __stdcall conventions
    ErrorMB: .type .function
      pushl %ebp
      movl %esp, %ebp
      ; call MessageBox(NULL, ErrStr, ErrStr, 0)
      ; it is a __stdcall function
      ; args are passed from left to right
      pushl $0
      movl $ErrStr, %eax ; is the same as: leal ErrStr, %eax
      pushl %eax
      pushl %eax
      pushl $0
      call MessageBox
      ; __stdcall says that stack is cleared by called function
      ; so if I understand correctly, we don't need pops here
      ; but this function returns now, so we need to clear up our waste
      movl %ebp, %esp
      popl %ebp
      ret    ; you should also somehow clear the arguments passed to this funtion here
      ; but since there are no arguments I'll leave it to you to find out how. You might want to look at an asm listing of a C function
    Now that you have created the dll AsmFuncs.dll, we can call the function from VB
    VB Code:
    1. Private Declare Sub ErrorMB Lib "AsmFuncs.dll" ()
    2.  
    3. 'Call
    4. ErrorMB

    This should theoretically work.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Ok, so you write
    ret (number of bytes to pop)
    to clear the stack.
    example:
    Code:
    ; now in MASM syntax
    ; int __stdcall add(int, int)
    ; returns the sum of the arguments
    PUBLIC add   ; correct name would be _add@8, but then you would need a .def file
    add PROC NEAR ; maybe needs to be far, who knows
      push ebp
      mov  ebp, esp
      ; get parameters
      mov eax, DWORD PTR 8[ebp] ; get first int
      add eax, DWORD PTR 12[ebp] ; add second int
      ; result is in eax, this is the return value
      ; leave esp untouched, it is still needed
      pop ebp
      ret 8 ; this will pop the 8 bytes off the stack and decrease esp and return
    This is about what VC++ makes of the function
    PHP Code:
    int __stdcall add(int iint j)
    {
      return 
    j;

    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width