CS 230 Notes

Module 1 - Arithmetic, Hardware and Data

Overview

  1. Number representation
  2. Boolean algebra and gate logic
  3. Integer arithmetic
  4. Floating point
  5. Non-numerical data types

Number Representation

Radix Representation

In a positional numeral system, the radix or base is the number of unique digits, including the digit zero, used to represent numbers. For example, for the decimal system (the most common system in use today) the radix is ten, because it uses the ten digits from 0 through 9.

In any standard positional numeral system, a number is conventionally written as $(x)_y$ with $x$ as the string of digits and $y$ as its base, although for base ten the subscript is usually assumed (and omitted, together with the pair of parentheses), as it is the most common way to express value. For example, $(100)_{10}$ is equivalent to $100$ (the decimal system is implied in the latter) and represents the number one hundred, while $(100)_2$ (in the binary system with base 2) represents the number four.

This representation is unique. Let b be a positive integer greater than 1. Then every positive integer a can be expressed uniquely in the form $${\displaystyle a=r_{m}b^{m}+r_{m-1}b^{m-1}+\dotsb +r_{1}b+r_{0},}$$ where $m$ is a nonnegative integer and the $r$'s are integers such that $0 < r_m < b$ and $0 \leq r_i < b$ for $i=0, 1, ... , m - 1$.

Examples

Number in Different Bases to Decimal

Number in different bases Convert to decimal
135dec $5 \cdot 10^0 + 3 \cdot 10^1 + 1 \cdot 10^2 = 135$
1440sep $0 \cdot 7^0 + 4 \cdot 7^1 + 4 \cdot 7^2 + 1 \cdot 7^3 = 567$
A32hex $2 \cdot {16}^0 + 3 \cdot {16}^1 + 10 \cdot {16}^2 = 2610$
Running it in Python
def convert_to_decimal(num, base): num = str(num) power = len(num) - 1 decimal = 0 for digit in num: decimal += int(digit, base) * (base ** power) power -= 1 return decimal # print() will not print to the DOM element below # but to browser console. # Please make sure this is the last evaluated value so that you # can see the output below. (convert_to_decimal(135, 10), convert_to_decimal(1440, 7), convert_to_decimal('A32', 16))
Output

Decimal to Number in Different Bases

Decimal Convert to number in different bases
3219dec $$\begin{align*} 3219 / 16 & = 201 \operatorname{R} 3 \\ 201 / 16 & = 12 \operatorname{R} 9 \\ 12 / 16 & = 0 \operatorname{R} 12 \\ & = C93_{16} \end{align*}$$Machine Code

Why does it work?

Recall that every positive integer can be expressed uniquely in the form $${\displaystyle a=r_{m}b^{m}+r_{m-1}b^{m-1}+\dotsb +r_{1}b+r_{0},}$$ where $m$ is a nonnegative integer and the $r$'s are integers such that $0 < r_m < b$ and $0 \leq r_i < b$ for $i=0, 1, ... , m - 1$.

The number in base $b$ are written as $r_m r_{m - 1} \dots r_1 r_0$. So, if you are given a decimal number, you are basically given the $a$ value and you want to find the individual $r_i$ for $i = 0, 1, ... , m - 1$. To do this, you can just divide by $b$ and keep track of the remainder. Let's see what happens: $$a / b = (r_m b^{m - 1} + r_{m - 1} b^{m - 2} + \dots + r_2 b + r_1) \operatorname{R} r_0$$ Now, the remainder is the $r_0$ value. The quotient is the $a$ value for the next iteration. So, you can just keep dividing by $b$ and keep track of the remainder. We denote the quotient to be $q_1 = (r_m b^{m - 1} + r_{m - 1} b^{m - 2} + \dots + r_2 b + r_1)$. $$q_1 / b = (r_m b^{m - 2} + r_{m - 1} b^{m - 3} + \dots + r_2) \operatorname{R} r_1$$

Continue the process until you get a quotient of 0. The remainders are the $r_i$ values in reverse order.

This is the same as the example above.

Binary Numbers

Binary number is just a special case of the radix representation where the base is 2.

Example
11101100bin $2^2 + 2^3 + 2^5 + 2^6 + 2^7 = 236$

Hexadecimal Numbers / Binary Numbers Conversion

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

Benefits of Binary Representation

Boolean Algebra

Please take a look at this wikipedia article on boolean algebra.

Circuit Elements

Circuit elements

Converting a Truth Table to Circuit

Adders

Please take a look at these wikipedia articles: Adder (electronics), Digital Circuits/Adders, and Carry-lookahead adder

Integer Arithmetic

In computer programming, an integer overflow occurs when an arithmetic operation attempts to create a numeric value that is outside of the range that can be represented with a given number of digits – either higher than the maximum or lower than the minimum representable value.

Sign Representation

One's Complement

Two's Complement

Addition, Subtraction, Multiplication, Division

Please take a look at the Wikipedia article Binary number#Binary arithmetic.

Binary Fractions

