View
241
Download
1
Embed Size (px)
Citation preview
Chapter 8 Bit Manipulation
Contents:Logical OperationsShift and Rotate Instructions
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
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
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.
Clear selected bits (marking) Clear all but the last four bits in EAX
AND eax , 0000000fh
Set selected bits Set all but the last four bits in EAX
OR eax , fffffff0h
negate selected bits negate all but the last four bits in EAX
XOR eax , fffffff0h Xor eax, eax
; 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
Perform certain arithmetic operation
mov edx , 0 mov ebx , 32 div ebx
mov edx , eax and edx , 0000001fh
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
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”
test al,01h ;测试 AL 的最低位 D0jnz there ;标志 ZF=0 ,即 D0=1
;则程序转移到 there... ;否则 ZF=1 ,即 D0=0 ,顺序执行there: ...
TEST 指令通常用于检测一些条件是否满足,但又不希望改变原操作数的情况
TESTTEST
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.
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
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.
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
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
;将 DX.AX 中 32 位数值左移一位shl ax,1rcl dx,1
DX AXCF
0
;把 AL 最低位送 BL 最低位,保持 AL不变
ror bl,1
ror al,1
rcl bl,1
rol al,1
AL、 BL CF
BL CF
ALCF
AL之 D0
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…
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
flags
CF: last bit shifted out goes to CFSF, ZF, PF: assigned corresponding
to the resultOF: undefined
Examples:
shld ecx, eax, 12; before ECX=12345678, EAX=90ABCDEF
shrd ecx, eax, CL; before ECX=12345678, EAX=90ABCDEF, CL=08
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…
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.
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.
;将 DX.AX 中 32 位数值左移一位shl ax,1rcl dx,1
DX AXCF
0
;把 AL 最低位送 BL 最低位,保持 AL不变
ror bl,1
ror al,1
rcl bl,1
rol al,1
AL、 BL CF
BL CF
ALCF
AL之 D0
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…
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
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:
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:
mov [ebp-4],eax ; save sign multipliermov eax,0 ; number being accumulatedmov cx,0 ; count of digits so far
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:
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
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
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
Exercises P276 Exercises8.1 1, 3, 4, 5 P289 Exercises8.2 1, 4, 5