assemblyx86carryflag

Assembly ADC (add with carry)


mov eax, ptr_to_num1 ; little endian
mov ebx, ptr_to_num2 ; little endian
xor ecx, ecx
xor edx, edx
clc
bytes_addition:
    mov dl, byte [eax+ecx] ; byte from shortest
    adc dl, byte [ebx+ecx]
    mov byte [eax+ecx], dl
    inc ecx
    cmp ecx, 4 ; counter, 
    jl bytes_addition

Consider at

EAX: 4F2252FF (big endian)

EBX: 00DFFC00 (big endian)

The result of this addition is wrong: 50024fff (big endian). It should be 50024eff. It looks like the carry flag is affected, but why?


Solution

  • cmp affects carry, which is how it's used for unsigned comparison.

    You could start ecx at -4, then use jnz bytes_addition. You already have the inc there, which will set the zero flag when ecx becomes zero, and won't affect carry.

    Of course this offset has to be compensated for, either by adding 4 to eax before the loop, or adding offsets of 4 in the addressing.