CMPT 295

Unit: Textbook Chapter 2 - Data Representation

Lecture 2 - Representing data in memory

Data = information = data, instructions (code, programs)

CAL Volunteer Note-Taker Position [1]

Last Lecture [2]

Feedback on Lecture 1 Activity [3]

Unit Objectives [4]

Chapter 2 of our textbook

Today’s Menu [5]

“Under the hood” Von Neumann architecture [6]

Architecture of most computers

Its features:

“Computer executes it” has a diagram attached.

Two I/O devices are shown (represented by a cylindar), one on the left, one one the right. On the left is “input peripheral”, on the right is “output peripheral”.

A red box surrounds the two central nodes (represented by squares), labeled “motherboard”. It contains two items inside it. On the left is the “Central Processing Unit (CPU)”, which executes instructions. On the right is “Memory”, which is “volatile” and stores data and instructions.

An arrow points from the far left input peripheral to the CPU. A bi-directional arrow points between the CPU and memory. A final arrow points from memory to the output peripheral.

How to diagram memory [7]

Address M[]
size−1  
 
0x0008  
0x0007  
0x0006  
0x0005  
0x0004  
0x0003  
0x0002  
0x0001  
0x0000 1 byte

Closer look at memory [8]

Typically, in a diagram, we represent memory (memory content) as a series of memory “cells” (or bits) in which one of two possible values (‘0’ and ‘1’) is stored

Address M[]
size−1  
 
0x0008  
0x0007  
0x0006  
0x0005  
0x0004  
0x0003  
0x0002  
0x0001  
0x0000 01000000

0x0000 is labled as “1 memory cell”

Compressed view of memory [9]

Address M[]
size−1  
 
0x0008  
0x0007 7
0x0006 6
0x0005 5
0x0004 4
0x0003 3
0x0002 2
0x0001 1
0x0000 0
Address M[]              
size−8                
               
0x0018                
0x0010 (annotation: why is this not `0x0016)                
0x0008                
0x0000 0 1 2 3 4 5 6 7

Each cell, no matter which diagram is used, represents 1 byte, or 8 bits. 0x0000 to 0x0008 is eight bytes, or 64 bits.

Why can only two possible values be stored in a memory “cell”?

A diagram shows two horizontal green bars. Bar one has a minimum of 0.0V, and a maximum of 0.2V. “0” Bar two has a minimum of 0.9V, and a maximum of 1.1V. “1” A wiggly line jiggles around somewhat randomly within the confines of the lower “0” bar–moving to the right with time, it moves up, temporarily traversing the whitespace between the two bars before settling back down in its jiggly pattern within the “1” bar. It then reverses this step, through the whitespace back to the “0” bar where it started.

A bit of history [11]

ENIAC: Electronic Numerical Integrator And Calculator

Source: https://en.wikipedia.org/wiki/ENIAC#/media/File:ENIAC_Penn1.jpg

Review [12] – Back to our bits – How to represent series of bits

Annotation: “Error prone!” encompasses “lengthy to write” and “difficult to read”.

Review – A solution: hexadecimal numbers

Decimal Binary Hexidecimal
0 0000 0
1 0001 1
2 0010 2
3 0011 (circled in annotation) 3
4 0100 4
5 0101 (circled in annotation) 5
6 0110 6
7 0111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 (circled in annotation) D
14 1110 E
15 1111 (circled in annotation) F

What could these 32 bits represent? – What kind of information could they encode?

011000100110100101110100011100112

Answer:

What kind of information (data) do series of bits represent?

Encoding Scheme

Bit pattern: 01100010 01101001 01110100 011100112

An arrow pointing to a box labeled “encoding schemes”:

An arrow pointing to the following list:

Definition: An encoding scheme is an interpretation (representation) of a series of bits

Bottom line: Which encoding scheme is used to interpret a series of bits depends on the application currently executing (the “context”) not the computer

Endian – Order of bytes in memory

Address M[]
size−1  
 
0x0003  
0x0002  
0x0001  
0x0000 011100112

Endian – Order of bytes in memory

Question: But how do we store several bytes in memory?

01000010 01101001 01110100 011100112

Way 1: Little endian Address|M[]|Hex (filled out as annotation) size-1|| …|| 0x0003|01000010|42 0x0002|01101001|69 0x0001|01110100|74 0x0000|01110011|73

Way 2: Big endian

Address M[] Hex
size−1    
   
0x0003 01110011 73
0x0002 01110100 74
0x0001 01101001 69
0x0000 01000010 42

Compressed view of memory:

Little-endian:

Address              
size-8              
             
0x0008              
0x0000 73 74 69 42      

Big-endian:

Address              
             
0x0008              
0x0000 42 69 74 73      

Review Bit – Bit Manipulation - Boolean algebra

No matter what a series of bits represent, they can be manipulated using bit-level operations:

AND -> A&B = 1 when both A=1 and B=1

& 0 1
0 0 0
1 0 1

NOT -> ~A = 1 when A=0

~  
0 1
1 0
OR -> A B = 1 when either A=1 or B=1
| 0 1
0 0 1
1 1 1

XOR (Exclusive-Or) -> A^B = 1 when either A=1 or B=1, but not both

^ 0 1
0 0 1
1 1 0

Interesting fact about Boolean algebra and digital logic

Diagram of an AND gate.

Two lines go into a black box. One is 1/high, one is 0/low. An output comes out the other side: 0/low.

Review (annotation: HW2)

Let’s try some Boolean algebra!

  01101001
& 01010101
= 01000001
  01101001
| 01010101
= 01111101
  01101001
^ 01010101
= 00111100

(notation on 2nd last bit of equals: error!)
~ 01010101
= 10101010

Useful bit manipulations

  10110011 <- some value x
& 00000001 <- binary mask
  00000001

The result tells us that the least significant bit (LSb) is set.

  1. XOR: Toggle specific bits Example: The result tells us that the least significant bit (LSb) of x is set
  10110011 <- some value x
^ 00011100 <- binary mask
= 10101111 We get a toggled version of the 3 original bits (of x) that

(red square around bits 4-6): “We get a toggled version of the three original bits (of x) that corresponds to the 3 set bits of the binary mask.”

  10110011 <- some value x
| 00011100 <- some value y
= 10111111

The result contains all the set bits of x and y.

Bit Manipulation - Shift operations

Bit Manipulation - Shift operations – Let’s try! (HW3)

Summary

NOTE: C logical operators and C bitwise (bit-level) operators behave differently! Watch out for && versus &,   versus , …

Next Lecture