In the code below I'm ading two 64 bit numbers a and b to get the sum and the carry. Its working. But now I wanted to change such that I can also add a former carry to this summation and get the total result and carry . In my case formercarry
is say 1, so i want a+b+formercarry = result + carry
.
I tried but couldn't access the formercarry
value using the gcc asm. I think I'm not very sure about %2
, %3
or %1
what to use at the asm code. Can anyone suggest anything please? I want the answer to be result =0x4B100361BFB66EB2
and carry =1
#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
void add64(int carry,uint64_t a, uint64_t b, uint64_t *d, int *c)
{
*c=0;
char carry1;
__asm__("mov %2,%%rax\n\t"
"adc %3,%%rax\n\t"
"setc %1\n\t"
"mov %%rax,%0\n\t"
:"=r"(*d),"=r"(carry1)
:"r"(a),"r"(b)
:"%rax");
if (carry1)
*c=1;
else
*c=0;
}
int main()
{
uint64_t a=0xA234BDFA12CD4379, b=0xA8DB4567ACE92B38;
uint64_t result;
int carry;
int formercarry = 1;
c1 = cpucycles();
add64(formercarry, a, b, &result, &carry);
printf("Sum = %lx,carry= %d\n", result, carry);
}