Question

I have a question, how to realize 64 bit subtraction of 2 nums without using asm commands sub and sbb? c flag must be changed in process to show carry from one register to other! I use Free Pascal IDE and built-in Intel style assembler. So here's what I did:

//1 number
mov esi, 0f000000h //low part
mov edi, 0         //high part
//2 number
mov ecx, 0f000000h //l
mov edx, 0         //h
//neg 2 num
neg ecx            //0f1000000h -> ecx
neg edx
//1 num l part + (- 2 num l part)
add esi, ecx       //0 -> esi, c=1!
//c -> eax
mov eax, 0
lahf
mov al, ah
mov ah, 0
and al, 1
neg eax            //0ffffffffh -> eax
//1 num h part + (- 2 num h part)
add edi, edx
//1 num h part + (- c flag)
add edi, eax       //0ffffffffh -> edi, s flag = 1
//verify result    
mov esi, 0f000000h
mov edi, 0
mov ecx, 0f000000h
mov edx, 0
sub esi, ecx //c=0!
sbb edi, edx

When I'm trying to verify result using sub and sbb it's correct and logical but not equal to above. So what I'm doing wrong? Please help.

Was it helpful?

Solution

You can't NEG both parts of the number and expect a correct result. Example: -1=0xFFFFFFFF 0xFFFFFFFF If you apply NEG to both halves you get 0x00000001 0x00000001 which is clearly wrong. You need to apply the general rule of flipping all the bits and adding one, for example:

not ecx
not edx
add ecx, 1
adc edx, 0

Furthermore, I assume the assignment just wants you to use add and adc, you don't have to handle carry yourself.

OTHER TIPS

OK, thanks to everyone who tried to help, I wanna hug all of you ^.^ So here's solution:

//1 num
mov eax, 0 //l
mov ebx, 0 //h
//2 num
mov ecx, 1 //l
mov edx, 0 //h
//2 num -> 2's complement
not ecx
not edx
add ecx, 1
adc edx, 0
//1 num + (- 2 num)
add eax, ecx
adc ebx, edx
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top