-4

How can I add two registers together without add, adc, sub, sbb, inc, and dec?

MD XF
  • 7,860
  • 7
  • 40
  • 71
  • 3
    You forgot to mention what architecture. For x86, you can use `LEA`. – Jester Jan 24 '17 at 18:48
  • @Jester For x86 – Elmira Sargsyan Jan 24 '17 at 18:51
  • @Jester How about `movd mm0,eax; movd mm0,ecx; paddd mm0,mm1; movd eax,mm0`? – fuz Jan 24 '17 at 20:00
  • x86 is (sort of) Turing-complete using just MOV instructions, doing math with memory addressing modes: https://stackoverflow.com/a/44799928/224132. (Of course LEA can do addition directly using address-mode syntax and machine encoding, but not subtraction. Although you didn't mention `neg`, so `neg` + `lea` can subtract.) – Peter Cordes Sep 20 '17 at 05:13

1 Answers1

0

Honestly, I just took this answer and translated it to assembly, the numbers to sum are in ax and bx, while registers cx and dx are used to get intermediate results, the final result is in dx :

  mov ax, 12801  ;◄■■ FIRST NUMBER.
  mov bx, 2017   ;◄■■ SECOND NUMBER.

l1:

  mov cx, ax
  and cx, bx

  mov dx, ax
  xor dx, bx

  mov ax, cx
  shl ax, 1

  mov bx, dx

  cmp ax, 0   ;◄■■ IF AX != 0 REPEAT.
  jne l1

;RESULT IN DX = 14818
Community
  • 1
  • 1
  • 1
    Nicely complicated. How about `mov si, ax; lea dx, [bx+si]` (if we stick to 16 bit which nobody said to). – Jester Jan 24 '17 at 19:15
  • 4
    I have this burning desire to optimize the code you present here, except that would be completely absurd because if you actually wanted optimal code, you'd use `add`! – Cody Gray - on strike Jan 25 '17 at 18:59