PC Architecture Overviewteacher.en.rmutt.ac.th/ktw/MicroProcessor/buses/Lecture_02 Review... · PC...

52
confidential Dept. of Computer Science and Information Engineering National Cheng Kung University PC Architecture Overview Chung-Ping Young

Transcript of PC Architecture Overviewteacher.en.rmutt.ac.th/ktw/MicroProcessor/buses/Lecture_02 Review... · PC...

confidential

Dept. of Computer Science and Information Engineering National Cheng Kung University

PC Architecture Overview

Chung-Ping Young

REVIEW OF PC ARCHITECTURE

Outlines

IA-32 ArchitectureInstruction set summaryModular programmingMemoryI/O interfacesInterrupts Human interface devicesMass storage devicesVideo

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 2

REFERENCESIA-32 Intel® Architecture Software Developer’s Manual Volume 1: Basic Architecture, http://www.intel.com/design/Pentium4/manuals/253665.htmThe Intel Microprocessors 8086/8088, 80186/80188, 80286, 80386, 80486, Pentium, and Pentium Pro Processor Architecture, Programming, and Interfacing, 6th ed., Barry Brey, Prentice Hall, 2003

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 3

IA-32 ARCHITECTURE

Modes of Operation

Protected modeAll instructions and architectural features are available, providing the highest performance and capability. This is the recommended mode

Real-address modeFollowing power-up or a reset, implementing 8086 processor programming environment

System management modeImplementing platform-specific functions, such as power management and system security

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 4

IA-32 ARCHITECTURE

Basic Execution Environment

Address spaceBasic program execution registersx87 FPU registersMMX registersXMM registersStack

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 5

IA-32 ARCHITECTURE

Basic Execution Environment

(cont’)

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 6

IA-32 ARCHITECTURE

Basic Execution Environment

(cont’)

I/O portsControl registersMemory management registersDebug registersMemory type range registersMachine specific registers Machine check registersPerformance monitoring counters

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 7

IA-32 ARCHITECTURE

Memory Organization

Physical address space0 – (236 – 1), 64 GBytes

Linear address space0 – (232 – 1), 4 GBytes

Flat memory model Segmented memory model

Logical address consists of a segment selector and an offsetAddress up to 16383 segments of different sizes and types, and each segmen tcan be as large as 232 bytes

Real-address mode memory model

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 8

IA-32 ARCHITECTURE

Memory Management

Models

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 9

IA-32 ARCHITECTURE

Basic Program Execution Registers

Segment registersCS (Code segment)SS (Stack segment)DS (Data segment)ES (Extra segment)FS (Data segment F)GS (Data segment G)

General-purpose registersEAX, AX, AH, AL (Accumulator)EBX, BX, BH, BL (Base index)ECX, CX, CH, CL (Count)EDX, DX, DH, DL (Data)EBP, BP (Base pointer)ESP, SP (Stack pointer)EDI, DI (Destination index)ESI, SI (Source index)

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 10

IA-32 ARCHITECTURE

Basic Program Execution Registers

(cont’)

Instruction pointer registerEIP, IP

Program status and control register, EFLAGS

Direction flag: DFStatus flags: CF, PF, AF, ZF, SF, OFSystem flags and IOPL field

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 11

IA-32 ARCHITECTURE

Addressing Modes

There are 1M-byte address space in real modeThe 1M bytes of memory can be partitioned into 64K-byte segmentsA segment is an area of memory including up to 64K bytes begins on an address evenly divisible by 16 (an address ending in 0H)The physical address is the 20-bit address that is actually put on the address pins of the 8086 microprocessor and decoded by the memory interfacing circuitryThe offset address is a location within a 64K-byte segment rangeThe logical address consists of a segment value and an offset address

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 12

IA-32 ARCHITECTURE

Addressing Modes

Register Operand AddressingImmediate Operand AddressingMemory Operand Addressing

Direct addressingRegister indirect addressingRegister relative addressing

Based addressingIndexed addressing

Based indexed relative addressingBase-plus-index addressingBase relative-plus-index addressingScaled-index addressing (80386 thru Pentium 4)

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 13

IA-32 ARCHITECTURE

