Instruction set architecturesProgramming and CPUsHigh-level languagesLow-level languagesCompilingAssembly and machine languagesData manipulation instructionsMore data manipulation instructionsRelation to the datapathWhat about RAM?Loading a register from RAMStoring a register to RAMLoading a register with a constantStoring a constant to RAMThe # and ( ) are important!A small exampleControl flow instructionsJumpsBranchesTypes of branchesHigh-level control flowTranslating the C if-then statementTranslating the C for loopSummaryISA 1Instruction set architectures•Last time we built a simple, but complete, datapath.•The datapath is ultimately controlled by a programmer, so today we’ll look at several aspects of this programming in more detail.–How programs are executed on processors–An introduction to instruction set architectures–Example instructions and programs•Next, we’ll see how programs are encoded in a processor. Following that, we’ll finish our processor by designing a control unit, which converts our programs into signals for the datapath.ISA 2Programming and CPUs•Programs written in a high-level language like C++ must be compiled to produce an executable program.•The result is a CPU-specific machine language program. This can be loaded into memory and executed by the processor.•CS231 focuses on stuff below the dotted blue line, but machine language serves as the interface between hardware and software.DatapathHigh-level programExecutable fileControl wordsCompilerControl UnitHardwareSoftwareISA 3High-level languages•High-level languages provide many useful programming constructs.–For, while, and do loops–If-then-else statements–Functions and procedures for code abstraction–Variables and arrays for storage•Many languages provide safety features as well.–Static and dynamic typechecking–Garbage collection•High-level languages are also relatively portable.Theoretically, you can write one program and compile it on many different processors.•It may be hard to understand what’s so “high-level” here, until you compare these languages with...ISA 4Low-level languages•Each CPU has its own low-level instruction set, or machine language, which closely reflects the CPU’s design.•Unfortunately, this means instruction sets are not easy for humans to work with!–Control flow is limited to “jump” and “branch” instructions, which you must use to make your own loops and conditionals.–Support for functions and procedures may be limited.–Memory addresses must be explicitly specified. You can’t just declare new variables and use them!–Very little error checking is provided.–It’s difficult to convert machine language programs to different processors.•Later we’ll look at some rough translations from C to machine language.ISA 5Compiling•Processors can’t execute programs written in high-level languages directly, so a special program called a compiler is needed to translate high-level programs into low-level machine code.•In the “good” old days, people often wrote machine language programs by hand to make their programs faster, smaller, or both.•Now, compilers almost always do a better job than people.–Programs are becoming more complex, and it’s hard for humans to write and maintain large, efficient machine language code.–CPUs are becoming more complex. It’s difficult to write code that takes full advantage of a processor’s features.•Some languages, like Perl or Lisp, are usually interpreted instead of compiled.–Programs are translated into an intermediate format.–This is a “middle ground” between efficiency and portability.ISA 6Assembly and machine languages•Machine language instructions are sequences of bits in a specific order. •To make things simpler, people typically use assembly language.–We assign “mnemonic” names to operations and operands.–There is (almost) a one-to-one correspondence between these mnemonics and machine instructions, so it is very easy to convert assembly programs to machine language.•We’ll use assembly code this today to introduce the basic ideas, and switch to machine language next time.ISA 7Data manipulation instructions•Data manipulation instructions correspond to ALU operations. •For example, here is a possible addition instruction, and its equivalent using our register transfer notation:•This is similar to a high-level programming statement likeR0 = R1 + R2•Here, all of the operands are registers.ADD R0, R1, R2operationdestination sourcesoperandsR0 R1 + R2Register transfer instruction:ISA 8More data manipulation instructions•Here are some other kinds of data manipulation instructions.NOT R0, R1 R0 R1’ADD R3, R3, #1 R3 R3 + 1SUB R1, R2, #5 R1 R2 - 5•Some instructions, like the NOT, have only one operand.•In addition to register operands, constant operands like 1 and 5 are also possible. Constants are denoted with a hash mark in front.ISA 9Relation to the datapath •These instructions reflect the design of our datapath from last week.•There are at most two source operands in each instruction, since our ALU has just two inputs.•The two sources can be two registers, or one register and one constant.•More complex operations likeR0 R1 + R2 - 3must be broken down into several lower-level instructions.•Instructions have just one destination operand, which must be a register.D data Write D address A address B addressA data B dataRegister FileWRDAAA BAA BALUFZNCVFSFSS D1 D0 Q Constant MBISA 10What about RAM?•Recall that our ALU has direct access only to the register file.•RAM contents must be copied to the registers before they can be used as ALU operands.•Similarly, ALU results must go through the registers before they can be stored into memory.•We rely on data movement instructions to transfer data between the RAM and the register file.D data Write D address A address B addressA data B dataRegister FileWRDAAA BAQ D1 D0 S RAM ADRS DATA CS WROUTMW+5VA BALUFZNCVFSFS MDS D1 D0 Q Constant MBISA 11Loading a register from RAM•A load instruction copies data from a RAM address to one of the registers.LD R1,(R3) R1 M[R3] •Remember in our datapath, the RAM address must come from one of the registers—in the example above, R3.•The parentheses help show which register operand holds the memory address.D data Write D address A address B addressA data B dataRegister FileWRDAAA BA
View Full Document