Author Topic: Calculate a Pythagorean triple in C++ and assembly  (Read 1119 times)

Ben756

  • Regular Member
  • *
  • Posts: 2
Calculate a Pythagorean triple in C++ and assembly
« on: February 22, 2017, 08:07:18 AM »
I have a c++ program. I need to covert calculation part to assembly.

Code: [Select]
#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

int main()
{
int m, n;
int a, b, c;
cout << "Pythagorean Triples Calculation" << endl;

do
{
cout << "Enter values for m an n: ";
cin >> m >> n;
if (m == 0 || m <0)
{
cout << "The value " << m << " is out of range- only positive integers are allowed" << endl;
}
if (n == 0 || n <0)
{
cout << "The value " << n << " is out of range- only positive integers are allowed" << endl;
}

} while (m <= 0 || n <= 0);

a = 2 * m*n;
b = pow(m, 2) - pow(n, 2);
c = pow(m, 2) + pow(n, 2);


cout << "Side a = " << a << endl;
cout << "Side b = " << b << endl;
cout << "Side c = " << c << endl;
cout << "Calculation ended." << endl;
return 0;
}

so, I need this part in assembly:
Code: [Select]
a = 2 * m*n;
b = pow(m, 2) - pow(n, 2);
c = pow(m, 2) + pow(n, 2);
« Last Edit: February 22, 2017, 05:41:24 PM by Ben756 »

ragdog

  • Member
  • ****
  • Posts: 524
Re: Convert C++ to assembly
« Reply #1 on: February 22, 2017, 09:26:22 AM »
Hello

"crt_pow" ?

I think it help you http://masm32.com/board/index.php?topic=3614.0

Greets,

jj2007

  • Member
  • *****
  • Posts: 7734
  • Assembler is fun ;-)
    • MasmBasic
Re: Convert C++ to assembly
« Reply #2 on: February 22, 2017, 10:02:00 AM »
include \masm32\MasmBasic\MasmBasic.inc      ; download
  SetGlobals m, n, intA, intB, intC, pos
  Init
  Let esi="3, 2"            ; start with a default set of values
  .While 1
      Let esi=Input$("m, n: ", esi)       ; ask for input (the string is editable)
      .Break .if !Len(Trim$(esi))         ; stop it if the string is empty
      mov pos, esi
      mov m, Val(pos)            ; get m
      .if edx>99
            deb 4, "your first value makes no sense"
      .else
            add pos, edx
            mov n, Val(pos)      ; get n
            .if edx>99
                  deb 4, "your second value makes no sense"
            .else
                  deb 4, "input values", m, n
                  fild m
                  fimul n          ; m*n
                  fadd ST, ST      ; *2
                  fistp intA       ; a = 2 * m*n
                  fild n
                  fmul ST, ST      ; m*m = pow(m, 2)
                  fild m
                  fmul ST, ST      ; n*n = pow(n, 2)
                  fld      st      ; make a copy of pow(n, 2)
                  fsub ST, ST(2)
                  fistp intB       ; b = pow(m, 2) - pow(n, 2)
                  fadd
                  fistp intC       ; b = pow(m, 2) + pow(n, 2)
                  deb 4, "calculated values:", intA, intB, intC
            .endif
      .endif
  .Endw
  PrintLine "bye"
  Delay 1000
EndOfCode


Testbed attached.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4924
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Convert C++ to assembly
« Reply #3 on: February 22, 2017, 03:02:36 PM »
I would be inclined to use the C compiler option to output an ASM file if it is 32 bit code. You will need to be able to isolate the section of calculation code and whether in fact it uses a C runtime library to do the calculation so that you can benchmark the technique against any assembler code that you may write.

Floating point code will give the right answer if its written correctly but depending on the precision required, it may not be the fastest option and I gather that you would want to write assembler code in this area to make the calculations faster. Sad to say many have learnt that the code they have written in assembler is not as fast as the runtime library. This is of course why you should benchmark the code you write against the C compiler generated code to see if you get any speed gain.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

Ben756

  • Regular Member
  • *
  • Posts: 2
Re: Convert C++ to assembly
« Reply #4 on: February 22, 2017, 05:39:10 PM »
I have done some parts of this assignment. The only part remaining is overflow and error checking. I put description and my code if anybody can help for error checking part.

