0

I have this inline assembler on my C code:

asm volatile ("mov %ah, 01\n"
              "int $0x16\n");

This code is a assembler version of getch()

It works, but now I want to get the value that returned this inline assembler (specifically I want to know the key that returns the interruption 16h).

How I can do it?

Edit: I'm making MS-DOS COM files using C and DosBox. Currently everything is working fine, except for the problem that I'm having right now.

Full code:

asm (".code16gcc\n"
     "call  dosmain\n"
     "mov   $0x4C,%ah\n"
     "int   $0x21\n");

static void print(char *string)
{
    asm volatile ("mov   $0x09, %%ah\n"
                  "int   $0x21\n"
                  : /* no output */
                  : "d"(string)
                  : "ah");
}

int dosmain(void)
{

    int a;
    asm volatile ("mov %%ah, 0\n"
                  "int $0x16\n"
                   : "=r"(a));

    print(a);
    print("Hello, World!\n$");
    return 0;
}

But causes a nice "buffer overflow?"

1 Answers1

3

You have a toolchain problem.

The version of GCC you are using is a 32-bit compiler. Inserting the asm(".code16gcc") directive at the top changes the behavior of the assembler to allow 32-bit instructions to be used, but does not make GCC aware of the segmented memory model used in real mode. Combined with the (unknown, and probably incorrect) steps you are using to generate a .com binary, the result will be a non-working executable.

If you want to build DOS executables, use a toolchain which targets this environment, such as OpenWatcom.