Back to home page

DOS ain't dead

Forum index page

Log in | Register

Back to the forum
Board view  Mix view

User exception handler in C (Developers)

posted by Laaca Homepage, Czech republic, 16.08.2020, 22:38

Aha, it was not so hard.

My implementation in FreePascal:
(not the mentioned SSE check but a very simple example how to catch out the division by zero and how to revert the handler to original state)


uses dpmiexcp;

var was_excp:boolean;

Function SignalHandler(i:longint):longint;cdecl;
var pexp:PException_State;
    jmp_rec:dpmi_jmp_buf;
    eip:longint;
begin
pexp:=djgpp_exception_state;
eip:=pexp^.__eip;
if {eip=$310f} 1=1 then
   begin
   was_excp:=true;
   Move(pexp^,jmp_rec,sizeof(dpmi_jmp_buf));
   jmp_rec.eip:=pexp^.__eip+2;
   jmp_rec.edx:=0;
   dpmi_LongJmp(jmp_rec,0);
   end;
end;


Procedure Do_Something;
var a:byte;
begin
a:=0;
writeln(2 div a); {I can't write just "2 div 0" because of the FPC sanity check :-)}
end;

{=========== main ===========}
begin
was_excp:=false;

Signal(SIGFPE,@SignalHandler);
Do_Something;
Signal(SIGFPE,@SIG_DFL);

writeln(was_excp);
end.

---
DOS-u-akbar!

 

Complete thread:

Back to the forum
Board view  Mix view
22049 Postings in 2034 Threads, 396 registered users, 120 users online (0 registered, 120 guests)
DOS ain't dead | Admin contact
RSS Feed
powered by my little forum