Your program should next use in-line assembler (the __asm{ } directive) to perform the calculation. For each step in the calculations, check for overflow or other errors. Should an error arise, abandon all further calculations and issue an error message (message 4 in the list below). (Issue all messages from the C++ part of the program. Do not attempt to use Windows system services to produce output.)

Code: [Select]
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

int m, n;
int a, b, c;
cout << "Pythagorean Triples Calculation" << endl;

do
{
cout << "Enter values for m an n: ";
cin >> m >> n;
if (m == 0 || m < 0)
{
cout << "The value " << m << " is out of range- only positive integers are allowed" << endl;
}
if (n == 0 || n < 0)
{
cout << "The value " << n << " is out of range- only positive integers are allowed" << endl;
}

} while (m <= 0 || n <= 0);

__asm {
mov eax, m
mov ecx, n
mul ecx
add eax, eax
mov a, eax

mov eax, m
imul eax, eax
mov ecx, n
imul ecx, ecx
sub eax, ecx
mov b, eax

mov eax, m
imul eax, eax
mov ecx, n
imul ecx, ecx
add eax, ecx
mov c, eax
}

cout << "Side a = " << a << endl;
cout << "Side b = " << abs(b) << endl;
cout << "Side c = " << c << endl;
cout << "Calculation ended." << endl;
return 0;
}


raymond

  • Member
  • **
  • Posts: 174
    • Raymond's page
Re: Calculate a Pythagorean triple in C++ and assembly
« Reply #5 on: February 23, 2017, 04:11:57 AM »
If you want to improve speed, you need to remember a few things such as:
i) minimize memory accesses,
ii) avoid repeating operations unnecessarily.

Modifying your example would probably reduce the timing by at least 30% and also reduce code size.

   __asm {
      mov eax, m
      mov ecx, n
      mov ebx,eax    ;keep it for later
      mul ecx
      add eax, eax
      mov a, eax

;      mov eax, m
      mov eax, ebx    ;retrieve the 'm' value from within the cpu
      imul eax, eax
;      mov ecx, n           no need to repeat this operation, ecx already contains 'n'
      mov ebx,eax      ;keep the product in eax to avoid repeating this multiplication
      imul ecx, ecx
      sub eax, ecx
      mov b, eax

      add ebx,ecx
      mov c,ebx
;those last two instructions avoided two more accesses to memory and another multiplication

;      mov eax, m
;      imul eax, eax
;      mov ecx, n
;      imul ecx, ecx
;      add eax, ecx
;      mov c, eax
   }

All superfluous instructions have been commented out.

Enjoy
Whenever you assume something, you risk being wrong half the time.
http://www.ray.masmcode.com/

jj2007

  • Member
  • *****
  • Posts: 7734
  • Assembler is fun ;-)
    • MasmBasic
Re: Calculate a Pythagorean triple in C++ and assembly
« Reply #6 on: February 23, 2017, 07:08:36 AM »
Modifying your example would probably reduce the timing by at least 30% and also reduce code size.

Your code (below: CPU) is indeed pretty fast, it clearly beats the FPU:
Code: [Select]
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz (SSE4)

758     cycles for 100 * FPU int
467     cycles for 100 * CPU
550     cycles for 100 * FPU Real4

749     cycles for 100 * FPU int
466     cycles for 100 * CPU
550     cycles for 100 * FPU Real4

747     cycles for 100 * FPU int
468     cycles for 100 * CPU
549     cycles for 100 * FPU Real4

752     cycles for 100 * FPU int
468     cycles for 100 * CPU
551     cycles for 100 * FPU Real4

745     cycles for 100 * FPU int
466     cycles for 100 * CPU
551     cycles for 100 * FPU Real4

57      bytes for FPU int
53      bytes for CPU
57      bytes for FPU Real4

raymond

  • Member
  • **
  • Posts: 174
    • Raymond's page
Re: Calculate a Pythagorean triple in C++ and assembly
« Reply #7 on: February 23, 2017, 07:59:07 AM »
I think that the reason for the FPU code being slower than my integer code is primarily due to the conversions to/from integers and floats.

