0

Let's have a unsigned 16 Bit number at the register bx and a 32-Bit number at eax:

    mov bx,3
    mov eax,10
    
    add eax,bx ; Producing an error

How can I add those numbers and store them in the EAX-register? How can I expand the 16 to 32 Bit to finally add them?

HeapUnderStop
  • 378
  • 1
  • 9
  • 5
    x86 does not support adding numbers of different size. You'll have to make them the same size before you can add them. For example, you could expand bx from a 16-bit number to a 32-bit number. – Raymond Chen Oct 31 '22 at 19:14
  • 3
    Look at how GCC compiles a function that adds `int16_t` and `int32_t` args; https://godbolt.org/ / [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) – Peter Cordes Oct 31 '22 at 19:17
  • 1
    The approach to binary operators with dissimilar operand types (sign or width) is to convert (widen) one (or both operands, when necessary) to a type that will hold all values of both operand types, and then the problem is reduced to doing arithmetic on this chosen type. – Erik Eidt Oct 31 '22 at 19:18
  • 6
    Use movsx or movxz (depending on whether the number is signed or unsigned) to extend the shorter number before adding. – fuz Oct 31 '22 at 19:22
  • 3
    I believe @fuz meant to say movzx there. – 500 - Internal Server Error Oct 31 '22 at 19:25
  • 1
    @500-InternalServerError Correct, sorry for the typo. – fuz Oct 31 '22 at 20:23

0 Answers0