dos timer

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <conio.h>
#include <mem.h>
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_PHILOSOPH_COUNT 6
#define DELAY_CYCLE_TACTS 5000
void interrupt (far* oldVector) (...);
int activeProccess;
struct contextTable {
unsigned char registers[18];
unsigned char returnAddr[4];
} proccessContexts[MAX_PHILOSOPH_COUNT];
void interrupt myTimer(...)
{
unsigned char *stackPointer;
asm mov stackPointer, sp;
memcpy(&(proccessContexts[activeProccess].registers), stackPointer, 18);
//IP, CS for IRET
memcpy(&(proccessContexts[activeProccess].returnAddr), stackPointer+16, 4);
int oldID = activeProccess;
activeProccess = rand()%MAX_PHILOSOPH_COUNT;
if(activeProccess!=oldID)
{
memcpy(stackPointer, &(proccessContexts[activeProccess].registers), 18);
memcpy((stackPointer+16), &(proccessContexts[activeProccess].returnAddr), 4);
}
}
void philosoph()
{
while(1)
{
for (int i=0; i<DELAY_CYCLE_TACTS; ++i);
setvect(0x1C, oldVector);
printf("%d", activeProccess);
setvect(0x1C, myTimer);
}
}
int main()
{
activeProccess = 0;
unsigned short proccessCS;
unsigned short proccessIP = (unsigned short)philosoph;
asm mov proccessCS, cs;
for(int i=0; i<MAX_PHILOSOPH_COUNT; ++i)
{
for(int j=0; j<18; j++) proccessContexts[i].registers[j]=(unsigned char)0;
memcpy(&(proccessContexts[i].returnAddr), &proccessIP, 2);
memcpy(&(proccessContexts[i].returnAddr[2]), &proccessCS, 2);
}
oldVector = getvect(0x1C);
setvect(0x1C, myTimer);
philosoph();
return 0;
}