C/C++ Windows API

Moderators: Lundin
Number of threads: 443
Number of posts: 1215

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
C to Assembly problem Posted by hands0m3 on 9 Oct 2008 at 1:59 AM
can someone please help me with this?

http://i65.photobucket.com/albums/h226/samcataps/mp.jpg[/img]
Report
Re: C to Assembly problem Posted by Lundin on 9 Oct 2008 at 5:26 AM
What do you need help with, more specifically?
Report
Re: C to Assembly problem Posted by hands0m3 on 9 Oct 2008 at 6:01 AM
: What do you need help with, more specifically?

i want to know what to use instead instead of int..
and also to know how to assign the sum by modifying the assembly code...
Report
Re: C to Assembly problem Posted by Ed Hall on 9 Oct 2008 at 7:11 AM
You can work with numbers as large as you want by assigning them to strings. Then you simply work backwards through the strings, character by character performing your math tasks. Adding two numbers is simple - there will either be a carry of 1 or not. More involved math is, of course, more involved, but doable. You might want to review something I wrote back in February of 2004 here on PH:

http://www.programmersheaven.com/mb/CandCPP/240492/240802/re-10-million-digits/#240802

So, basically, all your routine has to do is work backward converting the corresponding characters of the two strings to their numeric values, add the two digits, convert the rightmost digit (ones value) back to its character and place it in the appropriate position in the output sting. If there is a tens digit, carry it to the addition of the next two characters and repeat throught the strings.

Take Care,
Ed
Report
Re: C to Assembly problem Posted by BitByBit_Thor on 9 Oct 2008 at 3:18 PM
In this case, considering it's all unsigned integers (up to 100 digits), you could also use an array of DWORD's (32 bit integers, such as used by the processor registers like EAX).
This would have the advantage that you could loop through each element of the array and use assembly's 'add with carry' instruction (ADC) if it's available to you.
This way, to add two numbers both represented by DWORD arrays (of equal size) you start with clearing CF just before the loop, and then in the loop continuously ADC the pairs of array elements. You start by adding the last element of the first number array to the last element of the second number array, then you loop again, your counter goes down by one, and you add the second-last elements, etc etc, until you've added all elements. Your result will then be in the number array that is the destination in the ADC instruction ("ADC destination, source" does: destination = source + destination + CF).
The difficult step in this is only to convert the string numbers to binary numbers (and back again). I don't know of an effective way to do such right now, but if no one else does and you want to try this method, I'll get back to you on this. Time for bed ... :P

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
Report
Re: C to Assembly problem Posted by Lundin on 10 Oct 2008 at 12:38 AM
: This would have the advantage that you could loop through each
: element of the array and use assembly's 'add with carry' instruction
: (ADC) if it's available to you.

Unless performance is critical (I assume not since this is posted below Windows API), then I would stay away from inline assembler. Something like this should work:

int sum;

sum = str1[i] + str2[i];
sum -= 2 * '0';  /* convert from string to int */

if(sum > 9 )
{
  result_str[i-1] = sum / 10 + '0';
  result_str[i]   = sum % 10 + '0';
}
else
{
  result_str[i]   = sum;
}


Report
Re: C to Assembly problem Posted by BitByBit_Thor on 10 Oct 2008 at 5:37 AM
: Unless performance is critical (I assume not since this is posted
: below Windows API), then I would stay away from inline assembler.
: Something like this should work:
:

I think the question was getting this program to work in assembly (seeing the title and the image posted).
But your code is actually a string-enrollment of ADC, which might be more to the point here than converting strings to numbers and back to strings again.
If you'd loop this code for each entry in the string, accounting for carry over from the previous addition, then this could easily be translated to assembly code and your program would for the largest part be done.

:
: int sum;
: 
: sum = str1[i] + str2[i];
: sum -= 2 * '0';  /* convert from string to int */
: 
: if(sum > 9 )
: {
:   result_str[i-1] = sum / 10 + '0';
:   result_str[i]   = sum % 10 + '0';
: }
: else
: {
:   result_str[i]   = sum;
: }
: 
:
:
:

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
Report
Re: C to Assembly problem Posted by Lundin on 10 Oct 2008 at 7:12 AM
This is the C/C++ board, so I assume that's the language. The assembler board can be found here.
Report
Re: C to Assembly problem Posted by hands0m3 on 11 Oct 2008 at 6:19 AM
I'm done with the C part..


#include <stdio.h>

int main()
{
char num1[100];
char num2[100];
int i = 0;
int j = 0;
int z = 0;
int length = 0;
int carry = 0;

printf("Enter first number: ");
scanf("%s", num1);
printf("Enter second number: ");
scanf("%s", num2);

i = strlen(num1);

j = strlen(num2);

if (i>j)
{
length = i;
}
else
{
length = j;
}

int input1[length];
int input2[length];
int sum[length+1];
int x = 0;

while (x < length)
{
if(i>0)
{
input1[x] = (int)(num1[i-1]) - '0';
}
else {
input1[x] = 0;
}

if(j>0)
{
input2[x] = (int)(num2[j-1]) - '0';
}
else {
input2[x] = 0;
}

i--;
j--;
x++;
}
x = 0;

int y = 0;


y = 0;

while (y<length)
{


sum[y] = input1[y] + input2[y] + carry;

if (sum[y] > 9)
{
sum[y] = sum[y] - 10;
sum[y+1] = 1;
carry = 1;
}
else {
sum[y+1] = 0;
carry = 0;
}

y++;
}


z = 1;
printf("Answer: ");
if (sum[length] != 0)
{
printf("%d", sum[length]);
}
while (z < length+1)
{
printf("%d", sum[length-z]);
z++;
}

}



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.