Those are obviously time consuming operations which should be added to the list of things to avoid (whenever possible) for fastest code. But, there are occasions where such additional operations may be worthwhile such as when other ALU operations could be performed in parallel with the FPU being busy computing!!!
Whenever you assume something, you risk being wrong half the time.
http://www.ray.masmcode.com/

jj2007

  • Member
  • *****
  • Posts: 7734
  • Assembler is fun ;-)
    • MasmBasic
Re: Calculate a Pythagorean triple in C++ and assembly
« Reply #8 on: February 23, 2017, 12:00:21 PM »
I think that the reason for the FPU code being slower than my integer code is primarily due to the conversions to/from integers and floats.

These are indeed important; calc_fpuReal2 / Real4b below is floats only, and is exactly as fast as the integer version:
Code: [Select]
fld mR
fld nR
fld st ; n
fld st(2) ; m
fmul ; m*n
fadd ST, ST ; *2
fstp aR ; a = 2 * m*n= 12
fmul ST, ST ; m*m = pow(m, 2)
fxch
fmul ST, ST ; n*n = pow(n, 2)
fld st ; make a copy of pow(n, 2)
fsub ST, ST(2)
fstp bR ; b = pow(m, 2) - pow(n, 2)
fadd
fstp cR ; b = pow(m, 2) + pow(n, 2)

Code: [Select]
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz (SSE4)

731     cycles for 100 * FPU int
471     cycles for 100 * CPU
538     cycles for 100 * FPU Real4a
459     cycles for 100 * FPU Real4b

729     cycles for 100 * FPU int
458     cycles for 100 * CPU
539     cycles for 100 * FPU Real4a
460     cycles for 100 * FPU Real4b

raymond

  • Member
  • **
  • Posts: 174
    • Raymond's page
Re: Calculate a Pythagorean triple in C++ and assembly
« Reply #9 on: February 23, 2017, 12:42:44 PM »
Many thanks for the confirmation. :t
Whenever you assume something, you risk being wrong half the time.
http://www.ray.masmcode.com/

jj2007

  • Member
  • *****
  • Posts: 7734
  • Assembler is fun ;-)
    • MasmBasic
Re: Convert C++ to assembly
« Reply #10 on: August 16, 2017, 08:26:14 PM »
The provided code for the shared issue about C++ programming is worth to consider for C++ learning students.

Dear bot "JackPonting" from the Quality Dissertation experts,
I feel pleased that you quote my code but suggest that your author and your students switch to MasmBasic. C++ is mega out :P

aw27

  • Member
  • ****
  • Posts: 852
  • Let's Make ASM Great Again!
Re: Convert C++ to assembly
« Reply #11 on: August 16, 2017, 10:03:18 PM »
The provided code for the shared issue about C++ programming is worth to consider for C++ learning students.

Here when someone asks for ASM Help someone always appears with the snake oil.

jj2007

  • Member
  • *****
  • Posts: 7734
  • Assembler is fun ;-)
    • MasmBasic
Re: Convert C++ to assembly
« Reply #12 on: August 16, 2017, 10:24:07 PM »
Here when someone asks for ASM Help someone always appears with the snake oil.

José, really, get off your obsessions ::)

I am just making fun of a guy who pretends to sell C++ help but posts a link to an obscure "dissertation help" site, hoping to increase their Google rank. Where is that a**hole "asking for ASM help"?? He is even too dumb to write correct English in his signature: "Are a student looking for thesis writing help UK from the Quality Dissertation experts?" That's pidgin at best.

aw27

  • Member
  • ****
  • Posts: 852
  • Let's Make ASM Great Again!
Re: Calculate a Pythagorean triple in C++ and assembly
« Reply #13 on: August 16, 2017, 10:36:56 PM »
@JJ,
You are really good at scaring newbies!

jj2007

  • Member
  • *****
  • Posts: 7734
  • Assembler is fun ;-)
    • MasmBasic
Re: Calculate a Pythagorean triple in C++ and assembly
« Reply #14 on: August 16, 2017, 10:44:30 PM »
@JJ,
You are really good at scaring newbies!

He is not a newbie, he is a spammer. Check your profile settings, "look & layout" - perhaps you have disabled signatures: Uncheck "Don't show users' signatures", and you will see what this "newbie" is after.

It is crystal clear that the only reason he posted here is to insert a link to his illegal "dissertation help" site.