C to Assembly problem - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# C to Assembly problem

Posts: 4Member
can someone please help me with this?

[IMG]http://i65.photobucket.com/albums/h226/samcataps/mp.jpg[/IMG]

## Comments

• Posts: 3,711Member
What do you need help with, more specifically?
• Posts: 4Member
: 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...
• Posts: 395Member
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
• Posts: 2,444Member
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
• Posts: 3,711Member
: 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:

[code]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;
}
[/code]

• Posts: 2,444Member
: 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.

: [code]: 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;
: }
: [/code]:
:
:

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
• Posts: 3,711Member
This is the C/C++ board, so I assume that's the language. The assembler board can be found [link=http://www.programmersheaven.com/mb/pharabee/Board.aspx]here[/link].
• Posts: 4Member
I'm done with the C part..

#include

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 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++;
}

}
Sign In or Register to comment.