1 LẬP TRÌNH ASSEMBLY Đỗ Thanh Nghị dtnghi@cit.vn 12-2019 Giới thiệu lập trình assembly 2 Hợp ngữ (assembly language) và kiến trúc máy tính CISC (complex instruction set computer): x86 RISC (reduced instruction set computer): ARM, MIPS Lập trình cho kiến trúc x86 Microsoft Assembler (MASM) Borland Turbo Assembler (TASM) The GNU assembler (GAS) The Netwide Assembler (NASM) Chương trình assembly 3 Chương trình có 3 phần: data, bss, text data: khai báo dữ liệu khởi tạo, hằng section .data bss: khai báo biến section .bss text: bắt đầu chương trình, khai báo biến toàn cục _start để kernel biết đầu chương trình section .text 1/1/2020 Online Asm Compiler - Online Asm Editor - Online Asm IDE - Asm Coding Online - Practice Asm Online - Execute Asm Online - hello.asm Assembly Basic Program (Nasm v2.01) Execute | Share main.asm STDIN 4 1 section .text 2 global _start ;must be declared for linker (ld) 3 4 _start: ;tells linker entry point 5 mov edx,len ;message length 6 mov ecx,msg ;message to write 7 mov ebx,1 ;file descriptor (stdout) 8 mov eax,4 ;system call number (sys_write) 9 int 0x80 ;call kernel 10 11 mov eax,1 ;system call number (sys_exit) 12 int 0x80 ;call kernel 13 14 section .data 15 msg db 'Hello, world!', 0xa ;string to be printed 16 len equ $ - msg ;length of the string 17 18 19 Hợp dịch, liên kết 5 Hợp dịch tập tin .o nasm -f elf32 hello.asm (-f elf64) Nếu không có lỗi => hello.o Liên kết ld -m elf_i386 -s -o hello hello./hello Hello, world! Hợp dịch, liên kết 6 Lập trình online https://www.com/compile_assembly_online.5 Memory Layout Bộ nhớ The general memory layout for a program7 is as shown: high memory stack. heap BSS – uninitialized data data text (code) low memory reserved Illustration 4: General Memory Layout The reserved section is not available to user programs. The text (or code) section is where the machine language11 (i., the 1's and 0's that represent the code) is stored. The data section is where the initialized data is stored.
This includes declared variables that Các phân đoạn 8 Có thể thay thế section bằng các segment data segment: section .data và section .bss code segment: section .text stack: chứa dữ liệu truyền cho hàm, thủ tục Lập trình assembly 9 Các thanh ghi 10 Thanh ghi tổng quát Dữ liệu - 16 bits: AX, BX, CX, DX; 32 bits: EAX, EBX, ECX, EDX; 64 bits: RAX, RBX, RCX, RDX 16 bits => 32 bits (Extended) => 64 bits (Register) Con trỏ - lệnh: IP, EIP, RIP; ngăn xếp: SP, ESP, RSP; nền: BP, EBP, RBP Chỉ mục - nguồn: SI, ESI, RSI; đích: DI, EDI, RDI Thanh ghi điều khiển: Overflow Flag (OF), Zero Flag (ZF), Sign Flag (SF), Carry Flag (CF), Parity Flag (PF), Direction Flag (DF), Interrupt Flag (IF), Trap Flag (TF), Thanh ghi đoạn: Code Segment (CS), Data segment (DS), Stack Segment (SS) Các thanh ghi 11t3e `2;Bbi2`b kyyj Ĝ M2r `2;B 16 bits: AX, BX, CX, DX, MK2 7Q` BP, SI, DI, SP 32 bits: EAX, EBX, ECX, EDX, EBP, ESI, EDI, ESP 64 bits: RAX, RBX, RCX, RDX, RBP, RSI, RDI, RSP 1/1/2020 Online Asm Compiler - Online Asm Editor - Online Asm IDE - Asm Coding Online - Practice Asm Online - Execute Asm Online - Compile Asm … Các thanh ghi Assembly Segment Registers (Nasm v2.01) Execute | Share main.asm STDIN 12 Result 1 section .text 2 global _start ;must be declared for linker (gcc) 3 4 _start: ;tell linker entry point 5 mov edx,len ;message length 6 mov ecx,msg ;message to write 7 mov ebx,1 ;file descriptor (stdout) 8 mov eax,4 ;system call number (sys_write) 9 int 0x80 ;call kernel 10 11 mov edx,9 ;message length 12 mov ecx,s2 ;message to write 13 mov ebx,1 ;file descriptor (stdout) 14 mov eax,4 ;system call number (sys_write) 15 int 0x80 ;call kernel 16 17 mov eax,1 ;system call number (sys_exit) 18 int 0x80 ;call kernel 19 20 section .data 21 msg db 'Displaying 9 stars',0xa ;a message 22 len equ $ - msg ;length of message 23 s2 times 9 db '*' 24 25 Lời gọi hệ thống Linux 13 Thực hiện lời gọi hệ thống Đặt số hiệu lời gọi vào EAX Tham số cho lời gọi trong EBX, ECX, etc Gọi ngắt 80h Kết quả trả về trong EAX Lời gọi hệ thống Linux: ví dụ 14 Thoát khỏi hệ thống Ghi chuỗi msg (dài 4) ra màn hình Lời gọi hệ thống Linux: ví dụ 1/1/2020 Online Asm Compiler - Online Asm Editor - Online Asm IDE - Asm Coding Online - Practice Asm Online - Execute Asm Online - Compile Asm … Assembly System Calls (Nasm v2.01) Execute | Share main.asm STDIN 15 1 section .data ;Data segment 2 userMsg db 'Please enter a number: ' ;Ask the user to enter a number 3 lenUserMsg equ $-userMsg ;The length of the message 4 dispMsg db 'You have entered: ' 5 lenDispMsg equ $-dispMsg 6 7 section .bss ;Uninitialized data 8 num resb 5 9 10 section .text ;Code Segment 11 global _start 12 13 _start: ;User prompt 14 mov eax, 4 15 mov ebx, 1 16 mov ecx, userMsg 17 mov edx, lenUserMsg 18 int 80h 19 20 ;Read and store the user input 21 mov eax, 3 22 mov ebx, 0 2 23 mov ecx, num 24 mov edx, 5 ;5 bytes (numeric, 1 for sign) of that information 25 int 80h 26 27 ;Output the message 'The entered number is: ' Execute | Share main.asm STDIN 21 mov eax, 3 Lời gọi hệ thống Linux: ví dụ 22 mov ebx, 2 23 mov ecx, num 24 mov edx, 5 ;5 bytes (numeric, 1 for sign) of that information 25 int 80h 16 26 27 ;Output the message 'The entered number is: ' 28 mov eax, 4 29 mov ebx, 1 30 mov ecx, dispMsg 31 mov edx, lenDispMsg 32 int 80h 33 34 ;Output the number entered 35 mov eax, 4 36 mov ebx, 1 37 mov ecx, num 38 mov edx, 5 39 int 80h 40 41 ; Exit code 42 mov eax, 1 43 mov ebx, 0 44 int 80h 45 46 47 Mode địa chỉ 17 Các mode địa chỉ Địa chỉ thanh ghi Địa chỉ tức thời Mode địa chỉ 18 Các mode địa chỉ Địa chỉ bộ nhớ Lệnh mov 19 Cú pháp: mov dest, src 1/2/2020 Online Asm Compiler - Online Asm Editor - Online Asm IDE - Asm Coding Online - Practice Asm Online - Execute Asm Online - Compile Asm … Lệnh mov:Mode Assembly Addressing ví (Nasm dụ v2.01) Execute | Share main.asm STDIN 20 Result 1 section .text 2 global_start ;must be declared for linker (ld) 3 _start: ;tell linker entry point 4 5 ;writing the name 'Zara Ali' 6 mov edx,9 ;message length 7 mov ecx, name ;message to write 8 mov ebx,1 ;file descriptor (stdout) 9 mov eax,4 ;system call number (sys_write) 10 int 0x80 ;call kernel 11 12 mov [name], dword 'Nuha' ; Changed the name to Nuha Ali 13 14 ;writing the name 'Nuha Ali' 15 mov edx,8 ;message length 16 mov ecx,name ;message to write 17 mov ebx,1 ;file descriptor (stdout) 18 mov eax,4 ;system call number (sys_write) 19 int 0x80 ;call kernel 20 21 mov eax,1 ;system call number (sys_exit) 22 int 0x80 ;call kernel 23 24 section .data 25 name db 'Zara Ali ' Biến 21 Cú pháp: [var-name] def-directive init-value [,init-value]. Biến: ví dụ 22 Cú pháp: [var-name] def-directive init-value [,init-value]. Biến: ví dụ 24 Cú pháp: [var-name] def-directive init-value [,init-value]. Hằng 25 Cú pháp: [const-name] equ express Tính toán số học 26 Các phép toán số học inc dest dec dest add/sub dest, src ; dest = dest add/sub src ; Register to register ; Memory to register ; Register to memory ; Register to constant data ; Memory to constant data MUL/IMUL multiplier DIV/IDIV divisor 1/2/2020 Online Asm Compiler - Online Asm Editor - Online Asm IDE - Asm Coding Online - Practice Asm Online - Execute Asm Online - C Tính toán số Sum Assembly học: vív2.01) (Nasm dụ 27 Execute | Share main.asm STDIN 1 section .text 2 global _start ;must be declared for using gcc 3 4 _start: ;tell linker entry point 5 mov eax,'3' 6 sub eax, '0' 7 8 mov ebx, '4' 9 sub ebx, '0' 10 add eax, ebx 11 add eax, '0' 12 13 mov [sum], eax 14 mov ecx,msg 15 mov edx, len 16 mov ebx,1 ;file descriptor (stdout) 17 mov eax,4 ;system call number (sys_write) 18 int 0x80 ;call kernel 19 20 mov ecx,sum 21 mov edx, 1 14 mov ecx,msg 15 mov edx, len 16 mov ebx,1 ;file descriptor (stdout) 17 mov Tính toán số học: ví dụ eax,4 ;system call number (sys_write) 18 int 0x80 ;call kernel 19 28 20 mov ecx,sum 21 mov edx, 1 22 mov ebx,1 ;file descriptor (stdout) 23 mov eax,4 ;system call number (sys_write) 24 int 0x80 ;call kernel 25 26 mov eax,1 ;system call number (sys_exit) 27 int 0x80 ;call kernel 28 29 section .data 30 msg db "The sum is:", 0xA,0xD 31 len equ $ - msg 32 segment .bss 33 sum resb 1 Tính toán số học 29 Các phép toán số học MUL/IMUL multiplier 1/2/2020 Online Asm Compiler - Online Asm Editor - Online Asm IDE - Asm Coding Online - Practice Asm Online - Execute Asm Online - Comp Tính toán số MUL/IMUL Assembly học: ví dụ (Nasm v2.01) Instruction Execute | Share main.asm 30 STDIN 1 section .text $ 2 global _start ;must be declared for using gcc $ 3 T 4 _start: ;tell linker entry point 5 6 6 mov al,'3' 7 sub al,'0' 8 9 mov bl,'2' 10 sub bl,'0' 11 mul bl 12 add al,'0' 13 14 mov [res],al 15 mov ecx,msg 16 mov edx,len 17 mov ebx,1 ;file descriptor (stdout) 18 mov eax,4 ;system call number (sys_write) 19 int 0x80 ;call kernel 20 21 mov ecx,res 22 mov edx,1 15 mov ecx,msg 16 mov edx,len 17 mov ebx,1 ;file descriptor (stdout) 18 mov Tính toán số học: ví dụ eax,4 ;system call number (sys_write) 19 int 0x80 ;call kernel 31 20 21 mov ecx,res 22 mov edx,1 23 mov ebx,1 ;file descriptor (stdout) 24 mov eax,4 ;system call number (sys_write) 25 int 0x80 ;call kernel 26 27 mov eax,1 ;system call number (sys_exit) 28 int 0x80 ;call kernel 29 30 section .data 31 msg db "The result is:", 0xA,0xD 32 len equ $- msg 33 segment .bss 34 res resb 1 35 36 Tính toán số học 32 Các phép toán số học DIV/IDIV divisor 1/2/2020 Online Asm Compiler - Online Asm Editor - Online Asm IDE - Asm Coding Online - Practice Asm Online - Execute Asm Online - C Tính toán sốDIV/IDIV Assembly học:Instructions ví dụ (Nasm v2.01) Execute | Share main.asm 33 STDIN 1 section .