Download presentation
Presentation is loading. Please wait.
Published byGeorge Rose Modified over 9 years ago
1
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook
2
2 Logic Instructions Syntax for AND, OR, XOR, and TEST instructions: op-code destination, source They perform the Boolean bitwise operation and store the result into destination. TEST is just an AND but the result is not stored. TEST affects the flags just like AND does. Both operands must be of the same type either byte, word or dword Both operands cannot be mem again: mem to mem operations are forbidden They clear (ie: put to zero) CF and OF They affect SF and ZF according to the result of the operation (as usual)
3
3 Logic Instructions (cont.) The source is often an imm operand called a bit mask: used to fix certain bits to 0 or 1 To clear a bit we use an AND since: 0 AND b = 0 (b is cleared) 1 AND b = b (b is conserved) 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
4
4 Logic Instructions (cont.) To set (i.e: fix to 1) certain bits, we use OR: 1 OR b = 1 (b is set) 0 OR b = b (b is conserved) To set the sign bit of AH, we do: OR ah,80h To test if ECX=0 we can do: OR ecx,ecx since this does not change the number in ECX and set ZF=1 if and only if ECX=0
5
5 Logic Instructions (cont.) XOR can be used to inverse certain bits: b XOR 1 = NOT(b) (b is complemented) b XOR 0 = b (b is conserved) Ex: to initialize a register to 0 we can use: XOR ax,ax Since b XOR b = 0 (b is cleared) This instruction uses only 2 bytes of space. The next instruction uses 3 bytes of space: MOV ax,0 Compilers prefer the XOR method
6
6 Logic Instructions (cont.)* To convert from upper case letter to lower case we can use the usual method: ADD dl,20h But 20h = 0010 0000b and bit #5 is always 0 for chars from ‘A’ (41h) to ‘Z’ (5Ah). Uppercase (41h-5Ah) A-ZLowercase (61h-Ah) a-z 4X 0 1 0 0 X6X 0 1 1 0 X 5X 0 1 0 1 X 7X 0 1 1 1 X Hence, adding 20h gives the same result as setting this bit #5 to 1. Thus: OR dl,20h ;converts from upper to lower case AND dl,0DFh;converts from lower to upper case since DFh = 1101 1111b
7
7 Logic Instructions (cont.) To invert all the bits (ones complement), we use: NOT destination does not affect any flag and destination cannot be an imm operand Recall that to perform twos complement, we use NEG destination affect SF and ZF according to result CF is set to 1 unless the result is 0 OF=1 iff there is a signed overflow
8
8 Exercise 1 Use only one instruction among AND, OR, XOR, and TEST to do the following task: (A) Convert the ASCII code of a decimal digit ('0‘ to '9‘) contained in AL to its numerical value. (B) Fix to 1 the odd numbered bits in EAX (ie: the bits numbered 1, 3, 5…) without changing the even numbered bits. (C) Clear to 0 the most significant bit and the least significant bit of BH without changing the other bits. (D) Inverse the least significant bit of EBX without changing the other bits.
9
9 Shifting Bits to the Left To shift 1 bit to the left we use: SHL dest,1 each bit is shifted one position to the left the lsb (least significant bit) is filled with 0 the msb (most significant bit) is moved into CF (so the previous content of CF is lost) dest can be either byte, word or dword Example: mov bx, 80h ; BX = 0080h shl bl, 1 ; BX = 0000h, CF=1 (only BL is affected)
10
10 Shifting Multiple Times to the Left Two forms are permitted: SHL dest, CL ; CL = number of shifts SHL dest, imm8 SHL affects SF and ZF according to the result CF contains the last bit shifted out mov bh, 82h ;BH = 1000 0010b shl bh, 2 ;BH = 0000 1000b, CF=0 Effect on OF for all shift and rotate instructions (left and right): For any single-bit shift/rotate: OF=1 iff the shift or rotate changes the sign bit For multiple-bit shift/rotate: the effect on OF is undefined Hence, sign overflows are signaled only for single-bit shifts and rotates
11
11 Fast Multiplication Each left shift multiplies by 2 the operand for both signed and unsigned interpretations. Ex: mov ax, 4 ;AX = 0004h mov bx, -1 ;BX = FFFFh shl ax, 2 ;AX = 0010h = 16 shl bx, 3 ;BX = FFF8h = -8 Multiplication by shifting is very fast. Try to factor your multiplier into powers of 2: BX * 36 = BX * (32 + 4) = BX*32 + BX*4 So add (BX shifted by 5) to (BX shifted by 2)
12
12 Shifting bits to the right To shift to the right use either: SHR dest, CL ;value of CL = number of shifts SHR dest, imm8 the msb of dest is filled with 0 the lsb of dest is moved into CF Each single-bit right shift divides the unsigned value by 2. Ex: mov bh,13 ;BH = 0000 1101b = 13 shr bh,2 ;BH = 0000 0011b = 3 (div by 4),CF=0 (the remainder of the division is lost)
13
13 Arithmetic Shift SAR Is needed to divide the signed value by 2: SAR dest, CL ;value of CL = number of shifts SAR dest, imm8 the msb of dest is filled with its previous value (so the sign is preserved) the lsb of dest is moved into CF mov ah, -15 ;AH = 1111 0001b sar ah, 1 ;AH = 1111 1000b = -8 the result is rounded to the smallest integer (-8 instead of -7…) in contrast: shr ah, 1 ;gives ah = 0111 1000b = 78h
14
14 Rotate (without the CF) ROL rotates the bits to the left (same syntax) CF gets a copy of the msb ROR rotates the bits to the right (same syntax) CF gets a copy of the lsb CF reflect the action of the last rotate
15
15 Examples of ROL mov ah,40h ;ah = 0100 0000b rol ah,1 ;ah = 1000 0000b, CF = 0 rol ah,1 ;ah = 0000 0001b, CF = 1 rol ah,1 ;ah = 0000 0010b, CF = 0 mov ax,1234h ;ax = 0001 0010 0011 0100b rol ax,4 ;ax = 2341h rol ax,4 ;ax = 3412h rol ax,4 ;ax = 4123h
16
16 Rotate with CF RCL rotates to the left with participation of CF RCR rotates to the right with participation of CF
17
17 Ex: inverting the content of AL* Ex: whenever AL = 1 1 0 0 0 0 0 1b we want to have AL = 1 0 0 0 0 0 1 1b mov ecx, 8;number of bits to rotate start: shl al, 1;CF = msb of AL rcr bl, 1;push CF into msb of BL loop start;repeat for 8 bits mov al, bl;store result into AL
18
18 Exercise 2 Give the binary content of AX immediately after the execution of the each instruction below (Consider that AX = 1011 0011 1100 1010b before each of these instructions): (A) SHL AL,2 ; AX = (B) SAR AH,2 ; AX = (C) ROR AX,4 ; AX = (D) ROL AX,3 ; AX = (E) SHL AL,8 ; AX =
19
19 Application: Binary Output To display the binary number in EAX: MOV ECX,32 ; count 32 binary chars START: ROL EAX,1 ;CF gets msb JC ONE ;if CF =1 MOV EBX, ’0’ JMP DISP ONE: MOV EBX,’1’ DISP: PUTCH EBX LOOP START
20
20 Application: Binary Input To load EAX with the numerical value of a binary string (ex: 101100101...) entered at the keyboard: xor ebx, ebx ;clear ebx to hold entry next: getch cmp eax, 10 ;end of input line reached? je exit ;yes then exit and al, 0Fh ;no, convert to binary value shl ebx, 1 ;make room for new value or bl, al ;put value in ls bit jmp next exit: mov eax,ebx ;eax holds binary value In AL we have either 30h or 31h (ASCII code of ‘0’ and ‘1’) Hence, AND AL,0Fh converts AL to either 0h or 1h Hence, OR BL,AL possibly changes only the lsb of BL
21
21 Algorithm for Hex Output To display in hexadecimal the content of EAX Repeat 8 times { ROL EAX, 4 ;the ms 4bits goes into ls 4bits MOV DL, AL AND DL, 0Fh ;DL contains num value of 4bits If DL < 10 then convert to ‘0’..’9’ else convert to ‘A’..’F’ } end Repeat The complete ASM coding is left to the reader
22
22 Algorithm for Hex Input To load EAX with the numerical value of the hexadecimal string entered at the keyboard: XOR EBX, EBX ;EBX will hold result While (input char != ) DO { convert char into numerical value left shift EBX by 4 bits insert value into lower 4 bits of EBX } end while MOV EAX,EBX The complete ASM coding is left to the reader
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.