Introduction To Computer Science I Harvard College
Introduction To Computer Science I Harvard College
Comput er Sc i enc e 50
Introduction to Computer Science I
Har var d Col l ege
David J. Malan
[email protected]
Week 8
1
Morse Code
Image adapted from Wikipedia.
2
Huffman Coding
Immediate Decodability
Pseudocode by Larry Nyhoff.
1) Initialize a list of one-node binary trees containing weights w
1
, w
2
, ... , w
n
,
one for each of the characters C
1
, C
2
, ... , C
n
.
2) Do the following n 1 times:
a) Find two trees T ' and T '' in this list with roots of minimal weight w ' and w ''.
b) Replace these two trees with a binary tree whose root has weight w ' + w ''
and whose subtrees are T ' and T ''; label the pointers to these subtrees 0
and 1, respectively:
3) The code for character C
i
is the bit string labeling the path from root to
leaf C
i
in the final binary tree.
3
Huffman Coding
Example
4
Huffman Coding
Example
Figures by Larry Nyhoff.
5
Huffman Coding
Example
Figure by Larry Nyhoff.
6
Huffman Coding
Example
Figure by Larry Nyhoff.
7
Huffman Coding
Example
Figure by Larry Nyhoff.
8
Huffman Coding
Example
Figure by Larry Nyhoff.
9
Huffman Coding
Example
10
Huffman Coding
Problem?
0 1 0 1 0 1 1 0 1 0
11
Huffman Coding
In C
typedef struct tree
{
char symbol;
int frequency;
struct tree *left;
struct tree *right;
}
Tree;
12
Bitwise Operators
& bitwise AND
| bitwise OR
^ bitwise XOR
~ ones complement
<< left shift
>> right shift
13
Bitwise Operators
see
binary.c, tolower.c, toupper.c
14
Bitwise Operators
Swapping Values
see
swap2.c
15
Bitwise Operators
Swapping Values
see
swap2.c
16
Underneath the Hood
Software
Pre-Processing
Compiling
Assembling
Linking
Executing
17
From Source Code to Object Code
source code
assembly code
object code
01101001 01001010 10000010 11011001
00101011 01100000 10001011 10111010
# hello.asm -- prints hello world
# r2: string to be printed
lc r2, $hello # text location
sys r2, 4 # put string
sys r0, 0 # halt
_data_:
hello: .data 'h','e','l','l','o',' '
.data 'w','o','r','l','d','\n',0
#include <stdio.h>
int
main(int argc, char * argv[])
{
printf("hello, world\n");
}
compile
assemble
18
Linking against Libraries
01101001 01001010 10000010 11011001
00101011 01100000 10001011 10111010
hello.c
[uses printf]
compile
assemble
assembly code
for hello.c
19
Linking against Libraries
01101001 01001010 10000010 11011001
00101011 01100000 10001011 10111010
hello.c
[uses printf]
compile
assemble
01101001 01001010 10000010 11011001
00101011 01100000 10001011 10111010
compile
assemble
01101001 01001010 10000010 11011001
00101011 01100000 10001011 10111010
link
assembly code
for hello.c
assembly code
for stdio.c
stdio.c
[defines printf]
stdio.h
[describes printf]
describes
20
Underneath the Hood
Hardware
r1 r2 r3
1 1 2 +
21
Whats in the Box?
Registers
very fast temporary memory
few of them (16 or 32)
Program counter
special register for tracking
the next instruction to
execute
Memory
storage for program code
and data
Control unit
translates instructions into
commands for registers and
datapath
Datapath
carries out basic operations
(arithmetic, logical)
I/O devices
supports flow of data into and
out of the machine
22
Whats in the CPU?
CPU
Datapath
Register
File
Control
abstraction
23
What can a CPU do?
Ant-8s Instruction Set
Arithmetic
add dst,src1,src2
sub dst,src1,src2
mul dst,src1,src2
inc dst,const8
Load constant
lc dst,const8
Bitwise/Logical
and dst,src1,src2
nor dst,src1,src2
shf dst,src1,src2
Load and store
ld1 dst,base,uconst4
st1 src,base,uconst4
Branch
beq tgt,src1,src2
bgt tgt,src1,src2
jmp uconst8
System
hlt
in dst,channel
out src,channel
24
Machine Code
Ant-8
0x83EF
Machine Code
Assembly Code add r3, r14, r15
All Ant instructions are 16 bits wide
first 4 bits encode the opcode
format of other 12 bits depend upon opcode
Example:
25
From C to Ant-8
lc r5, $test1 # address of test1 in mem
ld1 r14, r5, 0 # value of test1
ld1 r15, r5, 1 # value of test2
add r3, r14, r15 # sum of test scores
st1 r3, r5, 2 # store in mem
hlt # end of program code
_data_:
test1: .byte 77
test2: .byte 96
totalPoints: .byte 0
# end of assembly file
/* C code */
int test1 = 77;
int test2 = 96;
int totalPoints = test1 + test2;
# ANT code
26
Comput er Sc i enc e 50
Introduction to Computer Science I
Har var d Col l ege
David J. Malan
[email protected]
Week 8