Search code examples
assemblyx86dosmasmturbo-c++

Program fails when linking a mix of C and assembly files with Turbo-C++ 3.0 tools


I have the following assembly code which is aimed to set the screen mode to mode 13:

.model small
.code

public _func

_func proc
        mov al,13h
        int 10h
    ret
_func endp
END

it's assembled successfully into an object file main.obj now I am trying to call the public _func from the following C code :

extern void func();

int main() {

    func();
    return(0);
}

but I have no idea how to link the two objects to produce a working exe I googled much , but most of the posts are in GCC compiler

I have tried the following command line : tcc cmain.c main.obj

I got the exe file CMAIN.EXE but it doesn't change the mode to mode 13 it just displays the message "Abnormal program termination"

I am using turbo c++ 3.0 compiler and masm5.11 assembler what is the proper command to get it work ?


Solution

  • What I didn't see until I wasted a lot of time is that you have an error in the code:

    mov al,13h
    int 10h
    ret
    

    You don't actually set AH to 0 for the Video Mode Set subfunction of INT 10h. Your code attempts to call Int 10h with an arbitrary value in AH left over from earlier instructions. The code should look like this:

    xor ah, ah    ; AH=0 Set Video Mode
    mov al,13h    ; Video Mode 13h
    int 10h
    ret
    

    Or simply:

    mov ax, 0013h ; AH = 0h Video Mode Set, AL = Video Mode 13h
    int 10h
    ret
    

    To get this to assemble and compile with MASM and TCC I had to split compiling and linking with:

    masm main.asm;
    tcc -Ic:\tcpp\include -ms -c cmain.c
    tlink c0s cmain.obj main.obj,chgmode.exe,,cs -Lc:\tcpp\lib
    

    c:\tcpp\include and c:\tcpp\lib have to be replaced with the the directories appropriate for your environment so TCC and TLINK can find the include files and libraries. c0s and cs are the C runtime startup object and library for the SMALL memory model. Replace the last letter with the letter appropriate for the memory model you need to build for.

    • c0t and ct for tiny
    • c0s and cs for small
    • c0c and cc for compact
    • c0m and cm for medium
    • c0l and cl for large
    • c0h and ch for huge

    The TCC command line sets the memory model as well for each C file you compile -ms is for SMALL memory model. Change the last letter similar to above.

    Effectively this process compiles the C files and assembly files to individual object files and then links them all together in the last step.