Search code examples
cassemblygccx86inline-assembly

GCC ASM set AH Register to 1h from C


is there a way to push this line to asm from C from GCC

    mov ah,1h
    int 21h

I can't find a way to set the AH register to 1h

 asm("mov %ah,1h");
 asm("int 21h");

Solution

  • 1h means 1 in hexadecimal number. You can use $0x1 to express that. ($ is required for integer literals in GCC assembly language and 0x is marking the number as hexadecimal).

    Also note that in GCC assembly language, the destination of mov instruction (and other instructions with two operands) should be the 2nd operand.

    asm("mov $0x1, %ah");
    asm("int $0x21");
    

    One more note is that if you want to make sure that %ah is 0x1 when the int is executed, the two lines should be put into one asm statement not to let the compiler put other instructions between them.

    asm(
        "mov $0x1, %ah\n\t"
        "int $0x21"
    );