interfacing assembler with c++

This is a discussion on interfacing assembler with c++ within the C++ Programming forums, part of the General Programming Boards category; Can anyone clear up the proper syntax for calling assembler code from within a c or c++ program? I am ...

  1. #1
    Unregistered
    Guest

    interfacing assembler with c++

    Can anyone clear up the proper syntax for calling assembler code from within a c or c++ program? I am using masm 6.00 and VC ver 1 (both 16 bit and both using the medium memory model) I want to expand on this basic example, but I can't seem to get it to work. Both the c program and the assembler program compile to obj files without errors. Link builds the exe from both obj files without any reported errors; however, when run, the program goes into lala land. TIA

    masm

    .MODEL MEDIUM, C
    .STACK
    .CODE
    .STARTUP

    PUBLIC set_mode
    set_mode PROC FAR C vmode:WORD

    mov ah,0
    mov al, BYTE PTR vmode
    int 10h
    ret
    set_mode ENDP
    END

    c source

    #include <stdio.h>
    #define MODE13 0x13

    extern set_mode(int);

    void main()
    {
    blah blah
    set_mode(MODE13);
    blah blah
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    You compile it as a DOS app right?
    // Gliptic

  3. #3
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You should do something like this, but I don't know have masm

    and vc++.



    First write the .h file. Fill in the prototypes to all the functions

    your going to write in assembly. Look up some documentation

    on vc++ calling order and name mangling, or you could write c++ stubs then compile into assembly. The calling convention is most likely to push the arguments from right to left. Then the return adress (eip+1) is pushed by the call instruction.



    For linux on intel, I end up with code which looks similar to



    f:

    ; linux c calling convention is that edi, esi, ebp, esp must be

    ; kept intact

    push ebp

    mov ebp, esp



    sub esp, 4 ; create a local variable



    ; ints are four bytes

    mov ebx, [ebp+8] ; first argument

    mov ebx, [ebp+12] ; second argument

    mov dword [ebp-4], 4h ; assign 4h to local variable



    ; leave does this

    ; mov esp, ebp

    ; pop ebp


    leave

    ret



    for this function



    void f(int a, int b);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Which assembler is best?
    By MathFan in forum Tech Board
    Replies: 6
    Last Post: 06-16-2005, 09:25 AM
  3. Inline Assembler Macros?
    By Aidman in forum C++ Programming
    Replies: 14
    Last Post: 07-15-2003, 05:10 AM
  4. Assembler
    By GaPe in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 02-03-2003, 12:01 PM
  5. MAINFRAME Assembler.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 12-05-2001, 04:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21