TExcelTAD
Home › x86-64 Assembly › Addition & Subtraction

Addition & Subtraction

Integer arithmetic instructions take the form op dest, src and store the result in the destination: add rax, rbx means rax = rax + rbx. They work the same for signed and unsigned numbers, thanks to two's complement, and they update the flags so that you can find out afterwards whether the result was zero, negative or overflowed.

The instructions

Instruction Effect Notes
add d, s d = d + s
sub d, s d = d - s
inc d d = d + 1 does not change the carry flag
dec d d = d - 1 does not change the carry flag
neg d d = -d two's complement negation
adc d, s d = d + s + CF add with carry, for big numbers
sbb d, s d = d - s - CF subtract with borrow

The destination can be a register or memory; the source a register, memory or an immediate (up to 32 bits, sign-extended for 64-bit operations).

nasm · try it yourself
section .text
global _start
_start:
    mov rax, 50
    add rax, 25                 ; 75
    sub rax, 5                  ; 70
    inc rax                     ; 71
    mov rdi, rax
    call print_int

    mov rbx, 10
    sub rbx, 35                 ; 10 - 35 = -25
    mov rdi, rbx
    call print_int

    neg rbx                     ; -(-25) = 25
    mov rdi, rbx
    call print_int

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_int: print rdi as a signed decimal number and a newline ----
print_int:
    test rdi, rdi
    jns print_uint          ; not negative: print it as it is
    neg rdi                 ; make it positive
    push rdi                ; keep it (write and syscall clobber registers)
    push '-'                ; the minus sign, as a byte on the stack
    mov eax, 1              ; write(1, rsp, 1)
    mov edi, 1
    mov rsi, rsp
    mov edx, 1
    syscall
    pop rdi                 ; drop the '-'
    pop rdi                 ; the positive value again
    jmp print_uint          ; print_uint's ret returns to our caller

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
71
-25
25

Arithmetic on memory

You can add directly to a variable in memory, without loading it into a register first. The CPU still reads, modifies and writes internally, but your code is shorter:

nasm · try it yourself
section .data
balance:    dq 1000
deposit:    dq 250

section .text
global _start
_start:
    mov rax, [deposit]
    add [balance], rax          ; balance += deposit
    sub qword [balance], 75     ; a 75 fee (size needed: no register)
    inc qword [balance]         ; one cent of interest...

    mov rdi, [balance]
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
1176

Evaluating an expression

A high-level expression has to be broken into single steps, using registers for intermediate results. Let's compute total = price * 3 - discount + shipping (with lea for the * 3):

nasm · try it yourself
section .data
price:      dq 40
discount:   dq 15
shipping:   dq 7

section .text
global _start
_start:
    mov rax, [price]
    lea rax, [rax + rax*2]      ; price * 3 = 120
    sub rax, [discount]         ; 105
    add rax, [shipping]         ; 112
    mov rdi, rax
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
112

Wrap-around and the carry

Registers have a fixed size, so results that don't fit wrap around. The extra bit is not lost entirely: it goes into the carry flag (CF). adc adds it to the next part of a bigger number. This is how a 64-bit CPU adds 128-bit numbers — two add steps, low half first:

nasm · try it yourself
section .text
global _start
_start:
    ; first number:  hi = 0, lo = 0xFFFFFFFFFFFFFFFF (= 2^64 - 1)
    ; second number: hi = 0, lo = 1
    mov r12, -1                 ; low half of the first number
    xor r13d, r13d              ; high half of the first number
    add r12, 1                  ; low halves: wraps to 0, CF = 1
    adc r13, 0                  ; high halves + carry: r13 = 1

    mov rdi, r13                ; the result is 2^64: high half 1 ...
    call print_uint
    mov rdi, r12                ; ... low half 0
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
1
0

Small sizes

The same instructions work on 8-, 16- and 32-bit registers. Remember that a 32-bit operation clears the upper half of the 64-bit register, while 8- and 16-bit operations don't touch it:

nasm · try it yourself
section .text
global _start
_start:
    mov rax, 0x1FF              ; 511
    add al, 1                   ; only al: 0xFF + 1 = 0x00, the 0x100 bit is not touched
    mov rdi, rax
    call print_uint             ; 0x100 = 256

    mov rax, -1
    add eax, 1                  ; 32-bit: 0xFFFFFFFF + 1 = 0, upper half cleared
    mov rdi, rax
    call print_uint             ; 0

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
256
0

Tip: xor eax, eax is the idiomatic way to set a register to zero: it is shorter than mov eax, 0, and the CPU recognises it as "zero this register" without waiting for the old value. Unlike mov, it changes the flags, so don't use it between a cmp and a conditional jump.

Test yourself

After mov rax, 5 and sub rax, 8, what does print_int show?

  1. 3
  2. -3
  3. 18446744073709551613
  4. 0

Which instruction adds the carry flag as well?

  1. add
  2. inc
  3. adc
  4. lea

Which flag is NOT changed by inc and dec?

  1. zero flag
  2. sign flag
  3. carry flag
  4. overflow flag