VIEWS: 33 PAGES: 30 CATEGORY: Technology POSTED ON: 1/13/2010 Public Domain
CSC3420 Tutorial 2 More about assembly language Ricky Wong 20 / 22 January 2009 1 Contents Typical Execution Cycle Basic ISA classes What is pseudo-instruction? SPIM 2 Typical execution cycle 3 Basic ISA classes acc (Accumulator) TOS (Top Of Stack) 4 Basic ISA classes 5 Basic ISA classes 6 What is pseudo-instruction? Real instructions are executed by the hardware There must be datapath in our CPU designed for each real instruction There are pseudo-instructions in assembly language which do not have hardware datapaths There does NOT exist any datapaths in our CPU designed for pseudo-instructions. Hardware cannot identify the pseudo- instruction and execute it. Instead the assembler performs mapping. It maps each pseudo- instruction to its corresponding real instructions E.g. move $t0, $t1 => add $t0,$zero,$t1 After the mapping, it makes use of the datapath of the mapped real instructions to achieve the same effect, as the pseudo- instruction meaning. 7 Why we need pseudoinstruction? 1. Convenient 2. Human readable 3. Less instructions as some of them are formed by multiple “real” instructions 8 SPIM A simulator for MIPS R2000/R3000 RISC computers Can read and execute MIPS assembly program Homepage: http://www.cs.wisc.edu/~larus/spim.html References: http://users.ece.gatech.edu/~sudha/2030/temp/spim/spi m-tutorial.html http://www.cs.jcu.edu.au/Subjects/cp2003/1997/tutes/intr oToSpim.soln/introToSpim.soln.html http://lion.ee.ntu.edu.tw/Class/CA_2008S/spim_tut.pdf 9 SPIM Interface Register Your Program Real MIPS Program Stack & Memory SPIM Output 10 Operation & Setting Single-Stepping: Run program an instruction at a time. Breakpoint: Tell SPIM stop before it execute a particular instruction. 11 SPIM code file example.s .text .align 2 .globl main main: addi $t0, $t0, 0 addi $t1, $t1, 1 addi $t2, $t2, 2 addi $t3, $t3, 3 addi $t4, $t4, 4 addi $t5, $t5, 5 jr $ra 12 Syntax .text <addr> The next item are put in the user text segment. If addr is present, the items are stored beginning at address addr. .align n Align the next data on a 2n byte boundary. .globl sym Declare the symbol sym is global 13 Quiz 1 preparation 14 Example 1: Design cost problem: Company A is selling processor X, a 1000 MIPS machine, for $1000. We, company B, want to develop a 1200 MIPS processor Y to compete with processor X. Suppose the development & marketing costs of the processor Y is $20 million. It is reasonable to assume the selling price of processor is proportional to its performance. We would like to make a profit of $5 million from this project. What is the minimum amount of processor Y we need to sell in order to achieve this profit target? 15 Example 1 solution: Unit cost of processor Y = $1000 x (1200 MIPS / 1000 MIPS) = $1200 Number of processors to be sold = (20M+5M)/1200 = 20834. 16 Example 2: Table 1 is the statistics collected for two machines M1 and M2 when application program P is executed. Machine 1 (M1) Machine 2 (M2) Time to execute program P 10 s 9s Number of Instructions Executed 2000 * 106 2300 * 106 Instruction Frequency Type A 65% 55% Type B 25% 25% Type C 10% 20% Cycles per Instruction Type A 2 1 Type B 5 10 Type C 10 15 Table 1: Performance statistics of machines M1 & M2 for P 17 Example 2 Q1: Calculate the MIPS values for machines M1 and M2? MIPS = Millions Instructions Per Second = (# of instructions/106 )/ time M1: MIPS = (2000x106/106)/10 = 200 M2: MIPS = (2300x106/106)/9 = 255.6 18 Example 2 Q2: Calculate the CPIs of machines M1 & M2 for the program P? CPI = Cycles Per Instruction = Weighted average of CPIi (where CPIi is the number of cycles for a specific instruction type i) For M1: CPI = 2x65% + 5x25% + 10x10% = 3.55 For M2: CPI = 1x55% + 10x25% + 15x20% = 6.05 19 Example 2 Q3: Calculate the clock rates of machines M1 & M2? Clock rate = Cycles per second = # of cycles / time = (# of instruction x CPI) /time For M1: clock rate = (2000x106x3.55) / 10 = 710MHz For M2: clock rate = (2300x106 x6.05) / 9 = 1546MHz 20 Example 2 Q3: Alternatively, Clock Rate = # of cycles / time = (# of instructions / time) x (# of cycles / # of instructions) = MIPS x 106 x CPI For M1: clock rate = 200 x 106 x 3.55 = 710MHz For M2: clock rate = 255.6 x 106 x 6.05 = 1546MHz 21 Example 2 Q4: Suppose machine M1 costs $8,000 and M2 costs $12,000, which machine has the better price-performance ratio for the data given in table 1? Explain your reasoning behind your answer. 22 Example 2 Q4 solution: Price-performance ratio = price / performance = price x execution time for the same program P M1: price-performance ratio = 8000x10 = 80000 M2: price-performance ratio = 12000x9 = 108000 As M1 has lower price-performance ratio, it has better price-performance ratio 23 Example 2 Q5: Suppose an alternative implementation of the same instruction set can reduce the number of cycles of M1 A-type instructions in table 1 from 2 to 1 (without changing the clock rate). How much faster is the new machine? 24 Example 2 Q5 solution: New CPI = 1x65% + 5x 25% + 10x10% = 2.9 Speedup = Old # of execution cycles / New # of execution cycles = (Old CPI x # of instructions) / (New CPI x # of instructions) = Old CPI / New CPI = 3.55 / 2.9 = 1.224 25 Example 3 Implement the following formula in MIPS assembly language n n A i 1 2 i 1 A2 j j 0 You can assume that the elements of array A are stored in the memory with starting address constant Aaddr and the element size is 4 bytes. The number n is stored in the memory with address naddr. The result should be stored in memory with address S. 26 Example 3 Result = A[1] – A[0] + A[3] – A[2] + A[5] – A[4] + …… A[2i-1] – A[2i-2] + …… A[2n-1] – A[2n-2] + (– A[2n]) 27 Example 3 solution Label Opcode Operands Comments li $s1, Aaddr # $s1 = the base pointer of Array A lw $s2, naddr # $s2 = n add $s0, $zero, $zero # $s0 = Result addi $t1, $zero, 1 # $t1 = i, i=1 FOR bgt $t1, $s2, ENDFOR # If i>n, exit the for loop lw $s3, 4($s1) # $s3 = A[2*i-1] add $s0, $s0, $s3 # Result = Result + A[2*i-1] lw $s3, 0($s1) # $s3 = A[2*i-2] sub $s0, $s0, $s3 # Result = Result - A[2*i-2] # Update $s1 to point to the next element addi $s1, $s1, 8 which is two elements ahead addi $t1, $t1, 1 # i=i+1 j FOR # jump back to the start of loop ENDFOR lw $s3, 0($s1) # $s3 = A[2*n] sub $s0, $s0, $s3 # Result = Result - A[2*n] sw $s0, S # Store Result back to the memory 28 EXIT # Exit End 29 Reminder - Quiz 1 Quiz 1 has been postponed. It will be held during Tuesday Lecture time on 3-2-2009 It is open book Please remember to bring the textbook , notes and calculators to the quiz 30