Chapter 10 Instruction Sets: Characteristics and Functions ...
description
Transcript of Chapter 10 Instruction Sets: Characteristics and Functions ...
Chapter 10Instruction Sets:Characteristicsand Functions คณลกษณะและหนาทของชดคำาสง
1 .คณลกษณะของคำาสงภาษาเครอง2 .ชนดของตวถกดำาเนนการ3 .ตวอยาง ชนดของขอมลในเครอง Pentium
และ PowerPC4 .ชนดการทำางานในเครอง Pentium และ
PowerPC5 .ภาษาแอสแซมบล
What is an instruction set?• The complete collection of instructions
that are understood by a CPU• Machine Code• Binary• Usually represented by assembly codes
สรปการทำางานของ CPU ถกกำาหนดโดยคำาสงทกำาลงทำาการประมวลผลอยในขณะนน ซงเรยกวา คำาสงเครอง คำาสงเครองเมอนำามารวมเขาไวดวยกน หรอเมออางกลมคำาสงสำาหรบคอมพวเตอร เรยกวา “ชดคำาสง”
Elements of an Instruction(องคประกอบของชดคำาสง)• Operation code (Op code)
—บอกใหทราบถงงานททำา เชนการบวก หรองาน I/O • Source Operand reference
—แหลงอางองแหลงทมาขอมล การทำางานอาจจะมการอางองถงตวถกดำาเนนการ หรอทบอกแหลงทเกบขอมลทตองนำามาใช
ในการกระทำานน• Result Operand reference
—การอางองแหลงทเกบขอมลเมอมการดำาเนนการ พดงายๆ คอทเกบผลลพธของการทำางาน
• Next Instruction Reference—การอางองคำาสงถดไป เปนการบอกให CPU ทราบตำาแหนงถด
ไปทจะทำางาน
Where have all the Operands gone?(ตวถกดำาเนนการทใชบอกตำาแหนงการอางองแหลงทมาหรอทเกบขอมล อาจจอยในรปแบบน
• Main memory (or virtual memory or cache) หนวยความจำาหลกหรอหนวยความจำาเสมอน
• CPU register ตำาแหนงของการอางองหรอทเกบขอมลคอ Register ทอยภายในตว CPU เอง เชน Ld a,01h
• I/O device ตำาแหนงของอปกรณทใชเกบขอมล หรอแสดงผลลพธของขอมล
Instruction Cycle State Diagram
Instruction Representation• In machine code each instruction has a
unique bit pattern(ใน Computer แตละชดคำาสงจะเรยงกนในรปแบบของ Bit Binary)
• For human consumption (well, programmers anyway) a symbolic representation is used (การทจะอธบายรายละเอยดโดยเลขฐาน 2 นน เปนเรองยากดงนนจงไดเลอกภาษาสญลกษณทงายตอการเขาใจ เชน—e.g. ADD, SUB, LOAD
• Operands can also be represented in this way (ตวถกดำาเนนการกใชคำายอเหมอนกน)—ADD A,B
Simple Instruction Format(รปแบบคำาสงแบบงาย)
Instruction Types
• Data processing คำาสงในการประมวลผลขอมลทางคณตศาสตร
• Data storage (main memory) คำาสงเกยวกบหนวยความจำา
• Data movement (I/O) คำาสงเกยวกบการ I/O• Program flow control คำาสงควบคมลำาดบการ
ประมวลผล
คอมพวเตอรควรมชดคำาสงทชวยใหผใชสามารถประมวลผลงานทตองการได ดงนนชดคำาสงจะตองมมากพอ ทจะใชแปลความหมายภาษา
ระดบสงโดยมากภาษาเครองจงถกแบงเปนประเภทตางๆ ดงน)
เปรยบเทยบคำสงทใชทอยององ Y=(A-B)/(C+D*E)Instruction CommentSUB Y , A, B
YA – BMPY T, D, E
TD EADD T, T, C TT + CDIV Y, Y, T
YY T(a)Three-address
instructionsMOVE Y, A
YASUB Y, B
YY – BMOVE T, D
TDMPY T, E
TT EADD T, C TT + CDIV Y, T
YY T(b) Two-address
instructions
InstructionComment
LOAD D ACDMPY E ACAC EADDC ACAC + CSTOR Y YACLOAD A ACASUB B ACAC –
BDIV Y ACAC
YSTOR Y YAC(c) One-address
instructions
Number of Addresses (a) จำานวนของทอยอางองในคำาสงเครอง• 3 addresses
—Operand 1, Operand 2, Result—a = b + c;—May be a forth - next instruction (usually
implicit)—Not common—Needs very long words to hold everything
Number of Addresses (b)• 2 addresses
—One address doubles as operand and result—a = a + b—Reduces length of instruction—Requires some extra work
– Temporary storage to hold some results
Number of Addresses (c)• 1 address
—Implicit second address—Usually a register (accumulator)—Common on early machines
Number of Addresses (d)• 0 (zero) addresses
—All addresses implicit—Uses a stack—e.g. push a— push b— add— pop c
—c = a + b
How Many Addresses• More addresses ถาใช Address มาก
—More complex (powerful?) instructions (ซบซอน)—More registers (ใช Register มากกวา)
– Inter-register operations are quicker—Fewer instructions per program (ใชชดคำาสงนอยตอ
โปรแกรม)• Fewer addresses ถาใช Address นอย
—Less complex (powerful?) instructions (ซบซอนนอยกวา)
—More instructions per program(ใชชดคำาสงมากกวาตอโปรแกรม)
—Faster fetch/execution of instructions (ประมวลผลเรวกวา)
Design Decisions (1) (การออกแบบชดคำาสงมปจจยไรบาง)• ชดคำาสงจะเปนตวกำาหนดหนาทการทำางานของ CPU จงมผล
โดยตรงตอการออกแบบและสราง CPU ขนมาใชงานและเปนเสมอนเครองมอของ Programmer ทใชควบคมการทำางานของ CPU ดงนนความตองการของ Programmer จะตองถกนำามาใชรวมพจารณาในระหวางออกแบบชดคำาสง
• Operation repertoire—How many ops? CPU ควรจะมฟงกชนการทำางานเปนเทาใด —What can they do? การทำางานแตละอยางควรทำาอะไร—How complex are they? การทำางานมความซบซอนมากเพยงใด
• Data types ชนดของขอมลทนำามาใช• Instruction formats ความยาวของคำาสง
—Length of op code field ความยาวของคำาสง—Number of addresses จำานวนของแอดเดรส
Design Decisions (2)• Registers
—Number of CPU registers available จำานวน Register
—Which operations can be performed on which registers?
• Addressing modes (later…) รปแบบการทำางานทใชกำาหนดชนดของทอยของตวถกดำาเนนการ
• RISC v CISC
Types of Operand ชนดของตวถกดำาเนนการ• Addresses ตำาแหนงทอย• Numbers ตวเลข
—Integer/floating point• Characters ตวอกษร
—ASCII etc.• Logical Data ขอมลตรรกะ
—Bits or flags• (Aside: Is there any difference between numbers and
characters? Ask a C programmer!)
Pentium Data Types• 8 bit Byte• 16 bit word• 32 bit double word• 64 bit quad word• Addressing is by 8 bit unit• A 32 bit double word is read at addresses
divisible by 4
Specific Data Types(ชนดขอมลของ Pentium)• General - arbitrary binary contents• Integer - single binary value• Ordinal - unsigned integer• Unpacked BCD - One digit per byte• Packed BCD - 2 BCD digits per byte• Near Pointer - 32 bit offset within segment• Bit field• Byte String• Floating Point
Pentium Floating Point Data Types
PowerPC Data Types (ชนดขอมลใน cpu Power PC)• 8 (byte), 16 (halfword), 32 (word) and 64
(doubleword) length data types• Some instructions need operand aligned
on 32 bit boundary• Can be big- or little-endian• Fixed point processor recognises:
—Unsigned byte, unsigned halfword, signed halfword, unsigned word, signed word, unsigned doubleword, byte string (<128 bytes)
• Floating point—IEEE 754—Single or double precision
Types of Operation (ชนดของ Opcode)• Data Transfer การเคลอนยายขอมล• Arithmetic การคำานวณทางคณตศาสตร• Logical การคำานวณตรรกะ• Conversion การเปลยนรปขอมล• I/O คำาสง I/O• System Control การควบคมระบบ• Transfer of Control การถายโอนการควบคมการ
ประมวลผล
Data Transfer ในการเคลอนยายจะตองกำาหนดการทำางานซงมองคประกอบดงน
• Specify—Source บอกตำาแหนงของขอมล—Destination บอกตำาแหนงสำาหรบบนทกผลลพธ—Amount of data กำาหนดความยาวของขอมลทเคลอนยาย
• May be different instructions for different movements จะใชชดคำาสงทแตกตางกนในการเคลอนยายขอมลแตละแบบ เชน Reg – Reg หรอ Mem - Mem—e.g. IBM 370
• Or one instruction and different addresses —e.g. VAX คอ VaX จะใชคำาสง Mov คำาสงเดยวในการเคลอนยายขอมลหลายขนาด จงงายในการพฒนาโปรแกรม
Arithmetic(การคำานวณทางคณตศาสตร)
• Computer สวนใหญจะจดใหมคำาสงสำาหรบการคำานวณพนฐานไดแก- Add, Subtract, Multiply, Divide
• Signed Integer• Floating point ?• May include
—Increment (a++)—Decrement (a--)—Negate (-a)
Logical (การคำานวณทางตรรกะ)• คำาสงสำาหรบการจดการขอมลแตละบต• Bitwise operations• AND, OR, NOT• นอกเหนอจากการทำางานกบบตแลวยงจดเตรยมฟงกชน
ในการ shift และ Rotate ดงน
Shift and Rotate Operations
Conversion (การเปลยนรปขอมล)• หมายถงคำาสงทเปลยนรปแบบ หรอคำาสงทำางานกบรป
แบบของขอมลเชน• E.g. Binary to Decimal (คำาสงการเปลยนเลขฐาน
สองเปนฐานสบ)
Input/Output (คำาสงทตดตอกบอปกรณ Input/Output)• May be specific instructions• May be done using data movement
instructions (memory mapped)• May be done by a separate controller
(DMA)
Systems Control(คำาสงสำาหรบการควบคมระบบ)• หมายถงคำาสงทสามารถควบคมระบบการทำางานของ
CPU ได โดยทวไปจะถกสวงนไวในระบบปฏบตการ
• Privileged instructions เปนชดคำาสงพเศษ
Transfer of Control (คำาสงการถายโอนการควบคมการประมวลผล)• หมายถงคำาสงทควบคมการทำางานลำาดบของการประมวล
ผลนนเอง(เปลยนลำาดบการประมวลผล ) เชน• Branch เงอนไขกระโดดเมอเปนไปตามเงอนไข
—e.g. branch to x if result is zero• Skip จะขามคำาสงในลำาดบถดไป
—e.g. increment and skip if zero—ISZ Register1—Branch xxxx—ADD A
• Subroutine call การเรยกใชฟงกชน—c.f. interrupt call
Branch Instruction
Nested Procedure Calls
Use of Stack
สรปตวอยงชนดของ OpcodeType Operation
NameDescription
Data transfer
Move (transfer)StoreLoad (fetch)ExchangeClear (reset)SetPushPop
Transfer word or block from source to destinationTransfer word from processor to memoryTransfer word from memory to processorSwap contents of source and destinationTransfer word of 0s to destinationTransfer word of 1s to destinationTransfer word from source to top of stackTransfer word from top of stack to destination
Arithmetic
AddSubtractMultiplyDivideAbsoluteNegateIncrementDecrement
Compute sum of two operandsCompute difference of two operandsCompute product of two operandsCompute quotient of two operandsReplace operand by its absolute valueChange sign of operandAdd 1 to operandSubtract 1 from operand
สรปตวอยงชนดของ Opcode
Logical
ANDORNOT (Complement)Exclusive-OR
TestCompareSet control variablesShiftRotate
Perform the specified logical operation bitwise
Test specified condition; set flag(s) based on outcomeMake logical or arithmetic comparison of two or more operands; set flag(s) based on outcomeClass of instructions to set controls for protection purposes,interrupt handling, timer control, etc.Left (right) shift operand, introducing constants at endLeft (right) shift operand, with wraparound end
สรปตวอยงชนดของ Opcode
TransferOf control
Jump (branch)Jump conditional
Jump to subroutine
Return
Execute
SkipSkip conditionalHaltWait (hold)
No operation
Unconditional transfer; load PC with specified addressTest specified condition; either load PC with specified address or do nothing, based on conditionPlace current program control information in known location; jump to specified addressReplace contents of PC and other register from known locationFetch operand from specified location and execute as instruction; do not modify PCIncrement PC to skip next instructionTest specified condition; either skip or do nothing based on conditionStop program executionStop program execution; test specified condition repeatedly: resume execution when condition is satisfiedNo operation is performed, but program execution is continued
สรปตวอยงชนดของ Opcode
Input/output
Input (read)
Output (write)
Start I/O
Test I/O
Transfer data from specified I/O port or device to destination(e.g., main memory or processor register)Transfer data from specified source to I/O port or deviceTransfer instructions to I/O processor to initiate I/O operationTransfer status information from I/O system to specified destination
conversion
Translate
Convert
Translate values in a section of memory based on a table of correspondencesConvert the contents of a word from one form to another (e.g., packed decimal to binary)
Byte Order ลำาดบของ Byte• ขอมลใน byte ตางๆ มวธการจดเรยงยงไง โดยทวไปวธ
การจดเรยงจะมดงนคอ— Little Endian— Big Endian
• ตวอยาง มขอมลขนาด 32 Bit (Hex) คอ 12345678 ซงถกเกบในหนวยความจำาทตำาแหนง 184 ในเครองคอมพวเตอร ลกษณะการเกบขอมลสามารถเกบไดสองวธคอ
Byte Order (example)• Address Value (1) Value(2)• 184 12 78• 185 34 56• 186 56 34• 186 78 12 Big Endian Little Endian
Standard…What Standard?• Pentium (80x86), VAX are little-endian• IBM 370, Moterola 680x0 (Mac), and most
RISC are big-endian• Internet is big-endian
—Makes writing Internet programs on PC more awkward!
—WinSock provides htoi and itoh (Host to Internet & Internet to Host) functions to convert