I am not an expert on assembly either, but I think this works:
Signed version:
function TryAdd(a, b: integer; out c: integer): boolean;
asm
ADD EAX, EDX // EAX := a + b;
MOV [c], EAX // c := EAX;
JO @@END // if overflow goto end;
MOV EAX, true // result := true
RET // Exit;
@@END:
XOR EAX, EAX // result := false;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
c: integer;
begin
if TryAdd(MaxInt - 5, 6, c) then
ShowMessage(IntToHex(c, 8))
else
ShowMessage('Overflowed!');
end;
Unsigned version:
function TryAdd(a, b: cardinal; out c: cardinal): boolean;
asm
ADD EAX, EDX // EAX := a + b;
MOV [c], EAX // c := EAX;
JC @@END // if overflow goto end;
MOV EAX, true // result := true
RET // Exit;
@@END:
XOR EAX, EAX // result := false;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
c: cardinal;
begin
if TryAdd($A0000000, $C0000000, c) then
ShowMessage(IntToHex(c, 8))
else
ShowMessage('Overflowed!');
end;