NASM - asembler
Transkrypt
NASM - asembler
NASM - asembler ✦ ✦ ✦ ✦ http://sourceforge.net/projects/nasm/files/ Netwide Assembler – składnia Intela B – byte; W – słowo (2B); D – podwójne słowo (4B); Q- 8B; T – 10B Inicjalizuj lokację: DB, DW, DD, DQ, DT ✦ L6 DD 1A92H ; podwójne słowo pod L6 o wartości 1A92H ✦ Zarezerwuj lokację: RESB, RESW, RESD, RESQ, REST ✦ BUFFER RESB 64 ; zarezerwuj 64B pod BUFFER ✦ Włącz plik binarny: INCBIN ✦ INCBIN ’’file.dat” ; włącz plik file.dat ✦ Definiuj stałą: EQU ✦ MSGLEN EQU 25 ; stała MSGLEN równa liczbie dziesiętnej 25 ✦ Powtórz instrukcję: TIMES ✦ ZEROBUF TIMES 64 DB 0 ; zainicjuj 64B w ZEROBUF na zero ✦ ✦ ✦ ✦ ✦ Definicja startu/końca sekcji: START/END Definicja startu sekcji/dummy kontrolnej: CSECT/DSECT Definicja rejestru bazowego: USING Kompilacja warunkowa: IF, THEN, ELSE Odwołanie zewnętrzne: EXTRN NASM – makra ✦ Makro: MOV AX, A(8) generuje MOV AX, 1+2*8, gdzie %DEFINE B(X) = 2*X %DEFINE A(X) = 1 + B(X) ✦ &Makro &MACRO PROLOGUE 1 PUSH EBP ; push contents of EBP onto stack ; pointed to by ESP and ; decrement contents of ESP by 4 MOV EBP, ESP ; copy contents of ESP to EBP SUB ESP, %1 ; substract first parameter value from ESP ✦ Wywołanie MYFUNC: PROLOGUE 12 generuje MYFUNC: PUSH EBP MOV EBP, ESP SUB ESP,12 Przykład (Linux) - NASM section msg .data ; data segment db "Hello, world!", 0x0a ; the string and newline char section global .text _start ; text segment ; Default entry point for ELF linking _start: ; SYSCALL: write(1, msg, 14) mov eax, 4 mov ebx, 1 mov ecx, msg mov edx, 14 int 0x80 ; put 4 into eax, since write is syscall #4 ; put 1 into ebx, since stdout is 1 ; put the address of the string into ecx ; put 14 into edx, since our string is 14 bytes ; Call the kernel to make the system call happen ; SYSCALL: exit(0) mov eax, 1 mov ebx, 0 int 0x80 ✦ nasm –f elf helloworld.asm ✦ ld helloworld.o ✦ ./a.out ; put 1 into eax, since exit is syscall #1 ; exit with success ; do the syscall Przykład (Cygwin) - NASM ; ---------------------------------------------------------------------------; fib.asm ; ; This is a Win32 console program that writes the first 40 Fibonacci numbers. ; It needs to be linked with a C library. ; http://www.cs.lmu.edu/~ray/notes/nasmexamples/ ; ---------------------------------------------------------------------------global extern _main _printf section .text _main: push ebx ; we have to save this since we use it mov xor xor inc ecx, 40 eax, eax ebx, ebx ebx ; ecx will countdown from 40 to 0 ; eax will hold the current number ; ebx will hold the next number ; ebx is originally 1 print: ; We need to call printf, but we are using eax, ebx, and ecx. printf ; may destroy eax and ecx so we will save these before the call and ; restore them afterwards. Przykład (Cygwin) – NASM (c.d.) push push eax ecx push push call add eax format _printf esp, 8 pop pop ecx eax mov mov add dec jnz edx, eax eax, ebx ebx, edx ecx print ; save the current number ; next number is now current ; get the new next number ; count down ; if not done counting, do some more pop ret ebx ; restore ebx before returning db '%10d', 0 format: ✦ nasm –f win32 fib.asm ✦ gcc –o fib.exe fib.obj NASM - przykłady ✦ http://www.nasm.us/doc/ ✦ Win32 ✦ nasm -fwin32 helloworld.asm ✦ gcc helloworld.obj ✦a ✦ Linux – usunąć podkreślenie: _ ✦ nasm –felf –g helloworld.asm ✦ gcc helloworld.o ✦ gdb –q ./a.out ✦ set dis intel ✦ break _start ✦ run global _main extern _printf section .text _main: push _message call _printf add esp,4 ret message: db ‘Hello, World’, 10, 0 NASM – wywołania w C ✦ nasm –felf maxoftree.asm ✦ gcc maxofthree.c maxofthree.o ✦ ./a.out #include <stdio.h> global maxofthree section .text int maxofthree(int, int, int); int main() { printf("%d\n", maxofthree(1, -4, -7)); printf("%d\n", maxofthree(2, -6, 1)); printf("%d\n", maxofthree(2, 3, 1)); printf("%d\n", maxofthree(-2, 4, 3)); printf("%d\n", maxofthree(2, -6, 5)); printf("%d\n", maxofthree(2, 4, 6)); return 0; } maxofthree: mov eax, [esp+4] mov ecx, [esp+8] mov edx, [esp+12] cmp eax, ecx cmovl eax, ecx cmp eax, edx cmovl eax, edx ret NASM - int main(int argc, char** argv) global main extern printf ✦ gcc echo.o ✦ ./a.out section .text main: mov mov ecx, [esp+4] edx, [esp+8] ; argc ; argv top: push ecx push edx push dword [edx] push format call printf add esp, 8 ; save registers that printf wastes pop pop ; restore registers printf used edx ecx add edx, 4 dec ecx jnz top ret format: db '%s', 10, 0 ; the argument string to display ; the format string ; remove the two parameters ; point to next argument ; count down ; if not done counting keep going NASM - przykład extern printf global main ✦ nasm –elf section .data format: db '%d', 10, 0 powers.asm section .text main: ✦ gcc powers.o push esi push edi ✦ ./a.out mov esi, 1 ✦ ebx, esi, edi, ebp, L1: mov edi, 31 push esi ds, es, ss są push format call printf przechowywane add esp, 8 add esi, esi dla programu dec edi jne L1 wywołującego pop edi pop ret esi ; callee-save registers ; current value ; counter ; push value to print ; push address of format string ; pop off parameters passed to printf ; double value ; keep counting ; restore callee-save registers NASM a C ✦ nasm –elf -g printl.asm ✦ gcc printl.o ✦ gdb -q ./a.out ✦ set dis intel ✦ list ✦ break 21 #include <stdio.h> int main() { int a=5; printf("a=%d, eax=%d\n", a, a+2); return 0; } extern printf global main a: fmt: ;the C function, to be called ; the standard gcc entry point section .data ; Data section, initialized variables dd 5 ; int a=5; db "a=%d, eax=%d", 10, 0 ; The printf format, "\n",'0' section .text main: push mov ebp ebp,esp mov add push push push call add mov pop mov ret eax, [a] eax, 2 eax dword [a] dword fmt printf esp, 12 esp, ebp ebp eax,0 ; Code section. ;the program label for the entry point ; set up stack frame ; put a from store into register ; a+2 ; value of a+2 ; value of variable a ; address of ctrl string ; Call C function ; pop stack 3 push times 4 bytes ; takedown stack frame ; same as "leave" op ; normal, no error, return value ; return