Introduction to x64 assembly
x64 assembly language is a low level programming language which is used to write software that runs directly on 64 bit x86 processors. It consist a series of instruction that are executed by the cpu to perform specific tasks.
(x64 == AMD64 == x86-64)
- Table of contents
Prerequisite - basic CPU-architecture , basic programming concepts
x64 Assembly Highlights
- x86_64 assembly language designed for the x86-64 cpu architecture.
- x64 is abbreviation for x86 64bit architecture
- Some processors who uses x86 architecture ➼ Intel core i3,i5,i7; AMD ryzen5,ryzen7 etc.
- The x64 assembly language is backward compatible with the x86 architecture, means it can execute code written for the x86 architecture(64/32/16/8 bits).
- x86_64 architecture was developed by AMD later adopted by the intel
- Some popular assemblers are NASM(Netwide Assembler), GAS(GNU Assembler), FASM(Flat Assembler)
Now we know that x64 assembly is a language based on instruction sets of a 64 bit extention of x86 cpu architecture. Lets make a dive in , setting up environment, understanding code and doing some practicals.
Tools for x64 assembly
- Assembler : Assembler is used to convert assembly code into binary (machine code). ex: NASM (Netwide Assembler) or GAS (GNU Assembler)
- IDE/Editer : A text editor utility for writing code. ex: VSCode , Vim , nano etc.
- Debugger : To analyze the code/errors. ex: GDB (GNU Debugger) or LLDB.
Structure of a x64 assembly code
A x64 assembly code generally have 3 sections -
- .data section
- .bss section
- .text section
(You can find other sections also like: .comment , .rodata , .note and you can create your own custom section.)
.data section
The .data section in x64 assembly is used to declare and initialize global and static variables . These variables are stored in memory and are writable during the program's execution.
section .data
message db "Hello, World!", 0 ; message→variable_name, db→size_type , "Enter your name: "→value
message_len equ $ - message ; message_len→var_name, $→current_address, message→starting_address_of_message
.bss section
bss section defines the uninitialized variables or buffers that will be allocated memory at runtime.
section .bss
name resb 32 ; Reserve 32 bytes for user input (used here only for showcase)
.text section
text section is the main section in which all the struction and program logics are defined. Learn about → Registers and Instructions
section .text
global _start ; making _start label globally accessable
_start:
; Write message to stdout
mov rax, 1 ; syscall: write
mov rdi, 1 ; File descriptor: stdout
lea rsi, [message] ; Address of message (use lea!)
mov rdx, len ; Length of the message
syscall
; Exit program
mov rax, 60 ; syscall: exit
xor rdi, rdi ; Exit code 0
syscall
Execute x64 assembly code
- Step 1 : Save the assembly code into a file and name it with a .asm extention. like: helloWorld.asm
- Step 2 : Assemble the asm file into a object file using a assembler, like: NASM.
command →nasm -f elf64 -o helloWorld.o helloWorld.asm
-f flag is used to select the format of output. like: win64, elf64 - Step 3 : Create an executable from the object file. example: ld linker.
command →ld -o helloWorld helloWorld.o
this will create a executable file helloWorld that can be run into linux using ./helloWorld.
Frequently Asked Questions
What is x64 assembly language?
x64 assembly language is a low-level programming language designed for 64-bit x86 processors. It is used to write software that runs directly on hardware, interacting with the CPU through specific instructions.
What are the basic sections of x64 assembly code?
The three main sections in x64 assembly code are the .data section (for global/static variables), .bss section (for uninitialized variables), and .text section (where program logic is defined).
What tools are used in x64 assembly development?
Key tools include an assembler (e.g., NASM, GAS), an IDE or text editor (e.g., VSCode, Vim), and a debugger (e.g., GDB, LLDB) for writing and analyzing code.
How do I execute x64 assembly code?
First, write your assembly code in a file with the .asm extension. Then, assemble the code with an assembler (e.g., NASM), link it to create an executable (using LD), and run the program in your system's terminal.
What is the difference between x64 and x86 assembly?
x64 assembly is designed for 64-bit x86 processors, while x86 assembly was originally designed for 32-bit processors. x64 supports both 32-bit and 64-bit code execution, providing backward compatibility.
Can I use x64 assembly code on Intel and AMD processors?
Yes, x64 assembly code works on both Intel and AMD processors that support the x86-64 architecture, including popular models like Intel Core i3, i5, i7, and AMD Ryzen 5, 7.