Tài liệu Bài giảng Introduction to Computing Systems - Chapter 5 The LC-3: Chapter 5The LC-3Instruction Set ArchitectureISA = All of the programmer-visible components and operations of the computermemory organizationaddress space -- how may locations can be addressed?addressibility -- how many bits per location?register sethow many? what size? how are they used?instruction setopcodesdata typesaddressing modesISA provides all information needed for someone that wants towrite a program in machine language (or translate from a high-level language to machine language).2LC-3 Overview: Memory and RegistersMemoryaddress space: 216 locations (16-bit addresses)addressability: 16 bitsRegisterstemporary storage, accessed in a single machine cycleaccessing memory generally takes longer than a single cycleeight general-purpose registers: R0 - R7each 16 bits widehow many bits to uniquely identify a register?other registersnot directly addressable, but used by (and affected by) instructionsPC (program counter), condition codes3LC-3 Overview: Instruction SetOpcodes15 op...
39 trang |
Chia sẻ: honghanh66 | Lượt xem: 1102 | Lượt tải: 0
Bạn đang xem trước 20 trang mẫu tài liệu Bài giảng Introduction to Computing Systems - Chapter 5 The LC-3, để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên
Chapter 5The LC-3Instruction Set ArchitectureISA = All of the programmer-visible components and operations of the computermemory organizationaddress space -- how may locations can be addressed?addressibility -- how many bits per location?register sethow many? what size? how are they used?instruction setopcodesdata typesaddressing modesISA provides all information needed for someone that wants towrite a program in machine language (or translate from a high-level language to machine language).2LC-3 Overview: Memory and RegistersMemoryaddress space: 216 locations (16-bit addresses)addressability: 16 bitsRegisterstemporary storage, accessed in a single machine cycleaccessing memory generally takes longer than a single cycleeight general-purpose registers: R0 - R7each 16 bits widehow many bits to uniquely identify a register?other registersnot directly addressable, but used by (and affected by) instructionsPC (program counter), condition codes3LC-3 Overview: Instruction SetOpcodes15 opcodesOperate instructions: ADD, AND, NOTData movement instructions: LD, LDI, LDR, LEA, ST, STR, STIControl instructions: BR, JSR/JSRR, JMP, RTI, TRAPsome opcodes set/clear condition codes, based on result:N = negative, Z = zero, P = positive (> 0)Data Types16-bit 2’s complement integerAddressing ModesHow is the location of an operand specified?non-memory addresses: immediate, registermemory addresses: PC-relative, indirect, base+offset4Operate InstructionsOnly three operations: ADD, AND, NOTSource and destination operands are registersThese instructions do not reference memory.ADD and AND can use “immediate” mode,where one operand is hard-wired into the instruction.Will show dataflow diagram with each instruction.illustrates when and where data moves to accomplish the desired operation5NOT (Register)Note: Src and Dstcould be the same register.6ADD/AND (Register)this zero means “register mode”7ADD/AND (Immediate)Note: Immediate field issign-extended.this one means “immediate mode”8Using Operate InstructionsWith only ADD, AND, NOTHow do we subtract?How do we OR?How do we copy from one register to another?How do we initialize a register to zero?9Data Movement InstructionsLoad -- read data from memory to registerLD: PC-relative modeLDR: base+offset modeLDI: indirect modeStore -- write data from register to memoryST: PC-relative modeSTR: base+offset modeSTI: indirect modeLoad effective address -- compute address, save in registerLEA: immediate modedoes not access memory10PC-Relative Addressing ModeWant to specify address directly in the instructionBut an address is 16 bits, and so is an instruction!After subtracting 4 bits for opcodeand 3 bits for register, we have 9 bits available for address.Solution:Use the 9 bits as a signed offset from the current PC.9 bits:Can form any address X, such that: Remember that PC is incremented as part of the FETCH phase;This is done before the EVALUATE ADDRESS stage.11LD (PC-Relative)12ST (PC-Relative)13Indirect Addressing ModeWith PC-relative mode, can only address data within 256 words of the instruction.What about the rest of memory? Solution #1: Read address from memory location,then load/store to that address.First address is generated from PC and IR(just like PC-relative addressing), thencontent of that address is used as target for load/store.14LDI (Indirect)15STI (Indirect)16Base + Offset Addressing ModeWith PC-relative mode, can only address data within 256 words of the instruction.What about the rest of memory?Solution #2:Use a register to generate a full 16-bit address.4 bits for opcode, 3 for src/dest register,3 bits for base register -- remaining 6 bits are usedas a signed offset.Offset is sign-extended before adding to base register.17LDR (Base+Offset)18STR (Base+Offset)19Load Effective AddressComputes address like PC-relative (PC plus signed offset) and stores the result into a register.Note: The address is stored in the register, not the contents of the memory location.20LEA (Immediate)21ExampleAddressInstructionCommentsx30F61 1 1 0 0 0 1 1 1 1 1 1 1 1 0 1R1 PC – 3 = x30F4x30F70 0 0 1 0 1 0 0 0 1 1 0 1 1 1 0R2 R1 + 14 = x3102x30F80 0 1 1 0 1 0 1 1 1 1 1 1 0 1 1M[PC - 5] R2M[x30F4] x3102x30F90 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0R2 0x30FA0 0 0 1 0 1 0 0 1 0 1 0 0 1 0 1R2 R2 + 5 = 5x30FB0 1 1 1 0 1 0 0 0 1 0 0 1 1 1 0M[R1+14] R2M[x3102] 5x30FC1 0 1 0 0 1 1 1 1 1 1 1 0 1 1 1R3 M[M[x30F4]]R3 M[x3102]R3 5opcode22Control InstructionsUsed to alter the sequence of instructions(by changing the Program Counter)Conditional Branchbranch is taken if a specified condition is truesigned offset is added to PC to yield new PCelse, the branch is not takenPC is not changed, points to the next sequential instructionUnconditional Branch (or Jump)always changes the PCTRAPchanges PC to the address of an OS “service routine”routine will return control to the next instruction (after TRAP)23Condition CodesLC-3 has three condition code registers: N -- negative Z -- zero P -- positive (greater than zero)Set by any instruction that writes a value to a register(ADD, AND, NOT, LD, LDR, LDI, LEA)Exactly one will be set at all timesBased on the last instruction that altered a register24Branch InstructionBranch specifies one or more condition codes.If the set bit is specified, the branch is taken.PC-relative addressing:target address is made by adding signed offset (IR[8:0])to current PC.Note: PC has already been incremented by FETCH stage.Note: Target must be within 256 words of BR instruction.If the branch is not taken,the next sequential instruction is executed.25BR (PC-Relative)What happens if bits [11:9] are all zero? All one?26Using Branch InstructionsCompute sum of 12 integers.Numbers start at location x3100. Program starts at location x3000.R1 x3100R3 0R2 12R2=0?R4 M[R1]R3 R3+R4R1 R1+1R2 R2-1NOYES27Sample ProgramAddressInstructionCommentsx30001 1 1 0 0 0 1 0 1 1 1 1 1 1 1 1R1 x3100 (PC+0xFF)x30010 1 0 1 0 1 1 0 1 1 1 0 0 0 0 0R3 0x30020 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0R2 0x30030 0 0 1 0 1 0 0 1 0 1 0 1 1 0 0R2 12x30040 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1If Z, goto x300A (PC+5)x30050 1 1 0 1 0 0 0 0 1 0 0 0 0 0 0Load next value to R4x30060 0 0 1 0 1 1 0 1 1 0 0 0 0 0 1Add to R3x30070 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1Increment R1 (pointer)X30080 0 0 1 0 1 0 0 1 0 1 1 1 1 1 1Decrement R2 (counter)x30090 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0Goto x3004 (PC-6)28JMP (Register)Jump is an unconditional branch -- always taken.Target address is the contents of a register.Allows any target address.29TRAPCalls a service routine, identified by 8-bit “trap vector.”When routine is done, PC is set to the instruction following TRAP.(We’ll talk about how this works later.)vectorroutinex23input a character from the keyboardx21output a character to the monitorx25halt the program30Another ExampleCount the occurrences of a character in a fileProgram begins at location x3000Read character from keyboardLoad each character from a “file”File is a sequence of memory locationsStarting address of file is stored in the memory locationimmediately after the programIf file character equals input character, increment counterEnd of file is indicated by a special ASCII value: EOT (x04)At the end, print the number of characters and halt(assume there will be less than 10 occurrences of the character)A special character used to indicate the end of a sequenceis often called a sentinel.Useful when you don’t know ahead of time how many timesto execute a loop.31Flow Chart32Program (1 of 2)AddressInstructionCommentsx30000 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0R2 0 (counter)x30010 0 1 0 0 1 1 0 0 0 0 1 0 0 0 0R3 M[x3102] (ptr)x30021 1 1 1 0 0 0 0 0 0 1 0 0 0 1 1Input to R0 (TRAP x23)x30030 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0R1 M[R3]x30040 0 0 1 1 0 0 0 0 1 1 1 1 1 0 0R4 R1 – 4 (EOT)x30050 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0If Z, goto x300Ex30061 0 0 1 0 0 1 0 0 1 1 1 1 1 1 1R1 NOT R1x30070 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1R1 R1 + 1X30080 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0R1 R1 + R0x30090 0 0 0 1 0 1 0 0 0 0 0 0 0 0 1If N or P, goto x300B33Program (2 of 2)AddressInstructionCommentsx300A0 0 0 1 0 1 0 0 1 0 1 0 0 0 0 1R2 R2 + 1x300B0 0 0 1 0 1 1 0 1 1 1 0 0 0 0 1R3 R3 + 1x300C0 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0R1 M[R3]x300D0 0 0 0 1 1 1 1 1 1 1 1 0 1 1 0Goto x3004x300E0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0R0 M[x3013]x300F0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0R0 R0 + R2x30101 1 1 1 0 0 0 0 0 0 1 0 0 0 0 1Print R0 (TRAP x21)x30111 1 1 1 0 0 0 0 0 0 1 0 0 1 0 1HALT (TRAP x25)X3012Starting Address of Filex30130 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0ASCII x30 (‘0’)34LC-3 Data PathRevisitedFilled arrow = info to be processed.Unfilled arrow = control signal.35Data Path ComponentsGlobal busspecial set of wires that carry a 16-bit signal to many componentsinputs to the bus are “tri-state devices,”that only place a signal on the bus when they are enabledonly one (16-bit) signal should be enabled at any timecontrol unit decides which signal “drives” the busany number of components can read the busregister only captures bus data if it is write-enabled by the control unitMemoryControl and data registers for memory and I/O devicesmemory: MAR, MDR (also control signal for read/write)36Data Path ComponentsALUAccepts inputs from register fileand from sign-extended bits from IR (immediate field).Output goes to bus.used by condition code logic, register file, memoryRegister FileTwo read addresses (SR1, SR2), one write address (DR)Input from busresult of ALU operation or memory readTwo 16-bit outputsused by ALU, PC, memory addressdata for store instructions passes through ALU37Data Path ComponentsPC and PCMUXThree inputs to PC, controlled by PCMUXPC+1 – FETCH stageAddress adder – BR, JMPbus – TRAP (discussed later)MAR and MARMUXTwo inputs to MAR, controlled by MARMUXAddress adder – LD/ST, LDR/STRZero-extended IR[7:0] -- TRAP (discussed later)38Data Path ComponentsCondition Code LogicLooks at value on bus and generates N, Z, P signalsRegisters set only when control unit enables them (LD.CC)only certain instructions set the codes(ADD, AND, NOT, LD, LDI, LDR, LEA)Control Unit – Finite State MachineOn each machine cycle, changes control signals for next phaseof instruction processingwho drives the bus? (GatePC, GateALU, )which registers are write enabled? (LD.IR, LD.REG, )which operation should ALU perform? (ALUK)Logic includes decoder for opcode, etc.39
Các file đính kèm theo tài liệu này:
- pattpatelch05_7946.ppt