Bitwise and Logical Manipulations - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Bitwise and Logical Manipulations

Description:

This will clear (set to 0) bit 7 of AH leaving all other bits unchanged. Mask out unwanted bits ... Bit Shifting. Slide bits in byte or word to left or right ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 18
Provided by: timma87
Category:

less

Transcript and Presenter's Notes

Title: Bitwise and Logical Manipulations


1
Bitwise and Logical Manipulations
  • Assembly Language Programming
  • University of Akron
  • Dr. Tim Margush

2
Boolean Data
  • 0 or 1
  • Requires only a single bit
  • 0 FALSE
  • 1 TRUE
  • Boolean operators
  • Unary NOT
  • Binary AND, OR, XOR

3
NOT
  • NOT destination
  • Register or memory
  • Does not affect flags
  • Each 0 becomes 1, 1 becomes 0
  • Sometimes called the 1's complement

4
AND, OR, XOR
  • ANDORXOR destination, source
  • reg, regmemimmed
  • mem, regimmed
  • SF, ZF, PF are meaningfully set, CFOF0
  • x AND y 1 IFF xy1
  • x OR y 0 IFF xy0
  • x XOR y 0 IFF xy

5
Applications of AND
  • Clear a bit
  • AND AH, 01111111B
  • This will clear (set to 0) bit 7 of AH leaving
    all other bits unchanged
  • Mask out unwanted bits
  • AND AX,000Fh
  • This will clear all but the low-nybble of AX,
    leaving that nybble unchanged

6
Turn Off NumLock
  • clear bit 5 in keyboard status byte
  • mov ax,40h
  • mov ds,ax set DS
  • mov bx,17h byte ptr
  • and byte ptr bx,0DFh
  • Keyboard Status Byte is at 00400017
  • bit 7 Insert Mode
  • bit 6 Caps Lock
  • bit 5 Num Lock
  • bit 4 Scroll Lock
  • bit 3 Alt Pressed
  • bit 2 Ctrl Pressed
  • bit 1 Left Shift
  • bit 0 Right Shift

7
Applications of OR
  • Setting a bit
  • OR BX, 0400h
  • This sets bit 10 of BX, leaving all other bits
    unchanged
  • Checking the value of certain bit
  • OR AX,AX
  • This sets flags, does not change AX
  • Bit 15 sign bit (JS, JNS, JG, JGE, JL, JLE)
  • ZF1 IFF AX0 (JZ, JNZ)

8
Converting Data
  • DL contains 0-9
  • OR DL,00110000b
  • DL now contains '0'-'9'
  • AH contains letter ('a'-'z','A'-'Z')
  • OR AH,00100000b
  • AH is now lower case
  • ASCII for digit x (0-9) is 3x
  • Setting bits 4 and 5 will turn a digit value
    stored in a byte to the digit's ASCII code
  • Upper lower case characters differ only in bit 5
    (1lowercase)

9
Application of XOR
  • Bit toggling
  • XOR AH, 10000000B
  • This will change bit 7 (only) of AH
  • Clearing a byte or word
  • XOR AX, AX
  • This sets AX to 0
  • Encryption/Decryption
  • XOR AL, Key encrypts/decrypts byte in AL

10
TEST
  • TEST destination, source
  • Performs AND, does not store result
  • Flags are set as if the AND were executed
  • Example
  • TEST CL, 10000001b
  • JZ EvenAndNonNegative
  • JS Negative
  • must be odd and positive

11
Bit Shifting
  • Slide bits in byte or word to left or right
  • What happens to bit that is shifted out?
  • It is copied into the CF
  • What bit value is shifted in?
  • SHR, SHL, SAL 0
  • SAR sign bit
  • ROR, ROL bit shifted out
  • RCR, RCL CF

12
Shifts and Rotates
  • Op destination, 1
  • Op destination, CL do n times (n is in CL)
  • destination is any 8 or 16-bit register or memory
    location
  • If CL is specified, it will be unchanged
  • SF, PF, and ZF are set according to result
  • CF is last bit shifted out
  • OF1 if last shift changes the sign

13
SxL and SxR
  • SAL and SHL are identical
  • Each shift doubles the numeric value (up to
    overflow)
  • SHR is a logical (or unsigned) shift
  • Each shift halves the unsigned value
  • SAR preserves the sign bit
  • Each shift halves number and rounds DOWN
  • SAR AL,1 when AL-3 gives -2, not -1

14
Multiplication by 5
  • Assume AX contains a number N to be multiplied
    by 5
  • MOV DX,AX DXN also
  • SHL AX,1 AX2N
  • SHL AX,1 AX4N
  • ADD AX,DX AX4NN5N
  • This is likely to be much faster than a multiply
    instruction

Overflow (signed or unsigned) would be checked
after each operation by examining OF or CF
15
Application Binary Output
  • Problem Output AX in binary format
  • Each bit must be translated to '0' or '1' and
    output
  • We can build up the string in memory, or output
    each character as it is determined
  • Our sample solution will put the bits in memory
  • Note '0' and '1' differ only in bit position 0

16
Binary Output - Details
  • result db 16 dup (?), ''
  • mov bx,15 offset and counter
  • nextbit ax contains bits to output
  • mov resultbx,'0' assume 0
  • ror ax, 1 CF bit 0
  • jnc digit0 digit is OK if CF0
  • inc resultbx '0'-gt'1'
  • digit0 rotating ax preserves its value
  • dec bx affects SF
  • jge nextbit loop while bxgt0
  • use DOS function 9 to display result

17
Homework
  • Page 135
  • 1, 2, 4, 7 (do not use multiply or divide
    instructions)
Write a Comment
User Comments (0)
About PowerShow.com