Chapter 2. Instruction-Language of the Computer.ppt
《Chapter 2. Instruction-Language of the Computer.ppt》由会员分享,可在线阅读,更多相关《Chapter 2. Instruction-Language of the Computer.ppt(79页珍藏版)》请在麦多课文档分享上搜索。
1、1,Chapter 2. Instruction: Language of the Computer,2,Five Basic Components,Memory,Processor,Input,Output,Since 1946 all computers have had 5 components!,3,A Translation Hierarchy,High Level Language (HLL) programs first compiled (possibly into assembly), then linked and finally loaded into main memo
2、ry.,4,Machine Language v.s. Assembly Language,In C, we can havea = b+c+d; The MIPS assembly code will look like this:add a, b, cadd a, a, dwhere a, b, c and d are registers The MIPS machine language will look like:000000 00010 00011 00001 00000 100000000000 00001 00100 00001 00000 100000 Comparison
3、The high level language is most efficient and readable The assembly language is less efficient yet readable The machine language directly corresponds to the assembly language, but not readable,5,High Level Language Operators/Operands,Consider C or C+Operators: +, -, *, /, % (mod); (7/4 equals 1, 7%4
4、 equals 3)Operands: Variables: fahr, celsius Constants: 0, 1000, -17, 15.4In most High Level Languages, variables declared and given a type int fahr, celsius; int a, b, c, d, e;,6,Assembly Operators/Instructions,MIPS Assembly Syntax is rigid: for arithmetic operations, 3 variables Why? Directly corr
5、esponds to the machine language and makes hardware simple via regularity Consider the following C statement: a = b + c + d - e; It will be executed as multiple instructions add a, b, c # a = sum of b & c add a, a, d # a = sum of b,c,d sub a, a, e # a = b+c+d-e The right of # is a comment terminated
6、by end of the line. Applies only to current line.,7,Compilation,How to turn the notation that programmers prefer into notation computer understands? Program to translate C statements into Assembly Language instructions; called a compiler Example: compile by hand this C code: a = b + c; d = a - e; Ea
7、sy:add a, b, c sub d, a, e Compile an entire program of 10,000 lines of C code by hand? Big Idea: compiler translates notation from one level of computing abstraction to lower level,8,Compilation 2,Example: compile by hand this C code: f = (g + h) - (i + j); First sum of g and h. Where to put result
8、? add f, g, h # f contains g+h Now sum of i and j. Where to put result? Compiler creates temporary variable to hold sum: t1 add t1, i, j # t1 contains i+j Finally produce difference sub f, f, t1 # f = (g+h)-(i+j),9,Compilation - Summary,C statement (5 operands, 3 operators): f = (g + h) - (i + j); B
9、ecomes 3 assembly instructions (6 unique operands, 3 operators): add f,g,h # f contains g+h add t1,i,j # t1 contains i+j sub f,f,t1 # f=(g+h)-(i+j) In general, each line of C produces many assembly instructions Why people program in C vs. Assembly?,10,Assembly Design: Key Concepts,Assembly language
10、is essentially directly supported in hardware, therefore . It is kept very simple! Limit on the type of operands Limit on the set operations that can be done to absolute minimum. if an operation can be decomposed into a simpler operation, dont include it.,11,MIPS R3000 Instruction Set Architecture (
11、Summary),Instruction Categories Load/Store Computational Jump and Branch Floating Point (coprocessor),3 Instruction Formats: all 32 bits wide,I:,R:,J:,Different Instruction Sets,Compute C = A + B under different instruction sets:,Stack,Accumulator,Push A,Load A,lw R1,A,Push B,Add B,lw R2,B,Add,Store
12、 C,add R3,R1,R2,Pop C,sw C,R3,MIPS,13,Assembly Variables: Registers (1/4),Unlike HLL, assembly cannot use variables Why not? Keep Hardware Simple Assembly Operands are registers limited number of special locations built directly into the hardware operations can only be performed on these! Benefit: S
13、ince registers are directly in hardware, they are very fast Registers are not what we normally call “memory”,14,Assembly Variables: Registers (2/4),32 registers in MIPS Why 32? Smaller is faster Each register can be referred to by number or name Number references: $0, $1, $2, $30, $31 Each MIPS regi
14、ster is 32 bits wide Groups of 32 bits called a word in MIPS Drawback: Since registers are in hardware, there are a predetermined number of them Solution: MIPS code must be very carefully put together to efficiently use registers,15,Assembly Variables: Registers (3/4),By convention, each register al
15、so has a name to make it easier to code For now: $16 - $23 $s0 - $s7 (correspond to C variables) $8 - $15 $t0 - $t7 (correspond to temporary variables) In general, use register names to make your code more readable,16,Comments in Assembly,Another way to make your code more readable: comments! Hash (
16、#) is used for MIPS comments anything from hash mark to end of line is a comment and will be ignored Note: Different from C. C comments have format /* comment */ , so they can span many lines,17,Addition and Subtraction (1/4),Syntax of Instructions: add a, b, c where: add: operation by name a: opera
17、nd getting result (destination) b: 1st operand for operation (source1) c: 2nd operand for operation (source2) Syntax is rigid: 1 operator, 3 operands Why? Keep Hardware simple via regularity,18,Addition and Subtraction (2/4),Addition in Assembly Example (in MIPS): add $s0, $s1, $s2 Equivalent to (in
18、 C): a = b + c where registers $s0, $s1, $s2 are associated with variables a, b, c Subtraction in Assembly Example (in MIPS): sub $s3, $s4, $s5 Equivalent to (in C): d = e - f where registers $s3, $s4, $s5 are associated with variables d, e, f,19,Addition and Subtraction (3/4),How to do the followin
19、g C statement? a = b + c + d - e; Break it into multiple instructions: add $s0, $s1, $s2 # a = b + c add $s0, $s0, $s3 # a = a + d sub $s0, $s0, $s4 # a = a - e,20,Immediates,Immediates are numerical constants. They appear often in code, so there are special instructions for them. Add Immediate: add
20、i $s0, $s1, 10 (in MIPS) f = g + 10 (in C) where registers $s0, $s1 are associated with variables f, g Syntax similar to add instruction, except that last argument is a number instead of a register.,21,Register Zero,One particular immediate, the number zero (0), appears very often in code. So we def
21、ine register zero ($0 or $zero) to always have the value 0. This is defined in hardware, so an instruction like addi $0, $0, 5will not do anything. Use this register, its very handy!,22,Assembly Operands: Memory,C variables map onto registers; what about large data structures like arrays? 1 of 5 com
22、ponents of a computer: memory contains such data structures But MIPS arithmetic instructions only operate on registers, never directly on memory. Data transfer instructions transfer data between registers and memory: Memory to register Register to memory,23,MIPS Addressing Formats (Summary),How memo
23、ry can be addressed in MIPS,24,Data Transfer: Memory to Reg (1/4),To transfer a word of data, we need to specify two things: Register: specify this by number (0 - 31) Memory address: more difficult Think of memory as a single one-dimensional array, so we can address it simply by supplying a pointer
24、to a memory address. Other times, we want to be able to offset from this pointer.,25,Data Transfer: Memory to Reg (2/4),To specify a memory address to copy from, specify two things: A register which contains a pointer to memory A numerical offset (in bytes) The desired memory address is the sum of t
25、hese two values. Example: 8($t0) specifies the memory address pointed to by the value in $t0, plus 8 bytes,26,Data Transfer: Memory to Reg (3/4),Load Instruction Syntax: lw a, 100(b) where lw operation (instruction) name a register that will receive value 100 numerical offset in bytes b register con
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CHAPTER2INSTRUCTIONLANGUAGEOFTHECOMPUTERPPT
