2.2: Bitwise Logical Operations



Similar documents
Useful Number Systems

ELEG3924 Microprocessor Ch.7 Programming In C

Comp151. Definitions & Declarations

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Chapter Binary, Octal, Decimal, and Hexadecimal Calculations

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

Lecture 11: Number Systems

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

Appendix K Introduction to Microsoft Visual C++ 6.0

Binary Representation

Lecture 8: Binary Multiplication & Division

Binary Numbers. Binary Octal Hexadecimal

PCSpim Tutorial. Nathan Goulding-Hotta v0.1

Assembly Language Programming

Sequential Program Execution

Lecture 2 Notes: Flow of Control

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

MIPS Assembler and Simulator

Lecture 2. Binary and Hexadecimal Numbers

Reduced Instruction Set Computer (RISC)

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Member Functions of the istream Class

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

COMP 303 MIPS Processor Design Project 4: MIPS Processor Due Date: 11 December :59

Verilog - Representation of Number Literals

Basics of I/O Streams and File I/O

5 MIPS Assembly Language

C++ Language Tutorial

Chapter 5 Functions. Introducing Functions

Introduction to MIPS Assembly Programming

Syscall 5. Erik Jonsson School of Engineering and Computer Science. The University of Texas at Dallas

Object Oriented Software Design

Goals. Unary Numbers. Decimal Numbers. 3,148 is s 100 s 10 s 1 s. Number Bases 1/12/2009. COMP370 Intro to Computer Architecture 1

Why using ATmega16? University of Wollongong Australia. 7.1 Overview of ATmega16. Overview of ATmega16

IBCM: The Itty Bitty Computing Machine

Computer Science 281 Binary and Hexadecimal Review

CSI 333 Lecture 1 Number Systems

Lab 2 - CMPS 1043, Computer Science I Introduction to File Input/Output (I/O) Projects and Solutions (C++)

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

PIC 10A. Lecture 7: Graphics II and intro to the if statement

Lecture N -1- PHYS Microcontrollers

Informatica e Sistemi in Tempo Reale

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

DNA Data and Program Representation. Alexandre David

MIPS Assembly Language Programming CS50 Discussion and Project Book. Daniel J. Ellard

Lab Work 2. MIPS assembly and introduction to PCSpim

CISC 181 Project 3 Designing Classes for Bank Accounts

IP Subnetting and Related Topics A Tutorial by Chris Uriarte <chrisjur@cju.com> Updated April 2001

This 3-digit ASCII string could also be calculated as n = (Data[2]-0x30) +10*((Data[1]-0x30)+10*(Data[0]-0x30));

Visual Studio 2008 Express Editions

Positional Numbering System

Moving from C++ to VBA

Figure 1: Graphical example of a mergesort 1.

C++ Input/Output: Streams

Volume Serial Numbers and Format Date/Time Verification

The string of digits in the binary number system represents the quantity

Base Conversion written by Cathy Saxton

Caml Virtual Machine File & data formats Document version: 1.4

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Technical Support Bulletin Nr.18 Modbus Tips

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

CSI33 Data Structures

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Chapter 7D The Java Virtual Machine

Binary Adders: Half Adders and Full Adders

10CS35: Data Structures Using C

1. The First Visual C++ Program

Levent EREN A-306 Office Phone: INTRODUCTION TO DIGITAL LOGIC

Memory Management Simulation Interactive Lab

Creating a Simple Visual C++ Program

Winter 2002 MID-SESSION TEST Friday, March 1 6:30 to 8:00pm

Factoring Methods. Example 1: 2x * x + 2 * 1 2(x + 1)

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

CS101 Lecture 11: Number Systems and Binary Numbers. Aaron Stevens 14 February 2011

Table 1 below is a complete list of MPTH commands with descriptions. Table 1 : MPTH Commands. Command Name Code Setting Value Description

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Let s put together a Manual Processor

Binary Representation. Number Systems. Base 10, Base 2, Base 16. Positional Notation. Conversion of Any Base to Decimal.

COSC 181 Foundations of Computer Programming. Class 6

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

