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

Slides:



Advertisements
Similar presentations
NEG Instruction Change operand content into two’s complement (negative value) and stored back into its operand mov bl, b neg bl; bl = mov.
Advertisements

1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.c: Logical Operations Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Computer Organization & Assembly Language
80x86 Instruction Set Dr. Qiang Lin.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers
CS2422 Assembly Language and System Programming Conditional Processing Department of Computer Science National Tsing Hua University.
9-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL x86 Instructions Part.
Shift and Rotate Instructions
Microcomputer & Interfacing Lecture 3
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Ch. 5 from Yu & Marut. Registers 14(16-bit) registers: 1.Data reg. – to hold data for an op. 2.Address reg – to hold addr of an instruction or data.
Introduction to Computer Engineering by Richard E. Haskell Shift and Rotate Instructions Module M16.2 Section 10.3.
Ch. 7 Logic, Shift and Rotate instr.
Khaled A. Al-Utaibi  Introduction  Arithmetic Instructions  Basic Logical Instructions  Shift Instructions  Rotate Instructions.
11.1/36 Repeat: From Bits and Pieces Till Strings.
Sahar Mosleh California State University San MarcosPage 1 CPU Flags and Boolean Instructions.
Bits and Bytes. BITWISE OPERATORS Recall boolean logical operators in Java… boolean logical operators: &, |, ^ not: ! Show truth tables.
ICS312 Set 9 Logic & Shift Instructions. Logic & Shift Instructions Logic and Shift Instructions can be used to change the bit values in an operand. The.
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Boolean and Comparison Instructions Operation Description ANDAND Destination, Source OROR Destination, Source XORXOR Destination, Source NOTNOT Destination.
Department of Computer Science and Software Engineering
Lecture 5 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Arithmetic Flags and Instructions
Logic Conditional Processing. Status flags - review The Zero flag is set when the result of an operation equals zero. The Carry flag is set when an instruction.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Assembly 05. Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 1.
Logical and Bit Operations Chapter 9 S. Dandamudi.
EEL 3801 Part V Conditional Processing. This section explains how to implement conditional processing in Assembly Language for the 8086/8088 processors.
LEA instruction The LEA instruction can be used to get the offset address of a variable Example ORG 100h MOV AL, VAR1 ; check value of VAR1 by moving it.
3.4 Addressing modes Specify the operand to be used. To generate an address, a segment register is used also. Immediate addressing: the operand is a number.
Lecture 12 Integer Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
COMP 2003: Assembly Language and Digital Logic Chapter 2: Flags and Instructions Notes by Neil Dickson.
Introduction to Computer Organization and Assembly Language
The Assemble, Unassemble commands of the debugger: U Command for converting machine code language source Equivalent machine code instructions Equivalent.
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Microprocessor & Assembly Language
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
Boolean, Shift and Rotate instructions Dr.Hadi AL Saadi.
CS2422 Assembly Language and System Programming 0 Week 13 & 14 Codes in Assembly Language.
1 Chapter 6 Conditional Processing Assembly Language for Intel-Based Computers, 3rd edition Kip R. Irvine.
Microprocessor Systems Design I
Practical Session 2.
Today we are going to discuss about,
Microprocessor Systems Design I
Chapter 3 Bit Operations
EE3541 Introduction to Microprocessors
Instruction System - Bit Manipulation Instruction
INSTRUCTION SET.
More on logical instruction and
Assembly Language Programming Part 2
16.317: Microprocessor System Design I
INSTRUCTION SET OF 8086 PAWAN KUMAR SINGH.
UNIT: 2 INSTRUCTION SET OF 8086.
Shift & Rotate Instructions)
Shift & Rotate Instructions)
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for Intel-Based Computers, 5th Edition
Shift, Multiply, and Divide
X86 Assembly Review.
Microprocessor and Assembly Language
Computer Organization and Assembly Language
Shift and Rotate Instructions.
CS-401 Computer Architecture & Assembly Language Programming
Ch. 5 – Intel 8086 – study details from Yu & Marut
Presentation transcript:

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

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/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

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

3/17/2016Dr. Tim Margush - Assembly Language Programming 5 Applications of AND Clear a bit ›ANDAH, B 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

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

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)

3/17/2016Dr. Tim Margush - Assembly Language Programming 8 Converting Data ;DL contains 0-9 OR DL, b ;DL now contains '0'-'9' ;AH contains letter ('a'-'z','A'-'Z') OR AH, b ;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)

3/17/2016Dr. Tim Margush - Assembly Language Programming 9 Application of XOR Bit toggling ›XOR AH, B 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

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, b JZ EvenAndNonNegative JS Negative ;must be odd and positive

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

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

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

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

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

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