Solved

C++ prime program Changing to assembly..Please help

Posted on 2004-03-24
7
1,097 Views
Last Modified: 2011-04-14
I am VERY new to assembly and am intrerested in trying to get this program to work in assembly. I can write it in C++ fine but I don't understand it when I try to convert it to assembly. Please HELP!!!!
I am using Irvine32 and Masm615 here is the C++ code....

#include <iostream>
using namespace std;
void main()
{
    int i,j;
    double prime;
    cout << "This will caluculate if a number is prime or not.\n";
      cout << "If you would like to quit enter a negative number\n";
      
      do{
             cout << "Please enter a number:";
             cin >> i;

      for (j=2 ; j<i ; j++)
            {
               prime=i%j;
              if (prime==0)
             {
                 cout << i << " Is not a prime number\n";
                   break;
             }
            else
            {
              if ((i-j)==1)
                  {
                     cout << i  << " Is a prime number!!\n";
                     break;
                  }
            }
                     }
      }while(i > -1);
                           
               
}

Thanks
            
0
Comment
Question by:ribaldry
7 Comments
 
LVL 6

Expert Comment

by:joghurt
ID: 10675439
If you'd supply your version of the solutions, we'd gladly comment it or help to solve any specific errors.
However, Experts doesn't solve school assignments, neither will do the programming instead of you.

Just as a first hint, I'd suggest to take the assembly output from your C++ compiler and try to understand that one.
And a basic problem: after you check for 2, there is no need to check for even numbers. This small idea will speed up your code by a factor of 2.
0
 

Author Comment

by:ribaldry
ID: 10678554
This will caluculate if a number is prime or not.
If you would like to quit enter a negative number
Please enter a number:4
4 Is not a prime number
Please enter a number:5
5 Is a prime number!!
Please enter a number:-1
Press any key to continue


That was the solution....not a homework asssigment though. I understand if you can't do the work so let me try to understand what I want better. I think I could do the program but there are a few lines in the code that I am not sure how to make an assembly equivelant. here are the lines.

  for (j=2 ; j<i ; j++)   ;how would I state this and have it cover what it does with the c++ program?

  prime = i%j              ;how do I do a mod in assembly

one more thing.

How to I get and view  the assembly output with vs .Net?  Is that option /Fa?

Thanks
ribaldry
















0
 
LVL 6

Expert Comment

by:joghurt
ID: 10678706
Notice the difference between the /FA and /Fa switches. /FAs puts the source code in the assembly file so you can see what instructions are generated from a given C line.
However, I don't see why don't you read/learn the VERY basics of assembly programming before doing anything. I mean the Intel Software Developer Manual, for example. You can download it from http://developer.intel.com/

There isn't modulus in x86 assembly so you have to use the following formula:
A mod B = A - B * INT(A / B)
so checking whether it's divisible by B
if (A == B * INT(A / B)) ...

You can divide using the DIV instruction.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Accepted Solution

by:
millsoft earned 500 total points
ID: 10683700
BTW, just from a mathematical standpoint, the line:

for (j=2 ; j<i ; j++)  

only needs to go as far as the square root of i.

It may not seem like a big deal, but it's a large efficiency gain if i is big, and/or you want to check a lot of values.

Of course, performing SQRT in assembly will be fun.  I don't know if there is a FPU instruction for it or not. :)

Cheers,
Brad


0
 
LVL 6

Expert Comment

by:joghurt
ID: 10685240
Of course there is FPU instruction for square root, it's called FSQRT. It's quite easy. However, if someone has problems even with the very basics of integer operations in assembly, I wouldn't lead him/her into dealing with floating point operations. Especially that FPU instructions can generate a lot of exceptions that would be hard for him/her to handle at the moment.
0
 
LVL 3

Expert Comment

by:Dancie
ID: 10705349
The mod testing in assembler would be the "div" instruction.
If a number is relativly prime to another number then the remainder after a divide is non
zero. With the "div" instuction it takes eax/edx for a 64 bit number,ax/dx for a 32 bit number,
and al/ah for a 16 bit number. The remainder comes back in edx/dx/ah. If these are zero
then the number is relativly prime to the divisor. If you check all odd numbers(ie. even numbers are divisible by 2) less than the square root of the number. You have shown the number to be prime. Yes there is a square root instruction on all pentium processors ,486DX-,and some 386 machines with a coprocessor. It is FSQRT, then just change it to an integer.
0
 
LVL 6

Expert Comment

by:joghurt
ID: 10731065
Dear ribaldry, please tell me what did help you in the accepted answer regarding your original question.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to implement a communication protocol 8 520
How to calculate time ticks with rdtsc in x86 computer? 2 606
File Not Found Exception 12 109
SQL Server RDS clr assembly 4 50
Note: This is the second blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   Every month t…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now