Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 6 Conditional Processing Assembly Language for Intel-Based Computers, 3rd edition Kip R. Irvine.

Similar presentations


Presentation on theme: "1 Chapter 6 Conditional Processing Assembly Language for Intel-Based Computers, 3rd edition Kip R. Irvine."— Presentation transcript:

1

2 1 Chapter 6 Conditional Processing Assembly Language for Intel-Based Computers, 3rd edition Kip R. Irvine

3 2 Conditional Processing n 6.1 Boolean and Comparison Instructions n 6.2 Conditional Jumps n 6.3 Conditional Loops n 6.4 High-Level Logic Structures

4 3 Logic Instructions n Syntax for AND, OR, XOR, and TEST: op-code destination, source n AND, OR and XOR perform the Boolean bitwise operation and store the result into destination. n TEST is just an AND but the result is not stored n Both operands must be of the same type u either byte, word or dword n Both operands cannot be memory u again: memory to memory operations are forbidden n They affect SF, OF, CF, ZF, PF according to the result of the operation (as usual)

5 4 Logic Instructions (cont.) n The source is often an immediate operand called a bit mask: used to fix certain bits to 0 or 1 n To clear a bit we use an AND since: u 0 AND b = 0 (b is cleared) u 1 AND b = b (b is conserved) n Ex: to clear the sign bit of AL without affecting the others, we do:  AND al,7Fh ;msb of AL is cleared since 7Fh = 0111 1111b

6 5 Logic Instructions (cont.) n To set (ie: set to 1) certain bits, we use OR: u 1 OR b = 1 (b is set) u 0 OR b = b (b is conserved) n To set the sign bit of AH, we do:  OR ah,80h n To test if ECX=0 we can do:  OR ecx,ecx u since this does not change the number in ECX and set ZF=1 if and only if ecx=0

7 6 Logic Instructions (cont.) n XOR can be used to complement, conserve, or clear certain bits because: u b XOR 1 = NOT(b) (b is complemented) u b XOR 0 = b (b is conserved) u b XOR b = 0 (b is cleared) n Ex: to initialize a register to 0 we can use a two-byte instruction: u XOR ax,ax u instead of the three-byte instruction:  MOV ax,0 This was faster on 8088's

8 7 Logic Instructions (cont.) n Using OR and XOR in strange ways: n XOR ax, ax MOV ax, 0 OR ax, ax CMP ax, 0 bytes 2 3 86/88 3 cycles 4 cycles 286 2 cycles 2 cycles 386 2 cycles 2 cycles 486 1 cycle 1 cycle n Machine code 33 C0 B8 0000 0B C0 83 F8 00

9 8 Logic Instructions (cont.) n To convert from upper case to lower case we can use the usual method:  ADD dl,20h n But"A" = 0100 0001b "a" = 0110 0001b  OR dl,20h ;converts from upper to lower case  AND dl,0DFh ;converts from lower to upper case since DFh = 1101 1111b OR dl,20h and AND dh, 20h leaves lower case characters in dl unchanged and upper case characters in dh unchanged Bit 5

10 9 Logic Instructions (cont.) n To invert all the bits (ones complement): NOT destination u does not affect any flag and destination cannot be an immediate operand n To perform twos complement: NEG destination u affect SF, PF, ZF according to result u CF is set to 1 unless the result is 0 u OF=1 iff destination is the smallest negative number (ie: 80h for byte, 8000h for word...)

11 10 The CMP instruction CMP destination,source n Performs: destination - source but does not store the result of this subtraction into destination n But the flags are affected like SUB n Same restrictions on operands as SUB n Very often used just before performing a conditional jump n CMP is similar to TEST except TEST does AND operation

12 11 Conditional Jumps n A conditional jump transfers control to the destination_label when a flag condition is true. Syntax: u Jxxx destination_label u destination_label must be within -128 to +127 bytes from the current location. (Short jump.) n Groupings of conditional jumps: u General comparison jumps (no interpretation) u Unsigned comparison jumps u Signed comparison jumps

13 12 General Comparison Jumps

14 13 Unsigned Comparison Jumps The unsigned interpretation is used

15 14 Signed Comparison Jumps The signed interpretation is used SF = OF and ZF = 0

16 15 Using conditional jumps Consider mov ax, x sub ax, y jz SpecialCase n If x equals y then ax is zero and the jump is taken n If x is not equal to y then ax is not zero and the jump is not taken We could have used je SpecialCase

17 16 Using conditional jumps n Simpler if CMP is used before a Jxxx n To branch to exit when AX > BX under a signed interpretation:  cmp ax,bx u jg exit  If AX=1, BX=FFFFh, the jump is taken n To branch to exit when AX > BX under a unsigned interpretation:  cmp ax,bx u ja exit u if AX=1 and BX =FFFFh, the jump is not taken

18 17 Unconditional Jump n To jump unconditionally: jmp destination_label u where the destination_label can occur anywhere within the same segment n This permits us to circumvent the 128-byte range limitation of conditional jumps.

19 18 Loops larger than 128 bytes n Ex: suppose we want to do:  TOP:...more than 128 bytes of instructions... cmp ax,bx jne TOP n illegal n We do instead: TOP:...more than 128 bytes of instructions... cmp ax,bx je EXIT jmp TOP EXIT: MASM will automatically convert the source code on the left side to the machine code for right side

20 19 The Isalpha Procedure ; Isalpha sets ZF = 1 iff the character ; in AL is alphabetic. Isalpha proc push ax ; save AX and al,11011111b ; clear bit 5 cmp al,'A' ; check 'A'..'Z' range jb B1 cmp al,'Z' ja B1 test ax,0 ; ZF = 1 B1: pop ax ; restore AX ret Isalpha endp

