ed53d4e5a2001f8ff9a20e8971220671.ppt
- Количество слайдов: 47
Feb 25, 2009 Lecture 5 -1 • instruction set architecture (Part II of [Parhami]) • subroutines and functions • implementing recursion – example • more examples of Mini-MIPS programs June 2005 Computer Architecture, Instruction-Set Architecture 1
Example 2: Compute n! for a given input n. First, write the code to implement factorial computation. We need an instruction for multiplication: mul $s 1, $s 2, $s 3 Here we assume that the result will be 32 bits long. (Note that in general the result would be 64 bits long. ) June 2005 Computer Architecture, Instruction-Set Architecture 2
Example 2: Compute n! for a given input n. The following code computes n! where n is in register $t 0 li li $t 1, 0 $t 2, 1 loop: addi mul beq j June 2005 # initialize loop counter # initialize product to 1 $t 1, 1 $t 2, $t 1 $t 0, $t 1, exit loop # # increment loop counter by 1 update value of t 2. if if counter = N, goto exit otherwise loop again Computer Architecture, Instruction-Set Architecture 3
Input through the console In this example, we will enter the input through the console. . text. globl __start: li $v 0, 4 # mesg 1 asking for a number la $a 0, msg 1 syscall li $v 0, 5 # system call that reads an integer syscall move $t 0, $v 0 June 2005 Computer Architecture, Instruction-Set Architecture 4
The program Works fine for n = 12 and computes n! correctly. But for n = 13, we get … June 2005 Computer Architecture, Instruction-Set Architecture 5
Caused by overflow! June 2005 Computer Architecture, Instruction-Set Architecture 6
The problem is caused by overflow 12! = 479001600 is less than 232 = 4294967296 But 13! = 6227020800 > 4294967296 We need an instruction that computes the product of 32 bits correctly. Here is the instruction level support for it in MIPS: mult $s 1, $s 2 # result goes into registers Hi and lo mfhi $t 1 # move the high 32 bits into $t 1 mflo $t 2 # move the low 32 bits into $t 2 June 2005 Computer Architecture, Instruction-Set Architecture 7
Overcoming the overflow problem • Suppose X is a 64 bit integer X = P x 232 + Q • Let Y be a 32 bit integer. • Then, X Y can be obtained as follows: First compute QY = (R x 232 + S) X Y = (P Y + R) 232 + S Check this with an example … June 2005 Computer Architecture, Instruction-Set Architecture 8
Computation of factorial for larger n’s li $t 1, 0 # initialize loop counter li $t 2, 1 li $t 3, 0 li $t 4, 0 loop: addi mult $t 1, 1 # initialize product to 1 # increment loop counter by 1 $t 2, $t 1 mfhi $t 4 mflo $t 2 mul $t 3, $t 1 add $t 3, $t 4 beq $t 0, $t 1, exit # if counter = N, goto exit label j loop # otherwise loop again June 2005 Computer Architecture, Instruction-Set Architecture 9
6 Procedures and Data Finish our study of Mini. MIPS instructions and its data types: • Instructions for procedure call/return, misc. instructions • Procedure parameters and results, utility of stack Topics in This Chapter 6. 1 Simple Procedure Calls 6. 2 Using the Stack for Data Storage 6. 3 Parameters and Results 6. 4 Data Types 6. 5 Arrays and Pointers 6. 6 Additional Instructions June 2005 Computer Architecture, Instruction-Set Architecture 10
6. 1 Simple Procedure Calls Using a procedure involves the following sequence of actions: 1. Put arguments in places known to procedure (reg’s $a 0 -$a 3) 2. Transfer control to procedure, saving the return address (jal) 3. Acquire storage space, if required, for use by the procedure 4. Perform the desired task 5. Put results in places known to calling program (reg’s $v 0 -$v 1) 6. Return control to calling point (jr) Mini. MIPS instructions for procedure call and return from procedure: proc # jump to loc “proc” and link; # “link” means “save the return # address” (PC)+4 in $ra ($31) jr June 2005 jal rs # go to loc addressed by rs Computer Architecture, Instruction-Set Architecture 11
Illustrating a Procedure Call Figure 6. 1 Relationship between the main program and a procedure. June 2005 Computer Architecture, Instruction-Set Architecture 12
A Simple Mini. MIPS Procedure Example 6. 1 Procedure to find the absolute value of an integer. $v 0 |($a 0)| Solution The absolute value of x is –x if x < 0 and x otherwise. abs: sub $v 0, $zero, $a 0 bltz $a 0, done add $v 0, $a 0, $zero done: jr $ra # # # put -($a 0) in $v 0; in case ($a 0) < 0 if ($a 0)<0 then done else put ($a 0) in $v 0 return to calling program In practice, we seldom use such short procedures because of the overhead that they entail. In this example, we have 3 -4 instructions of overhead for 3 instructions of useful computation. June 2005 Computer Architecture, Instruction-Set Architecture 13
Nested Procedure Calls Figure 6. 2 Example of nested procedure calls. June 2005 Computer Architecture, Instruction-Set Architecture 14
6. 2 Using the Stack for Data Storage Figure 6. 4 Effects of push and pop operations on a stack. push: addi sw June 2005 $sp, -4 $t 4, 0($sp) pop: lw addi Computer Architecture, Instruction-Set Architecture $t 5, 0($sp) $sp, 4 15
Memory Map in Mini. MIPS Figure 6. 3 Overview of the memory address space in Mini. MIPS. June 2005 Computer Architecture, Instruction-Set Architecture 16
6. 3 Parameters and Results Stack allows us to pass/return an arbitrary number of values Figure 6. 5 Use of the stack by a procedure. June 2005 Computer Architecture, Instruction-Set Architecture 17
Example of Using the Stack Saving $fp, $ra, and $s 0 onto the stack and restoring them at the end of the procedure proc: sw addi sw sw. . . lw lw addi lw jr June 2005 $fp, -4($sp) $fp, $sp, 0 $sp, – 12 $ra, -8($fp) $s 0, -12($fp) # # # save the old frame pointer save ($sp) into $fp create 3 spaces on top of stack save ($ra) in 2 nd stack element save ($s 0) in top stack element $s 0, -12($fp) $ra, -8($fp) $sp, $fp, 0 $fp, -4($sp) $ra # # # put top stack element in $s 0 put 2 nd stack element in $ra restore $sp to original state restore $fp to original state return from procedure Computer Architecture, Instruction-Set Architecture 18
6. 4 Data Types Data size (number of bits), data type (meaning assigned to bits) Signed integer: Unsigned integer: Floating-point number: Bit string: byte word doubleword Converting from one size to another Type 8 -bit number Value 32 -bit version of the number Unsigned 0010 1011 Unsigned 1010 1011 43 171 0000 0000 0010 1011 0000 0000 1010 1011 Signed 0010 1011 Signed 1010 1011 +43 – 85 0000 0000 0010 1011 1111 1111 1010 1011 June 2005 Computer Architecture, Instruction-Set Architecture 19
ASCII Characters Table 6. 1 ASCII (American standard code for information interchange) 0 1 2 0 1 2 3 4 5 6 7 8 -9 a-f NUL DLE SP 0 @ P ` p More SOH DC 1 ! 1 A Q a q controls STX DC 2 “ 2 B R b r ETX DC 3 # 3 C S c DC 4 $ 4 D T d t ENQ NAK % 5 E U e u ACK SYN & 6 F V f v 7 BEL ETB ‘ 7 G W g w 8 BS CAN ( 8 H X h x 9 HT EM ) 9 I Y i y a LF SUB * : J Z j z b VT ESC + ; K [ k { c FF FS , < L l | d CR GS - = M ] m } e SO RS . > N ^ n ~ f SI US / ? O _ o symbols s EOT More DEL 3 4 5 6 June 2005 Computer Architecture, Instruction-Set Architecture 8 -bit ASCII code (col #, row #)hex e. g. , code for + is (2 b) hex or (0010 1011)two 20
Loading and Storing Bytes can be used to store ASCII characters or small integers. Mini. MIPS addresses refer to bytes, but registers hold words. lb $t 0, 8($s 3) lbu $t 0, 8($s 3) sb $t 0, A($s 3) # # # load rt with mem[8+($s 3)] sign-extend to fill reg load rt with mem[8+($s 3)] zero-extend to fill reg LSB of rt to mem[A+($s 3)] Figure 6. 6 Load and store instructions for byte-size data elements. June 2005 Computer Architecture, Instruction-Set Architecture 21
Meaning of a Word in Memory Figure 6. 7 A 32 -bit word has no inherent meaning and can be interpreted in a number of equally valid ways in the absence of other cues (e. g. , context) for the intended meaning. June 2005 Computer Architecture, Instruction-Set Architecture 22
6. 5 Arrays and Pointers Index: Use a register that holds the index i and increment the register in each step to effect moving from element i of the list to element i + 1 Pointer: Use a register that points to (holds the address of) the list element being examined and update it in each step to point to the next element Figure 6. 8 Stepping through the elements of an array using the indexing method and the pointer updating method. June 2005 Computer Architecture, Instruction-Set Architecture 23
Selection Sort Example 6. 4 To sort a list of numbers, repeatedly perform the following: Find the max element, swap it with the last item, move up the “last” pointer Figure 6. 9 June 2005 One iteration of selection sort. Computer Architecture, Instruction-Set Architecture 24
Selection Sort Using the Procedure select Example 6. 4 (continued) Inputs to proc max In $a 0 In $v 1 In $a 1 Outputs from proc max This is the procedure presented in earlier. (Chapter 5. ) minor changes to make it into a procedure. Recall conventions about register for input parameters and for return values. June 2005 Computer Architecture, Instruction-Set Architecture 25
select procedure # procedure select finds the smallest key among #($a 0). . . ($a 1) and swaps it with $a 0 select: move loop: addi blt lw lw slt bne move j loop done: lw lw sw sw June 2005 jr $t 0, $t 1, $a 1, $t 2, $t 3, $t 4, $t 0, $a 0 $t 1, 4 $t 1, done ($t 0) ($t 1) $t 2, $t 3 $zero, loop $t 1 $t 2, ($a 0) $t 3, ($t 0) $t 2, ($t 0) $t 3, ($a 0) Computer Architecture, Instruction-Set Architecture $ra 26
ssort procedure that calls select # selection sorting procedure ssort: move $s 0, $a 0 loop 1: beq $a 1, $s 0, done 1 addi $sp, -8 sw $ra, ($sp) sw $a 0, 4 ($sp) move $a 0, $s 0 jal select lw $ra, ($sp) lw $a 0, 4 ($sp) addi $sp, 8 addi $s 0, 4 j loop 1 done 1: jr $ra June 2005 # finished sorting! # save $ra # save $a 1 # move $s 0 into $a 0 # restore the stack Computer Architecture, Instruction-Set Architecture 27
Print procedure # procedure print: move $t 0, $a 0 loop 2: blt $a 1, $t 0, exit lw $a 0, ($t 0) li $v 0, 1 syscall la $a 0, space li $v 0, 4 syscall addi $t 0, 4 exit: # if $t 0 > $a 1 exit # print comma # move to the next # array element j loop 2 jr $ra June 2005 Computer Architecture, Instruction-Set Architecture 28
Main program – calling ssort and print . data array: . word 12 11 10 9 8 7 6 5 4 3 2 1 length: . word 12 space: . asciiz " " newline: . asciiz "n" ans 1: . asciiz "The input array is " ans 2: . asciiz "The sorted array is " # text segment. text. globl main June 2005 Computer Architecture, Instruction-Set Architecture 29
Main program – calling ssort and print main: la $a 0, ans 1 li $v 0, 4 syscall la $a 0, array la lw sll addi add jal June 2005 $t 0, $a 1, length ($t 0) $t 0, 2 $t 0, -4 $a 0, $t 0 # print # $a 0 contains the # base address print # $a 1 contains the # address of the last # memory location Computer Architecture, Instruction-Set Architecture 30
Main program – calling ssort and print la $a 0, newline li $v 0, 4 syscall la jal $a 0, array ssort la $a 0, ans 2 li $v 0, 4 syscall la $a 0, array jal print li $v 0, 10 syscall June 2005 # print newline #call ssort # print # $a 0 contains the # base address # done Computer Architecture, Instruction-Set Architecture 31
6. 6 Additional Instructions Mini. MIPS instructions for multiplication and division: mult div $s 0, $s 1 mfhi mflo $t 0 # # # set and set Hi, Lo to ($s 0) ($s 1) Hi to ($s 0)mod($s 1) Lo to ($s 0)/($s 1) $t 0 to (Hi) $t 0 to (Lo) Figure 6. 10 The multiply (mult) and divide (div) instructions of Mini. MIPS. Figure 6. 11 Mini. MIPS instructions for copying the contents of Hi and Lo registers into general registers. June 2005 Computer Architecture, Instruction-Set Architecture 32
Logical Shifts Mini. MIPS instructions for left and right shifting: sll srl sllv srlv $t 0, $s 1, 2 $t 0, $s 1, $s 0 # # $t 0=($s 1) left-shifted by 2 right-shifted by 2 left-shifted by ($s 0) right-shifted by ($s 0) Figure 6. 12 The four logical shift instructions of Mini. MIPS. June 2005 Computer Architecture, Instruction-Set Architecture 33
Unsigned Arithmetic and Miscellaneous Instructions Mini. MIPS instructions for unsigned arithmetic (no overflow exception): addu subu multu divu $t 0, $s 1 $s 0, $s 1 # set $t 0 to ($s 0)+($s 1) # set $t 0 to ($s 0)–($s 1) # set Hi, Lo to ($s 0) ($s 1) # set Hi to ($s 0)mod($s 1) # and Lo to ($s 0)/($s 1) addiu $t 0, $s 0, 61 # set $t 0 to ($s 0)+61; # the immediate operand is # sign extended To make Mini. MIPS more powerful and complete, we introduce later: sra $t 0, $s 1, 2 srav $t 0, $s 1, $s 0 syscall June 2005 # sh. right arith (Sec. 10. 5) # shift right arith variable # system call (Sec. 7. 6) Computer Architecture, Instruction-Set Architecture 34
The 20 Mini. MIPS Instructions Copy from Chapter 6 (40 in all so far) Arithmetic Table 6. 2 (partial) Shift Memory access Control transfer June 2005 Instruction Usage Move from Hi Move from Lo Add unsigned Subtract unsigned Multiply unsigned Divide unsigned Add immediate unsigned Shift left logical Shift right arithmetic Shift left logical variable Shift right arith variable Load byte unsigned Store byte Jump and link System call mfhi rd mflo rd addu rd, rs, rt subu rd, rs, rt multu rs, rt divu rs, rt addiu rs, rt, imm sll rd, rt, sh sra rd, rt, sh sllv rd, rt, rs srlv rt, rd, rs srav rd, rt, rd lb rt, imm(rs) lbu rt, imm(rs) sb rt, imm(rs) jal L syscall Computer Architecture, Instruction-Set Architecture op fn 0 0 0 0 9 0 0 0 32 36 40 3 0 35 16 18 33 35 24 25 26 27 0 2 3 4 6 7 12
Table 6. 2 The 37 + 3 Mini. MIPS Instructions Covered So Far Instruction Usage Load upper immediate Add Subtract Set less than Add immediate Set less than immediate AND OR XOR NOR AND immediate OR immediate XOR immediate Load word Store word Jump register Branch less than 0 Branch equal Branch not equal lui add sub slt addi slti and or xor nor andi ori xori lw sw j jr bltz beq bne Move from Hi Move from Lo Add unsigned Subtract unsigned Multiply unsigned Divide unsigned Add immediate unsigned Shift left logical Shift right arithmetic Shift left logical variable Shift right arith variable Load byte unsigned Store byte Jump and link mfhi mflo addu subu multu divu addiu sll sra sllv srav lb lbu sb jal System call syscall June 2005 rt, imm rd, rs, rt rt, rs, imm rd, rs, rt rd, rs, rt rt, rs, imm rt, imm(rs) L rs rs, L rs, rt, L Computer Architecture, Instruction-Set Architecture rd rd rd, rs, rt rs, rt, imm rd, rt, sh rd, rt, rs rt, imm(rs) L 36
7 Assembly Language Programs Everything else needed to build and run assembly programs: • Supply info to assembler about program and its data • Non-hardware-supported instructions for convenience Topics in This Chapter 7. 1 Machine and Assembly Languages 7. 2 Assembler Directives 7. 3 Pseudoinstructions 7. 4 Macroinstructions 7. 5 Linking and Loading 7. 6 Running Assembler Programs June 2005 Computer Architecture, Instruction-Set Architecture 37
7. 1 Machine and Assembly Languages Figure 7. 1 Steps in transforming an assembly language program to an executable program residing in memory. June 2005 Computer Architecture, Instruction-Set Architecture 38
Symbol Table Figure 7. 2 An assembly-language program, its machine-language version, and the symbol table created during the assembly process. June 2005 Computer Architecture, Instruction-Set Architecture 39
7. 2 Assembler Directives Assembler directives provide the assembler with info on how to translate the program but do not lead to the generation of machine instructions tiny: max: small: big: array: str 1: str 2: . macro. end_macro. text. . data. byte 156, 0 x 7 a. word 35000. float 2 E-3. double 2 E-3. align 2. space 600. ascii “a*b”. asciiz “xyz”. global main June 2005 # # # start macro (see Section 7. 4) end macro (see Section 7. 4) start program’s text segment program text goes here start program’s data segment name & initialize data byte(s) name & initialize data word(s) # name short float (see Chapter # name long float (see Chapter align next item on word boundary reserve 600 bytes = 150 words name & initialize ASCII string null-terminated ASCII string # consider “main” a global name Computer Architecture, Instruction-Set Architecture 40
Composing Simple Assembler Directives Example 7. 1 Write assembler directive to achieve each of the following objectives: a. Put the error message “Warning: The printer is out of paper!” in memory. b. Set up a constant called “size” with the value 4. c. Set up an integer variable called “width” and initialize it to 4. d. Set up a constant called “mill” with the value 1, 000 (one million). e. Reserve space for an integer vector “vect” of length 250. Solution: a. noppr: b. size: c. width: d. mill: e. vect: June 2005 . asciiz “Warning: The printer is out of paper!”. byte 4 # small constant fits in one byte. word 4 # byte could be enough, but. . word 1000000 # constant too large for byte. space 1000 # 250 words = 1000 bytes Computer Architecture, Instruction-Set Architecture 41
7. 3 Pseudoinstructions Example of one-to-one pseudoinstruction: The following not $s 0 # complement ($s 0) is converted to the real instruction: nor $s 0, $zero # complement ($s 0) Example of one-to-several pseudoinstruction: The following abs $t 0, $s 0 # put |($s 0)| into $t 0 is converted to the sequence of real instructions: add slt beq sub June 2005 $t 0, $s 0, $zero $at, $t 0, $zero $at, $zero, +4 $t 0, $zero, $s 0 # # copy x into $t 0 is x negative? if not, skip next instr the result is 0 – x Computer Architecture, Instruction-Set Architecture 42
Mini. MIPS Pseudoinstructions Pseudoinstruction Copy Arithmetic Table 7. 1 Shift Logic Memory access Control transfer June 2005 Usage Move Load address Load immediate Absolute value Negate Multiply (into register) Divide (into register) Remainder Set greater than Set less or equal Set greater or equal Rotate left Rotate right NOT Load doubleword Store doubleword Branch less than Branch greater than Branch less or equal Branch greater or equal move la li abs neg mul div rem sgt sle sge rol ror not ld sd blt bgt ble bge Computer Architecture, Instruction-Set Architecture regd, regs regd, address regd, anyimm regd, regs regd, reg 1, reg 2 regd, reg 1, reg 2 regd, address reg 1, reg 2, L reg 1, reg 2, L 43
7. 4 Macroinstructions A macro is a mechanism to give a name to an oft-used sequence of instructions (shorthand notation) . macro name(args). . end_macro # macro and arguments named # instr’s defining the macro # macro terminator How is a macro different from a pseudoinstruction? Pseudos are predefined, fixed, and look like machine instructions Macros are user-defined and resemble procedures (have arguments) How is a macro different from a procedure? Control is transferred to and returns from a procedure After a macro has been replaced, no trace of it remains June 2005 Computer Architecture, Instruction-Set Architecture 44
7. 5 Linking and Loading The linker has the following responsibilities: Ensuring correct interpretation (resolution) of labels in all modules Determining the placement of text and data segments in memory Evaluating all data addresses and instruction labels Forming an executable program with no unresolved references The loader is in charge of the following: Determining the memory needs of the program from its header Copying text and data from the executable program file into memory Modifying (shifting) addresses, where needed, during copying Placing program parameters onto the stack (as in a procedure call) Initializing all machine registers, including the stack pointer Jumping to a start-up routine that calls the program’s main routine June 2005 Computer Architecture, Instruction-Set Architecture 45
7. 6 Running Assembler Programs Spim is a simulator that can run Mini. MIPS programs The name Spim comes from reversing MIPS Three versions of Spim are available for free downloading: PCSpim for Windows machines xspim for X-windows spim for Unix systems You can download SPIM by visiting: http: //www. cs. wisc. edu/~larus/spim. html June 2005 Computer Architecture, Instruction-Set Architecture 46
Input/Output Conventions for Mini. MIPS Table 7. 2 Input/output and control functions of syscall in PCSpim. ($v 0) Function Arguments Integer in $a 0 Cntl Input Output 1 Print integer Result Integer displayed 2 Print point floating- Float in $f 12 3 Print float double- Double-float Float displayed in Double-float displayed $f 12, $f 13 4 Print string Pointer in $a 0 Null-terminated string displayed 5 Read integer Integer returned in $v 0 6 Read point floating- Float returned in $f 0 7 Read float double- Double-float returned in 8 Read June 2005 $f 0, $f 1 string Pointer in $a 0, length in String Computer Architecture, Instruction-Set Architecture $a 1 returned in buffer 47 at pointer
ed53d4e5a2001f8ff9a20e8971220671.ppt