Operand Addressing

Register operandsmov ax, bx

Immediate operandsmov ax, 2550h

Memory operandsSpecifying a segment selectorSpecifying an offset

DisplacementBaseIndexScale factor

Offset = Base + (Index * Scale) + Displacement

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 14

IA-32 ARCHITECTURE

Operand Addressing

(cont’)

Memory operands (cont’)

DisplacementBaseBase + Displacement(Index * Scale) + DisplacementBase + Index + DisplacementBase + (Index * Scale) + Displacement

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 15

IA-32 ARCHITECTURE

Operand Addressing

I/O port addressingUp to 65536 8-bit I/O ports16- and 32-bit ports can also be defined in the I/O address spaceAddressed with an immediate operand or a value in the DX register

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 16

IA-32 ARCHITECTURE

Default Segment and Offset registers

16-bit

Segment Offset Special PurposeCS IP Instruction addressSS SP or BP Stack addressDS BX, DI, SI, an 8-bit number

or a 16-bit numberData address

ES DI for string instructions String destination

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 17

IA-32 ARCHITECTURE

Default Segment and Offset registers

(cont’)

32-bit

Segment Offset Special PurposeCS EIP Instruction addressSS ESP or EBP Stack addressDS EAX, EBX, ECX, EDX, EDI,

ESI, an 8-bit number or a 32-bit number

Data address

ES EDI for string instructions String destinationFS No default General addressGS No default General address

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 18

INSTRUCTION SET SUMMARY

General purposex87 FPUx87 FPU and SIMD state managementIntel MMX technologySSE extensionsSSE2 extensionsSystem

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 19

INSTRUCTION SET SUMMARY

General Purpose Instructions

Data transfer instructionsBinary arithmetic instructionsDecimal arithmeticLogical instructionsShift and rotate instructionsBit and byte instructionsControl transfer instructionsString instructions Flag control instructionsSegment register instructionsMiscellaneous instructions

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 20

INSTRUCTION SET SUMMARY

General Purpose Instructions

(cont’)

Data transfer instructionsMOVMOVSXXCHGBSWAPPUSHPOPINOUT

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 21

INSTRUCTION SET SUMMARY

General Purpose Instructions

(cont’)

Binary arithmetic instructionsADD/ADCSUB/SBBIMUL/MULIDIV/DIVINCDECNEGCMP

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 22

INSTRUCTION SET SUMMARY

General Purpose Instructions

(cont’)

Decimal arithmeticDAA: decimal adjust after additionDASAAA: ASCII adjust after additionAASAAMAAD

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 23

INSTRUCTION SET SUMMARY

General Purpose Instructions

(cont’)

Logical instructionsANDORXORNOT

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 24

INSTRUCTION SET SUMMARY

General Purpose Instructions

(cont’)

Shift and rotate instructionsSAR/SAL : shift arithmetic right/leftSHR/SHL : shift logical right/leftROR/ROLRCR/RCL

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 25

INSTRUCTION SET SUMMARY

General Purpose Instructions

(cont’)

Bit and byte instructionsBT/BTS/BTRSETX (X is E/NE, Z/NZ, A/AE, B/BE, etc.)TEST: logical compare

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 26

INSTRUCTION SET SUMMARY

General Purpose Instructions

(cont’)

Control transfer instructionsJMPJX (X is E/NE, Z/NZ, A/AE, B/BE, etc.)LOOPLOOPZ/LOOPNZ/LOOPE/LOOPNECALLRETIRETINT

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 27

INSTRUCTION SET SUMMARY

General Purpose Instructions

(cont’)

String instructionsMOVS/MOVSB/MOVSW/MOVSDCMPS/CMPSB/CMPSW/CMPSDSCAS/SCASB/SCASW/SCASDLODS/LODSB/LODSW/LODSDSTOS/STOSB/STOSW/STOSDREP/REPE/REPZ/REPNE/REPNZ

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 28

INSTRUCTION SET SUMMARY

General Purpose Instructions

(cont’)

Flag control instructionsSTC/CLC/CMCCLD/STDLAHF/SAHFPUSHF/POPFSTI/CLI

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 29

