Assembly x86_64 Arithmetic

I assume that you are fimiler with registers and signed/ unsigned numbers


1-INC and DEC

INC mean increment by one and DEC mean decrement by one.

                    

section .text global _start _start: xor eax, eax mov eax, 1 inc eax ret

2-ADD and SUB

add and sub instructions simply add the source and destination operands and the result will store in destination. The destination could be a register or location in memory.

add destination, source

                    

section .text global _start _start: xor eax, eax mov eax, 5 mov ebx, -1 add eax, ebx ; eax = 4 mov ebx, -10 sub ebx, eax ;ebx = -14 ret

3- MUL and IMUL

mul it is for multiply unsigned numbers, imul used for signed numbers. There is one thing you should consider when you try to use them, mul/imul take is one operand, and assume that the other operand already store in first general purpose register AX.

                    

section .text global _start _start: xor eax, eax mov eax, 5 mov ebx, 5 mul ebx ;eax = 25 mov ebx, -3 mul ebx ;eax = -75 ret

4-DIV and IDIV

As you guess, that div for division of unsigned numbers and idiv for signed numbers.


Note: rdx should be zero.

Note: the reminder of the divison will store in:

section .text global _start _start: xor eax, eax xor edx, edx mov eax, 25 mov ebx, 5 div ebx ;eax = 5 mov ebx, -5 idiv ebx ;eax = -1 ret

The End