Assembly For X86 Chapter 6 כתיבת תוכניות מלאות Chapter 6 כתיבת תוכניות...
-
Upload
earl-hudson -
Category
Documents
-
view
224 -
download
2
Transcript of Assembly For X86 Chapter 6 כתיבת תוכניות מלאות Chapter 6 כתיבת תוכניות...
Assembly For X86Assembly For X86
Chapter 6
כתיבת תוכניות מלאות
Chapter 6
כתיבת תוכניות מלאות
Example 1. The Hello World Program.
title Hello World Program (hello.asm); This program displays “Hello, world!”.model small.stack 100h.datamessage db “Hello, world!”,0dh,0ah,’$’.codemain proc mov ax,@data mov ds,ax mov ah,9 mov dx,offset message int 21h
mov ax,4C00h int 21hmain endpend main
Example 1. The Hello World Program.
title Hello World Program (hello.asm); This program displays “Hello, world!”.model small.stack 100h.datamessage db “Hello, world!”,0dh,0ah,’$’.codemain proc mov ax,@data mov ds,ax mov ah,9 mov dx,offset message int 21h
mov ax,4C00h int 21hmain endpend main
DirectivesDirectives
•titleכותרת הקובץ – •.modelהגדרת מודל הזיכרון – •.stackמקטע המחסנית – •.dataמקטע הנתונים – •.codeמקטע הקוד – •procשגרה – •endpסיום שגרה – •endסיום התוכנית –
•titleכותרת הקובץ – •.modelהגדרת מודל הזיכרון – •.stackמקטע המחסנית – •.dataמקטע הנתונים – •.codeמקטע הקוד – •procשגרה – •endpסיום שגרה – •endסיום התוכנית –
הרצת התוכניתהרצת התוכנית
• editor hello.asm
• compiler tasm hello (hello.obj)
• Linker tlink hello (hello.exe)
• loader(dos) hello
ניתן להשתמש בקבצים נוספים בתהליך:•–lib–lst
• editorhello.asm
• compiler) tasm hello (hello.obj
• Linker) tlink hello (hello.exe
• loader(dos)hello
ניתן להשתמש בקבצים נוספים בתהליך:•–lib–lst
הגדרת משתניםהגדרת משתנים
• a DB ?• b DB ‘hello’• c DB 100• d DB Ah• e DB 11001100b• a1 DW ?• a2 DW 3000• b1 DD ? • b2 DQ ?
• a DB ?• b DB ‘hello’• c DB 100• d DB Ah• e DB 11001100b• a1 DW ?• a2 DW 3000• b1 DD ? • b2 DQ ?
דוגמא-פקודות אריתמטיותדוגמא-פקודות אריתמטיות
•ADCחיבור עם התחשבות בדגל הנשא – DATA.X DB 80hY DB 20hCODE.….mov DX,3090hadd DL,xadc DH,y
•ADCחיבור עם התחשבות בדגל הנשא – .DATAX DB 80hY DB 20h.CODE….mov DX,3090hadd DL,xadc DH,y
Byte PtrByte Ptr
:1000 בכתובת 17הצבת הערך •mov dl,17hmov bx,1000hmov [bx],dl
ניתן לרשום זאת גם כך:mov bx,1000mov byte ptr [bx],17h
ניתן גם לכתוב זאת בשורה אחת:mov byte ptr[1000h],17h
:1000 בכתובת 17הצבת הערך •mov dl,17hmov bx,1000hmov [bx],dl
ניתן לרשום זאת גם כך:mov bx,1000mov byte ptr [bx],17h
ניתן גם לכתוב זאת בשורה אחת:mov byte ptr[1000h],17h
Word PtrWord Ptr
סיביות:16עבור •
mov DI,1000h
mov word ptr [DI],5678h
או בשורה אחת
mov word ptr[1000h],5678h
סיביות:16עבור •
mov DI,1000h
mov word ptr [DI],5678h
או בשורה אחת
mov word ptr[1000h],5678h
SBB – Sub With BorrowSBB – Sub With Borrow
• SBB op1,op2 ובנוסף מחסירה את op1 מ- op2הפקודה מחסירה את •
CFהערך שנמצא ב – DATA.X DD 70903798hY DD 50976549hCODE.…mov ax,word ptr xmov dx,word ptr x+2sub ax,word ptr ySbb dx,word ptr y+2
• SBB op1,op2 ובנוסף מחסירה את op1 מ- op2הפקודה מחסירה את •
CFהערך שנמצא ב – .DATAX DD 70903798hY DD 50976549h.CODE…mov ax,word ptr xmov dx,word ptr x+2sub ax,word ptr ySbb dx,word ptr y+2
סידרת פיבונאצ'י•MODEL SMALL.STACK 100h.DATA.num db 5? res dwCODE.mov ax,@DATAmov ds,axmov cx, word ptr nummov ax,1mov bx,ax ;fib0=fib1=1
סידרת פיבונאצ'י•.MODEL SMALL.STACK 100h.DATAnum db 5res dw ?.CODEmov ax,@DATAmov ds,axmov cx, word ptr nummov ax,1mov bx,ax ;fib0=fib1=1
fib:mov dx,axadd dx,bxmov bx,axmov ax,dxdec cxcmp cx,0jnz fib
mov res,dxmov ah,4chint 21hEND
fib:mov dx,axadd dx,bxmov bx,axmov ax,dxdec cxcmp cx,0jnz fib
mov res,dxmov ah,4chint 21hEND
title Exchange Two Variables .model small.stack 100h.datavalue1 db 0Ahvalue2 db 14h.codemain proc mov ax,@data ; initialize DS register mov ds,ax mov al,value1 ; load the AL register xchg value2,al ; exchange AL and value2 mov value1,al ; store AL back into value1 mov ax,4C00h ; exit program int 21hmain endpend main
title Exchange Two Variables .model small.stack 100h.datavalue1 db 0Ahvalue2 db 14h.codemain proc mov ax,@data ; initialize DS register mov ds,ax mov al,value1 ; load the AL register xchg value2,al ; exchange AL and value2 mov value1,al ; store AL back into value1 mov ax,4C00h ; exit program int 21hmain endpend main
XCHGXCHG
פעולות על סיביותפעולות על סיביות
• AND op1,op2 ; op1 = op1 & op2• TEST op1,op2 ; flags op1 & op2• OR op1,op2• XOR op1,op2• NEG op1• SHR op1,num ;op1=op1>>num• SHL op1,num ;CF הסיבית שיצאה נמצאת ב• ROR op1,num rotate right;• ROL op1,num rotate left;
• AND op1,op2 op1 = op1 & op2 ;• TEST op1,op2 flags ;op1 & op2 • OR op1,op2• XOR op1,op2• NEG op1• SHR op1,num op1=op1>>num;• SHL op1,num CF;הסיבית שיצאה נמצאת ב• ROR op1,num ;rotate right• ROL op1,num ;rotate left
RCR,RCLRCR,RCL
• RCR al,3
CF סיביות ימינה דרך 3הזזת •
וכו'7 זז לסיבית CF , CF זזה ל- 0סיבית •
• RCL dl,4
CF סיביות שמאלה דרך 4הזזת •
• RCR al,3
CF סיביות ימינה דרך 3הזזת •
וכו'7 זז לסיבית CF , CF זזה ל- 0סיבית •
• RCL dl,4
CF סיביות שמאלה דרך 4הזזת •
פניה לכתובות בזיכרוןפניה לכתובות בזיכרון
.DATA
x dw 6 ;x=0
y db 7 ;y=2
z dd 9 ;z=3
dw 88 ;z+4 or w-2
w dw 89 ;w=9
.DATA
x dw 6 ;x=0
y db 7 ;y=2
z dd 9 ;z=3
dw 88 ;z+4 or w-2
w dw 89 ;w=9
יצירת היסט עם תויתיצירת היסט עם תוית
var db 5…mov al,varmov al,[var]mov al,[bx+var]mov al,[bx]+varmov al,[bx+si+var+3]mov al,var[bx][si]+3var[bx] [bx]+var [bx+var]var[bx][si] [var+bx+si] [bx+si]+var
var db 5…mov al,varmov al,[var]mov al,[bx+var]mov al,[bx]+varmov al,[bx+si+var+3]mov al,var[bx][si]+3var[bx] [bx]+var [bx+var]var[bx][si] [var+bx+si] [bx+si]+var
קביעת סגמנטקביעת סגמנט
• mov ax,ds:[si+7]
ניתן גם לא לקבוע סגמנט ואז:•SS יבחר BPאם בתוך הסוגריים משתמשים ב- –אם בסוגריים יש תוית אזי יבחר הסגמנט של התוית–DS יבחר BXאם בסוגריים נמצא –mov dl,[2]אסור לרשום –mov dl,DS:[2]אבל מותר: –
• mov ax,ds:[si+7]
ניתן גם לא לקבוע סגמנט ואז:•SS יבחר BPאם בתוך הסוגריים משתמשים ב- –אם בסוגריים יש תוית אזי יבחר הסגמנט של התוית–DS יבחר BXאם בסוגריים נמצא –mov dl,[2]אסור לרשום –mov dl,DS:[2]אבל מותר: –
offsetהגדרת offsetהגדרת
מציינת את ההיסט של הכתובת יחסית לסגמנט •כלומר כתובת הנתון בתוך הסגמנט
data.aList db 10h,20h,30hsum db 0code. mov bx,offset aListmov al,[bx] AL = 10h ;inc bxadd al,[bx] AL = 30h ;inc bxadd al,[bx] AL = 60h ;mov si,offset sum get offset of sum ;mov [si],al store the sum ;
מציינת את ההיסט של הכתובת יחסית לסגמנט •כלומר כתובת הנתון בתוך הסגמנט
.dataaList db 10h,20h,30hsum db 0.codemov bx,offset aList mov al,[bx] ; AL = 10hinc bxadd al,[bx] ; AL = 30hinc bxadd al,[bx] ; AL = 60hmov si,offset sum ; get offset of summov [si],al ; store the sum
bit 16חיבור bit 16חיבור
.datawordList dw 1000h,2000h,3000hsum dw 0.codemov bx,offset wordListmov ax,[bx] ; first numberadd ax,[bx+2] ; second numberadd ax,[bx+4] ; third numbermov [bx+6],ax ; store the sum
.datawordList dw 1000h,2000h,3000hsum dw 0.codemov bx,offset wordListmov ax,[bx] ; first numberadd ax,[bx+2] ; second numberadd ax,[bx+4] ; third numbermov [bx+6],ax ; store the sum
קבועיםקבועים
.datastring db "This is a string."COUNT = ($ – string)
.codemov cx,COUNTmov si,offset string
L1: mov ah,2mov dl,[si]int 21hinc siloop L1
.datastring db "This is a string."COUNT = ($ – string)
.codemov cx,COUNTmov si,offset string
L1: mov ah,2mov dl,[si]int 21hinc siloop L1
16 Bit 16 Bit
.dataintarray dw 100h,200h,300h,400hCOUNT = ($ – intarray) / 2
.codemov ax,0mov di,offset intarray mov cx,COUNT
L1: add ax,[di]add di,2loop L1
.dataintarray dw 100h,200h,300h,400hCOUNT = ($ – intarray) / 2
.codemov ax,0mov di,offset intarray mov cx,COUNT
L1: add ax,[di]add di,2loop L1
מערכיםמערכים
1 מאותחל ב-10 בגודל arrהגדרת מערך בשם •
• arr DW 10 dup(1)
גישה לאבר החמישי:•
• mov ax,[arr+10]
• mov si,10
• mov ax,arr[si]
1 מאותחל ב-10 בגודל arrהגדרת מערך בשם •
• arr DW 10 dup(1)
גישה לאבר החמישי:•
• mov ax,[arr+10]
• mov si,10
• mov ax,arr[si]
LEA-Load Effective AddressLEA-Load Effective Address
ניתן לכתוב:•• mov bx,offset string[3]
אך אסור להשתמש באוגרים:•• mov bx,offset string[si+3]
כיוון שפענוח הכתובת נעשה בזמן הידור ולא בזמן ריצה.•הפתרון:•
• lea bx,string[3+si]
ניתן לכתוב:•• mov bx,offset string[3]
אך אסור להשתמש באוגרים:•• mov bx,offset string[si+3]
כיוון שפענוח הכתובת נעשה בזמן הידור ולא בזמן ריצה.•הפתרון:•
• lea bx,string[3+si]
LDSLDS
• lds bx,1000h
סיביות מהזיכרון ככתובת 32הפקודה טוענת •המורכבת ממקטע והיסט.
ds, המקטע לאוגר bxההיסט נטען לאוגר •
אם משתמשים בשם משתנה הוא חייב להיות • בתים)DD) 4מוגדר
LES ,LSS ,LFS ,LGSקיימות גם פקודות •
• lds bx,1000h
סיביות מהזיכרון ככתובת 32הפקודה טוענת •המורכבת ממקטע והיסט.
ds, המקטע לאוגר bxההיסט נטען לאוגר •
אם משתמשים בשם משתנה הוא חייב להיות • בתים)DD) 4מוגדר
LES, LSS, LFS, LGSקיימות גם פקודות •