Could someone please explain what this inline #define assembly is doing?

Jim*_*m C 12 x86 assembly gcc turbo-c carryflag

I'm an occasional C programmer. I've come across this bit of inline assembly code in a Turbo C program

#define ADC(dst,src)    { asm MOV AX, dst;  asm ADD AX, src;  \
              asm ADC AX, 0;    asm MOV dst, AX; }
Run Code Online (Sandbox Code Playgroud)

dst and src are both unsigned 16-bit integers.

It won't compile in GNU C++. Could someone please explain what it's doing? TIA!

NPE*_*NPE 13

The first two instructions add dst and src together, storing the result in the accumulator. The third instruction computes what's sometimes called the "end-around carry" and the fourth stores the result in dst.

The following is an equivalent C implementation:

int32_t sum = dst + (int32_t)src;
dst = (int16_t)((sum & 0xffff) + (sum >> 16));
Run Code Online (Sandbox Code Playgroud)

维基百科在其补语的覆盖范围内讨论了末端进位

要在此系统中表示的两个数字相加,一个数字会执行常规的二进制加法运算,但随后有必要进行一次末尾进位:即,将所有结果进位加回到结果和中。

例如,在计算IPv4校验和时使用末端进位进位。


fuz*_*fuz 7

A literal translation of this code to GNU C inline assembly is:

static inline short ADC(short dst, short src)
{
     asm ("add %1, %0; adc $0, %0" : "+r"(dst) : "rmi"(src));

     return (dst);
}
Run Code Online (Sandbox Code Playgroud)

But the version provided by NPE should be a bit more portable.