Clearing Registers in x86-64 Assembly#
Methods for setting registers to zero in NASM.
Fully working program that only purpose is to clear registers…
section .text
global _start
_start:
xor rax, rax ; Clear RAX
mov rax, 60 ; sys_exit
xor rdi, rdi ; exit code 0
syscall
Overview#
Clearing registers (setting them to zero) is a fundamental operation
Methods#
XOR Method#
xor rax, rax ; Clear RAX - preferred method
SUB Method#
sub rax, rax ; Alternative method
MOV Method#
mov rax, 0 ; Less efficient - larger instruction
Performance Comparison#
Method |
Code |
Size |
|---|---|---|
XOR |
|
3 bytes |
SUB |
|
3 bytes |
MOV |
|
7-10 bytes |
On this page