Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.
-
date post
19-Dec-2015 -
Category
Documents
-
view
239 -
download
1
Transcript of Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.
![Page 1: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/1.jpg)
Chapter 8 Bit Manipulation
Contents:Logical OperationsShift and Rotate Instructions
![Page 2: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/2.jpg)
8.1 Logical Operations AND destination , source TEST destination , source
The same as AND instruction but not change destination
NOT destination OR destination , source
inclusive XOR destination , source
exclusive
![Page 3: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/3.jpg)
True table
A B T
0 0 0
0 1 0
1 0 0
1 1 1
ANDsrc
reg mem imm
reg √ √ √mem √ √
dest
A B T
0 0 0
0 1 1
1 0 1
1 1 0
A B T
0 0 0
0 1 1
1 0 1
1 1 1
A T
0 1
1 0
![Page 4: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/4.jpg)
flags Logical operation except for NOT
will affect flag register. CF=0 OF=0 AF: undefined PF, SF, ZF: set by the value of result.
![Page 5: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/5.jpg)
Clear selected bits (marking) Clear all but the last four bits in EAX
AND eax , 0000000fh
![Page 6: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/6.jpg)
Set selected bits Set all but the last four bits in EAX
OR eax , fffffff0h
![Page 7: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/7.jpg)
negate selected bits negate all but the last four bits in EAX
XOR eax , fffffff0h Xor eax, eax
![Page 8: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/8.jpg)
; AND 指令可用于复位某些位(同 0 相与),不影响其他位:将 BL 中 D3 和 D0 位清 0 ,其他位不变
and bl,11110110B
; OR 指令可用于置位某些位(同 1 相或),不影响其他位:将 BL 中 D3 和 D0 位置 1 ,其他位不变
or bl, 00001001B
; XOR 指令可用于求反某些位(同 1 相异或),不影响其他位:将 BL 中 D3 和 D0 位求反,其他不变
xor bl, 00001001B
![Page 9: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/9.jpg)
Perform certain arithmetic operation
mov edx , 0 mov ebx , 32 div ebx
mov edx , eax and edx , 0000001fh
![Page 10: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/10.jpg)
Manipulate ASCII codes Convert ASCII code to integer
Sub eax , 00000030h And eax, 0000000fh
Convert integer to ASCII Or bl, 30h
Change the case of ASCII code Xor cl , 00100000b
![Page 11: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/11.jpg)
Application of TEST Examine a particular bit is “1” or
“0” Test dx, 2000h
Get information about a value Test cx , cx
The following instruction usually is “jcc”
![Page 12: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/12.jpg)
test al,01h ;测试 AL 的最低位 D0jnz there ;标志 ZF=0 ,即 D0=1
;则程序转移到 there... ;否则 ZF=1 ,即 D0=0 ,顺序执行there: ...
TEST 指令通常用于检测一些条件是否满足,但又不希望改变原操作数的情况
TESTTEST
![Page 13: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/13.jpg)
Shift and Rotate Shift and rotate instructions manipulate
binary numbers at the binary bit level, as did the AND, OR, Exclusive-OR, and NOT instructions.
Shifts and rotates find their most common applications in low-level software used to control I/O devices.
The microprocessor contains a complete set of shift and rotate instructions that are used to shift or rotate any memory data or register.
![Page 14: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/14.jpg)
Shift instructions Shift instructions position or move numbers to
the left or right within a register or memory location. Logical shift Arithmetic shift
Logical shifts multiply or divide unsigned data, and arithmetic shifts multiply or divide signed data. A shift left multiplies by 2 for each bit position
shifted a shift right divides by 2 for each bit position shifted
![Page 15: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/15.jpg)
Shift instructions
SHL dest, countSHR dest, countSAL dest, countSAR dest, count; dest is the target operand being shifted using register or memory addressing mode; count is the number of times (or bits) that the operand is shifted, it can be specified directly using an immediate shift count, or through the CL register.
![Page 16: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/16.jpg)
flags
Shift instructions will change flags: CF: change by shifted bit OF:
Multiple-bit shift: undefined Single-bit shift
0: if the sign bit of the result is the same as the sign bit of the original operand value.
1: they are different ZF,PF: assigned according to result AF: undesigned
![Page 17: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/17.jpg)
Examples sal cx , 1; before cx=a9d7 shr ax , 1; before ax=a9d7 sar bx, 1; before bx=a9d7 sal ace, 4; before ace=a9d7 shr dx, 4; before dx=a9d7 sar ax, cl; before ax=a9d7, cl=04
![Page 18: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/18.jpg)
;将 DX.AX 中 32 位数值左移一位shl ax,1rcl dx,1
DX AXCF
0
![Page 19: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/19.jpg)
;把 AL 最低位送 BL 最低位,保持 AL不变
ror bl,1
ror al,1
rcl bl,1
rol al,1
AL、 BL CF
BL CF
ALCF
AL之 D0
![Page 20: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/20.jpg)
Figure 8.7
… lea ebx, hexOut+7 mov ecx, 8forCount: mov edx, eax and edx, 0000000fh cmp edx, 9 jnle elseLetter or edx, 30h
jmp endifDigitelseLetter: add edx, ‘A’-10EndifDigit: mov BYTE PTR [ebx], dlDec ebx shr eax, 4Loop forCount…
![Page 21: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/21.jpg)
Double shift instructions sh-d dest, src, count
-: l(left shift) , r (right shift) dest: word or double word in a register o
r memory src: word or double word in a register count: immediate or CL
![Page 22: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/22.jpg)
flags
CF: last bit shifted out goes to CFSF, ZF, PF: assigned corresponding
to the resultOF: undefined
![Page 23: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/23.jpg)
Examples:
shld ecx, eax, 12; before ECX=12345678, EAX=90ABCDEF
shrd ecx, eax, CL; before ECX=12345678, EAX=90ABCDEF, CL=08
![Page 24: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/24.jpg)
Figure 8.7
… lea ebx, hexOut ;lea ebx, hexOut+7 mov ecx, 8forCount: shld edx, eax, 4 ;mov edx, eax and edx, 0000000fh cmp edx, 9 jnle elseLetter or edx, 30h
jmp endifDigitelseLetter: add edx, ‘A’-10EndifDigit: mov BYTE PTR [ebx], dl inc ebx ;dec ebx shl eax ,4 ;shr eax, 4 loop forCount…
![Page 25: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/25.jpg)
Rotate instructionsrotate dest, count; rotate is RCL (rotate left with carry), RCR (rotate right with carry), ROL (rotate left), and ROR (rotate right); dest is the target operand being rotated using register or memory addressing mode; count is the number of times (or bits) that the operand is rotated, it can be specified directly using an immediate shift count, or through the CL register.
![Page 26: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/26.jpg)
Rotate operation
Rotate instructions position binary data by rotating the information in a register or memory location, either from one end to another or through the carry flag.
Rotate instructions are often used to wide numbers to the left or right.
![Page 27: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/27.jpg)
;将 DX.AX 中 32 位数值左移一位shl ax,1rcl dx,1
DX AXCF
0
![Page 28: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/28.jpg)
;把 AL 最低位送 BL 最低位,保持 AL不变
ror bl,1
ror al,1
rcl bl,1
rol al,1
AL、 BL CF
BL CF
ALCF
AL之 D0
![Page 29: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/29.jpg)
Figure 8.7
… lea ebx, hexOut+7 ;lea ebx, hexOut mov ecx, 8forCount: rol eax, 4 mov edx, eax and edx, 0000000fh cmp edx, 9 jnle elseLetter or edx, 30h
jmp endifDigitelseLetter: add edx, ‘A’-10EndifDigit: mov BYTE PTR [ebx], dl inc ebx ;dec ebx ;shr eax, 4 loop forCount…
![Page 30: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/30.jpg)
ASCII to double integer conversion
Atod procedureatodproc PROC NEAR32 push ebp ; save base pointer mov ebp, esp ; establish stack frame sub esp, 4 ; local space for sign push ebx ; Save registers push ecx push edx pushf ; save flags
![Page 31: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/31.jpg)
mov esi,[ebp+8] ; get parameter (source addr)
WhileBlankD:cmp BYTE PTR [esi],' ' ; space? jne EndWhileBlankD ; exit if not inc esi ; increment character pointer jmp WhileBlankD ; and try againEndWhileBlankD:
![Page 32: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/32.jpg)
mov eax,1 ; default sign multiplierIfPlusD: cmp BYTE PTR [esi],'+' ; leading + ? je SkipSignD ; if so, skip overIfMinusD: cmp BYTE PTR [esi],'-' ; leading - ? jne EndIfSignD ; if not, save default + mov eax,-1 ; -1 for minus signSkipSignD: inc esi ; move past signEndIfSignD:
![Page 33: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/33.jpg)
mov [ebp-4],eax ; save sign multipliermov eax,0 ; number being accumulatedmov cx,0 ; count of digits so far
![Page 34: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/34.jpg)
WhileDigitD:cmp BYTE PTR [esi],'0' ; compare next character to '0'jl EndWhileDigitD ; not a digit if smaller than '0'cmp BYTE PTR [esi],'9' ; compare to '9'jg EndWhileDigitD ; not a digit if bigger than '9'imul eax,10 ; multiply old number by 10jo overflowD ; exit if product too largemov bl,[esi] ; ASCII character to BLand ebx,0000000Fh ; convert to single-digit integer add eax,ebx ; add to sum jc overflowD ; exit if sum too large inc cx ; increment digit count inc esi ; increment character pointer jmp WhileDigitD ; go try next characterEndWhileDigitD:
![Page 35: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/35.jpg)
cmp cx,0 ; no digits?jz overflowD ; if so, set overflow error flag
; if value is 80000000h and sign is '-', ; want to return 80000000h (-2^32)
cmp eax,80000000h ; 80000000h ? jne TooBigD? cmp DWORD PTR [ebp-4],-1 ; multiplier -1 ? je ok1D ; if so, return 8000h
TooBigD?: test eax,eax ; check sign flag jns okD ; will be set if number > 2^32 - 1
![Page 36: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/36.jpg)
overflowD: pop ax ; get flagsor ax,0000100001000100B ; set overflow, zero & parity flagsand ax,1111111101111110B ; reset sign and carry flags push ax ; push new flag values mov eax,0 ; return value of zero jmp AToDExit ; quit
okD: imul DWORD PTR [ebp-4] ; make signed numberok1D: popf ; get original flags test eax,eax ; set flags for new number pushf ; save flags
![Page 37: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/37.jpg)
AToDExit: popf ; get flags pop edx ; restore registers pop ecx pop ebx mov esp, ebp ; delete local variable space pop ebp ret 4 ; exit, removing parameteratodproc ENDP
![Page 38: Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.](https://reader035.fdocuments.us/reader035/viewer/2022062216/56649d3a5503460f94a15898/html5/thumbnails/38.jpg)
Exercises P276 Exercises8.1 1, 3, 4, 5 P289 Exercises8.2 1, 4, 5