Jump to content
Tuts 4 You

Need Help Asm in Delphi


X-88

Recommended Posts


unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
E1: TEdit;
E2: TEdit;
Button1: TButton;
RG: TRadioGroup;
Label1: TLabel;
Label2: TLabel;
E3: TEdit;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
Inp1 , Inp2, Res : Integer;
begin
Inp1 := StrToInt(E1.Text);
Inp2 := StrToInt(E2.Text);
case RG.ItemIndex of
0 : begin
asm
Mov Eax, Inp1 // Tmp Inp 1 ???
Add Eax, Inp2 // Eax = Inp 1 + Inp 2???
Mov Res, Eax // Result = Eax???
end;
E3.Text := IntToStr(Res);
end;
1 : begin
asm
Mov Eax, Inp1 // Tmp Inp 1 ???
Sub Eax, Inp2 // Eax = Inp 1 - Inp 2???
Mov Res, Eax // Result = Eax???
end;
E3.Text := IntToStr(Res);
end;
2 : begin
asm
Mov Eax, Inp1 // Tmp Inp 1 ???
Mul Eax, Inp2 // Eax = Inp 1 x Inp 2???
Mov Res, Eax // Result = Eax???
end;
E3.Text := IntToStr(Res);
end;
3 : begin
asm
Mov Eax, Inp1 // Tmp Inp 1 ???
Div Eax, Inp2 // Eax = Inp 1 : Inp 2???[color=#ff0000]//Is there something wrong with this section?[/color]
Mov Res, Eax // Result = Eax???
end;
E3.Text := IntToStr(Res);
end;
{ 4 : begin
asm
Mov Eax, Inp1 // Tmp Inp 1 ???
Mod Eax, Inp2 // Eax = Inp 1 Mod Inp 2???[color=#ff0000] //Is there something wrong with this section?[/color]
Mov Res, Eax // Result = Eax???
end;
E3.Text := IntToStr(Res);
end; }
end;
end;
end.

but if I apply to the console mode is not an error, but the GUI is always the message "Integer Overflow" why?...

Link to comment

1) There is no "mod" instruction in x86 asm.. :)

2) Div and mul instructions will modify EDX register and Delphi doesn't save it automatically. Try adding push edx/pop edx (or better - pushad/popad).

Link to comment

Another idea - maybe for some reasons GUI version has enabled overflow checks. In that case try placing "{$Q-}" directive in the beginning of the unit.

Link to comment

but that makes me wonder, if I apply the Console does not appear the message????...... :(


program ZN;{$APPTYPE CONSOLE}uses
SysUtils;//{$R ZN.Res}var
Inp1, Inp2, Res : Integer;
VK : Char;
begin
//WriteLN(' Path = ',CmdLine);
WriteLN('~~~~~~~~~~~~~~~~~~~~');
WriteLN('|[ZN - Calculator] |');
WriteLN('~~~~~~~~~~~~~~~~~~~~');
WriteLN(' 1 = Add [+]');
WriteLN(' 2 = Sub [-]');
WriteLN(' 3 = Mul [X]');
WriteLN(' 4 = Div [:]');
WriteLN(' 5 = Mod [?]');
WriteLN(' Else Exit');
WriteLN('~~~~~~~~~~~~~~~~~~~~');
Write(' Please Select 1 : ');
Read(VK);
case VK of
'1' :
begin
ReadLN;
Write(' Input 1 : ');
Read(Inp1);
Write(' Input 2 : ');
Read(Inp2);
asm
Mov Eax, Inp1 // Tmp Inp 1 ???
Add Eax, Inp2 // Eax = Inp 1 + Inp 2???
Mov Res, Eax // Result = Eax???
end;
WriteLN(' Result : ', Res);
ReadLN;
end;
'2' :
begin
ReadLN;
Write(' Input 1 : ');
Read(Inp1);
Write(' Input 2 : ');
Read(Inp2);
asm
Mov Eax, Inp1 // Tmp Inp 1 ???
Sub Eax, Inp2 // Eax = Inp 1 - Inp 2???
Mov Res, Eax // Result = Eax???
end;
WriteLN(' Result : ', Res);
ReadLN;
end;
'3' :
begin
ReadLN;
Write(' Input 1 : ');
Read(Inp1);
Write(' Input 2 : ');
Read(Inp2);
asm
Mov Eax, Inp1 // Tmp Inp 1 ???
Mul Eax, Inp2 // Eax = Inp 1 x Inp 2???
Mov Res, Eax // Result = Eax???
end;
WriteLN(' Result : ', Ord(Res));
ReadLN;
end;
'4' :
begin
ReadLN;
Write(' Input 1 : ');
Read(Inp1);
Write(' Input 2 : ');
Read(Inp2);
asm
Mov Eax, Inp1 // Tmp Inp 1 ???
Div Eax, Inp2 // Eax = Inp 1 : Inp 2???
Mov Res, Eax // Result = Eax???
end;
WriteLN(' Result : ', Res);
ReadLN;
end;
{
'5' :
begin
ReadLN;
Write(' Input 1 : ');
Read(Inp1);
Write(' Input 2 : ');
Read(Inp2);
asm
Mov Eax, Inp1 // Tmp Inp 1 ???
Mod Eax, Inp2 // Eax = Inp 1 + Inp 2???
Mov Res, Eax // Result = Eax???
end;
WriteLN(' Result : ', Ord(Res));
ReadLN;
end;
}
else
Exit;
end;
while true do
Sleep(1000);
end.

Another idea - maybe for some reasons GUI version has enabled overflow checks. In that case try placing "{$Q-}" directive in the beginning of the unit.

What is not required termination {$ Q-}

I had already tried to insert it {$ Q-}, but nonetheless

Edited by X-88
Link to comment

I have created a new console application and pasted your code. When running it, compiler complains about a missing ZN.Res file. If I remove this line:

{$R ZN.Res}

Then I'm getting all lines of your menu as a console mode asking "Please Select 1 :"...

Link to comment

it is a resource section that refers to the source of the icon to be included into the resource, I forgot to remove it :)

it was a choice between 1 .. 5 of operator:

1 = Add [+]

2 = Sub [-]

3 = Mul [X]

4 = Div [:]

5 = Mod [?]

else

Terminate

Edited by X-88
Link to comment

Both console and UI versions work fine on my PC, no overflow messages (Delphi 2007, most likely all default settings).

I made ​​it and run it with D7 & D 2010 on OS: win 7

but both are showing the same thing.

post-65081-0-96235300-1349922382_thumb.p

and I've turned it into EDX is the message will not appear again, but it did not yield the division also performed

Edited by X-88
Link to comment

Try to debug it in ollydbg to find where it reaches that error.

Probably it is due to any of CPU registers being modified in the inside of your asm routines and not being restored when exiting.

You'd rather use PUSHAD as the first asm instruction and POPAD as the last asm instruction for every set of asm instructions as kao suggested.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...