Search code examples
cassemblydosx86-16inline-assembly

What's the meaning of this print function in C and Assembly? (DOS)


I want to know what's the meaning of this function:

void dos_print(char *str)
{
    asm("mov $0x09, %%ah\n"
        "int $0x21\n"
        :
        :"d"(str)
        :"ah");
}

Solution

  • asm is a GCC extension to C that lets you insert assembly language into the program.

    mov $0x09, %%ah\n generates an instruction that moves 9 into the AH register. int $0x21 generates an instruction that is a request to the operating system, passing it the value 0x21. In DOS, an interrupt with value 0x21 and 9 in AH asks the system to print a string. The : line is where you would tell GCC where results of the assembly code are produced. It is empty because this code does not produce any values that the program cares about. The line :"d"(str) tells GCC to put the value of str (a pointer to the first character of the string) in the DX register before executing this assembly code. The :"ah" line tells GCC that the assembly code may change the contents of the AH register.

    This Wikipedia page has some information about DOS interrupts.