NOT NOT NOT destination NOT Web site Examples
NOT 指令 • NOT 指令會使運算元中的所有位元倒轉其� • 語法: NOT destination NOT Web site Examples 8
應用 (1 of 5) • 作業: 將在 AL 的字元轉換成大寫. • 解答:使用AND指令清除 5位元. mov al, 'a' and al, 11011111 b ; AL = 01100001 b ; AL = 01000001 b Web site Examples 9
應用 (2 of 5) • 作業: 把一個二進位十進制的位元組轉換成它的相等ASCII 十進位的數字。 • 解答: 使用OR指令設定 4位元和5位元 mov al, 6 or al, 00110000 b ; AL = 00000110 b ; AL = 00110110 b The ASCII digit '6' = 00110110 b Web site Examples 10
應用 (3 of 5) • 作業: 按下鍵盤 Caps. Lock 鍵 • 解答: 使用OR指令在鍵盤標旗位元組中設定 6位元在 0040: 0017 h 在BIOS資料區域中. mov ax, 40 h mov ds, ax mov bx, 17 h or BYTE PTR [bx], 01000000 b ; BIOS segment ; keyboard flag byte ; Caps. Lock on 這個編碼只在實時處理模式執行,而不在Windows NT, 2000, or XP. 下執行 Web site Examples 11
應用 (4 of 5) • 作業: 如果一個整數是偶數,跳到標籤。 • 解答: AND 最小的位元是 1. 如果結果是零,數字是偶數的 mov ax, word. Val and ax, 1 jz Even. Value ; low bit set? ; jump if Zero flag set JZ(跳躍如果零)在第 6. 3 節中被涵蓋。 Your turn: Write code that jumps to a label if an integer is negative. Web site Examples 12
CMP 指令 (1 of 3) • 對來源運算元的比較目的地運算元 • 目的地的來源的非破壞性減少(目的地運算元不被改變) • 語法: CMP destination, source • 例題: destination == source mov al, 5 cmp al, 5 ; Zero flag set • 例題: destination < source mov al, 4 cmp al, 5 ; Carry flag set Web site Examples 15
CMP 指令 (2 of 3) • 語法: destination > source mov al, 6 cmp al, 5 ; ZF = 0, CF = 0 (零和進位標旗很清楚) The comparisons shown so far were unsigned. Web site Examples 16
CMP 指令 (3 of 3) The comparisons shown here are performed with signed integers. • 例題: destination > source mov al, 5 cmp al, -2 ; Sign flag == Overflow flag • 例題: destination < source mov al, -1 cmp al, 5 ; Sign flag != Overflow flag Web site Examples 17
應用 (2 of 5) • 跳到標籤 L 1 如果無號的EAX 是少於或者等於Val 1 cmp eax, Val 1 jbe L 1 ; below or equal • 跳到標籤 L 1 如果有號EAX是少於或者等於Val 1 cmp eax, Val 1 jle L 1 Web site Examples 25
Applications (3 of 5) • Compare unsigned AX to BX, and copy the larger of the two into a variable named Large mov cmp jna mov Next: Large, bx ax, bx Next Large, ax • Compare signed AX to BX, and copy the smaller of the two into a variable named Small mov cmp jnl mov Next: Small, ax bx, ax Next Small, bx Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 26
應用 (5 of 5) • 作業: 跳到標籤L 1如果位元0, 1, 和3在AL全部被設定. • 解答: 除了位元0, 1和3清除所有的位元. 然後把結果與 00001011 二進作比較. and al, 00001011 b cmp al, 00001011 b je L 1 ; clear unwanted bits ; check remaining bits ; all set? jump to L 1 Web site Examples 28
加密字符串 下列的迴路使用XOR指令把在字串的每個字元轉變成新的值. KEY = 239. data buffer BYTE BUFMAX DUP(0) buf. Size DWORD ? . code mov ecx, buf. Size mov esi, 0 L 1: xor buffer[esi], KEY inc esi loop L 1 ; loop counter ; index 0 in buffer ; translate a byte ; point to next byte Web site Examples 30
有條件的迴圈指令 • LOOPZ 和 LOOPE • LOOPNZ 和 LOOPNE Web site Examples 33
LOOPNZ 和 LOOPNE • LOOPNZ (LOOPNE)是一個有條件的迴圈指令 • 語法: LOOPNZ destination LOOPNE destination • 語法: • ECX – 1; • if ECX > 0 and ZF=0, jump to destination • 否則將不進行跳越的動作,而且控制權會轉移到下一 個指令。 Web site Examples 35
LOOPNZ 例題 下列編碼發現陣列的第一個為整數數值: . data array SWORD -3, -6, -10, 10, 30, 4 sentinel SWORD 0. code mov esi, OFFSET array mov ecx, LENGTHOF array next: test WORD PTR [esi], 8000 h ; test sign bit pushfd ; push flags on stack add esi, TYPE array popfd ; pop flags from stack loopnz next ; continue loop jnz quit ; none found sub esi, TYPE array ; ESI points to value quit: Web site Examples 36
習題. . . 設置陣列第一個非零價值。ESI 會指向緊接在陣列之後的Sentinel 值: . data array SWORD 50 DUP(? ) sentinel SWORD 0 FFFFh. code mov esi, OFFSET array mov ecx, LENGTHOF array L 1: cmp WORD PTR [esi], 0 ; check for zero (fill in your code here) quit: Web site Examples 37
. . . (解). data array SWORD 50 DUP(? ) sentinel SWORD 0 FFFFh. code mov esi, OFFSET array mov ecx, LENGTHOF array L 1: cmp WORD PTR [esi], 0 pushfd add esi, TYPE array popfd loope next jz quit sub esi, TYPE array quit: ; check for zero ; push flags on stack ; ; pop flags from stack continue loop none found ESI points to value Web site Examples 38
區塊結構式IF 敘述 彙編語言電腦程式設計師能容易地翻譯邏輯陳述編寫C++/Java彙編 語言: if( op 1 == op 2 ) X = 1; else X = 2; mov cmp jne mov jmp L 1: mov L 2: Web site eax, op 1 eax, op 2 L 1 X, 1 L 2 X, 2 Examples 40
習題. . . 彙編語言實行下列的虛擬碼。所有的值是 32位元有號整 數: if( var 1 var 3 = else { var 3 = var 4 = } <= var 2 ) 10; 6; 7; mov cmp jle mov jmp L 1: mov L 2: eax, var 1 eax, var 2 L 1 var 3, 6 var 4, 7 L 2 var 3, 10 (這一個問題有多樣的正確解決辦法. ) Web site Examples 42
複合式運算AND (2 of 3) if (al > bl) AND (bl > cl) X = 1; 這是可能的履行. . . cmp al, bl ja L 1 jmp next ; first expression. . . cmp bl, cl ja L 2 jmp next ; second expression. . . L 1: L 2: mov X, 1 next: ; both are true ; set X to 1 Web site Examples 44
複合式運算AND (3 of 3) if (al > bl) AND (bl > cl) X = 1; 此程式碼的大小縮減了29% ( 七個指令降為五個),這是因為如 果第一個JBE 指令沒有進行跳越,那麼CPU 便會直接進入第二 個CMP 指令 : cmp jbe mov next: al, bl next bl, cl next X, 1 ; ; ; first expression. . . quit if false second expression. . . quit if false both are true Web site Examples 45
習題. . . 彙編語言實行下列的虛擬碼。所有的值是無號的: if( ebx <= ecx && ecx > edx ) { eax = 5; edx = 6; } cmp ja cmp jbe mov next: ebx, ecx next ecx, edx next eax, 5 edx, 6 (這一個問題有多樣的正確解決辦法. ) Web site Examples 46
複合式運算OR (1 of 2) if (al > bl) OR (bl > cl) X = 1; 我們能使用 “ 秋天直通式 ” 邏輯使編碼儘可能保持短: cmp ja cmp jbe L 1: mov next: al, bl L 1 bl, cl next X, 1 ; ; ; is AL > BL? yes no: is BL > CL? no: skip next statement set X to 1 Web site Examples 48
WHILE 迴圈 WHILE 結構在執行一個區塊的敘述以前,會先測試迴圈條件 是否成立。 考慮下列例題: while( eax < ebx) eax = eax + 1; 這是可能履行的: top: cmp jae inc jmp next: eax, ebx next eax top ; ; check loop condition false? exit loop body of loop repeat the loop Web site Examples 49
習題. . . 實行下列的迴圈,使用無號 32位元整數: while( ebx <= val 1) { ebx = ebx + 5; val 1 = val 1 - 1 } top: cmp ja add dec jmp next: ebx, val 1 next ebx, 5 val 1 top ; check loop condition ; false? exit loop ; body of loop ; repeat the loop Web site Examples 50
表格驅動式選擇(2 of 3) Step 1: 產生一個表格包含查詢值和彌補程序: . data Case. Table BYTE 'A' ; lookup value DWORD Process_A ; address of procedure Entry. Size = ($ - Case. Table) BYTE 'B' DWORD Process_B BYTE 'C' DWORD Process_C BYTE 'D' DWORD Process_D Number. Of. Entries = ($ - Case. Table) / Entry. Size Web site Examples 52
表格驅動式選擇(3 of 3) Step 2: 使用一個迴圈搜尋表格: mov ebx, OFFSET Case. Table mov ecx, Number. Of. Entries L 1: cmp al, [ebx] jne L 2 call NEAR PTR [ebx + 1] jmp L 3 L 2: add ebx, Entry. Size loop L 1 ; point EBX to the table ; loop counter ; ; ; match found? no: continue yes: call the procedure and exit the loop point to next entry repeat until ECX = 0 L 3: required for procedure pointers Web site Examples 53
實行FSM 下列各項在整數FSM中是來陳述編碼: State. A: call Getnext cmp al, '+' je State. B cmp al, '-' je State. B call Is. Digit jz State. C call Display. Error. Msg jmp Quit ; ; ; ; read next char into AL leading + sign? go to State B leading - sign? go to State B ZF = 1 if AL = digit go to State C invalid input found 顯示 Finite. asm 原始碼. Web site Examples 58
使用. IF指令 • • • Runtime 語法 Relational and Logical 運算 MASM-Generated 編碼. REPEAT D指令. WHILE 指令 Web site Examples 61
MASM產生編碼. data val 1 DWORD 5 result DWORD ? . code mov eax, 6. IF eax > val 1 mov result, 1. ENDIF Generated code: mov eax, 6 cmp eax, val 1 jbe @C 0001 mov result, 1 @C 0001: MASM自動地產生一次無號的跳躍(JBE). Web site Examples 64
MASM-產生編碼. data val 1 SDWORD 5 result SDWORD ? . code mov eax, 6. IF eax > val 1 mov result, 1. ENDIF 產生編碼: mov eax, 6 cmp eax, val 1 jle @C 0001 mov result, 1 @C 0001: MASM自動地產生一次無號的跳躍(JLE). Web site Examples 65
. REPEAT 指令 在嘗試與迴圈情況有關的之前執行環體, UNTIL 執行. 例題: ; Display integers 1 – 10: mov eax, 0. REPEAT inc eax call Write. Dec call Crlf. UNTIL eax == 10 Web site Examples 66
. WHILE 指令 在執行環體之前測試迴圈情況.ENDW 指令標示迴圈的結束. 例題: ; Display integers 1 – 10: mov eax, 0. WHILE eax < 10 inc eax call Write. Dec call Crlf. ENDW Web site Examples 67
The End Web site Examples 68
- Slides: 68