Please take a look at the Wikipedia article Fractals/Mathematics/binary.

Floating Point

Scientific Notation

Scientific notation is a way of expressing numbers that are too large or too small to be conveniently written in decimal form, since to do so would require writing out an unusually long string of digits.

In scientific notation, nonzero numbers are written in the form $$m \times 10^n$$

or m times ten raised to the power of n, where n is an integer, and the coefficient m is a nonzero real number (usually between 1 and 10 in absolute value, and nearly always written as a terminating decimal).

Normalized number

A number is normalized when it is written in scientific notation with one non-zero decimal digit before the decimal point. Thus, a real number, when written out in normalized scientific notation, is as follows:

$${\displaystyle \pm d_{0}.d_{1}d_{2}d_{3}\dots \times 10^{n}}$$ where $n$ is an integer, ${\textstyle d_{0},d_{1},d_{2},d_{3},\ldots ,}$ are the digits of the number in base 10, and ${\displaystyle d_{0}}$ is not zero.

Radix Point

The character used to separate the integer and fractional part of a number in a digital representation of a number, regardless of the base used.

Floating Point vs. Fixed-point Notation

Floating Point

In computing, floating-point arithmetic (FP) is arithmetic that represents real numbers approximately, using an integer with a fixed precision, called the significand, scaled by an integer exponent of a fixed base. For example, 12.345 can be represented as a base-ten floating-point number: $${\displaystyle 12.345=\underbrace {12345} _{\text{significand}}\times \underbrace {10} _{\text{base}}\!\!\!\!\!\!^{\overbrace {-3} ^{\text{exponent}}}}$$

Fixed-point Notation

In computing, fixed-point is a method of representing fractional (non-integer) numbers by storing a fixed number of digits of their fractional part. Dollar amounts, for example, are often stored with exactly two fractional digits, representing the cents (1/100 of dollar).

Terminology

Notes

Representation

We still need to represent fraction, exponent, signs.

In order to interpret a floating point number, you must know the number of bits used for the sign (usually 1), the exponent part and the fraction part, as well as the bias.

Format for a 32-bit Number

Please take a look at the Wikipedia page: Single-precision floating-point format.
Binary representation of a 32-bit floating-point number. The value depicted, 0.15625, occupies 4 bytes of memory: 00111110 00100000 00000000 00000000

Interpretation

Rationale of the Format

All numbers with sign "+", arranged by size from +0 up to +Inf correspond to all the bit sequences (0 0...0 0...0) up to (0 1...1 0...0), arranged as binary integers. Therefore it is easy to compare two machine numbers, or to find the next smaller or larger machine number.

Biased Exponent

Comparing Two Floating-point Number

Comparison with constant 0

Comparison between numbers

Arithmetic

Addition

Multiplication

Simplified 8-Bit Model

This section is mostly for examples. If you are interested, please check the slides.

Special Cases

Overflow

Overflow is still possible.

Invalid Result - NaN

Both the overflow and NaN can propagate during computation.

This allows us to have no exception (like integer division by zero) and no silent error (like integer overflow).

Underflow - assume no subnormal

Subnormal Representation

Representable Numbers

The representable numbers of a representation format are the numbers that can be exactly represented in the format.

We cannot represent all fractional numbers in the floating-point format. This is a tradeoff of using integers to represent fractions.

Floating Point Caveats

Consequences

Distributive law is not guaranteed! You should pay attention to order of calculations!

Fixed Point Representation

Interpret first N bits as integer, rest as fraction

Non-numerical data types

Bytes

8 bits = 1 byte.

Word

In computing, a word is the natural unit of data used by a particular processor design. A word is a fixed-sized datum handled as a unit by the instruction set or the hardware of the processor. The number of bits or digits in a word (the word size, word width, or word length) is an important characteristic of any specific processor design or computer architecture.

Endianness

Diagram of how a 32-bit integer is arranged in memory when stored from a register on a big-endian computer system. Contrast with Little-Endian.svg Diagram of how a 32-bit integer is arranged in memory when stored from a register on a little-endian computer system. Contrast with Big-Endian.svg

Endianness can become an issue when moving data external to the computer – as when transmitting data between different computers, or a programmer investigating internal computer bytes of data from a memory dump – and the endianness used differs from expectation. In these cases, the endianness of the data must be understood and accounted for.

In CS 230, bits are written in big-endian.

Characters

Please take a look at the Wikipedia page ASCII.

Unicode

Please take a look at the Wikipedia page Unicode.

Big Integers

Data Interpretation

Module 2 – Assembly Language

Overview

Assembly language: MIPS

Machine Code

Binary code – comprised of 0s and 1s

Assembly Language

Instruction Set

Instruction set is the vocabulary of commands understood by a given architecture.

Different processors have different sets.

Turing Completeness

MIPS Architecture

MIPS Assembly Language

Special Registers

MIPS Assembly Language

Label Naming

Memory Model

Memory Model

Memory Access

