Arithmetic

ADD and SUB

ADD and SUB work like MOV: two operands of the same size, destination first. Both set the flags, so one SUB can serve as a compare:

add rax, rbx ; rax = rax + rbx sub rax, 1 ; rax = rax - 1 inc rax ; rax = rax + 1 dec rcx ; rcx = rcx - 1

MUL and IMUL

One-operand MUL multiplies RAX by the operand and stores the 128-bit result in RDX:RAX. Two- and three-operand IMUL keeps the low half and is the form compilers emit:

imul rax, rbx, 10 ; rax = rbx * 10 (three-operand) imul rax, rbx ; rax = rax * rbx (two-operand) mul rcx ; rdx:rax = rax * rcx (one-operand)

DIV and IDIV

DIV divides RDX:RAX by the operand: the quotient lands in RAX and the remainder in RDX. Clear RDX first, or the high dividend bits corrupt the result:

xor rdx, rdx ; clear the high dividend div rcx ; rax = quotient, rdx = remainder

Next: Bitwise operations

Article author: Arthur Isaev