Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 Example 2: Compute n! for a given input n. First, write the code to implement factorial computation. We need an instruction for multiplication: mul $s1, $s2, $s3 Here we assume that the result will be 32 bits long. (Note that in general the result would be 64 bits long.) Example 2: Compute n! for a given input n. The following code computes n! where n is in register $t0 li li $t1, 0 $t2, 1 loop: addi mul beq j # initialize loop counter # initialize product to 1 $t1, $t1, 1 $t2, $t2, $t1 $t0, $t1, exit loop # # # # increment loop counter by 1 update value of t2. if if counter = N, goto exit otherwise loop again Input through the console In this example, we will enter the input through the console. .text .globl __start __start: li $v0, 4 # mesg1 asking for a number la $a0, msg1 syscall li $v0,5 # system call that reads an integer syscall move $t0, $v0 The program Works fine for n = 12 and computes n! correctly. But for n = 13, we get … Caused by overflow! 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 $s1, $s2 # result goes into registers Hi and lo mfhi $t1 # move the high 32 bits into $t1 mflo $t2 # move the low 32 bits into $t2 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 … Computation of factorial for larger n’s li $t1, 0 # initialize loop counter li $t2, 1 li $t3, 0 li $t4, 0 loop: addi mult $t1, $t1, 1 # initialize product to 1 # increment loop counter by 1 $t2, $t1 mfhi $t4 mflo $t2 mul $t3, $t3, $t1 add $t3, $t3, $t4 beq $t0, $t1, exit # if counter = N, goto exit label j loop # otherwise loop again 6 Procedures and Data Finish our study of MiniMIPS 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 6.1 Simple Procedure Calls Using a procedure involves the following sequence of actions: 1. 2. 3. 4. 5. 6. Put arguments in places known to procedure (reg’s $a0-$a3) Transfer control to procedure, saving the return address (jal) Acquire storage space, if required, for use by the procedure Perform the desired task Put results in places known to calling program (reg’s $v0-$v1) Return control to calling point (jr) MiniMIPS instructions for procedure call and return from procedure: jal proc # jump to loc “proc” and link; # “link” means “save the return # address” (PC)+4 in $ra ($31) jr rs # go to loc addressed by rs Illustrating a Procedure Call main PC jal proc Prepare to call Prepare to continue proc Save, etc. Restore jr $ra Figure 6.1 Relationship between the main program and a procedure. A Simple MiniMIPS Procedure Example 6.1 Procedure to find the absolute value of an integer. $v0 |($a0)| Solution The absolute value of x is –x if x < 0 and x otherwise. abs: sub $v0,$zero,$a0 bltz $a0,done add $v0,$a0,$zero done: jr $ra # # # # # put -($a0) in $v0; in case ($a0) < 0 if ($a0)<0 then done else put ($a0) in $v0 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. Nested Procedure Calls main PC jal abc Prepare to call Prepare to continue abc Procedure abc Save xyz jal Procedure xyz xyz Restore jr Figure 6.2 $ra Example of nested procedure calls. jr $ra 6.2 Using the Stack for Data Storage sp Push c sp c b a Figure 6.4 push: addi sw b a Pop x sp sp = sp – 4 mem[sp] = c b a x = mem[sp] sp = sp + 4 Effects of push and pop operations on a stack. $sp,$sp,-4 $t4,0($sp) pop: lw addi $t5,0($sp) $sp,$sp,4 Hex address Memory Map in MiniMIPS 00000000 Reserved 1 M words Program Text segment 63 M words 00400000 10000000 Addressable with 16-bit signed offset Static data 10008000 1000ffff Data segment Dynamic data $gp $28 $29 $30 448 M words $sp $fp Stack Stack segment 7ffffffc Second half of address space reserved for memory-mapped I/O Figure 6.3 Overview of the memory address space in MiniMIPS. 6.3 Parameters and Results Stack allows us to pass/return an arbitrary number of values $sp Local variables z y .. . Saved registers Frame for current procedure Old ($fp) $sp c b a .. . $fp Frame for current procedure c b a .. . $fp Before calling Figure 6.5 After calling Use of the stack by a procedure. Frame for previous procedure Example of Using the Stack Saving $fp, $ra, and $s0 onto the stack and restoring them at the end of the procedure proc: sw addi addi sw sw . . . lw lw addi lw jr $fp,-4($sp) $fp,$sp,0 $sp,$sp,–12 $ra,-8($fp) $s0,-12($fp) # # # # # save the old frame pointer save ($sp) into $fp create 3 spaces on top of stack save ($ra) in 2nd stack element save ($s0) in top stack element $s0,-12($fp) $ra,-8($fp) $sp,$fp, 0 $fp,-4($sp) $ra # # # # # put top stack element in $s0 put 2nd stack element in $ra restore $sp to original state restore $fp to original state return from procedure 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 byte byte word word word word doubleword 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 0000 0000 0000 0000 0010 1011 0000 0000 0000 0000 0000 0000 1010 1011 Signed Signed +43 –85 0000 0000 0000 0000 0000 0000 0010 1011 1111 1111 1111 1111 1111 1111 1010 1011 0010 1011 1010 1011 ASCII Characters Table 6.1 ASCII (American standard code for information interchange) 0 0 NUL 1 DLE 2 SP 1 SOH DC1 2 STX 3 3 4 5 6 7 8-9 0 @ P ` p ! 1 A Q a q DC2 “ 2 B R b r ETX DC3 # 3 C S c s 4 EOT DC4 $ 4 D T d t 5 ENQ NAK % 5 E U e u 6 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 DEL a-f More More controls symbols 8-bit ASCII code (col #, row #)hex e.g., code for + is (2b) hex or (0010 1011)two Loading and Storing Bytes Bytes can be used to store ASCII characters or small integers. MiniMIPS addresses refer to bytes, but registers hold words. 31 I lb $t0,8($s3) lbu $t0,8($s3) sb $t0,A($s3) op 25 rs # # # # # 20 rt load rt with mem[8+($s3)] sign-extend to fill reg load rt with mem[8+($s3)] zero-extend to fill reg LSB of rt to mem[A+($s3)] 15 immediate / offset 0 1 0 x x 0 0 1 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 lb = 32 lbu = 36 sb = 40 Figure 6.6 Base register Data register Address offset Load and store instructions for byte-size data elements. Meaning of a Word in Memory Bit pattern (02114020) hex 0000 0010 0001 0001 0100 0000 0010 0000 00000010000100010100000000100000 Add instruction 00000010000100010100000000100000 Positive integer 00000010000100010100000000100000 Four-character string 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. 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 Array index i Add 1 to i; Compute 4i; Add 4i to base Base Array A A[i] A[i + 1] Pointer to A[i] Add 4 to get the address of A[i + 1] Array A A[i] A[i + 1] Figure 6.8 Stepping through the elements of an array using the indexing method and the pointer updating method. 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 A first A first max A first x y last last last Start of iteration Figure 6.9 y x Maximum identified End of iteration One iteration of selection sort. Selection Sort Using the Procedure select Example 6.4 (continued) A A first Inputs to proc max first In $a0 A first max x In $v0 In $v1 In $a1 y Outputs from proc max last last last Start of iteration y x Maximum identified End of iteration 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. select procedure # procedure select finds the smallest key among #($a0) ... ($a1) and swaps it with $a0 select:move move loop:addi blt lw lw slt bne move j loop done: lw lw sw sw jr $t0, $t1, $t1, $a1, $t2, $t3, $t4, $t4, $t0, $a0 $a0 $t1, 4 $t1, done ($t0) ($t1) $t2, $t3 $zero, loop $t1 $t2, $t3, $t2, $t3, $ra ($a0) ($t0) ($t0) ($a0) ssort procedure that calls select # selection sorting procedure ssort: move $s0, $a0 loop1: beq $a1, $s0, done1 addi $sp, $sp, -8 sw $ra, ($sp) sw $a0, 4 ($sp) move $a0, $s0 jal select lw $ra, ($sp) lw $a0, 4 ($sp) addi $sp, $sp, 8 addi $s0, $s0, 4 j loop1 done1: jr $ra # finished sorting! # save $ra # save $a1 # move $s0 into $a0 # restore the stack Print procedure # procedure print print: move $t0, $a0 loop2: blt $a1,$t0,exit lw $a0,($t0) li $v0,1 syscall la $a0, space li $v0,4 syscall addi $t0, $t0, 4 exit: j loop2 jr $ra # if $t0 > $a1 exit # print comma # move to the next # array element 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" ans1: .asciiz "The input array is " ans2: .asciiz "The sorted array is " # text segment .text .globl main Main program – calling ssort and print main: la $a0,ans1 li $v0,4 syscall la $a0, array la lw sll addi add $t0, $t0, $t0, $t0, $a1, length ($t0) $t0, 2 $t0, -4 $a0, $t0 jal print # print # $a0 contains the # base address # $a1 contains the # address of the last # memory location Main program – calling ssort and print la $a0, newline li $v0, 4 syscall la jal $a0, array ssort la $a0,ans2 li $v0,4 syscall la $a0, array jal print li $v0, 10 syscall # print newline #call ssort # print # $a0 contains the # base address # done 6.6 Additional Instructions MiniMIPS instructions for multiplication and division: mult div $s0, $s1 $s0, $s1 mfhi mflo $t0 $t0 31 R op 25 rs 20 rt set set and set set 15 Hi,Lo to ($s0)($s1) Hi to ($s0)mod($s1) Lo to ($s0)/($s1) $t0 to (Hi) $t0 to (Lo) rd 10 sh 5 fn 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 x 0 ALU instruction Figure 6.10 # # # # # Source register 1 Source register 2 Unused Unused mult = 24 div = 26 The multiply (mult) and divide (div) instructions of MiniMIPS. 31 R op 25 rs 20 rt 15 rd 10 sh 5 fn 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 x 0 ALU instruction Unused Unused Destination register Unused mfhi = 16 mflo = 18 Figure 6.11 MiniMIPS instructions for copying the contents of Hi and Lo registers into general registers . Logical Shifts MiniMIPS instructions for left and right shifting: sll srl sllv srlv $t0,$s1,2 $t0,$s1,2 $t0,$s1,$s0 $t0,$s1,$s0 31 R op 25 20 rt 15 left-shifted by 2 right-shifted by 2 left-shifted by ($s0) right-shifted by ($s0) rd 10 sh 5 fn 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 x 0 ALU instruction 31 R rs # $t0=($s1) # $t0=($s1) # $t0=($s1) # $t0=($s1) op Unused 25 rs Source register 20 rt Destination register 15 rd Shift amount 10 sh sll = 0 srl = 2 5 fn 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 x 0 ALU instruction Figure 6.12 Amount register Source register Destination register Unused sllv = 4 srlv = 6 The four logical shift instructions of MiniMIPS. Unsigned Arithmetic and Miscellaneous Instructions MiniMIPS instructions for unsigned arithmetic (no overflow exception): addu subu multu divu $t0,$s0,$s1 $t0,$s0,$s1 $s0,$s1 $s0,$s1 # set $t0 to ($s0)+($s1) # set $t0 to ($s0)–($s1) # set Hi,Lo to ($s0)($s1) # set Hi to ($s0)mod($s1) # and Lo to ($s0)/($s1) addiu $t0,$s0,61 # set $t0 to ($s0)+61; # the immediate operand is # sign extended To make MiniMIPS more powerful and complete, we introduce later: sra $t0,$s1,2 srav $t0,$s1,$s0 syscall # sh. right arith (Sec. 10.5) # shift right arith variable # system call (Sec. 7.6) The 20 MiniMIPS Instructions Copy from Chapter 6 (40 in all so far) Arithmetic Table 6.2 (partial) Shift Memory access Control transfer Instruction Usage Move from Hi Move from Lo Add unsigned Subtract unsigned Multiply Multiply unsigned Divide Divide unsigned Add immediate unsigned Shift left logical Shift right logical Shift right arithmetic Shift left logical variable Shift right logical variable Shift right arith variable Load byte Load byte unsigned Store byte Jump and link System call mfhi mflo addu subu mult multu div divu addiu sll srl sra sllv srlv rd rd rd,rs,rt rd,rs,rt rs,rt rs,rt rs,rt rs,rt rs,rt,imm rd,rt,sh rd,rt,sh rd,rt,sh rd,rt,rs rt,rd,rs srav rd,rt,rd lb rt,imm(rs) lbu rt,imm(rs) sb rt,imm(rs) jal L syscall op fn 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 32 36 40 3 0 16 18 33 35 24 25 26 27 0 2 3 4 6 7 12 Table 6.2 The 37 + 3 MiniMIPS Instructions Covered So Far Instruction Usage 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 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 Multiply unsigned Divide Divide unsigned Add immediate unsigned Shift left logical Shift right logical Shift right arithmetic Shift left logical variable Shift right logical variable Shift right arith variable Load byte Load byte unsigned Store byte Jump and link mfhi mflo addu subu mult multu div divu addiu sll srl sra sllv srlv rd rd rd,rs,rt rd,rs,rt rs,rt rs,rt rs,rt rs,rt rs,rt,imm rd,rt,sh rd,rt,sh rd,rt,sh rd,rt,rs rd,rt,rs srav lb lbu sb jal rd,rt,rs rt,imm(rs) rt,imm(rs) rt,imm(rs) L System call syscall rt,imm rd,rs,rt rd,rs,rt rd,rs,rt rt,rs,imm rd,rs,imm rd,rs,rt rd,rs,rt rd,rs,rt rd,rs,rt rt,rs,imm rt,rs,imm rt,rs,imm rt,imm(rs) rt,imm(rs) L rs rs,L rs,rt,L rs,rt,L 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 7.1 Machine and Assembly Languages $2,$5,$5 $2,$2,$2 $2,$4,$2 $15,0($2) $16,4($2) $16,0($2) $15,4($2) $31 00a51020 00421020 00821020 8c620000 8cf20004 acf20000 ac620004 03e00008 Executable machine language program Loader add add add lw lw sw sw jr Machine language program Linker Assembly language program Assembler MIPS, 80x86, PowerPC, etc. Library routines (machine language) Memory content Figure 7.1 Steps in transforming an assembly language program to an executable program residing in memory. Symbol Table Assembly language program addi sub add test: bne addi add j done: sw Symbol table $s0,$zero,9 $t0,$s0,$s0 $t1,$zero,$zero $t0,$s0,done $t0,$t0,1 $t1,$s0,$zero test $t1,result($gp) done result test 28 248 12 Location 0 4 8 12 16 20 24 28 Machine language program 00100000000100000000000000001001 00000010000100000100000000100010 00000001001000000000000000100000 00010101000100000000000000001100 00100001000010000000000000000001 00000010000000000100100000100000 00001000000000000000000000000011 10101111100010010000000011111000 op rs rt rd sh fn Field boundaries shown to facilitate understanding Determined from assembler directives not shown here Figure 7.2 An assembly-language program, its machine-language version, and the symbol table created during the assembly process. 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: str1: str2: .macro .end_macro .text ... .data .byte 156,0x7a .word 35000 .float 2E-3 .double 2E-3 .align 2 .space 600 .ascii “a*b” .asciiz “xyz” .global main # # # # # # # # # # # # # # 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 12) name long float (see Chapter 12) align next item on word boundary reserve 600 bytes = 150 words name & initialize ASCII string null-terminated ASCII string consider “main” a global name 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,000 (one million). e. Reserve space for an integer vector “vect” of length 250. Solution: a. b. c. d. e. noppr: size: width: mill: vect: .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 7.3 Pseudoinstructions Example of one-to-one pseudoinstruction: The following not $s0 # complement ($s0) is converted to the real instruction: nor $s0,$s0,$zero # complement ($s0) Example of one-to-several pseudoinstruction: The following abs $t0,$s0 # put |($s0)| into $t0 is converted to the sequence of real instructions: add slt beq sub $t0,$s0,$zero $at,$t0,$zero $at,$zero,+4 $t0,$zero,$s0 # # # # copy x into $t0 is x negative? if not, skip next instr the result is 0 – x MiniMIPS Pseudoinstructions Copy Arithmetic Table 7.1 Shift Logic Memory access Control transfer Pseudoinstruction 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 regd,regs regd,address regd,anyimm regd,regs regd,regs regd,reg1,reg2 regd,reg1,reg2 regd,reg1,reg2 regd,reg1,reg2 regd,reg1,reg2 regd,reg1,reg2 regd,reg1,reg2 regd,reg1,reg2 reg regd,address regd,address reg1,reg2,L reg1,reg2,L reg1,reg2,L reg1,reg2,L 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 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 7.6 Running Assembler Programs Spim is a simulator that can run MiniMIPS 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 Input/Output Conventions for MiniMIPS Table 7.2 Input/output and control functions of syscall in PCSpim. Cntl Input Output ($v0) Function Arguments Result 1 Print integer Integer in $a0 Integer displayed 2 Print floating-point Float in $f12 Float displayed 3 Print double-float Double-float in $f12,$f13 Double-float displayed 4 Print string Pointer in $a0 Null-terminated string displayed 5 Read integer Integer returned in $v0 6 Read floating-point Float returned in $f0 7 Read double-float Double-float returned in $f0,$f1 8 Read string Pointer in $a0, length in $a1 String returned in buffer at pointer 9 Allocate memory Number of bytes in $a0 10 Exit from program Pointer to memory block in $v0 Program execution terminated