Decimal to Binary Conversion

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

CS61: Systems Programing and Machine Organization

Chapter 8 Selection 8-1

Copyright 2012 Pearson Education, Inc. Chapter 1 INTRODUCTION TO COMPUTING AND ENGINEERING PROBLEM SOLVING

COMPUTER SCIENCE 1999 (Delhi Board)

COMPSCI 210. Binary Fractions. Agenda & Reading

Tamper protection with Bankgirot HMAC Technical Specification

1. Convert the following base 10 numbers into 8-bit 2 s complement notation 0, -1, -12

RFID MODULE Mifare Reader / Writer SL025B User Manual Version 1.4 Nov 2012 StrongLink

7.7 Case Study: Calculating Depreciation

EP241 Computer Programming

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Transcription:

2.2: Bitwise Logical Operations Topics: Introduction: logical operations in C/C++ logical operations in MIPS In 256 lecture, we looked at bitwise operations in C/C++ and MIPS. We ll look at some simple programs to illustrate these operations in this exercise. Steps: Earlier, we copied logical.cc from the instructor s directory: /* logical.cc: demonstrates bitwise logical operations in C/C++ */ #include <iostream> using namespace std; int main() { int x = 0x456789ab, y = 0x9abcdef0, z; int w1 = 0xaaaaaaaa; unsigned int w2 = 0xaaaaaaaa; cout << "x = " << hex << x << " y = " << hex << y << endl; z = ~x; cout << "~x = " << hex << z << endl; z = x & y; cout << "x & y = " << hex << z << endl; z = x y; cout << "x y = " << hex << z << endl; z = x ^ y; cout << "x ^ y = " << hex << z << endl; cout << "\nw1 = " << hex << w1 << " w2 = " << w2 << endl; z = w1 << 3; cout << "w1 << 3 = " << hex << z << endl; z = w1 >> 3; CSc 256 Lab Manual 1

cout << "w1 >> 3 = " << hex << z << endl; z = w2 >> 3; cout << "w2 >> 3 = " << hex << z << endl; } All it does is perform some logical operations and print the results. Let s compile and run it: unixlab% g++ logical.cc -o logical unixlab%./logical x = 456789ab y = 9abcdef0 ~x = ba987654 x & y = 2488a0 x y = dfffdffb x ^ y = dfdb575b w1 = aaaaaaaa w2 = aaaaaaaa w1 << 3 = 55555550 w1 >> 3 = f5555555 w2 >> 3 = 15555555 unixlab% Let s go through the results and make sure we understand how the operations work. x is initialized to 0x456789ab, which in binary is 0100 0101 0110 0111 1000 1001 1010 1011 y is initialized to 0x9abcdef0, which in binary is 1001 1010 1011 1100 1101 1110 1111 0000 ~x means the bitwise not of x. We complement each bit of x to get ~x = 1011 1010 1001 1000 0111 0110 0101 0100 In hexadecimal, this is 0xba987654, which agrees with what the program printed. x & y means the bitwise and of x and y: x & y = 0000 0000 0010 0100 1000 1000 1010 0000 In hexadecimal, this is 0x002488a0, which agrees with what the program printed. (Leading zeros are usually not printed.) CSc 256 Lab Manual 2

