Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Bitwise and Logical Manipulations Assembly Language Programming University of Akron Dr. Tim Margush."— Presentation transcript:

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

2 3/17/2016Dr. Tim Margush - Assembly Language Programming 2 Boolean Data 0 or 1 Requires only a single bit ›0 = FALSE ›1 = TRUE Boolean operators ›Unary: NOT ›Binary: AND, OR, XOR

3 3/17/2016Dr. Tim Margush - Assembly Language Programming 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 3/17/2016Dr. Tim Margush - Assembly Language Programming 4 AND, OR, XOR AND|OR|XOR destination, source ›reg, reg|mem|immed ›mem, reg|immed SF, ZF, PF are meaningfully set, CF=OF=0 x AND y = 1 IFF x=y=1 x OR y = 0 IFF x=y=0 x XOR y = 0 IFF x=y

5 3/17/2016Dr. Tim Margush - Assembly Language Programming 5 Applications of AND Clear a bit ›ANDAH, 01111111B This will clear (set to 0) bit 7 of AH leaving all other bits unchanged Mask out unwanted bits ›ANDAX,000Fh This will clear all but the low-nybble of AX, leaving that nybble unchanged

6 3/17/2016Dr. Tim Margush - Assembly Language Programming 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 0040:0017 ›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 3/17/2016Dr. Tim Margush - Assembly Language Programming 7 Applications of OR Setting a bit ›ORBX, 0400h This sets bit 10 of BX, leaving all other bits unchanged Checking the value of certain bit ›ORAX,AX This sets flags, does not change AX Bit 15 = sign bit (JS, JNS, JG, JGE, JL, JLE) ZF=1 IFF AX=0 (JZ, JNZ)

8 3/17/2016Dr. Tim Margush - Assembly Language Programming 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 (1=lowercase)

9 3/17/2016Dr. Tim Margush - Assembly Language Programming 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 ›XORAL, Key ;encrypts/decrypts byte in AL

10 3/17/2016Dr. Tim Margush - Assembly Language Programming 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 3/17/2016Dr. Tim Margush - Assembly Language Programming 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 3/17/2016Dr. Tim Margush - Assembly Language Programming 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 OF=1 if last shift changes the sign

13 3/17/2016Dr. Tim Margush - Assembly Language Programming 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 3/17/2016Dr. Tim Margush - Assembly Language Programming 14 Multiplication by 5 ;Assume AX contains a number N to be multiplied by 5 MOV DX,AX;DX=N also SHL AX,1;AX=2N SHL AX,1;AX=4N ADDAX,DX;AX=4N+N=5N 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 3/17/2016Dr. Tim Margush - Assembly Language Programming 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 3/17/2016Dr. Tim Margush - Assembly Language Programming 16 Binary Output - Details resultdb16 dup (?), '$' movbx,15;offset and counter nextbit: ;ax contains bits to output movresult[bx],'0' ;assume 0 rorax, 1;CF = bit 0 jncdigit0;digit is OK if CF=0 incresult[bx];'0'->'1' digit0: ;rotating ax preserves its value decbx;affects SF jgenextbit;loop while bx>=0 ;use DOS function 9 to display result


Download ppt "Bitwise and Logical Manipulations Assembly Language Programming University of Akron Dr. Tim Margush."

Similar presentations


Ads by Google