0

I would like to add the value stored in dx:ax with the value in cx:bx.

 add ax, bx 
 add dx, cx  

In this way the result will be in dx:ax? or do I need to pay attention to Carry flags using adc?

user3840170
  • 26,597
  • 4
  • 30
  • 62
  • 2
    Add the low order words first, then use add with carry (ADC) for the high order words. After the second add you should test the carry flag to see if overflow has occurred. – Jim Rhodes Jan 04 '22 at 21:49
  • 1
    For a small example of why you need to use consider `dx:ax` = `0000_0001h` and `cx:bx` = `0000_FFFFh`. The desired result is `0001_0000h`. If you use only `add` as in your question then the `add ax, bx` will result in 0000h with Carry Flag set (CY). Then the second `add` would ignore the CY and yield zero too. Only if you use `adc` the carry from the lower word is preserved. – ecm Jan 04 '22 at 21:54
  • Thanks! and before `add ax, bx` should I use `clc` or it is not necessary? – always_improve Jan 04 '22 at 22:05
  • 1
    No need to use CLC before ADD – Jim Rhodes Jan 04 '22 at 22:08
  • @always_improve: How to figure out whether `clc` is needed before the first `add`, or at this point to understand why not: Ask yourself (or look it up): "does `add` read the carry flag as an input?" (And does it write it unconditionally to 0 or 1, rather than just ORing into it as a "sticky" flag, in case that's what you were worried about.) Or just look at compiler output for adding a uint64_t in 32-bit mode to see what it does. – Peter Cordes Jan 05 '22 at 09:46

0 Answers0