lecture4 handnotes 2013b.pptcaspl132/wiki.files/lecture4b_handnotes_2013.pdf20/04/2013 1 x86...
TRANSCRIPT
20/04/2013
1
x86הכתובת במבנה הפקודה
1
חישוב הכתובת האפקטיבית
X ++
• Displacement — ביטים 32או 16, 8ערך של
• Base — הערך ברגיסטר כללי
• Index — הערך ברגיסטר כללי
)2-בשיטת המשלים ל(ל יכול להיות חיובי או שלילי "כל אחד מהנ
• Scaling factor — 2, 4, 8
2
תסוגי פקודות ק
3
20/04/2013
2
4
מבנה כללי של תכנית בשפה עילית
Code & Dataהפרדה בין •
5
מבנה כללי של תכנית בשפה עילית
Code & Dataהפרדה בין •
קסוגי פקודות עיקריים• ע ק
השמה סטאטית ודינאמית•
Data transfer instructions – e.g.: etc.
Memory allocation
6
20/04/2013
3
סוגי מידע עליהם פועלים
• Bytes, words, doublewords )ביטים 256כיום גם (
• Signed and unsigned byte, word, dword integers
• Pointers (near & far)
• Bit fields
• BCD integers
• Floating point
• Packed data types (SIMD – single instruction multiple data)
7
סוגי משתנים
8
BCD
Decimal Binary
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
9
20/04/2013
4
סוגי פקודות
פעולות להעברת מידע ) מלים כפולות ומרובעות, מילים, בתים(
בין הזיכרון לרגיסטרים
ובין רגיסטרים
• Data transfer
• Binary, decimal & logical
• Shift & rotate
• Bit & byte manipulation
• Control transfer• Control transfer
• String
• I/O
• Flag
• System
• Atomic
• Misc.10
Data transfer instructions - MOV
MOV פקודות המעבירות מידע בין הזיכרון לבין הרגיסטרים
:MOVסוגי מעבר מידע
!!! strings -רק ל זיכרון MOVS זיכרון
11
Conditional Data transfer instructions
CMOVccהעברה מותנית
CMOVE/CMOVZ
CMOVC
12
20/04/2013
5
Data transfer - convert/extend
• CBW
CWDECDQ
• MOVSX ; Move and sign extend
MOVZX ; Move and zero extend
13
Multiple Data Transfers
:יש פקודות המאפשרות העברת כמה משתנים בפקודה אחת
פקודות החלפה•
•PUSH/POP של מספר משתנים
ופעולות נוספות) strings" (מחרוזות"העתקת •
מיוחדות) MOV(הזזה /פקודות הצבה•
14
Multiple Data Transfers -exchange
:Exchange–החלפה
)x86 :(XCHG src1, src2
15
20/04/2013
6
Multiple Data Transfers -exchange
?מה קורה אם אחד מהאופרנדים בזיכרון
?מה קורה אם מספר מעבדים או מספר תהליכים רצים במקביל
16
Multiple Data Transfers
:דוגמה נוספת
XADD ;Exchange and addCMPXCHG ;Compare and exchangeCMPXCHG8B ;Compare and exchange 8 bytes
17
The LOCK prefix
OPCODE: F0
Command: LOCK
ADD, ADC, AND, BTC, BTR, BTS, CMPXCHG, CMPXCH8B,
LOCK signal -מעלה את ה•
:לפקודות רקמתאים •
CMPXCHG16B, DEC, INC, NEG, NOT, OR, SBB, SUB, XOR, XADD, XCHG
רק אם אופרנד היעד הוא אופרנד זיכרון•
Atomic instructionsנתייחס שוב כשנדבר על *
18
20/04/2013
7
סוגי פקודות
פעולות חשבוניות ולוגיות, חשבון בינארי על שלמים בינאריים•
הקטנה /הגדלה, השוואה, חילוק, כפל, חיסור, חיבור
שינוי סימן, 1-ב
• Data transfer
• Binary, decimal & logical
• Shift & rotate
• Bit & byte manipulation
• Control transferBCDחשבון עשרוני על מידע בייצוג •
-ול) לאחר חיבור וחיסור(התאמה לייצוג עשרוניASCII )כפל וחילוק, חיסור, לאחר חיבור(
פעולות לוגיות בסיסיות •AND, OR, NOT, XOR
(byte, word, dword)
• Control transfer
• String
• I/O
• Flag
• System
• Atomic
• Misc.19
20
Integer arithmetic & logic
ADD ,SUBפעולות כמו
unsignedעל ו signedעובדות על
, IMUL, MUL, IDIV, DIV פעולות כמו , ,
)unsigned או signed(על סוג אחד רקפועלות
21
20/04/2013
8
Integer arithmetic & logic
?כיצד מרחיביםאם צריך פעולות על משתנים ארוכים או דיוק רב
Long operations
Unlimited precision integer operations
22
Integer arithmetic & logic )80x86 (משתמשים ב- carry flag
:ADD ,SUBTRACTלפקודות ADD eax, [x_low]
++ +
23
מניעת פיצול: דוגמה
neg ecx
sbb eax, eax
and eax, edx
24
20/04/2013
9
• Data transfer
• Binary, decimal & logical
• Shift & rotate
• Bit & byte manipulation
• Control transfer
סוגי פקודות
מזיזות את הביטים באופרנד
(word, dword)
• Control transfer
• String
• I/O
• Flag
• System
• Atomic
• Misc.
SAR/SAL בערך /0- הביטים הריקים מתמלאים באריתמטי - )בהתאמה(הביט הגבוה
SHR, SHL לוגי - 0- הביטים הריקים מתמלאים ב
SHRD, SHLD למספרים גדולים
ROR, ROL הזזה סיבובית של ביטים
RCR, RCL ביטים 33או 17, 9 -הזזה סיבובית ב ,FLAG CARRY-דרך ה
25
Shift & Rotate
)80x86 (משתמשים ב- carry flag לפקודותSHIFT ו - ROTATE:
SHL dword [x_low], 1
RLC dword [x_high], 1
ביטים 5-מספר ההזזות מוגבל לSHRD SHLD / : בין רגיסטרים SHIFTניתן גם לעשות
26
דוגמה
SHLD EAX, EBX, 4
SHRD EAX EBX 4SHRD EAX, EBX, 4
27
20/04/2013
10
• Data transfer
• Binary, decimal & logical
• Shift & rotate
• Bit & byte manipulation
• Flags
סוגי פקודות
בדיקה ושינוי ביטים בודדיםפעולות על ביטים ומלים כפולות, באופרנדים של מילים
•קביעת ערך של אופרנד בגודלפעולות על בתים Flags
• Control transfer
• String
• I/O
• System
• Atomic
• Misc.
ע ךע קbyte ,כדי להראות מצב של דגלים
פועלות על הדגלים פעולות על הדגלים ברגיסטר הדגלים
28
Individual Bit manipulation
• Bit testBT oper1, oper2 BTC oper1, oper2 BTS oper1, oper2 BTR oper1, oper2
29
30
20/04/2013
11
Bit test: tests the value of a specified bit in a register or memory location; sets or clears flags
BTST - Bit Test ; Motorola 680x0, Motorola 68300BT - Bit Test ; Intel x86
Bit test & clear/reset: tests the value of a specified bit; clears/resets this bit to zero; sets or clears flags BCLR - Bit Test and Clear ; Motorola 680x0, Motorola 68300BTR - Bit Test and Reset ; Intel x86
Bit test & set: tests the value of a specified bit; sets this bit to one; sets or clears flags BSET - Bit Test and Set ; Motorola 680x0, Motorola 68300BTS - Bit Test and Set ; Intel x86
Bit test & change/complement: tests the value of a specified bit; change/complement 1 to 0, or 0 to 1; sets or clears flagsBCHG Bit Test and Change ; Motorola 680x0, Motorola 68300BTC Bit Test and Complement ; Intel x86
31
...עוד
Bit scan
BSF oper1 oper2oper2 -ב' 1'ביותר בעל ערך הנמוךאת הביט ) 0מביט (מחפש
zero flag -מאפס את ה–אם מצא , oper1 -שומר את מקומו ב
BSR oper1 oper2 oper2 - ב' 1'ביותר בעל ערך הגבוהאת הביט ) 15,31,63מביט (מחפש
zero flag -מאפס את ה–אם מצא , oper1 -שומר את מקומו ב
32
Bit field
היכולה להתחיל בכל מקום , שרשרת של ביטים סמוכים•או ברגיסטר, בכל בית בזיכרון
33
20/04/2013
12
Bit field manipulation
• Bit field operation: Compressed structures
Example – Motorola 680x0:
BFCLR (ea)o:w ; clear bit field
BFEXTS (ea)o:w, Dn ; sign extend
BFFFO (ea)o:w, Dn ; Find 1st MSB ‘1’
BFINS Dn,(ea)o:w ; Insert to field: inserts a bit field (1 to 32 bits) from a data register to a location in data register or memory located by field offset and field width
Also:
BFCNG - invert , BFEXTU – zero extend, BFSET, BFTST
34
Bit Fields
:Intel 64-וב x86 -ב•MPSADBW — Compute Multiple Packed Sums of
Absolute Difference
35
Status Flags• Zero flag (ZF)
In the last instruction that affects the zero flag: ZF = 1 for zero result, ZF = 0 otherwise.
• Carry flag (CF) The result of an arithmetic operation on unsigned numbers is out of range of the destination (register or memory).
• Overflow flag (OF) The operation on signed numbers has produced a result that is out of rangeThe operation on signed numbers has produced a result that is out of range.
• Sign flag (SF) The sign of the result of an operation, 0 - positive, 1 - negative.
• Auxiliary flag (AF) An operation has produced a result that has generated a carry out of or a borrow into the low-order four bits (BCD operations – binary coded decimal)
• Parity flag (PF)The parity of the 8-bit result produced by an operation; 1 - the byte contains an even number of 1 bits, 0 - if there are an odd number of 1bits
36
20/04/2013
13
Flags
•Flags כ כתוצאה מפעולות ב"משתנים בד- ALU
ישירותניתן גם לשנותם •
:דוגמאות:דוגמאות
Carry flag: STC, CLC, CMC
Direction Flag: CLD, STD(used for string operations, such as MOVS, CMPS)
37
38
Flag Control
•Load/store flag registerLAHF, SAHF
•Push/POP flag registerPUSHF POPFPUSHF, POPF
LAHF: דוגמה
AND AH, mask
SAHF39