- Arm Assembly는 여기에 정리중
- x86_64 Assembly Tutorial
- 어셈블리 비교하기 (x86_64 vs arm vs NASM 기타 등등)
General Instruction |
Function Call Instructions |
Stack Instructions |
---|---|---|
mov jmp add sub |
call ret |
push pop |
- 출처 (29분 14초)
@m_ou_se ⚛️📋 I made an overview of the ARMv8 and x86-64 machine instructions for all the common atomic operations:
https://twitter.com/m_ou_se/status/1590333332012662784/photo/1
https://www.gnu.org/software/ddd/manual/
https://github.com/BetelGeuseee/x86-assembly-yasm
https://youtube.com/playlist?list=PLcMveqN_07mb4_M06LrKJK5LHVKGFyICB&si=7PUFD9S4OuLJQCHv
- 뒤에 최적화 옵션
-C opt-level=3 --target i686-unknown-linux-gnu
set tabstop=4
set shiftwidth=4
:set ft=nasm " assembly highlight syntax
-
https://github.com/bergercookie/asm-lsp
- Using cargo
- Install using the cargo package manager, either from crates.io or from github:
- Using cargo
cargo install asm-lsp
# or to get the latest version from github
cargo install --git https://github.com/bergercookie/asm-lsp
-
https://marketplace.visualstudio.com/items?itemName=13xforever.language-x86-64-assembly
-
ASM Code Lens
To debug NASM you might want to use DDD. The following course teaches assembly for Intel/Linux using NASM and DDD, you might want to have a look at it as you might find it helpful:
Getting started with NASM and DDD --> https://www.youtube.com/watch?v=YyovCxsMVio
Assembly integer arithmetic --> https://www.youtube.com/watch?v=-KfZaJclqk4
Assembly floating-point arithmetic --> https://www.youtube.com/watch?v=n1gIv40VSgA
Assembly control instructions --> https://www.youtube.com/watch?v=s38DcLv1wYk
Assembly and process stack --> https://www.youtube.com/watch?v=7nPru8b7SjY
Assembly functions --> https://www.youtube.com/watch?v=QOPOeDPlNZo
Stack buffer overflow --> https://www.youtube.com/watch?v=BW2obfJtkPw
System services --> https://www.youtube.com/watch?v=qzTlkJsq3Xo
https://www.reddit.com/r/asm/comments/ba4qi9/debugging_nasm/
https://github.com/compilepeace/SHELLCODING_INTEL_x86-64/
https://youtu.be/aMSFaAcup50?si=91Qr4sMRqIsa5_TT
- Assembly Language in 100 Seconds
https://youtu.be/75gBFiFtAb8?si=skDTgz3WiarSaKbY
http://ref.x86asm.net/coder64.html
1.4.4.1 YASM References The YASM assembler is an open source assembler commonly available on Linux-based systems. The YASM references are as follows:
-
Yasm Web Site http://yasm.tortall.net/
-
Yasm Documentation http://yasm.tortall.net/Guide.html
Additional information regarding YASM may be available a number of assembly language sites and can be found through an Internet search.
The DDD debugger is an open source debugger capable of supporting assembly language.
- DDD Web Site https://www.gnu.org/software/ddd/
- DDD Documentation https://www.gnu.org/software/ddd/manual/
Additional information regarding DDD may be at a number of assembly language sites and can be found through an Internet search.
- x86_64_Assembly Language Programming with Ubuntu
http://www.egr.unlv.edu/~ed/assembly64.pdf
$ sudo apt install nasm
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
nasm
0 upgraded, 1 newly installed, 0 to remove and 11 not upgraded.
Need to get 375 kB of archives.
After this operation, 3,345 kB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu jammy/universe amd64 nasm amd64 2.15.05-1 [375 kB]
Fetched 375 kB in 2s (217 kB/s)
Selecting previously unselected package nasm.
(Reading database ... 263485 files and directories currently installed.)
Preparing to unpack .../nasm_2.15.05-1_amd64.deb ...
Unpacking nasm (2.15.05-1) ...
Setting up nasm (2.15.05-1) ...
Processing triggers for man-db (2.10.2-1) ...
- elf64
nasm -felf64 add.asm && ld add.o && ./a.out
- elf32
$ nasm -f elf32 -o hello.o hello.asm
$ ls
hello.asm hello.o
$ ld -m elf_i386 -o hello hello.o
$ ls
hello hello.asm hello.o
$ ./hello
Hello World!
- Nasm Tutorial
; World World x86_64 Intel Cpu Assembly
; hello.asm
global _start
section .text:
_start:
mov eax, 0x4 ; use the write syscall
mov ebx, 1 ; use stdout as the fd
mov ecx, message ; use the message as the buffer
mov edx, message_length ; and supply the length
int 0x80 ; invoke the syscall
; now gracefully exit
mov eax, 0x1
mov ebx, 0
int 0x80
section .data:
message: db "Hello World!", 0xA
message_length equ $-message
choco install nasm
-
This package uses the official nasm Windows installer, which doesn't add nasm to PATH. You may voice out your request in the nasm issue tracker, in which there is an existing issue filed
-
PATH설정해줘야함. 나 같은 경우는 "C:\Program Files\NASM" 여기 PATH 설정함.
https://community.chocolatey.org/packages/nasm
https://doc.rust-lang.org/reference/inline-assembly.html
https://doc.rust-lang.org/rust-by-example/unsafe/asm.html
$ python
Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 0x0a
10
>>> chr(0x0a)
'\n'
>>>
====================================================
Data Storage Sizes | ||
Storage | Size(bits) | Size(bytes) |
Byte | 8-bits | 1 byte |
Word | 16-bits | 2 bytes |
Double-word | 32-bits | 4 bytes |
Quadword | 64-bits | 8 bytes |
Double quadword | 128-bits | 16 bytes |
http://www.egr.unlv.edu/~ed/assembly64.pdf
Data Storage Sizes(page9.) C/C++ declarations are mapped as follows: |
|||
C/C++ Declaration |
Storage | Size(bits) | Size(bytes) |
char | Byte | 8-bits | 1 byte |
Word | 16-bits | 2 bytes | |
Double-word | 32-bits | 4 bytes | |
Quadword | 64-bits | 8 bytes | |
Double quadword | 128-bits | 16 bytes |
- General-purpose register layout
- The x86-64 general-purpose registers are
- aliased: each has multiple names, which refer to overlapping bytes in the register.
General-purpose register layout | |||||||
rax <-- 64 bits --> |
|||||||
8bit | 8bit | 8bit | 8bit | eax (Lowest 32-bits <---------------- ----------------------> |
|||
8bit | 8bit | 8bit | 8bit | 8bit | 8bit | <--- ax ---> Lowest 16-bits |
|
8bit | 8bit | 8bit | 8bit | 8bit | 8bit | ah <-- 8-bits --> |
al <-- 8-bits --> |
Byte 7 | Byte 6 | Byte 5 | Byte 4 | Byte 3 | Byte 2 | Byte 1 | Byte 0 |
General Purpose Registers(GPRs) | |||
64-bit register | Lowest 32-bits |
Lowest 16-bits |
Lowest 8-bits |
rax | eax | ax | al |
rbx | ebx | bx | bl |
rcx | ecx | cx | cl |
rdx | edx | dx | dl |
rsi | esi | si | sil |
rdi | edi | di | dil |
rbp | ebp | bp | bpl |
rsp | esp | sp | spl |
r8 | r8d | r8w | r8b |
r9 | r9d | r9w | r9b |
r10 | r10d | r10w | r10b |
r11 | r11d | r11w | r11b |
r12 | r12d | r12w | r12b |
r13 | r13d | r13w | r13b |
r14 | r14d | r14w | r14b |
r15 | r15d | r15w | r15b |
http://www.egr.unlv.edu/~ed/assembly64.pdf
- Stack Pointer Register
Stack Pointer Register | RSP |
- One of the CPU registers, rsp, is used to point to the current top of the stack. The rsp register should not be used for data or other uses. Additional information regarding the stack and stack operations is provided in Chapter 9, Process Stack.
Base Pointer Register | RBP |
- One of the CPU registers, rbp, is used as a base pointer during function calls. The rbp register should not be used for data or other uses. Additional information regarding the functions and function calls is provided in Chapter 12, Functions.
Instruction Pointer Register | RIP |
- In addition to the GPRs, there is a special register, rip, which is used by the CPU to point to the next instruction to be executed. Specifically, since the rip points to the next instruction, that means the instruction being pointed to by rip, and shown in the debugger, has not yet been executed. This is an important distinction which can be confusing when reviewing code in a debugger.
-
Assembly Language & Computer Architecture | MIT OpenCourseWare(33min05sec)
-
The flag register, rFlags, is used for status and CPU control information
-
This register stores status information about the instruction that was just executed. Of the 64-bits in the rFlag register, many are reserved for future use.
-
Arithmetic and logic operations update status flags in the RFLAGS register.
decq %rbx
jne .LBB7_1
decq %rbx
; Decrement%rbx
, and set ZF if the result is 0.
Flag Register (rFlags) RFLAGS Register |
|||
Name Description |
Symbol or (Abbreviation) |
Bit(s) | Use |
Carry | CF | 0 | Used to indicate if the previous operation resulted in a carry. |
Reserved | 1 | ||
Parity | PF | 2 | Used to indicate if the last byte has an even number of 1's (i.e., even parity). |
Reserved | 3 | ||
Adjust | AF | 4 | Used to support Binary Coded Decimal operations. |
Reserved | 5 | ||
Zero | ZF | 6 | Used to indicate if the previous operation resulted in a zero result. |
Sign | SF | 7 | Used to indicate if the result of the previous operation resulted in a 1 in the most significant bit (indicating negative in the context of signed data). |
Trap | TF | 8 | |
Interrupt enable |
IF | 9 | |
Direction | DF | 10 | Used to specify the direction (increment or decrement) for some string operations. |
Overflow | OF | 11 | Used to indicate if the previous operation resulted in an overflow. |
System flags or reserved |
12 - 63 |
- Assembly Language & Computer Architecture(15min33sec)
Number | Width (bits) |
Names(s) | Purpose | ★ | ||||
16 | 64 | (many) | General-purpose registers | ★ | ||||
6 | 16 | %ss,%[c-g]s | Segment registers | |||||
1 | 64 | RFLAGS | Flags register | ★ | ||||
1 | 64 | %rip | Instruction pointer register | ★ | ||||
7 | 64 | %cr[0-4,8], %xcr0 | Control registers | |||||
8 | 64 | %mm[0-7] | MMX registers | |||||
1 | 32 | mxcsr | SSE2 control register | |||||
16 | 128 | %xmm[0-15] | XMM registers (for SSE) | ★ | ||||
256 | %ymm[0-15] | YMM registers (for AVX) | ★ | |||||
8 | 80 | %st([0-7]) | x87 FPU data registers | |||||
1 | 16 | x87 CW | x87 FPU control register | |||||
1 | 16 | x87 SW | x87 FPU status register | |||||
1 | 48 | x87 FPU instruction pointer register |
||||||
1 | 48 | x87 FPU data operand pointer register |
||||||
1 | 16 | x87 FPU tag register | ||||||
1 | 11 | x87 FPU opcode register |
x86-64 Data Types | ||||
C declaration | C constant |
x86-64 size (bytes) |
Assembly suffix |
x-86-64 data type |
char | 'c' | 1 | b | Byte |
short | 172 | 2 | w | Word |
int | 172 | 4 | l or d | Double word |
unsigned int |
172U | 4 | l or d | Double word |
long | 172L | 8 | q | Quad word |
unsigned long | 172UL | 8 | q | Quad word |
char * | "6.172" | 8 | q | Quad word |
float | 6.172F | 4 | s | Single precision |
double | 6.172 | 8 | d | Double precision |
long double | 6.172L | 16(10) | t | Extended precision |
28min30sec https://youtu.be/L1ung0wil9Y?si=XEBdCmwbP48LLYOE
- Assembly Language & Computer Architecture(25min33sec)
Opcodes | |||
Type of peration | Example | ||
Data movement |
Move | mov | |
Conditional move |
cmov | ||
Sign or zero extension |
movs, movz | ||
Stack | push, pop | ||
Arithmetic and logic |
Integer arithmetric |
add, sub, mul, imul,div, idiv, lea,sal, sar, shl, shr, rol, ror, inc, dec,neg |
|
Binary logic | and, or, xor, not | ||
Boolean logic | test, cmp | ||
Control transfer |
Unconditional jump |
jmp | |
Conditional jump |
j<condition> | ||
Subroutines | call, ret |
Condition Codes | ||
Condition code | Translation | RFLAGS status flags checked |
a | if above | CF = 0 and ZF = 0 |
ae | if above or equal | CF = 0 |
c | on carry | CF = 1 |
e | if equal | ZF = 1 |
ge | if greater or equal | SF = OF |
ne | if not equal | ZF = 0 |
o | on overflow | OF = 1 |
z | if zero | ZF = 1 |
stateDiagram-v2
state CPU__Chip {
Core__0 --> L1_Cache
Core__1 --> L1_Cache_
L1_Cache --> L2_Cache
L1_Cache_ --> L2_Cache
}
L2_Cache --> BUS
General Memory Layout | |
high memory low memory |
stack . . . heap |
BSS - uninitialized data | |
data | |
text (code) | |
reserved |
http://www.egr.unlv.edu/~ed/assembly64.pdf
Memory Hierarchy | |
Smaller, faster, and more expensive Larger,slower, and less expensive |
CPU Registers |
Cache | |
Primary Storage Main Memory(RAM) |
|
Secondary Storage (disk drives, SSD's, etc.) |
|
Tertiary Storage (remote storage, optical, backups,etc.) |
http://www.egr.unlv.edu/~ed/assembly64.pdf
Memory Unit | Example Size | Typical Speed |
Registers | 16, 64-bit registers | ~1 nanoseconds13 |
Cache Memory | 4 - 8+ Megabytes14 (L1 and L2) |
~5-60 nanoseconds |
Primary Storage (i.e., main memory) |
2 - 32+ Gigabytes15 | ~100-150 nanoseconds |
Secondary Storage (i.e., disk, SSD's, etc.) |
500 Gigabytes - 4+ Terabytes16 |
~3-15 milliseconds17 |
Refer to the following sections for a series of examples using various data types.
The supported data types are as follows:
section .data All initialized variables and constants |
|
Declaration | |
db | 8-bit variable(s) |
dw | 16-bit variable(s) |
dd | 32-bit variable(s) |
dq | 64-bit variable(s) |
ddq | 128-bit variable(s) -> integer |
dt | 128-bit variable(s) -> float |
http://www.egr.unlv.edu/~ed/assembly64.pdf
ex)
; The general format is:
; <variableName> <dataType> <initialValue>
section .data
bVar db 10 ; byte variable
cVar db "H" ; single character
strng db "Hello World" ; string
wVar dw 5000 ; 16-bit variable
dVar dd 50000 ; 32-bit variable
arr dd 100, 200, 300 ; 3 element array
flt1 dd 3.14159 ; 32-bit float
qVar dq 1000000000 ; 64-bit variable
The value specified must be able to fit in the specified data type. For example, if the value of a byte sized variables is defined as 500, it would generate an assembler error.
Uninitialized data is declared in the "section .bss" section.
The supported data types are as follows:
section .bss Uninitialized data |
|
Declaration | |
resb | 8-bit variable(s) |
resw | 16-bit variable(s) |
resd | 32-bit variable(s) |
resq | 64-bit variable(s) |
resdq | 128-bit variable(s) |
ex)
; The general format is:
; <variableName> <resType> <count>
section .bbs
bArr resb 10 ; 10 element byte array
wArr resw 50 ; 50 element word array
dArr resd 100 ; 100 element double array
qArr resq 200 ; 200 element quad array
; The allocated array is not initialized to any specific value.
http://www.egr.unlv.edu/~ed/assembly64.pdf
; Code Section
section .text
global _start
_start:
; Simple example demonstrating basic program format and layout.
; Ed Jorgensen
; July 18, 2014
; ************************************************************
; Some basic data declarations
section .data
; -----
; Define constants
EXIT_SUCCESS equ 0 ; successful operation
SYS_exit equ 60 ; call code for terminate
; -----
; Byte (8-bit) variable declarations
bVar1 db 17
bVar2 db 9
bResult db 0
; -----
; Word (16-bit) variable declarations
wVar1 dw 17000
wVar2 dw 9000
wResult dw 0
; -----
; Double-word (32-bit) variable declarations
dVar1 dd 17000000
dVar2 dd 9000000
dResult dd 0
; -----
; quadword (64-bit) variable declarations
qVar1 dq 170000000
qVar2 dq 90000000
qResult dq 0
; ************************************************************
; Code Section
section .text
global _start
_start:
; Performs a series of very basic addition operations
; to demonstrate basic program format.
; ----------
; Byte example
; bResult = bVar1 + bVar2
mov al, byte [bVar1]
add al, byte [bVar2]
mov byte [bResult], al
; ----------
; Word example
; wResult = wVar1 + wVar2
mov ax, word [wVar1]
add ax, word [wVar2]
mov word [wResult], ax
; ----------
; Double-word example
; dResult = dVar1 + dVar2
mov eax, dword [dVar1]
add eax, dword [dVar2]
mov dword [dResult], eax
; ----------
; Quadword example
; qResult = qVar1 + qVar2
mov rax, qword [qVar1]
add rax, qword [qVar2]
mov qword [qResult], rax
; ************************************************************
; Done, terminate program.
last:
mov rax, SYS_exit ; Call code for exit
mov rdi, EXIT_SUCCESS ; Exit program with success
syscall