kaetzacoatl
kaetzacoatl

Reputation: 1469

x86 Assembly Add operands with different sizes

I like to add a byte from a memory location to a 32bit register, is this possible in x86 assembly? add edx, byte [ebx] causes error: mismatch in operand sizes

Upvotes: 4

Views: 3387

Answers (2)

Филя Усков
Филя Усков

Reputation: 312

It is possible if first operand is register or memory and second operand is immediate.

Opcode   |Instruction     | Description
83 /0 ib |ADD r/m32, imm8 | Add sign-extended imm8 to r/m32.

from here https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf

Upvotes: 0

BlackBear
BlackBear

Reputation: 22989

You need to make sure that the operands are of the same size.

This involves a problem with the sign though. If you are working with signed integers you should use movsx, or use movzx if you are working with unsigned integers.

movsx/movzx eax, byte ptr [ebx]
add edx, eax

Upvotes: 10

Related Questions