INSTRUCTION SET SUMMARY

General Purpose Instructions

(cont’)

Segment register instructionsLDSLESLFSLGSLSS

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 30

INSTRUCTION SET SUMMARY

General Purpose Instructions

(cont’)

Miscellaneous instructionsLEANOPXLAT/XLATB

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 31

MODULAR PROGRAMMING

Assembler and LinkerLibrariesDirectivesMacrosModulesBIOS Function CallsDOS Function Calls

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 32

MODULAR PROGRAMMING

Assembler and Linker

Assembler.OBJ.LST.CRF

Linker.EXE (.COM).MAP

Library.LIB

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 33

MODULAR PROGRAMMING

Libraries

Libraries is a good place to store a collection of related procedures

A library file is created with the LIB command at the DOS promptA library file is a collection of assembled .OBJ files that each perform one procedure or taskWhen linked with a program, only the procedures required by the program are removed from the library file and added to the program

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 34

MODULAR PROGRAMMING

Directives

Directives (pseudo-operations) give directions to the assembler about how the assembly process should be conducted

SEGMENT, ENDSPROC, ENDPENDASSUMEDB, DW, DD, DUP, EQUORG, LABELPAGE, TITLE

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 35

MODULAR PROGRAMMING

Macros

Macros allow the programmer to write the task (set of codes to perform a specific job) once only and to invoke it whenever it is needed wherever it is neededname MACRO dummy1, dummy2, …, dummyN

…..…..ENDM

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 36

MODULAR PROGRAMMING

Macros(cont’)

LOCALINCLUDEConditional statements

REPEAT, has a parameter associated with it to repeat the macro sequence a fixed number of timesWHILE, determines how many times the loop is repeated by the associated expressionFOR, iterates a list of dataIF, ELSE and ENDIF, is used to to make decision, based on the parameters sent to the macro

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 37

MODULAR PROGRAMMING

Procedures

A more efficient way to develop software is to treat each subroutine as a separate program (or module) with a separate filename

PROC, ENDPEXTRNPUBLIClink exmp1.obj+exmp2.obj+exmp3.obj

The CALL instruction transfers the flow of the program to the procedure

Near callFar callCalls with register operandsCalls with indirect memory addresses

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 38

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 39

MODULAR PROGRAMMING

Procedures(cont’)

Passing parameters among modulesVia registersVia memory Via stack

main PROC FAR….

push VALUE3push VALUE2push VALUE1call subProg

….main ENDP

subProg PROC FAR….

push bpmov bp, spmov ax, [bp]+6mov cx, [bp]+8mov dx, [bp]+10

….pop bpret 6

….subProg ENDP

MODULAR PROGRAMMING

Combining Assembly and C

Inserting x86 Assembly code into C programsC programs calling Assembly procedures

C calling conventionHow parameters are returned to CPassing array addresses from C to stackLinking Assembly routines with C

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 40

MODULAR PROGRAMMING

Combining Assembly and C

(cont’)

C calling conventionThe parameters are passed by value to the stack in reverse order, but array is passed by referenceIf C is compiled in MEDIUM, LARGE or HUGE (or if procedure is FAR), both CS and IP are saved in the stack (CS is passed first, then IP)BP must be saved on the stack and then the parameters must be accessed by BPThe last instruction should be RET without number after it, since it is the job of C to restore the stackAny name shared publicly with C must be prefaced with underscore, and only the first 8 characters of the name are recognized by CIf C is compiled in MEDIUM, LARGE or HUGE model, use the FAR option for the Assembly language procedure

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 41

MODULAR PROGRAMMING

Combining Assembly and C

(cont’)

How parameters are returned to CAL, char, shortAX, int or IP if NEARDX:AX, long or CS:IP if FAR

Passing array addresses from C to stackThe offset address is pushed if memory model is SMALL or MEDIUM, otherwise, both segment and offset are pushed

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 42

MODULAR PROGRAMMING

Combining Assembly and C

(cont’)

Linking Assembly routines with CMake sure that the Assembly language procedure declares the procedure as PUBLIC. The procedure name should begin with underscoreMake the Assembly language procedure NEAR for small model and FAR for medium modelIn the C program, declare the procedure as externalAssemble the Assembly program to produce the object fileCompile the C program to produce the object fileLink them together to produce the executable file

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 43

