uses dos,crt;
var _b:byte;
i:longint;
buffer,old_int9:pointer;
kb:byte;
var oldbb,bb:byte;
procedure Int9;interrupt;
begin
asm
push ax
push di
push es
in al,$60
mov bb,al
pop es
pop di
in al,$61
mov ah,al
or al,$80
out $61,al
xchg ah,al
out $61,al
mov al,$20
out $20,al
pop ax

{cli
call @wait_kbd
mov al,$ED
out $60,al
call @wait_kbd
mov al,bb
out $60,al
jmp @finisch
@wait_kbd:
mov cx,$ffff
@test_kbd:
in al,$60
test al,$02
loopnz @test_kbd
ret
@finisch:
{sti}

end;
if oldbb<>bb then
 begin
 writeln(bb,' ');
 oldbb:=bb;
 end;
end;
procedure writekey(_key:char);
begin
asm
cli
push ds
push es
mov ax,$40
mov es,ax
mov bx,es:$1C
mov al,_key
mov ah,0
mov es:[bx],ax
inc bx
inc bx
mov es:$1C,bx
pop es
pop ds
sti
end;
end;

begin
writeln('_________');
{repeat
gotoxy(1,1);
writeln(mem[$40:$1C],' ',mem[$40:$1A],' ',mem[$40:$82]);
until false;}
{writekey('w');
writekey(#1);
writeln(ord(readkey));
writeln(ord(readkey));
halt;}
{buffer:=addr(kb);}
getintvec(9,old_int9);
setintvec(9,@int9);
repeat
{asm
in al,$60
mov bb,al
end;}
until bb=1;
setintvec(9,old_int9);
halt;

writeln;
for i:=0 to 500 do
{repeat}
 begin
asm
cli
call @wait_kbd
mov al,$AC
out $60,al
{call @wait_kbd
mov al,$00
out $60,al}
call @wait_dat
in al,$60
mov _b,al
sti
jmp @finisch
@wait_kbd:
mov cx,$ffff
@test_kbd:
in al,$60
test al,$02
loopnz @test_kbd
ret
@wait_dat:
mov cx,$ffff
@test_dat:
in al,$60
test al,$01
loopnz @test_dat
ret
@finisch:
end;
write(_b,' ');
 end;
{until _b=4;}
end.