# COMP 1321 Digital Infrastructure Richard Henson University of

• Slides: 38

COMP 1321 Digital Infrastructure Richard Henson University of Worcester October 2018

Week 3: Programming a CPU Explain how logic gates can create a CPU n Understand how registers and memory addresses are used to process input and store the results n

How can logic gates make a CPU? n Overview: Øhttps: //www. youtube. com/watch? v=1 I 5 ZM mr. Ofn. A ØEarly “chips” called integrated circuits Øe. g. Intel 7481 ALU (Arithmetic & Logic Unit) » do arithmetic e. g. “adding” » provide logic…

Minimalist CPU What is needed to build a successful CPU? ALU (Arithmetic Logic Unit) Memory (to store intermediate data)) Input & Output “Execution Unit” A Good Name! Intel’s first (1972) was called… “ 4004”… because it had a 4 -bit bus!

Arithmetic Logic Unit (ALU) Input A (or… more sophisticated… Integer Execution Unit) Input B 3 add Output 2 5 3 sub How could these numbers be represented as data that passes into the ALU? 2 1

Programming Commands and Machine Code n A set of instructions for the CPU can be put into a program that can be executed one after the other Øthese instructions are known as machine code ØEach type of CPU has its own type of “machine language”.

Storing Results of Calculations n Memory is needed (let’s start with 8 -bit data): Øto feed ALU input Øto record ALU output n Overview: Øhttps: //www. youtube. com/watch? v=fpn. E 6 U Afbt. U

Processing Idea Nr. 1 Move data in and out of data memory store 1. Move data from memory 3 2. add 2 5 0 3 1 2 2 3 5 Memory DRAM, Hard Disk. . 4 3. Move data into memory

Sequence of CPU commands If the CPU is going to do something useful, it needs a sequence of commands n e. g. n 1. 2. 3. 4. move number into ALU from memory move another number in from memory add them together move the result into memory…

Processing Idea Nr. 2 Move instructions into CPU from code memory Instruction Memory (Code Memory) mov 3 in from memory 3 mov 2 in from memory IP add the two numbers mov the result to memory Program add 2 5 0 3 1 2 2 3 4 5

Registers are high-speed memory stores on the CPU chip Parking places for data on the move AX and BX are used for ALU operations MAR is memory address register, here 4. So result, 6+8=14 will go into memory cell address 4 AX BX 6 8 0 6 1 8 MAR 4 4

The computer so far … 0 1 Instruction Memory Data Memory mar ip 4

A couple of extra bits. . Memory Data Register Instruction Register 1. add ax, bx 2. 34 Instruction Memory Data • • • Energize ax Energize bx Select ALU “add" 1. Line of code goes in… 0 2 1 8 Data Memory 2 Address 2. Electrical bit signals come out 4 34

Moving data into Registers For example … mov ax , [1] mov bx , [2] Instruction Memory AX BX 8 7 mar 0 5 1 8 2 7 3 6 4 1 mov ax , [1] mov bx , [2]

Moving data into Memory For example … mov [3] , ax mov [0] , bx Instruction Memory AX BX 8 7 mov [3] , ax mov [0], bx mar 0 7 5 1 8 2 7 3 8 6 4 1

Adding Numbers For example … Add ax, bx Instruction Memory AX BX 8 7 15 mar 0 5 1 8 2 7 3 6 4 1 add ax , bx … this means ‘ add ax to bx, put the answer in ax’

Now let’s slow things down… n Today’s CPUs process billions of instructions every second Øin the early days (Intel 4004) it was merely millions! n Many great simulators have been produced that can work through a machine code program one cycle, or one instruction, at a time…

What is “Processing”? n Mostly… calculations by the ALU: Øneed data input » from register » from external memory Øneed to store output » from register » from external memory n Could also be just a command, no data needed…

CPU types n Most frequently used: ØIntel 8086 family ØMotorola (esp. 68000 family) ØARM (many mobile phones) n We’ll focus on Intel 8086 family Ødates back to original IBM PC…

Role of “Registers” n Memory stores inside the CPU Øjust the right size “word” of data for ALU » typically 1, 2, 4 bytes i. e. very small! n Advantage: CPU reads/writes the data very quickly to/from the registers

Architecture and Buses n Design of CPU Øinternal connections Øexternal connections to motherboard » data bus (same word as registers) » address bus (depends on no of memory locations) » control bus (messages to/from components) Ølayout of components on motherboard

Registers (summary…) high-speed memory on the CPU chip Parking places for data on the move AX and BX registers are used for ALU operations MAR is memory address register, 4 in eg. Result of processing, 6+8=14, will go into memory address 4 AX BX 6 8 0 6 1 8 MAR R 4 4

The computer so far… (identify & name components) 0 1 Instruction Memory Data Memory mar ip 4

A couple of extra registers. . Memory Data Register Instruction Register 1. add ax, bx 2. 34 Instruction Memory Data • • • Energize ax Energize bx Select ALU “add" 1. Line of code goes in… 0 2 1 8 Data Memory 2 Address 2. Electrical bit signals come out 4 34

Moving data into Registers (ie from specified location) for example … mov ax , [1] mov bx , [2] Instruction Memory AX BX 8 7 mar 0 5 1 8 2 7 3 6 4 1 mov bx , [2]

Moving data into Memory For example … mov [3] , ax mov [0] , bx Instruction Memory AX BX 8 7 mov [3] , ax mov [0], bx mar 0 7 5 1 8 2 7 3 8 6 4 1

8086 CPU family registers n 8086 chip always used a 16 -bit word ØSAM simulates an 8 -bit word » popular on most early microcomputers… n Typical 8086 registers (stores): Øgeneral purpose data: AX, BX, CX, DX Øspecific use e. g. » program counter (PC): instruction address in memory » stack pointer SP): address of the top of the “stack”

Data and Addressing n General purpose register contents… Ødata Ømemory address that points to data n Convention: Ødata written as hexadecimal equivalent » e. g. 4 A Ømemory location also has square brackets » e. g. [4 A]

CPU Instructions Used to tell the CPU what to do… n MOV is for moving data around… n ØMOV AX, 4 A – move “ 4 A” into AX register ØMOV AX, [4 A] – move data contained in address 4 A into AX register n Many other instructions; range of operations… Øcollectively known as an instruction set Øeach CPU family has its own unique codes

8086 in practice n Four 16 -bit General Purpose registers Øeach gen register (e. g. AX) can be read/written to upper (AH) & lower (AL) byte upper byte lower byte AX AH AL BX BH BL CX CH CL DX DH DL

Another 8086 Instruction: ADD Takes values from two registers n Adds them together n Deposits results back in one of the registers n Which one? n Øthe register that appeared first Øe. g. “MOV, AX, BX” puts result in AX

8088: Brains of the IBM PC