nasmdocumentationnasmtutorial- x86 Assembly Guide
- x86_64 NASM Assembly Quick Reference
- Basic differences between x86 Assembly and X86-64 Assembly
- github.com/0xAX/asm
My machine setting as of today is Linux 64-bit:
$ cat /proc/cpuinfo | grep "model name" | head -1; model name : Intel(R) Core(TM) i7-4750HQ CPU @ 2.00GHz $ lsb_release -a Distributor ID: Ubuntu Description: Ubuntu 14.04.3 LTS Release: 14.04 Codename: trusty $ uname -sm Linux x86_64
x86-64 (also known as x64, x86_64 and
AMD64) is the 64-bit version of the x86 instruction set.x86-64 by Wikipedia
And we will use nasm to write assembly:
What Is NASM?
The Netwide Assembler, NASM, is an 80x86 and x86-64 assembler designed for portability and modularity. It supports a range of object file formats, including Linux and *BSD a.out, ELF, COFF, Mach-O, Microsoft 16-bit OBJ, Win32 and Win64. It will also output plain binary files. Its syntax is designed to be simple and easy to understand, similar to Intel's but less complex. It supports all currently known x86 architectural extensions, and has strong support for macros.
To install:
sudo apt-get install nasm;; ------------------------------------- ; nasm -f elf64 -o hello.o hello.asm ; ld -o hello hello.o ; ./hello ; ------------------------------------- section .data ; data section stores constants myMessage db "Hello World!" ; 1. myMessage is label ; 2. db is instruction ; 3. "Hello World!" is operand(object) section .text ; text section contains code global _start ; beginning of exported symbols _start: mov rax, 1 ; copy 1 to rax (temporary register) mov rdi, 1 ; copy 1 to rdi (1st argument) mov rsi, myMessage ; copy myMessage to rsi (2nd argument) mov rdx, 13 ; copy 13 to rdx (3rd argument) syscall ; invoke system call write mov rax, 60 ; copy 60 to rax (temporary register) mov rdi, 0 ; copy 0 to rdi (1st argument) syscall ; invoke system call write
And then to build:
nasm -f elf64 -o hello.o hello.asm;
nasm command builds an object file hello.o from hello.asm
using the elf64 object file format. object file is a file
that contains object code, mostly in machine code, and information
for linkers.
And then to link:
ld -o hello hello.o;
ld is a linker that takes the object file hello.o generated
by the compiler and combines it into a single executable file.
And then to execute:
./hello
This prints Hello World!.
section .data ; data section stores constants myMessage db "Hello World!" ; 1. myMessage is label ; 2. db is instruction ; 3. "Hello World!" is operand(object)
And:
section .text ; text section contains code
And:
global _start _start:
global directive exports symbols in the code to object code.
And _start marks the beginning of exported symbols.
And:
mov rax, 1 ; copy 1 to rax (temporary register) mov rdi, 1 ; copy 1 to rdi (1st argument) mov rsi, myMessage ; copy myMessage to rsi (2nd argument) mov rdx, 13 ; copy 13 to rdx (3rd argument)
mov instruction copies data into its first operand, from the second operand.
move A, B copies the value in B into A.
This explains following are registers in 64-bit mode. Note that registers in assembly language are not case-sensitive:
RAX,RCX,RDX,RBX,RSP,RBP,RSI,RDI,R8-R15
And register is:
In computer architecture, a processor register is a small amount of storage available as part of a digital processor, such as a central processing unit (CPU). Such registers are typically addressed by mechanisms other than main memory and can be accessed faster. Almost all computers, load-store architecture or not, load data from a larger memory into registers where it is used for arithmetic, manipulated or tested by machine instructions. Manipulated data is then often stored back into main memory, either by the same instruction or a subsequent one. Modern processors use either static or dynamic RAM as main memory, with the latter usually accessed via one or more cache levels.
Processor registers are normally at the top of the memory hierarchy, and provide the fastest way to access data. The term normally refers only to the group of registers that are directly encoded as part of an instruction, as defined by the instruction set.
Processor register by Wikipedia
So:
move rax, 1copies 1 intoraxregister.mov rdi, 1copies 1 intordiregister.mov rsi, myMessagecopiesmyMessageintorsiregister.mov rdx, 13copies 13 intordxregister.
Then what are rax, rdi, rsi, rdx? They are different registers.
https://www.exploit-db.com/papers/13136 explains:
rax: temporary register; with variable arguments.rdi: used to pass 1st argument to functions.rsi: used to pass 2nd argument to functions.rdx: used to pass 3rd argument to functions; 2nd return.
Then what function are we passing 1, myMessage, 13, etc. to?
It calls the operating system to do the write.
And:
syscall ; invoke system call write
syscall in nasm invokes the operating system call write.
I am running this with Linux, which is written in C.
Then the system call write invokes
this:
ssize_t write(int fd, const void *buf, size_t nbytes);
fd: file descriptor.0is for standard input,1for standard output, and2for standard error.*buf: pointer to a character array that stores data from the file pointed byfd.nbytes: number of bytes to write onto the file, from the character array.
And:
mov rax, 60 ; copy 60 to rax (temporary register) mov rdi, 0 ; copy 0 to rdi (1st argument) syscall ; invoke system call write
mov rax, 60: copies60intorax, which means exit syscall.mov rdi, 0: copies0intordi, which means successful program exit.