1

I am learning assembly language and there are a lot of details that I am not sure about. Do the two operands after ADD and SUB instructions have to be the same number of register bits if they are both registers?

I checked the x86 Instruction Set Reference and it seems to require the same, but I'm not quite sure, so any help would be appreciated.

  • 2
    They need to be the same size so `add ax, bl` is not valid. – Jester Feb 06 '23 at 13:27
  • 2
    https://www.felixcloutier.com/x86/add shows the available forms of ADD. If you don't believe the manual (e.g. you wonder if maybe AMD or some other vendor defined a different form of `add` that their CPUs support but not Intel), try assembling with any good assembler like NASM, and look at the error message. Only a very few instructions take operands of different sizes, like `movzx edx, al` or `shl eax, cl`. Not normal arithmetic or bitwise. – Peter Cordes Feb 06 '23 at 14:59
  • Both indeed must be the same size, as `add al,bl` will not increment `ah` for you if the addition wraps from 255 back to 0. If you really want to add `bl` to `ax`, you need to first set `bh` equal to zero then `add ax,bx`. – puppydrum64 Feb 06 '23 at 15:13
  • 2
    @puppydrum64 You can choose `add al, bl` then `adc ah, 0` as well, which also has the effect of adding `bl` (zero-extended) to `ax`. – ecm Feb 06 '23 at 16:02

0 Answers0