x y means the bitwise or of x and y: x y = 1101 1111 1111 1111 1101 1111 1111 1011 In hexadecimal, this is 0xdfffdffb, which agrees with what the program printed. x ^ y means the bitwise xor of x and y: x ^ y = 1101 1111 1101 1011 0101 0111 0101 1011 In hexadecimal, this is 0xdfdb575b, which agrees with what the program printed. Finally, we come to the shift operations. w1 and w2 are both initialized to 0xaaaaaaaa, except w1 is a signed int and w2 is an unsigned int. 0xaaaaaaaa in binary is 1010 1010 1010 1010 1010 1010 1010 1010 w1 << 3 means w1 shift left 3 bits: w1 << 3 = 0101 0101 0101 0101 0101 0101 0101 0000 In hexadecimal, this is 0x55555550, which agrees with what the program printed. w1 >> 3 means w1 shift right 3 bits; since w1 is a signed int, the sign bit is extended: w1 >> 3 = 1111 0101 0101 0101 0101 0101 0101 0101 In hexadecimal, this is 0xf5555555, which agrees with what the program printed. w2 >> 3 means w2 shift right 3 bits; since w2 is an unsigned int, bits of zero are padded into the most significant bits: w2 >> 3 = 0001 0101 0101 0101 0101 0101 0101 0101 In hexadecimal, this is 0x15555555, which agrees with what the program printed. Logical.s is the MIPS version of logical.c. Since spim does not allow us to print integers in hexadecimal, we'll use the print command in spim to show integers in hex. # logical.s: demonstrates bitwise logical operations in MAL.data CSc 256 Lab Manual 3

.text.globl bk main: li $s0, 0x456789ab li $s1, 0x9abcdef0 li $s2, 0xaaaaaaaa bk: not $t0,$s0 and $t0,$s0,$s1 or $t0,$s0,$s1 xor $t0,$s0,$s1 sll $t0,$s2,3 srl $t0,$s2,3 sra $t0,$s2,3 li $v0,10 syscall We'll invoke spim, set a breakpoint at bk, and run the program: lo "logical.s" bre bk run Breakpoint encountered at 0x00400038 pr $s0 Reg 16 = 0x456789ab (1164413355) Reg 17 = 0x9abcdef0 (-1698898192) Reg 18 = 0xaaaaaaaa (-1431655766) We step through the next instruction not $t0, $s0. The bitwise not of 0x456789ab is 0xba987654: step [0x00400038] 0x02004027 nor $8, $16, $0 $t0,$s0 pr $t0 Reg 8 = 0xba987654 (-1164413356) ; 13: not We step through the next instruction and $t0, $s0, $s1. The bitwise and of 0x456789ab and 0x9abcdef0 is 0x002488a0: step [0x0040003c] 0x02114024 and $8, $16, $17 ; 14: and pr $t0 $t0,$s0,$s1 CSc 256 Lab Manual 4

Reg 8 = 0x002488a0 (2394272) We step through the next instruction or $t0, $s0, $s1. The bitwise or of 0x456789ab and 0x9abcdef0 is 0xdfffdffb: step [0x00400040] 0x02114025 or $8, $16, $17 $t0,$s0,$s1 pr $t0 Reg 8 = 0xdfffdffb (-536879109) ; 15: or We step through the next instruction xor $t0, $s0, $s1. The bitwise xor of 0x456789ab and 0x9abcdef0 is 0xdfdb575b: step [0x00400044] 0x02114026 xor $8, $16, $17 $t0,$s0,$s1 pr $t0 Reg 8 = 0xdfdb575b (-539273381) ; 16: xor We step through the next instruction sll $t0, $s2 3. 0xaaaaaaaa shift left logical by 3 bits is 0x55555550: step [0x00400048] 0x001240c0 sll $8, $18, 3 ; 18: sll $t0,$s2,3 pr $t0 Reg 8 = 0x55555550 (1431655760) We step through the next instruction srl $t0, $s2 3. 0xaaaaaaaa shift right logical by 3 bits is 0x15555555: step [0x0040004c] 0x001240c2 srl $8, $18, 3 ; 19: srl $t0,$s2,3 pr $t0 Reg 8 = 0x15555555 (357913941) We step through the next instruction sra $t0, $s2 3. 0xaaaaaaaa shift right arithmetic by 3 bits is 0xf5555555 (same as srl, but extend sign bit): step [0x00400050] 0x001240c3 sra $8, $18, 3 ; 20: sra $t0,$s2,3 CSc 256 Lab Manual 5

pr $t0 Reg 8 = 0xf5555555 (-178956971) Summary: In this exercise, we traced through some bitwise logical operations in C/C++ and MAL. Bitwise operations are very useful for extracting bitfields (or groups of data bits) from within a word or larger piece of storage. We ll see some of these uses later in the course. CSc 256 Lab Manual 6