Recent

Author Topic: [Solved] Problem divide.  (Read 6519 times)

Leonor

  • New Member
  • *
  • Posts: 21
[Solved] Problem divide.
« on: September 18, 2012, 08:16:09 pm »
Hello, I'm new in this area, I'm trying to learn Assembler, I have a problem when I try to divide the first number and the second number, here is the code:

procedure TForm1.CmdDivideClick(Sender: TObject);
     var
        LngFirstValue: longint = 0;
        LngSecondValue: longint = 0;
        LngAnswer: longint = 0;
begin
     try
        LngFirstValue := 8;
        LngSecondValue := 2;
        if LngSecondValue > 0 then
        begin
             asm
                MOV EAX, LngFirstValue
                IDIV LngSecondValue
                MOV LngAnswer, EAX
             end;
             LblAnswer.Caption:= IntToStr(LngAnswer);
        end else begin
            ShowMessage('Can not divide by zero.');
        end;
     except
           on ErrHandle: Exception do
           begin
                 ShowMessage(ErrHandle.Message);
           end;
     end;
end;

I have the error in this line: IDIV LngSecondValue
This is the message error: Project AssemblerTest.exe raised exception class 'External SIGFPE'.
Please if somebody can help me, thanks in advance.
« Last Edit: September 18, 2012, 09:10:17 pm by Leonor »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Problem divide.
« Reply #1 on: September 18, 2012, 08:48:34 pm »
             asm
                MOV EAX, LngFirstValue
                IDIV LngSecondValue
                MOV LngAnswer, EAX
             end;
try this
             asm
                XOR EDX,EDX
                MOV EAX, LngFirstValue
                MOV EBX, LngSecondValue
                IDIV EBX
                MOV LngAnswer, EAX
             end;     

icey

  • New Member
  • *
  • Posts: 19
Re: Problem divide.
« Reply #2 on: September 18, 2012, 08:57:25 pm »
             asm
                XOR EDX,EDX
                MOV EAX, LngFirstValue
                MOV EBX, LngSecondValue
                IDIV EBX
                MOV LngAnswer, EAX
             end;     

He forgot to tell you that EDX stores the modulo of your division :D

Leonor

  • New Member
  • *
  • Posts: 21
Re: Problem divide.
« Reply #3 on: September 18, 2012, 09:04:18 pm »
Hello howardpc and icey, thank you for replying, this code is not working:

             asm
                XOR EDX,EDX
                MOV EAX, LngFirstValue
                MOV EBX, LngSecondValue
                IDIV EBX
                MOV LngAnswer, EAX
             end;

This is the message error: Project AssemblerTest.exe raised exception class 'External
Working around on Internet I found the solution and here is the answer:

procedure TForm1.CmdDivideClick(Sender: TObject);
     var
        LngFirstValue: longint = 0;
        BytSecondValue: byte = 0;
        LngAnswer: longint = 0;
begin
     try
        LngFirstValue := 8;
        BytSecondValue := 2;
        if LngSecondValue > 0 then
        begin
             asm
                MOV EAX, LngFirstValue
                IDIV BytSecondValue
                MOV LngAnswer, EAX
             end;
             LblAnswer.Caption:= IntToStr(LngAnswer);
        end else begin
            ShowMessage('Can not divide by zero.');
        end;
     except
           on ErrHandle: Exception do
           begin
                 ShowMessage(ErrHandle.Message);
           end;
     end;
end;

Thank you.
« Last Edit: September 18, 2012, 09:07:07 pm by Leonor »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Problem divide.
« Reply #4 on: September 18, 2012, 09:09:14 pm »
Try to put there normal pascal code and look how fpc translates it.

Code: [Select]
var first, second, res: LongInt;
begin                           
res:= first div second;

Code: [Select]
000000000048E6E5 4863c0                   movslq %eax,%rax
000000000048E6E8 4863c9                   movslq %ecx,%rcx
000000000048E6EB 4899                     cqto   
000000000048E6ED 48f7f9                   idiv   %rcx

(at -O3, amd64)
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Leonor

  • New Member
  • *
  • Posts: 21
Re: [Solved] Problem divide.
« Reply #5 on: September 18, 2012, 09:51:38 pm »
Hello Blaazen, thank you for replying, I don't know if this answer your question:

untfrmassemblertest.pas:129       LngAnswer := LngFirstValue div LngSecondValue;
00421FCB 8b45f4                   mov    -0xc(%ebp),%eax
00421FCE 99                       cltd   
00421FCF f77df0                   idivl  -0x10(%ebp)
00421FD2 8945ec                   mov    %eax,-0x14(%ebp)

untfrmassemblertest.pas:142       LblAnswer.Caption:= IntToStr(LngAnswer);
00421FD5 8d4594                   lea    -0x6c(%ebp),%eax
00421FD8 e8031afeff               call   0x4039e0 <fpc_ansistr_decr_ref>
00421FDD c7459400000000           movl   $0x0,-0x6c(%ebp)
00421FE4 8d5594                   lea    -0x6c(%ebp),%edx
00421FE7 8b45ec                   mov    -0x14(%ebp),%eax
00421FEA e8e1760100               call   0x4396d0 <SYSUTILS_INTTOSTR$LONGINT$$ANSISTRING>
00421FEF 8b5594                   mov    -0x6c(%ebp),%edx
00421FF2 8b45f8                   mov    -0x8(%ebp),%eax
00421FF5 8b8058040000             mov    0x458(%eax),%eax
00421FFB e850c80b00               call   0x4de850 <TCONTROL__SETTEXT>
00422000 eb0a                     jmp    0x42200c <TFORM1__CMDDIVIDECLICK+268>

this code assemble execute when I execute this code:

LngAnswer := LngFirstValue div LngSecondValue;

I did not find this value (-O3, amd64)

gulyone

  • Guest
Re: [Solved] Problem divide.
« Reply #6 on: September 19, 2012, 12:06:52 am »
just one thing = please avoid to enclose asm language by a try / except block , because this feature relies to high level languages, and if an exception occurs inside your asm block of code , results could be really unpredicable/unhandled
« Last Edit: September 19, 2012, 12:08:38 am by gulyone »

Leonor

  • New Member
  • *
  • Posts: 21
Re: [Solved] Problem divide.
« Reply #7 on: September 20, 2012, 12:45:10 am »
Hello gulyone, thank you for replying, and thank you for the advice, that's something I did not know.  :)

Zoran

  • Hero Member
  • *****
  • Posts: 1988
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: [Solved] Problem divide.
« Reply #8 on: September 20, 2012, 08:41:20 am »
just one thing = please avoid to enclose asm language by a try / except block , because this feature relies to high level languages, and if an exception occurs inside your asm block of code , results could be really unpredicable/unhandled

I might be wrong, but it doesn't seem so to me.

The programmers use (and put inside try blocks) procedures and functions written by other people, which are part of libraries (fcl, lcl, rtl, or third party), not knowing (and do not need to know, that is the point) which are all procedures/functions that get called by these.
Certainly, many routines written in assembly language get called (and fall in try blocks) this way, don't they?
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

 

TinyPortal © 2005-2018