21 20 Ex: checking for alphabetic input n The Isalpha procedure sets ZF to 1 iff the character in AL is alphabetic (upper or lower case). u When comparing characters: use unsigned comparison jumps u Trick: clear bit #5 and check if ‘A’ <= char <= ‘Z’ (converts lower case to upper case)  cmp al, ‘A’ will clear ZF when char < ‘A’  cmp al, ‘Z’ will clear ZF when char > ‘Z’  test ax, 0 is executed only if ‘A’ <= char <= ‘Z’ and sets ZF to 1

22 21 Ex: file encryption n The following property of XOR b XOR 0 = b (bit b is conserved) b XOR 1 = NOT(b) (bit b is complemented) can be used to encrypt files n If a character is stored in AL, XORing AL once encrypts the character. Using it the second time decrypts (restores) the character.  key = 137 ;a 1-byte secret key  xor al,key ;AL is encrypted  xor al,key ;AL is restored to its original value

23 22 Ex: file encryption (cont) n When AH=6 and DL=0FFh, INT 21h loads AL with the next char from the keyboard buffer or sets ZF to 1 when keyboard buffer is empty u (unlike function 1) it does not "busy" wait until a char enters the keyboard buffer n DOS input functions can be redirected from a file on the DOS command line n Hence, encrypt.asm can be used to encrypt and decrypt a fileencrypt.asm u C> encrypt coded (encryption) u C> encrypt my_file (decryption)

24 23 main proc ;encrypt.asm mov ax,@data mov ds,ax L1: mov ah,6 ; direct console input mov dl,0FFh ; don't wait for char int 21h ; AL = character jz L2 ; quit if ZF = 1 (EOF) xor al,key ; or any other value mov ah,2 ; write to output mov dl,al int 21h jmp L1 ; repeat the loop L2: mov ax,4C00h ; return to DOS int 21h main endp To encode and decode a file

25 24 Programming the Keyboard Status n Keyboard status byte: 0040:0017h n Bit assignments: ; 0 Right shift key down ; 1 Left shift key down ; 2 Left or right ctrl key down ; 3 Left or right alt key down ; 4 Scroll lock (toggle) ; 5 Num lock (toggle) ; 6 Caps lock (toggle) ; 7 Insert (toggle)

26 25 Programming the Keyboard Status n The keyboard status is stored in location 0040:0017. NumLock is bit 5. Lets turn it on  Numlock = 00100000b PUSH DS ; if needed MOV AX, 0040h MOV DS, AX MOV BX, 17h OR BYTE PTR [BX], Numlock; Set Numlock POP DS ; if needed

27 26 Programming the Keyboard Status n Easier: Assume that ES (extra segment) is not being used. Use the ES instead of DS n Numlock = 00100000b... mov ax, 40 ; this may only have mov es, ax ; to be done once... MOV BX, 17h OR BYTE PTR ES:[BX], Numloc ; Set Numlock

28 27 Programming the Keyboard Status n Other operations mov ax, 40 ; this may only need mov es, ax ; to be done once Numlock = 00100000b NotNumlock = not Numlock... MOV BX, 17h AND BYTE PTR ES:[BX], NotNumlock ; Clear Numlock OR BYTE PTR ES:[BX], Numlock ; Set Numlock XOR BYTE PTR ES:[BX], Numlock ; Toggle Numlock TEST BYTE PTR ES:[BX], Numlock ; Test Numlock See keyboard.asm. It continually reads the keyboard status byte and reports the statuskeyboard.asm

29 28 High-Level Flow Control Structures n High-level languages uses high-level structures such as if-then-else, case, while... to control the flow of execution u algorithms are normally expressed in terms of these high-level structures n Processors only provide conditional and unconditional jumps and loops u thus we need to decompose the high-level control flow structures into low-level ones n We give here a few examples; see textbook for more

30 29 If-Then-Else n Example: if (op1 < op2) then statement 1 else statement 2 end if n Analysis: u there is a jxxx to else when op1 >= op2 u there is a jump from end of statement 1 to end_if n ASM solution for signed comparison: cmp op1,op2 jge else_  statement 1 jmp end_if else_:  statement 2  end_if: n Note: “else” is a ASM reserved word. We use “else_” instead

31 30 While n Example : do while (op1 < op2) statement end do n Analysis: u jxxx to end_do when op1 >= op2 u jump from before the end_do to do_ while n AMS solution for an unsigned comparison: do_while: cmp op1,op2  jae end_do statement jmp do_while end_do:

32 31 Case n Example: case input of ‘A’ :procA ‘B’ :procB ‘C’ :procC end case n Analysis: cmp and jxxx for each case ASM solution: cmp input,’A’ jne L1 call procA jmp L3 L1: cmp input,’B’ jne L2 call procB jmp L3 L2: cmp input,’C’ jne L3 call procC L3:

33 32 Conditional Loops n The LOOPZ and LOOPE (loop if zero, loop if equal) continues a loop while ZF = 1 and CX > 0 u CX is first decremented, then the condition is tested (a trivial extension of LOOP) n LOOPNZ and LOOPNE continues the loop while ZF = 0 and CX > 0 n Syntax (same as LOOP): loopxx destination_label n ECX is used when in the 32 bit mode

34 33 Ex: scanning an integer array until a nonzero value is found mov bx,offset Warr-2 ;Warr is a word array mov cx,N ;of N elements next: add bx,2 cmp word ptr [bx], 0 loopz next ;loop until nonzero value is found n Note: it is an error to inverse the CMP and ADD operations (ADD affects the flags!)


Download ppt "1 Chapter 6 Conditional Processing Assembly Language for Intel-Based Computers, 3rd edition Kip R. Irvine."

Similar presentations


Ads by Google