Computer Fundamentals
Computer Fundamentals
6L for CST/NST 1A
Michaelmas 2010
MWF @ , Arts “ hool A
Aims & Objectives
• This course aims to:
– give you a general understanding of how a
computer works
– introduce you to assembly-level programming
– prepare you for future courses. . .
• At the e d of the ourse ou ll e a le to:
– describe the fetch-execute cycle of a computer
– understand the different types of information
which may be stored within a computer memory
– write a simple assembly language program
2
Recommended Reading
• This ourse does t follo a parti ular ook
exactly, but any of the following are useful:
– Computer Organization & Design (4th Ed),
Patterson and Hennessy, Morgan Kaufmann 2008
• also used i C“T Part B Co puter Desig
– Digital Design and Computer Architecture, Harris
and Harris, Morgan Kaufmann 2007
• also used i C“T Part A Digital Ele tro i s
– Structured Computer Organization (5th Ed),
Tannenbaum, Prentice-Hall 2005
• good general overview book; somewhat broader in
scope, and somewhat simpler to digest than above
3
Course Outline
• We ll o er the follo i g topi s:
– A Brief History of Computing
– Operation of a Simple Computer
– Input / Output
– MIPS Assembly Language
• This course is new this year, but derives from
Part I of pre- C“T A Operati g “ ste s
– This will help in finding e.g. past exam questions
• Feel free to ask questions during the lecture
– or after it, or via email – see course web page 4
A Chronology of Early Computing
• (several BC): abacus used for counting
• 1614: logarithms discovered (John Napier)
• 1622: invention of the slide rule (Robert Bissaker)
• 1642: First mechanical digital calculator (Pascal)
• Charles Babbage (U. Cambridge) invents:
– 1812: Differe e E gi e
– 1833: A al ti al E gi e
• 1890: First electro-mechanical punched card data-
processing machine (Hollerith)
• 1905: Vacuum tube/triode invented (De Forest)
5
The War Years…
• 1935: the relay-based IBM 601 reaches 1 MPS.
• 1939: ABC - first electronic digital computer (Atanasoff
& Berry)
• 1941: Z3 - first programmable computer (Zuse)
• Jan 1943: the Harvard Mark I (Aiken)
• Dec 1943: Colossus uilt at “tatio X – Bletchley Park
• 1945: ENIAC (Eckert & Mauchley, U. Penn):
– 30 tons, 1000 square feet, 140 kW,
– 18K vacuum tubes, 20×10-digit accumulators,
– 100KHz, circa 300 MPS.
– Used to calculate artillery firing tables.
– (1946) blinking lights for the media. . .
• But progra i g is ia plug-board: tedious and slow
6
The Von Neumann Architecture
7
Further Progress…
• 1947: poi t o ta t tra sistor i e ted
(Shockley, Bardeen & Brattain)
• 1949: ED“AC, the orld s first stored-program
computer (Wilkes & Wheeler)
– 3K vacuum tubes, 300 square feet, 12 kW,
– 500KHz, circa 650 IPS, 225 MPS.
– 1024 17-bit words of memory in mercury
ultrasonic delay lines – early DRAM ;-)
– ord operati g s ste !
• 1954: TRADIC, first electronic computer
without vacuum tubes (Bell Labs)
8
The Silicon Age
• 1954: first silicon (junction) transistor (TI)
• 1959: first integrated circuit (Kilby & Noyce, TI)
• 1964: IBM System/360, based on ICs.
• 1971: Intel 4004, first micro-processor (Ted
Hoff):
– 2300 transistors, 60 KIPS.
• 1978: Intel 8086/8088 (used in IBM PC).
• 1980: first VLSI chip (> 100,000 transistors)
• Today: ~800M transistors, 45nm, ~3 GHz.
9
Languages and Levels
13
A (Simple) Modern Computer
Processor (CPU): Memory: stores
executes programs programs & data
| Bus: connects
everything together
Devices: for input
and output
14
Registers and the Register File
R00 0x5A R08 0xEA02D1F
R01 0x102034 R09 0x1001D
R02 0x2030ADCB R10 0xFFFFFFFF
R03 0x0 R11 0x1020FC8
R04 0x0 R12 0xFF0000
R05 0x2405 R13 0x37B1CD
R06 0x102038 R14 0x1
R07 0x20 R15 0x20000000
19
Dynamic RAM (DRAM)
• 8 possible functions:
1. a AND b, a AND b
2. a OR b, a OR b
3. a + b, a + b with carry
4. a − b, a − b with borrow
• To make n-bit ALU bit, connect together (use carry-lookahead on adders)
30
Conditional Execution
• Seen C,N,V flags; now add Z (zero), logical NOR of all bits in output.
• Can predicate execution based on (some combination) of flags, e.g.
subs d, a, b // compute d = a - b
beq proc1 // if equal, goto proc1
br proc2 // otherwise goto proc2
• If read back a byte from address 0x4, get 0xDE if big-endian, or 0xEF
if little-endian.
– If you always load and store things of the same size, things are fine.
• Today have x86 little-endian; Sparc big-endian; Mips & ARM either.
• Annoying. . . and burns a considerable number of CPU cycles on a
daily basis. . .
33
Accessing Memory
• To load/store values need the address in memory.
• Most modern machines are byte addressed: consider memory a big
array of 2A bytes, where A is the number of address lines in the bus.
• Lots of thi gs o sidered e or ia address de oder, e.g.
• Note that CPU ge erall does t are what is being held within the
memory – up to programmer to interpret whether data is an
integer, a pixel or a few characters in a novel...
35
Representing Text
• Two main standards:
1. ASCII: 7-bit code holding (English) letters, numbers,
punctuation and a few other characters.
2. Unicode: 16-bit code supporting practically all international
alphabets and symbols.
• ASCII default on many operating systems, and on the early
Internet (e.g. e-mail).
• Unicode becoming more popular (especially UTF-8!)
• In both cases, represent in memory as either strings or
arrays: e.g. Pu Ti e! i AC“II:
N (here 2) bytes
hold length,
followed by
characters
Byte per character,
terminated with 0
36
Floating Point
• In many cases need very large or very small numbers
• Use idea of s ie tifi otatio , e.g. = m × 10e
– m is called the mantissa
– e is called the exponent.
e.g. C = 3.01 × 108 m/s.
leaf tag sa s
e re do e…
agi ode
tag => 4 words
poi ts to
next node
39
Instruction Encoding
• An instruction comprises:
a. an opcode: specifies what to do.
b. zero or more operands: where to get values
• Old machines (and x86) use variable length encoding for
low code density; most other modern machines use fixed
length encoding for simplicity, e.g. ARM ALU instructions:
31 28 27 26 25 24 21 20 19 16 15 12 11 0
1. CU fetches & decodes instruction and generates (a) control signals and (b) operand
information.
2. I EU, o trol sig als sele t fu tio al u it instruction class a d operatio .
3. If ALU, then read 1–2 registers, perform op, and (probably) write back result.
4. If BU, test condition and (maybe) add value to PC.
5. If MAU, ge erate address addressing mode a d use us to read/ rite alue.
6. Repeat ad infinitum
41
A (Simple) Modern Computer
42
Input/Output Devices
• Devices connected to processor via a bus (e.g. PCI)
• Includes a wide range:
– Mouse,
– Keyboard,
– Graphics Card,
– Sound card,
– Floppy drive,
– Hard-Disk,
– CD-Rom,
– Network card,
– Printer,
– Modem
– etc.
• Often two or more stages involved (e.g. USB, IDE, SCSI,
RS-232, Centronics, etc.) 43
UARTs
45
Graphics Cards
51
Interrupts (2)
• I terrupt li es ~ − are part of the us.
• Often only 1 or 2 pins on chip ⇒ need to encode.
• e.g. ISA & x86:
55
MIPS Instructions
• MIPS has 3 instruction formats:
– R-type - register operands
– I-type - immediate operands
– J-type - jump operands
• All instructions are 1 word long (32 bits)
• Examples of R-type instructions:
add $8, $1, $2 # $8 <= $1 + $2
sub $12, $6, $3 # $12 <= $6 - $3
and $1, $2, $3 # $1 <= $2 & $3
or $1, $2, $3 # $1 <= $2 | $3
• Register 0 ($0) always contains zero
add $8, $0, $0 # $8 <= 0
add $8, $1, $0 # $8 <= $1
56
R-Type Instructions
• These take three register operands ($0 .. $31)
• R-type instructions have six fixed-width fields:
31 26 25 21 20 16 15 11 10 6 5 0
57
I-Type Instructions
31 26 25 21 20 16 15 0
• I = Immediate
– Value is encoded in instruction & available directly
– MIPS allows 16-bit values (only 12-bits on ARM)
• Useful for loading constants, e.g:
– li $7, 12 # load constant 12 into reg7
• This is a big win in practice since >50% of
arithmetic instructions involve constants!
• MIPS supports several immediate mode
instructions: opcode deter i es hi h o e…
58
Immediate Addressing on MIPS
• or, and, xor and add instructions have immediate
for s hi h take a i” suffix, e.g:
ori $8, $0, 0x123 # puts 0x00000123 into r8
ori $9, $0, -6 # puts 0x0000fffa into r9
addi $10, $0, 0x123 # puts 0x00000123 into r10
addi $11, $0, -6 # puts 0xfffffffa into r11
# (note sign extension...)
• lui instruction loads upper 16 bits with a constant
and sets the least-significant 16 bits to zero
lui $8, 0xabcd # puts 0xabcd0000 into r8
ori $8, $0, 0x123 # sets just low 16 bits
# result: r8 = 0xabcd0123
• li pseudo-instruction (see later) generates lui/ori
or ori code sequence as needed...
59
J-Type Instruction
• Last instruction format: Jump-type (J-Type)
31 26 25 0
6 bits 26 bits
blockA:
… i structio s of blockA …
j exit
blockB:
… i structio s of blockB …
exit:
… ext part of progra …
68
repeat-until
repeat … u til $t > $t
• 1/ th
25 the perfor a e at o e of the ost
• Simulates a MIPS-based machine with some
basic virtual hardware (console)
• Installation
1. From the Patterson & Hennesey textbook CD
2. From the internet
http://www.cs.wisc.edu/~larus/spim.html
• Versions for Windows, Mac and Linux
72
PC Spim
reset a hi e , load asm
programs, run them, etc
register state
(incl status reg)
.text section:
(program)
.data section
and the stack
diagnostic
messages
73
Using SPIM
• Combines an assembler, a simulator and BIOS
• Assembly language program prepared in your
favourite way as a text file
• Label your first instruction as main, e.g.
main: add $5, $3, $4 # comment
• Read program into SPIM which will assemble it and
may indicate assembly errors (1 at a time!)
• Execute your program (e.g. hit F5)
• Results output to window which simulates console
(or by inspection of registers)
• Let s look at a e a ple...
74
SPIM System Calls
• As ou ll ha e oti ed, “PIM allo s us to use
special code sequences, e.g.
li $a0, 10 # load argument $a0=10
li $v0, 1 # call code to print integer
syscall # print $a0
– ill pri t out o the o sole
• The syscall instruction does various things
depending on the value of $v0
– this is very similar to how things work in a modern
PC or Mac BIOS, albeit somewhat simpler
• We ll see h these are alled s ste alls
later o i the ourse…
75
SPIM System Call Codes
Procedure code $v0 argument
print int 1 $a0 contains number
print float 2 $f12 contains number
print double 3 $f12 contains number
print string 4 $a0 address of string
read int 5 res returned in $v0
read float 6 res returned in $f0
read double 7 res returned in $f0
read string 8 $a0 buffer, $a1 length
exit program 10 /* none */
76
Example: Print numbers 1 to 10
.data
newln: .asciiz “\n”
.text
.globl main
main:
li $s0, 1 # $s0 = loop counter
li $s1, 10 # $s1 = upper bound of loop
loop:
move $a0, $s0 # print loop counter $s0
li $v0, 1
syscall
li $v0, 4 # syscall for print string
la $a0, newln # load address of string
syscall
addi $s0, $s0, 1 # increase counter by 1
ble $s0, $s1, loop # if ($s0<=$s1) goto loop
li $v0, 10 # exit
syscall
77
Example: Increase array elems by 5
.text
.globl main
main:
la $t0, Aaddr # $t0 = pointer to array A
lw $t1, len # $t1 = length (of array A)
sll $t1, $t1, 2 # $t1 = 4*length
add $t1, $t1, $t0 # $t1 = address(A)+4*length
loop:
lw $t2, 0($t0) # $t2 = A[i]
addi $t2, $t2, 5 # $t2 = $t2 + 5
sw $t2, 0($t0) # A[i] = $t2
addi $t0, $t0, 4 # i = i+1
bne $t0, $t1, loop # if $t0<$t1 goto loop
# ... exit here ...
.data
Aaddr: .word 0,2,1,4,5 # array with 5 elements
len: .word 5
78
Procedures
• Long assembly programs get very unwieldy!
• Procedures or subroutines (similar to methods
or functions) allow us to structure programs
• Makes use of a new J-type instruction, jal:
• jal addr # jump-and-link
– stores (current address + 4) into register $ra
– jumps to address addr
• jr $ra
– e e see this efore – an indirect jump
– after a jal, this will return back to the main code
79
Example Using Procedures
.data
newline:.asciiz “\n”
.text
print_eol: # procedure to print "\n"
li $v0, 4 # load system call code
la $a0, newline # load string to print
syscall # perform system call
jr $ra # return
print_int: # prints integer in $a0
li $v0, 1 # load system call code
syscall # perform system call
jr $ra # return
main:
li $s0, 1 # $s0 = loop counter
li $s1, 10 # $s1 = upper bound
loop: move $a0, $s0 # print loop counter
jal print_int #
jal print_eol # print "\n“
addi $s0, $s0, 1 # increment loop counter
ble $s0, $s1, loop # continue unless $s0>$s1
80
Non-leaf Procedures
• Procedures are great, but what if have
procedures invoking procedures?
procA: … i structio s to do stuff procA does …
li $a0, 25 # prep to call procB
jal procB # $ra = next address
jr $ra # return to caller
procB: … i structio s to do stuff procB does …
jr $ra # return to caller
$ra INFINITE LOOP!
main:
li $a0, 10 # prep to call procA
jal procA # $ra = next address
… rest of progra …
81
The Stack
• Pro le as that there s o l o e $ra!
– generally need to worry about other regs too
• We can solve this by saving the contents of
registers in memory before doing procedure
– Restore values from memory before return
• The stack is a way of organizing data in memory
which is ideally suited for this purpose
– Has so-called last-in-first-out (LIFO) semantics
– push items onto the stack, pop items back off
• Think of a pile of paper on a desk
– pushing a ite is addi g a pie e of paper
– popping is re o i g it
– size of pile grows and shrinks over time
82
The Stack in Practice
• Register $sp holds address of top of stack
– In SPIM this is initialized to 0x7FFF.EFFC
• A push stores data, a d de re e ts $sp
• A pop reads a k data, a d i re e ts $sp
Higher
# $a0 holds 0xFEE $sp 8($sp) 0xEACD0000 Addresses
# „push‟ $a0
sub $sp, $sp, 4 4($sp) 0x00000001
sw $a0, 0($sp) 0($sp) 0x20003CFC
# „pop‟ $a0 -4($sp) 0x00000FEE
lw $a0, 0($sp) -8($sp)
add $sp, $sp, 4
-12($sp) Lower
Addresses
• We use the stack for parameter passing, storing return
addresses, and saving and restoring other registers
83
Fi o a i… i asse l !
fib(0) = 0
fib(1) = 1
fib(n) = fib(n-1) + fib(n-2)
, , , , , , , , ,…
85
Optional Assembly Ticks
• Tick 0: download SPIM (some version) and
assemble + run the hello world program
• Tick 1: write an assembly program which takes an
array of 10 values and swaps the values (so e.g.
A := A , A := A , … A := A
• Tick 2: write an assembly program which reads in
any 10 values from the keyboard, and prints them
out lowest to highest
• Tick 3 (*hard*): write an optimized version of the
Fibonacci code presented here. You may wish do
custom stack frame management for the base
cases, and investigate tail-recursion.
– see what Fibonacci number you can compute in 5
minutes with the original and optimized versions
86