Foundations of Sequential Programming

Professors: Nomair Naeem, Gregor Richards
Winter 2021 - notes by Yousof Hosny

Module 1 - Data Representations

Data in computers is stored in bits. We need to know what data representation is used to know what the bits mean. 1000011 could be decimal, binary (unsigned vs. two's complement), etc..

Binary Representation

Definition 1: A bit is a binary digit, that is a 0 or 1 (on or off)

Definition 2: A nibble is 4 bits.

Definition 3: A byte is 8 bits.

Definition 4: A word is a machine-specific grouping of bytes. For us, a word will be 4 bytes (32-bit architecture) though 8 byte (or 64-bit architectures) words are more common now.

Definition 5: The Most Significant Bit (MSB) is the left-most bit (highest value/sign bit).

Definition 6: The Least Significant Bit (LSB) is the right-most bit (lowes value)

We will discuss two types of binary representations:

Unsigned Integers In Binary

This works the same as non-negative decimal numbers but each position is a power of 2 instead of 10.

Arithmetic works the same way with binary numbers (you can just add and carry the 1s)

Converting From Decimal to Binary

Approach 1:

Approach 2: Constantly dvide the number by 2

Signed Integers in Binary

A computer must store both positive and nevative integers. Sign-Magnitude is one way to do it where the first bit is 0 to indicate positive and 1 to indicate negative. This approach has a multiple problems and is not used in practice.

The more common approach is to represent signed integers using Two's complement form:

Converting to and from Two's Complement

Decimal to two's complement:
First write the number in binary next take the complement of all bits and add 1.

Two's complement to decimal:
Flip all the bits and add 1 then convert (remember the sign).
Another way is to treat the number as an unsigned number and convert to decimal then subtract 282^8

The idea behind Two's Complement notaiton is based on the following:

Arithmetic of Signed Integers

Arithmetic works by ignoring overflow because arithmetic works in Z256\mathbb{Z}_{256}

Hexadecimal numbers

Base 16 system using 0 to 9 and A,B,C,D,E,F

Converting to hexadecimal is the same as binary, either dvide and read remainders bottom up, or find largest powers and subtract.

Binary can be converted to hexadecimal as well by converting each nibble to its its hexadecimal value so,
1001 1101 = 9D = 0x9D

Example:
What is -11 + 6 corresponging to words in Two's complement notation.
Answer: recall a word is 8 bytes, and -11 + 6 = -5 so 5 in binary is 0000 0000 0000 0000 0000 0000 0000 0101 and thus -5 is 0000 0000 0000 0000 0000 0000 0000 1011 in two's complement and so it is 0xFFFFFFFB in hexadecimal

ASCII Representation

A common convention used to represent the English alphabet and printable characters (it uses 7 bits). 95 of the codes are printable and the rest are obsolute control codes that came from Teletype machines.

Highlights:

ASCII is outdated and most things use Unicode now which contains multiple encodings such as UTF-8, UTF-16, UTF-32.

Bit-Wise operations

Bitwise AND (&): Computes the value given by applying logical AND to each corresponding bits in the operands

Bitwise OR (|): Computes the value given by applying logical OR to each pair of bits in the operands

Left Bit Shift (<<): Used as x << n and shifts the bits to the left n times and padds with additional n zeroes on the right (overflow is ignored). It is equivalent to x×2nx \times 2^n

Right Bit Shift (>>>): Our intended use of the Right Bit Shift operator is to perform arithmetic right shift. We expect x >> n to be equivalent to x2n\frac{x}{2^n}

Module 2 - Machine Language

Different processors understand different machine languages. A machine language is made up of multiple patterns (machine instruction)

In this course we use MIPS (Microprocessor Without Interlocked Pipeline Stages).

MIPS Hardware

Control Unit: The Control Unit is the brain of the processor.

Arithmetic Logic Unit: The ALU performs mathematical operations such as addition, multiplication, logical comparison and decisions requested by the control unit.

Geral Purpose Registers: Our MIPS processor has 32 general purpose registers (labelled $0 through $31).

Program Counter (PC) and Instruction Register (IR): PC and IR are two special 32-bit registers used by the Control Unit in fetching and decoding instructions.

Memory Data Register (MDR) and Memory Address Register (MAR): Data travels from registers and memory (RAM) through the data bus.

hi and lo Registers: These are two special registers used by the multiplication and division machine instructions.

Everything above is physically part of our simplified MIPS processor.
Off-chip memory (RAM):

How Mips Executes Programs

Programs operate on data by can also themselves be viewed as data. This is the classic von Neumann architecture.

To run a program the Operating System uses a special program called the Loader to load the program (on the hard drive) into memory.

Once the loader is done a hardware-based algorithm known as the Fetch-Execute Cycle begins:

PC = 0x00 // whatever the starting adress is for the program while true do IR = MEM[PC] PC += 4 Decode and execute instructions in IR end while

MIPS Machine Language

We will use the following simplified set of 18 instructions in our MIPS

Add and Subtract Instructions

The behavior is $d = $s + $t

The 32-bit instruction patter for add is:

add $d, $s, $t: 000000 sssss ttttt ddddd 00000 100000

An alternate format we see is seperating each nibble:

add $d, $s, $t: 0000 00ss ssst tttt dddd d000 0010 0000

Note: the difference in order in our syntax and the machine instruction

Subtract is similar to add

sub $d, $s, $t: 000000 sssss ttttt ddddd 00000 100010

Jump Instruction

The jump instruction sets PC to the value in $S.

Unlike most high-level programming languages, MIPS programs are expected to have an explicity jump to the return addrress stored by the loader to exit the program (stored in $31)

jr $s: 000000 sssss 00000 00000 00000 001000
jr $s: 0000 0011 1110 0000 0000 0000 0000 1000. In hexadecimal: 0x03e00008

Load Immediate and Skip Instruction

Shorthand: lis $8 .word 11 lis $9 .word 0xD add $3, $8, $9 jr $31

MIPS Assembly Language

MIPS assembly language abstracts MIPS machin language so that we do not have to program in binary.

Definition 1 - Assembly language: a textual representation of a machine language

Our shorthand for MIPS machine code is what the syntax for MIPS assembly code is.

Multiply and divide

For our course, unless specified, we assume that multiplication (mult) fits in the lo register (it is a 32-bit value).

Division stores the quotient in the lo register and the remainder in the hi register

move from hi and lo

since these are special registers we have special instructions to move values from them

mfhi $d // Move from register hi into register $d mflo $d // Move from register lo into register $d
lis $8 .word 2 ; $8 contains 2 lis $9 .word 3 ; $9 contains 3 lis $2 .word 11 ; $2 contains 11 div $1, $8 ; $mfhi <-- $1 % $8 mfhi $3 ; $3 <-- mfhi beq $3, $0, 1 ; if $3 == $0 skip 1 instruction add $2, $9, $0 ; store 3 in $2 jr $31 ; exit the program

You can branch uncoditionally if you compare the zero register to itself

Set less than

slt $d $s $t
// Version 1: slt $2, $1, $0 ; $2 is 1 if $1 < 0 beq $2, $0, 1 ; skip 1 instruction if $2 is 0 ; jump if $1 is NOT less than 0 sub $1, $0, $1 ; $1 = $0 - $1 (negate) jr $31 ; exit // Version 2: slt $2, $1, $0 bne $2, $0, 1 ; skip 1 if $2 is 1 ; i.e. $1 was negative jr $31 ; exit as $1 is positive sub $1, $0, $1 ; $1 = $0 - $1 (negate) jr $31 ; exit

sltu does the same thing but uses unsigned binary

Conditional loops

lis $2 .word 13 ; $2 = 13 lis $1 .word -1 ; $1 = -1 add $3, $0, $0 ; initialize $3 to 0 add $3, $3, $2 ; $3 += $2 add $2, $2, $1 ; --$2 bne $2, $0, -3 ; if $2 != 0, go up 3 instructions ; (remember PC is already at the next instruction) jr $31 ; exit

In assembly language we have the convenience of labels

0x00 lis $2 0x04 .word 13 0x08 lis $1 0x0c .word -1 0x10 add $3, $0, $0 loop: 0x14 add $3, $3, $2 0x18 add $2, $2, $1 0x1c bne $2, $0, loop 0x20 jr $31