Build a DOS program with a protected mode wrapper using Open Watcom 2.0

Compile with wcl386.exe to use the protected mode wrapper and skip all the DOS memory model junk.

Open Watcom v2

This is the C/C++ compiler I've been using the most lately. It installs pretty cleanly into a virtual directory using DosBox-X.

https://open-watcom.github.io/

`setup.sh`

snippet.sh
export WATCOM=~/Applications/open-watcom-v2/rel
export INCLUDE=$WATCOM/h
export LIB=$WATCOM/lib286/dos:$WATCOM/lib286
export PATH=$WATCOM/binl64:$WATCOM/binl:$PATH

x86 assembler in Open Watcom

Inline

snippet.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

snippet.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
snippet.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

snippet.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;
}
snippet.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