# Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions.

## Presentation on theme: "Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions."— Presentation transcript:

Chapter 8 Bit Manipulation Contents: Logical Operations Shift 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 ABT 000 010 100 111 AND src regmemimm reg√√√ mem√√ dest ABT 000 011 101 110 ABT 000 011 101 111 AT 01 10

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 中 D 3 和 D 0 位清 0 ，其他位 不变 and bl,11110110B ； OR 指令可用于置位某些位（同 1 相或），不 影响其他位：将 BL 中 D 3 和 D 0 位置 1 ，其他位不 变 or bl, 00001001B ； XOR 指令可用于求反某些位（同 1 相异或）， 不影响其他位：将 BL 中 D 3 和 D 0 位求反，其他不 变 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 的最低位 D 0 jnz there ；标志 ZF=0 ，即 D 0 =1 ；则程序转移到 there... ；否则 ZF=1 ，即 D 0 =0 ，顺序执行 there:... TEST 指令通常用于检测一些条件是否 满足，但又不希望改变原操作数的情况 TEST

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, count SHR dest, count SAL dest, count SAR 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.

SHL / SAL instruction

SHR instruction

SAR instruction

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,1 rcl dx,1 DX AX CF 0

；把 AL 最低位送 BL 最低位，保持 AL 不 变 ror bl,1 ror al,1 rcl bl,1 rol al,1 AL 、 BL CF BL CF AL CF AL 之 D 0

Figure 8.7 … lea ebx, hexOut+7 mov ecx, 8 forCount: mov edx, eax and edx, 0000000fh cmp edx, 9 jnle elseLetter or edx, 30h jmp endifDigit elseLetter: add edx, ‘ A ’ -10 EndifDigit: mov BYTE PTR [ebx], dl Dec ebx shr eax, 4 Loop forCount …

Double shift instructions sh-d dest, src, count -: l(left shift), r (right shift) dest: word or double word in a register or memory src: word or double word in a register count: immediate or CL

flags CF: last bit shifted out goes to CF SF, ZF, PF: assigned corresponding to the result OF: 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, 8 forCount: shld edx, eax, 4 ;mov edx, eax and edx, 0000000fh cmp edx, 9 jnle elseLetter or edx, 30h jmp endifDigit elseLetter: add edx, ‘ A ’ -10 EndifDigit: mov BYTE PTR [ebx], dl inc ebx ;dec ebx shl eax,4 ;shr eax, 4 loop forCount …

Rotate instructions rotate 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.

ROL instruction

ROR instruction

RCL instruction

RCR instruction

；将 DX.AX 中 32 位数值左移一位 shl ax,1 rcl dx,1 DX AX CF 0

；把 AL 最低位送 BL 最低位，保持 AL 不 变 ror bl,1 ror al,1 rcl bl,1 rol al,1 AL 、 BL CF BL CF AL CF AL 之 D 0

Figure 8.7 … lea ebx, hexOut+7 ;lea ebx, hexOut mov ecx, 8 forCount: rol eax, 4 mov edx, eax and edx, 0000000fh cmp edx, 9 jnle elseLetter or edx, 30h jmp endifDigit elseLetter: add edx, ‘ A ’ -10 EndifDigit: mov BYTE PTR [ebx], dl inc ebx ;dec ebx ;shr eax, 4 loop forCount …

ASCII to double integer conversion Atod procedure atodproc 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 again EndWhileBlankD:

mov eax,1 ; default sign multiplier IfPlusD: cmp BYTE PTR [esi],'+' ; leading + ? je SkipSignD ; if so, skip over IfMinusD: cmp BYTE PTR [esi],'-' ; leading - ? jne EndIfSignD ; if not, save default + mov eax,-1 ; -1 for minus sign SkipSignD: inc esi ; move past sign EndIfSignD:

mov [ebp-4],eax ; save sign multiplier mov eax,0 ; number being accumulated mov 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 10 jo overflowD ; exit if product too large mov bl,[esi] ; ASCII character to BL and 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 character EndWhileDigitD:

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 flags or ax,0000100001000100B ; set overflow, zero & parity flags and 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 number ok1D: 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 parameter atodproc ENDP

Exercises P276 Exercises8.1 1, 3, 4, 5 P289 Exercises8.2 1, 4, 5

Download ppt "Chapter 8 Bit Manipulation Contents: Logical Operations Shift and Rotate Instructions."

Similar presentations