System call Overview

DOS, BIOS or any operating system provides system services via system calls. System calls are software interrupts. These are the interrupts generated by CPU with the execution of INT instruction.

System call flowchart

System calls are kernel services to be used by users. User context runs in higher privilege level with protection and thus user context cannot jump or call to kernel functions directly. Operating system reserves a software interrupt vector for this purpose. This is a vector number which should be invoked with INT <vector>. For MSdos this number is 0x21. BIOS also provides a number of services via software interrupts like 0x10 for display, 0x16 for keyboard, 0x13 for disk storage service.

Now calling these system interrupts are tricky. These are not like function calls where compiler pushes argument one by one and calls the function. Here every arguments has to be in CPU registers. Then arguments are set to the registers using MOV. Last is the INT 0xNN instruction. After INT instruction context will jump to kernel interrupt handler. Kernel saves user context and switch to kernel context. Context is a snapshot of CPU registers. Kernel reads all arguments and does all the operations for this service and moves return value to AX and executes an IRET instruction. IRET woul switch context back to user mode and user will get return values and all output arguments in user context.

System calls like file handling open/close,read/writes are system calls. We do not require to write inline assembly or int86 call as these are already implemented by C runtime library. However some DOS and BIOS system calls are not implemented by C runtime. We can call these system interrupts via inline assembly or via int86 routine.

Inline Assembly

/* our own getch() function using inline assembly and INT 16 service */
/* INT 16,0 - Wait for Keypress and Read Character */
/* AH = 00 */
/* on return: */
/* AH = keyboard scan code */
/* AL = ASCII character or zero if special function key */
/* - halts program until key with a scancode is pressed */
char getkey(void)
10  {
11    char ret;
12    _asm{
13      MOV ah, 0
14      INT 0x16
15      MOV ret, AL
16      }
17    return ret;
18  }

INT86

Int86 is a C library function facilitates access to bare bone DOS and BIOS service interrupts . It is a wrapper over inline assembly interrupt call. It takes CPU register values with object to a structure where member variables are equivalent to CPU registers. It takes three arguments.

int int86( int intno,
           union REGS *inregs,
           union REGS *outregs );
  • intno - Interrupt vector /service number
  • inregs - Input argument registers as REGS
  • outregs - Output argument registers as REGS

Data structures

struct WORDREGS {
    unsigned int    ax, bx, cx, dx, si, di, cflag, flags;
};
struct BYTEREGS {
    unsigned char   al, ah, bl, bh, cl, ch, dl, dh;
};
union   REGS    {
10      struct  WORDREGS x;
11      struct  BYTEREGS h;
12  };
13 
14  struct  SREGS   {
15      unsigned int    es;
16      unsigned int    cs;
17      unsigned int    ss;
18      unsigned int    ds;
19  };

Additional Functions

/* int86 also inclues segment registers */
int int86x( int intno, union REGS * inregs, union REGS *__outregs, struct SREGS *segregs );
/* Access DOS system interrupts vector 0x21*/
int intdos( union REGS *inregs, union REGS *outregs );
/* Access DOS system interrupts vector 0x21 with segment registers */
int intdosx( union REGS *inregs, union REGS *outregs, struct SREGS *segregs );

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

printf, scanf, ioctl, mmap, DOS low level IO access, DOS BIOS int86, sleep/delay, sound, nosound, DOS graphics, Turbo C graphics,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan