DOC PREVIEW
UCLA COMSCI M151B - Lecture4

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Week 3 - Monday2_3Representing Instructions Instructions are encoded in binary They are called machine code MIPS instructions Encoded as 32-bit instruction words Small number of formats encoding operation code (opcode), register numbers, ... Regularity! Register numbers $t0-$t7 are registers 8-15 $t8-$t9 are registers 24-25 $s0-$s7 are registers 16-23MIPS R-format Instructions Useful for a variety of reasons 32 bit value format: op(6) rs(5) rt(6) rd(shamt) shamt(5) funct(6) Instruction fields: op: operation code (opcode) Number that indicates that it's an R-format instruction More interesting role for other format instructions For R-format, it's all the same Defines what instructions to use rs: first source register number Register source rt: second source register number Second register source rd: destination register number rs, rt, and rd are 5 bits because there are 32 registers and we needto specify properly which register we are referring to shamt: shift amount (00000 for now) Indication of how much to shift Most of R-format, we're not going to use it Also for the rest of the class funct: function code (extends opcode) Indicate what type of operation should be performed by the R-format instruction Add, Subtract, AND or whateverR-format Example add $t0, $s1, $s2 # Destination is first! special $s1 $s2 $t0 0 add 0 17 18 8 0 32 # $s1 is register 17, $s2 is register 18, $t0 is register 8. 000000 10001 10010 01000 00000 100000 00000010001100100100000000100000_2 = 0x02324020MIPS I-format Instructions 32 bit value format: op(6) rs(5) rt(5) constant/address(16) Instruction fields: rs: destination register number rt: destination or source register number constant: -2^15 to 2^15-1 address: offset added to base address in rs Embedded literal/constant Design Principle 4: Good design demands good compromises Different formats complicate decoding, but allow 32-bit instructions uniformly Keep formats as similar as possibleStored Program ComputersInstructions represented in binary, just like data Instructions and data stored in memory Memory is broken down into: Accounting program (machine code) Editor program (machine code) C compiler (machine code) Payroll data Book text Source code in C for editor program Programs can operate on programs Compilers, linkersLogical Operations Instructions for bitwise manipulation Operation C Java MIPS Shift left << << sll Shift right >> >>> srl Bitwise AND & & and, andi Bitwise OR | | or, ori Bitwise NOT ~ ~ nor Useful for extracting and inserting groups of bits in a wordShift Operations shamt: how many positions to the shift Shift left logical Shift left and fill with 0 bits sll by i bits multiplies by 2^i Shift right logical Shift right and fill with 0 bits srl by i bits divides by 2^i (unsigned only) Other than shift logical, there's also shift arithmetic which takes the signbit into account and therefore can be used in signed integersAND Operations Useful to mask bits in a word Select some bits, clear others to 0 and $t0, $t1, $t2 0011 & 1111 = 0011OR Operations Useful to include bits in a word Set some bits to 1, leave others unchanged or $t0, $t1, $t2 0011 | 1101 = 1111NOT Operations Useful to invert bits in a word Change 0 to 1, and 1 to 0 MIPS has NOR 3-operand instruction a NOR b == NOT (a OR b) nor $t0, $t1, zero ~1101 = 00102_4Conditional Operations Instructions that would result from if statements, while, for loop, where the control of the condition is from the data beq Branch equal beq rs, rt, L1 Takes two operands, check if they're equal. If they are, then jump to L1 bneBranch not equal bne rs, rt, L1 Takes two operands, check if they're equal. If they are, then jump to L1 j Jump j L1 Unconditional jump to L1 Program counter track what instructions are currently executing in the processor Instructions will change the PC In sequential code, it will incrementCompiling If Statements C code: if (i == j) f = g+h; else f = g-h; f, g, ... in $s0, $s1, ... Compiled MIPS code: bne $s3, $s4, Else add $s0, $s1, $s2 j Exit Else: sub $s0, $s1, $s2 Exit: ... Instruction is always 1 byte Instructions need 32 bits to be stored Increment address by 32 to get the next 32-bit instructionCompiling Loop Statements C code: while (save[i] == k) i += 1; i in $s3, k in $s5, address of save in $s6 Compiled MIPS code: Loop: sll $t1, $s3, 2 # Multiplying s3 by 4 because save is aninteger array add $t1, $t1, $s6 # Adding the offset to the address of save lw $t0, 0($t1) # Load the value contained in address that t1 is holding bne $t0, $s5, Exit # Checks if value is not equal to k. If yes, jump to Exit addi $s3, $s3, 1 # Increments i by 1 j Loop # Loops Exit: ... Similar to if, but the branches is used for exiting the loopBasic Blocks Sequence of instructions that have no other branches except at the end No targets of branches (no places where a branch might take you) except at the beginning Everything between the start and the end will be executed regardless of control flow and will not execute only if there is an interrupt or an exception A single entry point and a single exit point will allow the compiler to manipulate the code however it wants A compiler identifies basic blocks for optimization An advanced process can accelerate the execution of these basic blocks Hyperblocks More sophisticated uses like predication and combining blocksMore optimizationMore Conditional Operations Set result to 1 if a


View Full Document

UCLA COMSCI M151B - Lecture4

Download Lecture4
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture4 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture4 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?