lw $t, i($s) sw $t, i($s)

Low Level Errors

Stack

Stack Save/Restore

Stack Frame

Register Conventions

Who Saves What?

Local Variables

Preserving the Stack

MIPS Reference Sheet (CS 230/CS 241 dialect)

mipsref.pdf

Module 3 – Machine Internals

Overview

Multiplexor/Multiplexer

In electronics, a multiplexer (or mux; spelled sometimes as multiplexor), also known as a data selector, is a device that selects between several analog or digital input signals and forwards the selected input to a single output line.

Clock

Cycle Execution

Typical Instruction Cycle

CPU Clocking

IF – Instruction Fetch

ID – Instruction Decode

EX – Execute

MEM – Memory Access

WB – Write Back

Performance

Execution Time

CPU Clocking

Clock period

Clock frequency

Instruction Count and CPI

Cycles per instruction

CPI is defined as the average number of clock cycles per instruction for a program or program fragment.

CPI is determined by CPU hardware. Different instructions might require a different number of cycles. A single process will have an average CPI

Instruction count

Instruction count is determined by program, instruction set, and compiler.

CPU Time

Performance equation

Performance Summary

Performance depends on

Pipelining

Pipeline Speedup

Instruction Set for Pipelining

Pipeline Hazards

Structural Hazard

Data Hazard

add $s0, $t0, $t1
sub $t2, $s0, $t3

reorder instructions, if possible

Forwarding / Bypassing

Load-Use Data Hazard

Forwarding “Connections” Diagram

Forwarding diagram

Control Hazard

Branch Prediction

More Realistic Branch Prediction

Pipeline Summary

RISC vs. CISC

Reduced Instruction Set Computer (RISC) and Complex Instruction Set Computer (CISC)

RISC

CISC

Memory Characteristics

Memory Hierarchy

Memory hierarchy Some examples are:

Registers

Main Memory

Disk

Random Access Memory (RAM)

Static RAM (SRAM)

Dynamic RAM (DRAM)

Memory Technology

  • Typical performance and cost figures
    Technology Access Time \$/GB
    SRAM 0.5-2.5ns \$2000-\$5000
    DRAM 50-70ns \$20-\$75
    Disk 5,000,000-7,000,000ns \$0.20-\$2
  • Ideally: have large amounts of fast memory
  • Memory Stalls

    Locality

    Locality Principle

    Temporal locality

    Spatial locality

    Example: books in library and on desk.

    Caching

    A cache is a small amount of fast memory used to keep recently used (and nearby) data quickly accessible. It exploits the locality principle.

    Typical Cache Types

    Terminology

    Memory Caching

    Questions to answer:

    Cache Miss

    Direct-Mapped Cache

    Direct Mapped Cache

    Cache Entries

    Example

    Please check the course notes for the detailed examples.

    Block Size Considerations

    Writing

    Write through

    Write back – many updates in same block

    Write buffer

    Associative Caches

    Fully associative

    n-way set associative

    Replacement Policy

    How Much Associativity

    Cache Performance

  • CPU time
  • program execution cycles, including cache hit time
  • memory stall cycles from cache misses
  • With simplifying assumptions:
  • $$\begin{align*} & \quad \; \text{Memory stall cycles}\\ &= \frac{\text{Memory accesses}}{\text{Program}} \times \text{Miss rate} \times \text{Miss penalty} \\ &= \frac{\text{Instructions}}{\text{Program}} \times \frac{\text{Misses}}{\text{Instructions}} \times {\text{Miss penalty}} \\ \end{align*}$$

    Cache Performance Example

    Average Access Time

    Cache Performance Summary

    Multilevel Caches

    Multilevel Cache Example

    Eff. CPI = 1 + 0.5 * 0.02 * 20 + 0.5 * 0.005 * 400 = 1 + 0.2 + 1 = 2.2

    Summary

    Module 4 – Build and Execute

    Overview

    Classical Tool Chain

    Classical Tool Chain

    Other Execution Approaches

    Interpretation

    Byte Code

    Just-In-Time Compilation

    Compiler

    Basic Compilation Steps

    Scanner / Tokenizer

    Background

    Deterministic Finite Automata (DFA)

    Non-deterministic Finite Automata (NFA)

    Languages

    Regular Expression

    Basic Regex Operations

    Basic Regex Examples

    Regular Expressions and DFA/NFA

    UNIX Tools

    Syntax / Extensions

    Scanner

    Language Specification

    Objectives

    Specification Components

    Derivation

    Context-Free Grammar

    Leftmost Derivation

    Parse Trees

    Associativity and Precedence

    Ambiguity

    More than one derivation tree for same input => ambiguous

    Why Do We Care?

    Parse Tree - Evaluation

    Assembler

    Linking

    Object File Format – Basics

    Relocation

    Symbol Resolution

    Library

    Loading

    Dynamic Linking

    Dynamic Shared Library

    Summary

    The Classical Tool Chain takes high level code and converts it to machine code that can be executed by the CPU.