NASM Assembler
התקנת אסמבלר NASM את האסמבלר יש להוריד מהקישור הבא: http://cs.hadassah.ac.il/staff/martin/nasm/index.html 32-bit Windows XP/Vista/7 הורידו את האסמבלר מהקישור "Assembler" שמרו את כל הקבצים בספריה חדשה, לדוגמא c:\nasm לעבודה עם האסמבלר יש לעבור לספריה בה שמרתם אותו דרך חלון הפקודות. nasm.exe is assembler Nasm -h for help.1.2.3 ndisasm.exe is disassembler
התקנת אסמבלר NASM 64-bit Windows XP/Vista/7/8 פתחו את הספריה בקישור archive" "binaries בספריה כלשהו c:\directoryname( )cd ושמרו את הקבצים.1 1 2. המשיכו לפי הוראות ההתקנה המקושרות ב- instructions 3. בשלב ADD assembly directories שמרו את הקבצים שהורדתם בשלב לספרית bin כמתואר בהוראות. 4. כדי לעבוד עם האסמבלר עליכם לעבור לחלון DOS מדומה דרך ה nasm ומשם להפעיל את DOSbox
התקנת אסמבלר NASM התקנה עבור Ubuntu Linux אין צורך להוריד קבצים מהקישור אלא לכתוב את הפקודה sudo apt-get install nasm בזמן שיש קישור לאינטרנט. מערכת ההפעלה תדע להוריד את הנדרש. man nasm for help ndisasm.exe is disassembler
תכנות בסיסי בשפת סף בסביבה בה אנו עובדים ניתן ליצור קבצי ריצה בשני פורמטים: קבצי COM -תוכנית פשוטות בנפח עד 64KB קבצי EXE -תוכניות מורכבות בנפח עד 640KB שניתן ליצור ממודולים נפרדים ולקשר ביניהם.
תכנות בסיסי בשפת סף-שלבים בתהליך העבודה 1. כתיבת תוכנית כתיבת הטקסט באמצעות עורך טקסט פשוט )notepad( ושמירה כקובץ program.asm מקור בשם 2. פעולת הרכבה )Assemble( הפעלת האסמבלר על קובץ המקור. שני סוגי פלטים: 3. פעולת קישור Executable file program.com (for com program) Object file program.obj (for exe program) Convert program.obj to program.exe )Link( ניתן להפוך את הכיוון בעזרת Disassemblr לקבצים בשפת סף ולהמיר קבצי הרצה
מבנה כללי: פעולת הרכבה עם NASM nasm [-f <format>] <filename> [-o <output>] Valid output formats for -f : bin (default) flat-form binary files (DOS.com) obj MS-DOS 16-bit/32-bit OMF object files elf UNIX/Linux object files win32 Microsoft Win32 (i386) object files במידה ולא מציינים פורמט לפלט הפורמט הוא ברירת המחדל )DOS.com( The -o option NASM chooses default name of output file Depends on object file format
דוגמא: DOS Common File כתיבת התוכנית כקובץ טקסט: example.asm Start notepad, notepad++, vim, emacs, Enter assembly instructions MOV AX, BX ADD AX, CX שמירה בספריית :NASM...\nasm\example.asm cd...\nasm> מעבר לחלון פקודות שינוי ספריה לספרית NASM פעולת ההרכבה \nasm>nasm example.asm -o example.com <ENTER> Produces default flat-form binary file example.com
תוכנית NASM כוללת: תחביר לקבצי COM הוראות בשפת סף- instructions x86 assembly מבנה כללי של הוראה label: instruction operands ; comment קריאות מערכת- calls System חלוקת התוכנית לאיזורים- declarations Section section.data section.bss section.text נתונים מאותחלים- data Initialized Non-initialized data and buffers- נתונים לא מאותחלים וחוצצים הוראות- Instructions שמות משתנים ותוויות - sensitive) Variable names and line labels (case
ערכים מספריים: תחביר לקבצי COM ברירת מחדל: בסיס עשרוני: Default = decimal (written as 123 or 123d) בסיס הקסדצימלי: Hexadecimal value written as 0x1234 or 1234h Binary value written as 01101100b בסיס בינארי: מחרוזת תוים-מחרוזת בין מרכאות, אפס מציין סיום מחרוזת: Null-terminated string data in quotes: 'this is a string', 0
תבנית לתוכניות *.com ORG 0x100 section.data ; initialized data and variables ; common file organization section.bss ; uninitialized data and variables section.text ; user instructions mov ax,4c00h int 21h ; exit code ; DOS system call to end ; program
Data Declarations מבנה כללי: )רשימת ערכים ) value_1,value_2, (רוחב הנתון ( db/dw/dd Label: label: db 0x55,0x56,0x57 דוגמאות: הערכים 55 56 57 בבסיס הקסאדצימלי נשמרים בזיכרון. התוית מצביע לכתובת הנתון הראשון. מכיוון שרוחב נתון צוין כבית (db) הנתונים מאוחסנים בבית בודד. הכתובת העוקבת לתוית היא כתובת הנתון השני וכן הלאה: label points to 0x55, label+1 points to 0x56,... label: db ABC ייצוג התוים בקוד ASCII נשמר בזיכרון: )הערכים 41( 42 43 התווית תצביע לבית הראשון. הכתובת העוקבת לתוית היא כתובת הנתון השני וכן הלאה: label points to 0x41, label+1 points to 0x42,...
Data Declarations label: dw 0x22, ABC שמירת הערכים כמילים: הערך 22hex יורחב ל 0022 והמחרוזת כולה תורחב כדי ליצור כפולה שלמה של מילה: Stores string 22 00 41 42 43 00 in memory 1 byte per character appends 00 to odd-length string סדר שמירת הבתים הוא בשיטת little-endian בית תחתון בכתובת הנמוכה. label: dw 0x1234, 0x5678 שמירת שתי מילים בבסיס הקסאדצימלי בשיטת little-endian Stores HEX bytes 34 12 78 56 in memory label: dd 0x12345678,2 שמירת שני ערכים של מילים כפולות-הערך 2hex יורחב בהתאם: Stores HEX bytes 78 56 34 12 02 00 00 00 in memory הסדר הפנימי מתייחס הן למילה הכפולה והן לכל מילה בנפרד (little-endian order in each dword and in each word)
; in data section zerobuf: times 64 db 0 ; in BSS section Uninitialized Data ניתן להקצות מקום בזיכרון לקבוצת נתונים עם ערך יחיד: ; Writes 64 0-bytes in variable named zerobuf ניתן להקצות מקום בזיכרון לקבוצת נתונים מבלי לקבוע ערך התחלתי: buffer: resb 64 ; Reserves 64 bytes in variable named buffer wordvar: resw 1 ; Reserves 1 word in variable named wordvar בתוך התוכנית אפשר לפנות באמצעות התגית לאזור שהוקצה בזיכרון: ; assign value to variable in text section MOV [wordvar], 0x1122
הפעלת Disassembler C:\nasm\programs\examples>ndisasm -h usage: ndisasm [-a] [-i] [-h] [-r] [-u] [-b bits] [-o origin] [-s sync...] [-e bytes] [-k start,bytes] [-p vendor] file -a or -i activates auto (intelligent) sync -u sets USE32 (32-bit mode) -b 16 or -b 32 sets number of bits -h displays this text -r or -v displays the version number -e skips <bytes> bytes of header -k avoids disassembling <bytes> bytes from position <start> -p selects the preferred vendor instruction set (intel, amd, cyrix, idt)
הפיכת קובץ הריצה דוגמא- Disassembler ex9.exe לתוכנית בשפת סף. הפקודה e200h מדלגת על מספר הבתים הנקוב מראש הקובץ )מדלגת על שורות הפתיחה(. C:\nasm\programs\examples>ndisasm -e200h ex9.exe 00000000 B80000 mov ax,0x0 00000003 8ED8 mov ds,ax 00000005 55 push bp 00000006 89E5 mov bp,sp 00000008 81EC0400 sub sp,0x4 0000000C B8004C mov ax,0x4c00 0000000F CD21 int 0x21 פקודות בשפת סף קידוד בשפת מכונה )ייצוג הקסאדיצמלי ) כתובת
דוגמא לקובץ *.com Edit ex1.asm in text editor ORG 0x100 section.data v1: dw 0x0005 ; v1 = name of pointer to integer section.text XOR AX,AX ; zero accumulator (AX 0) MOV BX,0001 ; BX 1 L1: ADD AX,BX ; add BX to accumulator INC BX CMP BX,[v1] ; BX++ ; set CF,OF,SF,ZF ; according to value of BX 5 JLE L1 ; loop to L1 if BX 5 mov ax, 4C00h int 21h C:\nasm>nasm ex1.asm o ex1.com NASM places executable program file ex1.com in directory C:\nasm>
דוגמא: כתיבת ערכים לתוך טבלה ORG 0x100 section.bss base: resb 6 ;reserve 6 bytes ; in bss section section.text MOV SI, 0 L1: MOV AX,SI IMUL AL ADD AL,5 MOV [base + SI],AL INC SI CMP SI,5 JLE L1 mov ax,4c00h int 21h base+05 base+04 base+03 base+02 base+01 base+00 1E 15 0E 09 06 05
דוגמא: פעולות חשבוניות עם נתונים מאוחסנים ORG 0x100 section.data table: dw 1,2,3,4,5,6,7,8,9,10,0 ;stores ;words in ;memory section.text MOV SI,table ;SI point to first value (1) MOV AX,0000 L1: ADD AX,[SI] ADD SI,2 ;SI point to next value CMP WORD [SI],0 ; WORD operate on ; 16-bit word = 2 bytes ; from [SI+1].[SI] JNZ L1 mov ax,4c00h int 21h AX = 0 1 3 6 10 15 21 28 36 45 55
דוגמא ללולאת if הקטע בשפת C main() { int x = 0, y = 2; if (x == y) { x = 2 * y; y = 2 * x; } } ORG 0x100 section.bss x: resw 1 ; allocate memory ; for integer x y: resw 1 ; allocate memory ; for integer y section.text MOV word [x],0 ; x 0 MOV word [y],2 ; y 2 MOV AX,[x] ; AX x CMP AX,[y] ; AX - y JNZ end ; JMP IF NOT EQUAL MOV AX,[y] ; AX y SHL AX,1 ; AX AX * 2 MOV [x],ax ; x AX MOV AX,[x] ; AX x SHL AX,1 ; AX AX * 2 MOV [y],ax ; y AX end: mov ax,4c00h int 21h
ORG 0x100 section.bss i: resw 1 j: resw 1 section.text MOV WORD [i],0 MOV BX,3 L1: CMP WORD [i],10 דוגמא ללולאת for main() { הקטע בשפת C int i,j; for (i = 0; i < 10; i++){ j = 3 * i; } } JGE end ; break on i 10 MOV AX,[i] ; AX i IMUL BX ; AX AX * 3 MOV [j],ax ; j AX INC WORD [i] ; i++ JMP L1 ; loop end: mov ax,4c00h int 21h
if עם שילוב תנאים ORG 0x100 section.bss x: resw 1 y: resw 1 z: resw 1 section.text MOV WORD [x],0 ; x 0 MOV WORD [y],2 ; y 2 MOV WORD [z],3 ; z 3 MOV AX,[x] ; AX x CMP AX,[y] ; CMP AX = x, y JZ L1 ; if x = y, JMP L1 MOV AX,[y] ; AX y CMP AX,[z] ; CMP AX = y, z JLE end ; if y <= z, JMP end MOV AX,[x] ; AX x CMP AX,[z] ; CMP AX = x, z JLE end ; if x <= z, JMP end L1: MOV AX,[y] ; AX y SHL AX,1 ; AX 2 * AX MOV [x],ax ; x AX end: mov ax,4c00h int 21h דוגמא: לולאת main() { int x = 0, y = 2, z = 3; if (x == y y > z && x > z) { x = 2 * y; } } x בדיקת התנאים : y שווה ל- x או ( y גדול מ z וגם גדול מ z(
ORG 0x100 section.bss M: resw 1 N: resw 1 section.text דוגמא: השוואת תוצאת קומפיליציה לתכנות מראש בשפת סף )חישוב!7( compile MOV WORD [M],1 ; M 1 MOV WORD [N],2 ; N 2 L1: CMP WORD [N],7 ; compare N, 7 JG end ; break if N > 7 MOV AX,[N] ; AX N IMUL WORD [M] ; AX AX * M MOV [M],AX ; M AX INC WORD [N] ; N++ JMP L1 ; loop end: mov ax,4c00h int 21h main() { int N, M = 1; for (N = 2 ; N <= 7 ; ++N){ M = N * M; } }
דוגמא: השוואת תוצאת קומפיליציה לתכנות מראש בשפת סף )חישוב!7( main() { int N, M = 1; for (N = 2 ; N <= 7 ; ++N){ M = N * M; } } אין שימוש בהקצאת משתנים ולכן תוכנית פשוטה יותר. חסכון נוסף בזמן: פחות גישות לזיכרון rewrite in assembly language MOV CX,0007 ; counter CX 7 MOV AX,0001 ; accumulator AX 1 L1: IMUL CX ; AX AX * CX LOOP L1 ; CX-- loop if CX!= 0 MOV [M],AX ; M AX