## About ### x86 assembler in Open Watcom #### Inline ```c int myCode(int); #pragma aux myCode = \ "mov eax,ebx" \ value [eax] \ modify [eax] \ parm [ebx]; int main(void) { printf("%d\n", myCode(10)); //=> 10 return 0; } ``` Labels should start with an alphanumeric character. Starting them with a `.` dot won't work. #### External file ##### Stack style, for use with DOS extender Compile with `wasm` ```asm PUBLIC _myCode PUBLIC myData_ .386 .model flat,c .DATA myData_: BYTE 32 .CODE _myCode: push ebp mov ebp, esp mov eax, [ebp + 8] pop ebp ret end ``` ```c extern int myCode(int); #pragma aux (__cdecl) myCode extern char myData; int main(void) { printf("%d\n", myCode(myData)); //=> 32 } ``` ### Examples #### Protected Mode Keyboard interrupt handler in assembler called from Open Watcom C with shared variable Two object files are linked using `wcl386` ```c // main.c extern void __cdecl far interrupt keyboardHandler(); extern char keyboardPressed; void far (interrupt *int9Save)(); int main(void) { int9Save = _dos_getvect(9); _dos_setvect(9, keyboardHandler); while (keyboardPressed == 0) { printf("%d\n", keyboardPressed); } printf("wow\n"); delay(200); _dos_setvect(9, int9Save); return 0; } ``` ```asm ; kb.asm PUBLIC _keyboardPressed PUBLIC _keyboardHandler .386 .model flat,c .DATA _keyboardPressed db 0 .CODE _keyboardHandler: cli push eax push ebx in al, 0x60 mov bl, al ; acknowledge keyboard in al, 0x61 mov ah, al or al, 0x80 out 0x61, al xchg ah, al out 0x61, al and bl,0x80 jne _keyboardHandler_notKeydown mov [_keyboardPressed], 1 _keyboardHandler_notKeydown: ; acknowledge interrupt mov al, 0x20 out 0x20, al pop ebx pop eax sti iretd ; needed for 32 bit protected mode ``` #### Links * https://users.pja.edu.pl/~jms/qnx/help/watcom/compiler-tools/cpasm.html * https://tuttlem.github.io/2015/10/04/inline-assembly-with-watcom.html * https://montcs.bloomu.edu/Information/LowLevel/Assembly/asm-stack.html * https://en.wikipedia.org/wiki/X86_calling_conventions#stdcall * https://blarg.ca/2018/04/16/using-watcoms-register-calling-convention-with-tasm * https://github.com/open-watcom/open-watcom-v2/discussions/528#discussioncomment-180994 * https://stackoverflow.com/a/73243437 ## References ### Further reading * [Open Watcom 2.0 clib docs](https://open-watcom.github.io/open-watcom-v2-wikidocs/clib.html) * [David Brackeen's 256 Color VGA Programming in C](http://www.brackeen.com/vga/index.html) * [Ralf Brown's Interrupt List](http://www.ctyme.com/rbrown.htm) * [FreeVGA project](http://www.osdever.net/FreeVGA/home.htm) ## Uncategorized/Unhandled * https://wiki.osdev.org/User:Omarrx024/VESA_Tutorial