0

can you please help me, I have this ams code:

; subs two long number
;    rsi -- address of minuend (long number)
;    rdi -- address of subtrahend (long number)
;    rcx -- length of long numbers in qwords
; result:
;    sub is written to rdi
sub_long_long:
                push            rdi
                push            rsi
                push            rcx

                clc
.loop:
                mov             rax, [rsi]
                lea             rsi, [rsi + 8]
                sbb             rax, [rdi]
                mov             [rdi], rax
                lea             rdi, [rdi + 8]
                dec             rcx
                jnz             .loop

                pop             rcx
                pop             rsi
                pop             rdi
                ret

now, if I'm change lea rsi, [rsi + 8] to add rsi, 8 program give me wrong result on pretty big numbers, e.g.:

10000000000000000000000000000000000000000000000000000000000000000000 1000000000000000000000000000 9999999999999999999999999999999999999999000000018446744073709551616

Why is it happening?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Alexandr
  • 105
  • 7
  • 10
    Because `LEA` doesn't affect the carry flag, which your subtraction with borrow (`SBB`) later relies on. That is in fact precisely why it is used, to preserve the carry flag between the words of the multi-word arithmetic. Note that `INC` also only affects the negative/zero flags and not carry. – doynax Jun 24 '15 at 14:32
  • 1
    As a side note, why are you preserving the parameters ? I don't know your code so maybe it's intended, but all calling conventions don't expect parameters to be preserved. You can remove your push/pop and still be callable from C. – ElderBug Jun 24 '15 at 14:36
  • 2
    @doynax why don't you convert your comment to an answer? That would be useful :) –  Jun 24 '15 at 15:45
  • duplicates: [LEA or ADD instruction?](https://stackoverflow.com/q/6323027/995714), [What is the difference between `lea eax, [ebx + eax]` and `add eax, ebx` in x86-32 assembly?](https://stackoverflow.com/q/4316526/995714) – phuclv Apr 16 '18 at 05:15

0 Answers0