517-1 Yhtomit
CSCI 1800 Cybersecurity and International Relations
Computer Architecture
John E. Savage
Brown University
Overview
• Definition of computer
• Binary numbers
• Very brief history of computer
• Architecture of CPU – CPU and RAM
– Typical instructions
• Simple assembly level program
• Assembler and compiler
Lect03 3/30/2013 © JE Savage 2
The Computer
• A computer takes input, produces output, potentially storing data in the process.
• Most computers can be configured to perform many tasks, i.e. they are programmable.
Lect03 3/30/2013 © JE Savage 3
Very Brief History of Computers
• Mechanical computers have existed for millennia. – Abacus
• Electronic computers emerged in the 1940s. – Eniac
• Embedded computers are used in appliances. – iPod, smart phones
• Specialized control hardware less expensive to implement with a general purpose computer
Lect03 3/30/2013 © JE Savage 4
Binary Numbers
• Today computers store data as bits, 0s and 1s. • Addition/subtraction is done with binary numbers. • Here is a mapping of decimal to binary numbers 0 000 = 0*22 + 0*21 + 0*20 1 001 = 0*22 + 0*21 + 1*20 2 010 = 0*22 + 1*21 + 0*20 3 011 = 0*22 + 1*21 + 1*20 4 100 = 1*22 + 0*21 + 0*20 5 101 = 1*22 + 0*21 + 1*20 6 110 = 1*22 + 1*21 + 0*20 7 111 = 1*22 + 1*21 + 1*20
Lect03 3/30/2013 © JE Savage 5
4 = 22, 2 = 21, 1 = 20
Binary Numbers
• How can we represent 1019 in binary? – Find the largest power of 2 that is less than 1019
• 20 =1, 21 = 2, 22 = 4, 23 = 8, 24 = 16, 25 = 32, 26 = 64, 27 = 128, 28 = 256, 29 = 512, 210 = 1024
• Answer is 512 = 29
• Subtract 512 from 1019 = 507
– Find largest power of 2 that is less than 507 • Answer 256
• Subtract 256 from 507 = 251
– Repeat to obtain 1019 = 512+256+128+64+32+8+2+1
– 101910 = 2 9 +28 +27 +26 +25 +24 +23 +21 +20 = 111110112
Lect03 3/30/2013 © JE Savage 6
Computer Architecture
• Computers have central processing units (CPUs) and random access memories (RAMs).
• Programs stored in the RAM direct the action of the CPU. • The CPU implements the fetch-execute cycle
– Fetch an instruction and execute it.
Lect03 3/30/2013 © JE Savage 7
Random Access Memory (RAM)
• A word is a collection of bits, typically 32 bits.
• RAM is given command (read, write, no-op), input word, and an address. It produces an output word.
• read changes out_wrd
to word at address addr.
• write replaces word at
address addr with in_wrd.
• no-op makes no changes.
Lect03 3/30/2013 © JE Savage 8
Central Processing Unit (CPU)
• Fetch-execute cycle: Repeat forever: a) read an instruction from the RAM and b) execute it.
• Typical instructions: ADD, SUBTRACT, SHIFT LEFT, MOVE, COMPARE, JUMP, JUMPC (conditional), READ, WRITE
• A program is a set of instructions.
• A register is a storage location
• CPU has registers, e.g. rega , that regb, hold temporary results. – Permanent results stored in the RAM.
Lect03 3/30/2013 © JE Savage 9
Architecture of the CPU
• The CPU also has a program counter (PC) such as prog_ctr. PC holds location of next CPU instruction.
• Normally, instructions are executed from sequential RAM locations (change PC to PC+ 1). – ADD, SUB, SHIFT, COMPARE, MOVE cause PC to increment
• But program may jump to remote instruction by changing PC to new address, not from PC to PC+ 1. – JUMP and JUMPC (Jump Conditional)
– JUMPC allows program to choose between two different paths.
Lect03 3/30/2013 © JE Savage 10
Addition
• The instruction ADD A B adds the contents of registers A and B and puts the sum into A.
• E.g. Consider addition of 310 = 0112 and 510 = 1012.
011 = 0*22 + 1*21 + 1*20
101 = 1*22 + 0*21 + 1*20
1000 = 1*23 + 0*22 + 0*21 + 0*20
• Subtraction (SUB) implemented by adding a negative number.
Lect03 3/30/2013 © JE Savage 11
COMPARE and MOVE Instructions
• COMPARE A B compares contents of registers A and B and sets CMP = 1 (0) if same (different).
• MOVE A B moves the contents of register (or location) A to that of register (or location) B.
• SHIFT LEFT A performs left shift of contents of rega SHIFT LEFT 1011 = 0110
SHIFT LEFT 0110 = 1100
• SHIFT LEFT can be used to multiply two numbers.
Lect03 3/30/2013 © JE Savage 12
JUMP Instructions
• The program counter PC is a CPU register with the address of the next instruction in memory.
• Normally the PC is incremented on each step. – That is, the next instruction in memory is executed
• The JUMP and JUMPC (conditional) instructions change the contents of the PC. – The CPU jumps to a new point in the program.
• JUMP A sets PC to A. • JUMPC A sets PC to A if compare bit CMP is 1.
– This command allows for a conditional branch.
Lect03 3/30/2013 © JE Savage 13
A Simple Multiplication Algorithm
• To multiply integers x and y, set u = 0, z = 0 and then add y to z a total of x times. – E.g. x = 3, y = 2, u = 0, z = 0.
COMMAND MEANING COMMENT 1. ADD y z Add y to z (z = 0 initially) 2. SUB x 1 Subtract 1 from x 3. COMPARE x u Compare x to u (u remains at 0) 4. JUMPC 6 If x = 0, jump to instruction 6. 5. JUMP 1 If x ≠ 0, jump to beginning of loop 6. DONE
Lect03 3/30/2013 © JE Savage 14
External CPU Connections
• READ reads from external input, e.g. keyboard
• WRITE writes to external output, e.g. display
• These commands require a way to specify which device is connected to the CPU.
Lect03 3/30/2013 © JE Savage 15
Assembly Language
• CPU instructions are bit sequences.
– Most computers operate on 32-bit words, though 64-bit is becoming more common.
– Some bits in a word represent an opcode (e.g. MOVE)
– Other bits represent addresses and values.
• Assembly languages use mnemonics, such as ADD, SHIFT LEFT, MOVE, COMPARE, JUMPC instead of bits.
• An assembler is a program that translates assembly language programs into binary CPU instructions.
Lect03 3/30/2013 © JE Savage 16
Compilers
• High level languages are more expressive than assembly level languages.
– Fewer statements, each having more meaning.
• Compilers are programs to translate high level languages into machine level languages.
Lect03 3/30/2013 © JE Savage 17
Review
• Definition of computer
• Brief history of computer
• Architecture of CPU
– CPU and RAM
– Typical instructions
• Simple assembly level program
• Assembler and compiler
Lect03 3/30/2013 © JE Savage 18