Chap 4 e

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Logic, Shift, and Rotate Instructions

Read Sections 6.2, 7.2 and 7.3 of textbook

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)
2

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

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

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

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-Z Lowercase (61h-Ah) a-z
4X 0 1 0 0 X 5X 0 1 0 1 X 6X 0 1 1 0 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


6

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

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.

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)

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
10

Fast Multiplication
Each left shift multiplies by 2 the operand for both signed and unsigned interpretations. Ex:
mov mov shl shl ax, bx, ax, bx, 4 -1 2 3 ;AX ;BX ;AX ;BX = = = = 0004h FFFFh 0010h = 16 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)

11

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)


12

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 sar ah, 1 ;AH = 1111 0001b ;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
13

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


14

Examples of ROL
mov rol rol rol mov rol rol rol ah,40h ah,1 ah,1 ah,1 ;ah ;ah ;ah ;ah = = = = 0100 1000 0000 0000 0000b 0000b, CF = 0 0001b, CF = 1 0010b, CF = 0

ax,1234h ax,4 ;ax ax,4 ;ax ax,4 ;ax

;ax = 0001 0010 0011 0100b = 2341h = 3412h = 4123h

15

Rotate with CF
RCL rotates to the left with participation of CF

RCR rotates to the right with participation of CF

16

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 start: shl al, 1 rcr bl, 1 loop start mov al, bl ;number of bits to rotate ;CF = msb of AL ;push CF into msb of BL ;repeat for 8 bits ;store result into AL

17

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 =

18

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
19

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
20

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

21

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 != <CR>) 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

22

You might also like