TExcelTAD
Home › x86-64 Assembly › Shifts & Rotates

Shifts & Rotates

Shifts move all the bits of a register to the left or right. They are the fastest way to multiply or divide by powers of two, and together with and/or they let you pack and unpack bit fields.

The shift instructions

Instruction Effect Bit shifted in Use
shl d, n (= sal) shift left 0 multiply by 2^n
shr d, n logical shift right 0 unsigned divide by 2^n
sar d, n arithmetic shift right copy of the sign bit signed divide by 2^n (rounds down)
rol d, n rotate left the bit that fell out on the left circular bit patterns, hashing
ror d, n rotate right the bit that fell out on the right

The count n is either an immediate or the register cl — no other register is allowed. The last bit shifted out lands in the carry flag.

text
shl:  1011 0001  ->  0110 0010    (0 enters on the right)
shr:  1011 0001  ->  0101 1000    (0 enters on the left)
sar:  1011 0001  ->  1101 1000    (sign bit 1 is copied)
rol:  1011 0001  ->  0110 0011    (the 1 that fell out re-enters)

Multiplying and dividing by powers of two

nasm · try it yourself
section .text
global _start
_start:
    mov edi, 13
    shl edi, 3                  ; 13 * 8
    call print_int

    mov edi, 1000
    shr edi, 2                  ; 1000 / 4
    call print_int

    mov rdi, -40
    sar rdi, 3                  ; -40 / 8, sign preserved
    call print_int

    mov rdi, -40
    shr rdi, 3                  ; WRONG for negatives: the sign bit becomes 0
    call print_int

    mov ecx, 10
    mov edi, 1
    shl edi, cl                 ; variable count: 2^10
    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
104
250
-5
2305843009213693947
1024

sar rounds down, idiv rounds toward zero

For negative numbers that are not exact multiples, sar and idiv give different answers: -7 sar 1 is -4 (rounded towards minus infinity), while -7 / 2 with idiv is -3 (truncated towards zero, like C). Compilers dividing a signed value by a power of two therefore add a small correction before the sar.

nasm · try it yourself
section .text
global _start
_start:
    mov rdi, -7
    sar rdi, 1                  ; floor(-7 / 2)
    call print_int

    mov rax, -7
    cqo
    mov ecx, 2
    idiv rcx                    ; trunc(-7 / 2)
    mov rdi, rax
    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
-4
-3

Rotates

A rotate is a shift where the bits falling off one end come back in at the other end; nothing is lost. Rotating a 64-bit register by 4 bits moves one hex digit from the top to the bottom — which is exactly how print_hex from the Registers lesson walks through the digits from left to right:

nasm · try it yourself
section .text
global _start
_start:
    mov rdi, 0x123456789ABCDEF0
    rol rdi, 4                  ; top digit 1 moves to the bottom
    call print_hex

    mov rdi, 0x123456789ABCDEF0
    ror rdi, 8                  ; bottom byte F0 moves to the top
    call print_hex

    mov eax, 60
    xor edi, edi
    syscall

; print_hex: print rdi as 16 hex digits with a 0x prefix and a newline
print_hex:
    sub rsp, 32
    mov word [rsp], '0x'        ; the prefix (NASM stores '0' first, then 'x')
    mov ecx, 16                 ; 16 hex digits
.digit:
    rol rdi, 4                  ; bring the top 4 bits to the bottom
    mov eax, edi
    and eax, 0xF                ; isolate one hex digit (0-15)
    cmp eax, 10
    jb .decimal
    add eax, 'A' - '0' - 10     ; 10-15 -> 'A'-'F'
.decimal:
    add eax, '0'
    mov rdx, 18
    sub rdx, rcx                ; position 2..17 in the buffer
    mov [rsp + rdx], al
    dec ecx
    jnz .digit
    mov byte [rsp + 18], 10     ; newline
    mov eax, 1                  ; write(1, rsp, 19)
    mov edi, 1
    mov rsi, rsp
    mov edx, 19
    syscall
    add rsp, 32
    ret
0x23456789ABCDEF01
0xF0123456789ABCDE

Now you can read print_hex: it rotates rdi left by 4 sixteen times; after each rotation the digit that used to be on top sits in the lowest 4 bits, where and eax, 0xF isolates it. After 16 rotations of 4 bits, rdi is back to its original value.

Packing bit fields

Shifts and or combine small values into one. A date can be packed into 16 bits as yyyyyyy mmmm ddddd (7 bits for years since 1980, 4 for the month, 5 for the day) — the format FAT file systems use:

nasm · try it yourself
section .text
global _start
_start:
    ; pack 2026-03-14
    mov eax, 2026 - 1980        ; year field
    shl eax, 4
    or eax, 3                   ; month
    shl eax, 5
    or eax, 14                  ; day
    mov ebx, eax                ; the packed date
    mov edi, eax
    call print_uint

    ; unpack again
    mov edi, ebx
    shr edi, 9                  ; year field
    add edi, 1980
    call print_uint
    mov edi, ebx
    shr edi, 5
    and edi, 0xF                ; month: 4 bits
    call print_uint
    mov edi, ebx
    and edi, 0x1F               ; day: 5 bits
    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
23662
2026
3
14

Shift details worth knowing

  • The count is masked to 6 bits for 64-bit operands (5 bits for 32-bit ones): shl rax, 64 shifts by 0, not "everything out".
  • A shift count of zero leaves the flags unchanged.
  • shl by a constant is a cheap multiply; for constants like 3, 5 and 9, lea is even more flexible.
  • Newer CPUs (BMI2) add shlx, shrx and sarx, which take the count from any register and don't touch the flags.

Test yourself

Which instruction divides a SIGNED value in rax by 4, rounding down?

  1. shr rax, 2
  2. sar rax, 2
  3. shl rax, 2
  4. ror rax, 2

Which register can hold a variable shift count?

  1. any register
  2. al
  3. cl
  4. dl

What is 0b1001 0110 rotated left by 1 (8 bits)?

  1. 0b0010 1100
  2. 0b0010 1101
  3. 0b1100 1011
  4. 0b0100 1011