MODULAR PROGRAMMING

Interrupts

An interrupt is either a hardware-generated CALL (externally derived from a hardware signal) or a software-generated CALL (internally derived from the execution of an instruction or by some other internal event)

Interrupt vectorsInterrupt instructions

INTsIRET/IRETDINT3Int0

An interrupt service procedureInterrupt controlInterrupts in the PC

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 44

MODULAR PROGRAMMING

Interrupts(cont’)

Interrupt vectorsFour-byte number stored in the first 1024 bytes of the memory (0 – 3FFH) when microprocessor operated in real modeThere are 256 different interrupt vectors, and each vector contains the value for IP (first two bytes) and CS (last two bytes) of an interrupt service procedureThe first 32 interrupt vectors are reserved, while the remaining (32 – 255) are available for users In the protected mode, the vector table is replaced by an interrupt descriptor table that used eight-byte descriptors to describe each of the interrupts

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 45

MODULAR PROGRAMMING

Interrupts(cont’)

Interrupt instructionsINTs

- There are 256 different software interrupt instructions available to the programmer

- The address of interrupt service procedure of INT n is stored beginning at memory location n * 4 in the real mode

- When INT instruction is executed• Pushes the flags onto the stack• Clears the T and I flag bits• Pushes CS onto the stack• Fetches new value for CS from interrupt

vector• Pushes IP/EIP onto the stack• Fetches new value for IP/EIP from the vector• Jumps to the new location addressed by CS

and IP/EIP

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 46

MODULAR PROGRAMMING

Interrupts(cont’)

Interrupt instructionsIRET/IRETD

- The IRET instruction will• Pop stack data back into the IP• Pop stack data back into the CS• Pop stack data back into the flag

registerINT3

- A special software interrupt designed to function as a breakpoint

- It is common to insert an INT 3 instruction in software to interrupt or break the flow of the software

Int0- Interrupt on overflow is a conditional

software interrupt that tests the overflow flag

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 47

MODULAR PROGRAMMING

Interrupts(cont’)

An interrupt service procedureDiffers from a normal far procedure is that

- it ends with the IRET instruction - the contents of the flag register are saved on

the stack during its execution

Interrupt controlTwo instructions are introduced that control the INTR pin

- STI, set interrupt flag instruction places a 1 into the I flag bit, which enables the INTR

- CLI, disables the INTR pin- In a software interrupt service procedure,

hardware interrupts are enabled as one of the first steps, accomplished by the STI instruction

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 48

MODULAR PROGRAMMING

Interrupts(cont’)

Interrupts in the PCInterrupts 00H – 1FH and 70H – 77H are present in the computer, no matter what operating system is installedIf DOS is installed, interrupts 20H – 2FH are also presentThe BIOS uses interrupts 11H through 1FH, the video BIOS uses INT 10H, and the hardware in the system uses interrupts 00H through 0FH and 70H through 77H

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 49

MODULAR PROGRAMMING

BIOS Function Calls

Video function calls: INT 10h00H, select video mode

Entry AH = 00HAL = mode number

Exit mode changed and screen cleared

02H, select cursor positionEntry AH = 02H

BH = page numberDH = row number (beginning with 0)DL = column number (beginning with 0)

Exit changes cursor to new position

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 50

MODULAR PROGRAMMING

BIOS Function Calls(cont’)

Mass storage devices: INT 13h00H, reset the system disk02H, read sector03H, write sector08H, get drive parameters10H, get drive status

System services: INT 15hKeyboard: INT 16h

00H, read keyboard character01H, get keyboard status02H, get keyboard flags

Timer and real-time clock: INT 1Ah

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 51

MODULAR PROGRAMMING

DOS Function Calls

INT 21HPlace the function number into register AH and load all other pertinent information into registersInvoke INT 21H to execute the DOS function

ex. display the ASCII character ‘A’ on the console video screen

mov ah, 6mov dl, ‘A’int 21h

confidentialDepartment of Computer Science and Information Engineering National Cheng Kung University 52