Chapter 4 PCSPIM Simulator Register Window shows the

  • Slides: 65
Download presentation
Chapter 4 PCSPIM Simulator Register Window: shows the contents of registers. Text Window: shows

Chapter 4 PCSPIM Simulator Register Window: shows the contents of registers. Text Window: shows the contents of memory location in which the instructions are stored. Data Window: shows the contents of memory location in which the data are stored. Message Window: error messages and other messages. Console: for inputting and outputting data.

This is the program for computing the sum of N integers. See the slide

This is the program for computing the sum of N integers. See the slide of the data s Location [0 x 00400000] [0 x 00400004] Machine Instruction Assembly Instruction 0 x 34020004 ori $2, $0, 4 main: li $v 0, 4 0 x 3 c 041001 lui $4, 4097 [Prompt] (Why 4097? ) la $a 0, Prompt is assigned to address 0 x 10010000(see the next slide). The immediate segment of instruction la(1001) is determined by shifting right the address of promp 16 bits. Note that 4097 is the decimal representation of 0 x 00001001. [0 x 00400008] [0 x 0040000 c] [0 x 00400010] [0 x 00400014] 0 x 0000000 c syscall 0 x 34020005 ori $2, $0, 5 li $v 0, 5 0 x 0000000 c syscall 0 x 1840000 d blez $2 52 [End-0 x 00400014] blez $v 0, End ( Why 52 and 0 x 1840000 d? ) 52 is difference in bytes between the address of End and the current address ( 0 x 00400048 -0 x 00400014= 34 in Hex and 52 in decimal ). The immediate segment of the instruction blez is determined by converting the difference into words( 52/4= 13 in decimal and 000 d in Hex. [0 x 00400018] 0 x 34080000 ori $8, $0, 0 li $t 0, 0 [0 x 0040001 c] 0 x 01024020 add $8, $2 loop: add $t 0, $v 0 [0 x 00400020] 0 x 2042 ffff addi $2, -1 addi $v 0, -1 [0 x 00400 0 x 1440 fffe bne $2, $0, -8 [Loop-0 x 00400024] bnez $v 0, Loop (Why -8 and 0 x 1440 fffe? ) -8 is the difference in bytes between the address of Loop and the current address ( 0 x 0040001 c-0 x 00400024= -8 in decimal. The 16 bits of immediate segment is determined by number of words i. e. -8/4=-2 in decimal and fffe in Hex. Note the negative integers are represented using 2’s complement. [0 x 00400028] 0 x 34020004 ori $2, $0, 4 li $v 0, 4 [

Sum of Integers (cont. ) [0 x 0040002 c] 0 x 3 c 011001

Sum of Integers (cont. ) [0 x 0040002 c] 0 x 3 c 011001 lui $1, 4097 [Result] la $a 0, Result [0 x 00400030] 0 x 34240023 ori $4, $1, 35 [Result] Why? Result is the address of the second string in the data section after the first string which is stored in memory at address 0 x 1001000. Since the first string contains 35 characters in decimal( 0023 in Hex), the data segment of the first instruction contains 1001, exactly the same value for prompt , and the data segment of second instruction contains 0023. Note this macro la is converted into two MIPS instructions. [0 x 00400034] 0 x 0000000 c syscall [0 x 00400038] 0 x 34020001 ori $2, $0, 1 li $v 0, 1 [0 x 0040003 c] 0 x 00082021 addu $4, $0, $8 move $a 0, $t 0 [0 x 0040] 0 x 0000000 c syscall [0 x 00400044] 0 x 0401 ffef bgez $0 -68 [main-0 x 00400044] Why? b main [0 x 00400048] 0 x 34020004 ori $2, $0, 4 End: li $v 0, 4 [0 x 0040004 c] 0 x 3 c 011001 lui $1, 4097 [Bye] Why? la $a 0, Bye [0 x 00400050] 0 x 3424004 e ori $4, $1, 78 [Bye] Why? [0 x 00400054] 0 x 0000000 c syscall [0 x 00400058] 0 x 3402000 a ori $2, $0, 10 li $v 0, 10 [0 x 0040005 c] 0 x 0000000 c syscall

Analyzing the Data Segment. data prompt: . asciiz result: . asciiz bye: . asciiz

Analyzing the Data Segment. data prompt: . asciiz result: . asciiz bye: . asciiz [0 x 10010000] [0 x 10010010] [0 x 10010020] [0 x 10010030] [0 x 10010040] “n Please Input a value for N = ” “ The sum of the integers from 1 to N is ” “ **** Adios Amigo – Have a good day ****” 0 x 2020200 a 0 x 76206120 0 x 20200020 0 x 20656874 0 x 2031206 d a e l P e s 0 x 61656 c 50 0 x 49206573 0 x 7475706 e 0 x 65756 c 61 0 x 726 f 6620 0 x 3 d 204 e 20 0 x 65685420 0 x 6 d 757320 0 x 20666 f 20 0 x 65746 e 69 0 x 73726567 0 x 6 f 726620 0 x 4 e 206 f 74 0 x 20736920 0 x 20200 a 00 This is an example of an addressing structure called Little Indian where the right most byte in a word has the smaller address.

Example of how MIPS represents 32 -bit address using 16 bits Given the following

Example of how MIPS represents 32 -bit address using 16 bits Given the following segment of the MIPS assembly program and its corresponding machine code. Fill in the blanks assuming the message 1 occupies 10 bytes and stored in locations 0 x 12120000 -0 x 12120009 and message 2 follows message 1. Location Machine Code 0 x 00400000 0 x 00400004 0 x 00400008 0 x 0040000 c 0 x 00400010 0 x 00400014 0 x 00400018 0 x 0040001 c 0 x 3402 ------FFFD-------0 x 3 c 04 -------1212 ------0 x 1844 ---0002 -----0 x 3408 ---0002 -----0 x 01024020 0 x 1440 ----FFFC-----0 x 3 c 0 ------1212 -------0 x 3424 ----000 A------- Assembly Program li $v 0, -3 loop: la $a 0, message 1 blez $v 0, next li $t 0, 2 next: add $t 0, $v 0 bnez $v 0, loop la $a 0, messge 2

Translating Assembly Language to Machine language Use the information in Appendix C to verify

Translating Assembly Language to Machine language Use the information in Appendix C to verify that 0 x 3402000 A is the correct machine language encoding of the instruction ori $2, $0, 10 li $v 0, 10 In Appendix C we are shown how this instruction is encoded in binary ori Rt, Rs, Imm # RF[Rt] = RF[Rs] OR Imm Op-Code Rs Rt Imm 001101 ssssstttttiiiiiiii 00110100000000000001010 0 x 3 4 0 2 0 0 0 A

Translating Assembly Language to Machine language R-Type Instruction Use the information in Appendix C

Translating Assembly Language to Machine language R-Type Instruction Use the information in Appendix C to verify that 0 x 01024020 is the correct machine language encoding of the instruction add $8, $2 add $t 0, $v 0 In Appendix C we are shown how this instruction is encoded in binary add Rd, Rs, Rt # RF[Rd] = RF[Rs] + RF[Rt] Op-Code Rs Rt Rd Function Code 000000 ssssstttttddddd 000001000000010010000100000 0 x 0 1 0 2 4 0 2 0

Exercise 4. 1 OPTIONAL Translate the following assembly language instructions to their corresponding machine

Exercise 4. 1 OPTIONAL Translate the following assembly language instructions to their corresponding machine language codes as they would be represented in hexadecimal. (Hint – Refer to Appendix C and Appendix D. ) loop: addu $a 0, $t 0 ori $v 0, $0, 4 syscall addi $t 0, -1 bnez $t 0, loop andi $s 0, $s 7, 0 xffc 0 or $a 0, $t 7, $s 0 sb $a 0, 4($s 6) srl $s 7, 4 # # # # # 0 x 00082021 0 x 34020004 0 x 0000000 C 0 x 2108 FFFF 0 x 1500 FFFB 0 x 32 F 0 FFC 0 0 x 01 F 02025 0 x. A 2 C 40004 0 x 0017 B 902

Translating Assembly Language “addi” to Machine Language. OPTIONAL In Appendix C we are shown

Translating Assembly Language “addi” to Machine Language. OPTIONAL In Appendix C we are shown how addi is encoded in binary addi Rt, Rs, Imm #RF[Rt] = RF[Rs] + se Imm (see means segment) Op-Code Rs Rt Imm 001000 ssssstttttiiiiiiii addi $8, -1 Note $t 0=$8 addi $t 0, -1 0010000100011111111 0 x 2 1 0 8 f f

Translating Assembly Language “Store Byte” to Machine Language OPTIONAL In Appendix C we are

Translating Assembly Language “Store Byte” to Machine Language OPTIONAL In Appendix C we are shown how Store Byte is encoded in binary sb Rt, offset(Rs) # Mem[RF[Rs] + Offset] = RF[Rt]. The 16 bit Offset is sign extended and added to RF[Rs] to form effective address. The least significant byte in RF[Rt] is stored in memory at the effective address. Op-Code Rs Rt Offset 101000 ssssstttttiiiiiiii sb $4, 4($22) sb $a 0, 4($s 6) 1010001011000000000100 0 x. A 2 C 4 0 0 0 4

Translating Assembly Language “Shift” Instruction to Machine Language OPTIONAL In Appendix C we are

Translating Assembly Language “Shift” Instruction to Machine Language OPTIONAL In Appendix C we are shown how Shift Right Logical is encoded in binary srl Rd, Rt, sa # Rd = Rt >> sa Op-Code Rt Rd sa code 000000 tttttdddddvvvvv 000010 srl $23, 4 srl $s 7, 4 0000001011100100000010 0 x 0 0 1 7 B 9 0 2

PCSpim Translation [0 x 00082021 [0 x 34020004 [0 x 0000000 c [0 x

PCSpim Translation [0 x 00082021 [0 x 34020004 [0 x 0000000 c [0 x 2108 ffff [0 x 1500 fffc [0 x 32 f 0 ffc 0 [0 x 01 f 02025 [0 xa 2 c 40004 [0 x 0017 b 902 addu $4, $0, $8 addu $a 0, $t 0 ori $2, $0, 4 ori $v 0, $0, 4 syscall addi $8, -1 addi $t 0, -1 bne $8, $0, -16 [main-0 x 00400030] bnez $t 0, main andi $16, $23, -64 $s 0, $s 7, 0 xffc 0 or $4, $15, $16 or $a 0, $t 7, $s 0 sb $4, 4($22) sb $a 0, 4($s 6) srl $23, 4 srl $S 7, $s 7, 4

Exercises 4. 2 OPTIONAL What is the character string corresponding to the following ASCII

Exercises 4. 2 OPTIONAL What is the character string corresponding to the following ASCII codes? Remember that for simulations running on Intel–based platforms, the characters are stored in reverse order within each word. ) 0 x 2 a 2 a 0 x 69644120 0 x 4120736 f 0 x 6 f 67696 d 0 x 48202 d 20 0 x 20657661 **** i d A A s o ogim H- eva **** Adios Amigo – Have

Chapter 5 Efficient Algorithm Development

Chapter 5 Efficient Algorithm Development

Logical operators and $Rd, $Rs, $Rt $Rd $Rs & $Rt or $Rd, $Rs, $Rt

Logical operators and $Rd, $Rs, $Rt $Rd $Rs & $Rt or $Rd, $Rs, $Rt $Rd $Rs | $Rt xor $Rd, $Rs, $Rt $Rd $Rs xor $Rt nor $Rd, $Rs, $Rt (and immediate) $Rd $Rs nor $Rt andi $Rd, $Rs, imm $Rd $Rs & sign extended (imm) ori $Rd, $Rs, $Rt $Rd $Rs | sign extended (imm) xori $Rd, $Rs, imm $Rd $Rs xor sign extended (imm) not $Rd, $Rs $Rd bitwise complement ($Rs)

Shift operators • Shift logical srl $Rd, $Rt, sa The contents (including the sign

Shift operators • Shift logical srl $Rd, $Rt, sa The contents (including the sign bit or the left most bit of 2’s complement) of Register $Rt are shifted right sa bits, inserting zeros into the highorder bits. the result is stored in Register $Rd. sll $Rd, $Rt, sa The contents (including the sign bit or the left most bit of 2’s complement) of Register $Rt are shifted left sa bits, inserting zeros into the loworder bits. the result is stored in Register $Rd.

Shift operators (cont. ) · Can the shift right logical operator (srl) change the

Shift operators (cont. ) · Can the shift right logical operator (srl) change the signs of integers? Show some examples. · sra $Rd, $Rt, sa: Shift right arithmetic (sra) does not change the sign because instead of inserting zeros into the high-order bits, it inserts a sequence of the sign bit, i. e. it extends the sign bit. · Shift left arithmetic is not available.

Shift with Rotation • ror Rd, Rs, sa: rotate the register Rs to the

Shift with Rotation • ror Rd, Rs, sa: rotate the register Rs to the right sa bits and store the result in Rd. • rol Rd, Rs, sa: rotate the register Rs to the left sa bits and store the result in Rd. • ror Rd, Rs, Rt: rotate the register Rs to the right n=content(Rt) bits and store the result in Rd. • rol Rd, Rs, Rt: rotate the register Rs to the left n=content(Rt) bits and store the result in Rd.

Function · Usually we use the four Registers $a 0 -$a 3 to pass

Function · Usually we use the four Registers $a 0 -$a 3 to pass parameters (arguments) to functions and the two Registers $v 0 and $v 1 for storing the return values from functions. · Every function is identified by a name which represents the address of the first instruction of function. · The main program or another function call a specific function by placing the arguments in the Registers $a 0 -$a 1 and then using the instruction jal function’s name This instruction stores the next instruction in the main program (contents of PC) in the Register $ra and then branches to the function. · For this reason, every function definition is terminated with instruction jr $ra which loads the PC with the address stored in $ra. In this way the CPU resumes executing the main program. · The next slide shows an example of program which reads integers in an array and uses function to compute the sum of the positive integers and the sum of the negative integers.

Example with Function. data. word. asciiz. globl. text array: msg 1: msg 2: -4,

Example with Function. data. word. asciiz. globl. text array: msg 1: msg 2: -4, 5, 8, -1 "n The sum of the positive values = " "n The sum of the negative values = " main: li la syscall la li $v 0, 4 # system call code for print_str $a 0, msg 1 # load address of msg 1. into $a 0 # print the string $a 0, array # Initialize address Parameter $a 1, 4 # Initialize length Parameter jal sum move li syscall li la syscall li move syscall li syscall $a 0, $v 0, 1 # Call sum # move value to be printed to $a 0 # system call code for print_int # print sum of positive values $v 0, 4 # system call code for print_str $a 0, msg 2 # load address of msg 2. into $a 0 # print the string $v 0, 1 # system call code for print_int $a 0, $v 1 # move value to be printed to $a 0 # print sum of negative values $v 0, 10 # terminate program run and # return control to system

Example (cont. ) sum: li $v 0, 0 #$v 0 will hold the sum

Example (cont. ) sum: li $v 0, 0 #$v 0 will hold the sum of the positives li $v 1, 0 #$v 1 will hold the sum of the negatives blez addi lw addi $a 1, retzz $a 1, -1 $t 0, 0($a 0) $a 0, 4 # If (a 1 <= 0) Branch to Return # Decrement loop count # Get a value from the array # Increment array pointer to next bltz $t 0, negg # If value is negative Branch to negg add b $v 0, $t 0 loop # Add to the positive sum # Branch to loop add b jr $v 1, $t 0 loop $ra # Add to the negative sum # Branch to loop # Return loop: word negg: retzz:

Exercise • Write a program that reads, stores, and prints an array of any

Exercise • Write a program that reads, stores, and prints an array of any number of integers.

Performance Evaluation Time and Space Tradeoff The Figure of Merit (FM) we will use

Performance Evaluation Time and Space Tradeoff The Figure of Merit (FM) we will use is: Total clock cycles required to execute the code. Total number of memory locations required to store the code. Assume: • Multiplication requires 32 clock cycles • Division requires 38 clock cycles

Exercise 5. 1 Write a MIPS assembly language program to find the Sum of

Exercise 5. 1 Write a MIPS assembly language program to find the Sum of the first 100 words of data in the memory data segment with the label “pomona”. Store the resulting sum in the next memory location beyond the end of the array pomona.

Exercise 5. 1 (Pseudo Code) $a 0 = &pomona; # “&” means “Address of”

Exercise 5. 1 (Pseudo Code) $a 0 = &pomona; # “&” means “Address of” $t 0 = 0; For ($t 1= 100; $t 1 > 0; $t 1= $t 1 - 1) { $t 0 = $t 0 + Mem($a 0); $a 0 = $a 0 + 4; } Mem($a 0) = $t 0;

Exercise 5. 1 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2 .

Exercise 5. 1 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2 . data pomona: . space result: . word. globl. text main: la li li loop: lw addi bgtz sw li syscall Comments 400 0 main $a 0, pomona $t 0, 0 $t 1, 100 # Load address pointer # Clear sum # Initialize loop count $t 2, 0($a 0) $t 0, $t 2 $a 0, 4 $t 1, -1 $t 1, loop $t 0, 0($a 0) $v 0, 10 # $t 2 = Mem(a 0) # $t 0 = $t 0 + $t 2 # Inc. address pointer # Dec. loop count # if ($t 1 > 0) branch # Store the result # End of program

Exercise 5. 2 Write an efficient segment of MIPS assembly language code to transfer

Exercise 5. 2 Write an efficient segment of MIPS assembly language code to transfer a block of 100 words starting at memory location “SRC” to another area of memory beginning at memory location “DEST”.

Exercise 5. 2 (Pseudo Code) $a 1= &SRC; # “&” means “Address of” $a

Exercise 5. 2 (Pseudo Code) $a 1= &SRC; # “&” means “Address of” $a 2= &DEST; for ($t 0 = 100; $t 0 > 0; $t 0 =$t 0 -1) {$t 1 = Mem($a 1); Mem($a 2) = $t 1; $a 1= $a 1 + 4; $a 2= $a 2 + 4; }

Exercise 5. 2 (MIPS Assembly Language) Label SRC: DEST: Op-Code Dest. S 1, S

Exercise 5. 2 (MIPS Assembly Language) Label SRC: DEST: Op-Code Dest. S 1, S 2. data. space 400. globl main. text Comments la la li lw sw addi bgtz li syscall # $a 1 = &SRC # $a 2 = &DEST # $t 0 = 100 # $t 1= Mem($a 1) # Mem($a 2) = $t 1 # $a 1 = $a 1+4 # $a 2 = $a 2+4 # $t 0 = $t 0 - 1 #Branch if $t 0 > 0 main: loop: $a 1, SRC $a 2, DEST $t 0, 100 $t 1, 0($a 1) $t 1, 0($a 2) $a 1, 4 $a 2, 4 $t 0, -1 $t 0, loop $v 0, 10

Exercise 5. 3 Write a MIPS function which accepts an integer word in register

Exercise 5. 3 Write a MIPS function which accepts an integer word in register $a 0 and returns its absolute value in $a 0. Also show an example code segment that calls the ABS function twice, to test the function.

Exercise 5. 3 (Pseudo Code) Function ABS($a 0); if ($a 0 < 0) $a

Exercise 5. 3 (Pseudo Code) Function ABS($a 0); if ($a 0 < 0) $a 0 = $0 - $a 0; return;

Exercise 5. 3 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2 Comments

Exercise 5. 3 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2 Comments . text ABS: bgez $a 0, return # If ($a 0 >= 0) go to return sub $a 0, $a 0 # $a 0 = 0 - $a 0 (negate $a 0) return: jr $ra # Return ####################. globl main. text main: li $a 0, -9876 jal ABS li $v 0, 1 # Output result syscall li $a 0, 9876 jal ABS li $v 0, 1 # Output result syscall li $v 0, 10 # End of program syscall

Exercise 5. 4 Write a function PENO (&X, N, SP, SN) that will find

Exercise 5. 4 Write a function PENO (&X, N, SP, SN) that will find the sum of the positive even values and the negative odd values in an array X of length “N”. "X" the address of an array, passed through $a 0. "N" is the length of the array, passed through $a 1. The procedure should return two values: (1) The sum of all the positive even elements in the array, passed back through $v 0. (2) The sum of all the negative odd elements in the array, passed back through $v 1.

Exercise 5. 4 (Pseudo Code) $v 0 = 0; $v 1 = 0; for

Exercise 5. 4 (Pseudo Code) $v 0 = 0; $v 1 = 0; for ( ; $a 1 > 0; $a 1 = $a 1 -1) {$t 0 = Mem($a 0); $t 1 = $t 0 & 1; $a 0 = $a 0 + 4; if ($t 0 >= 0 & $t 1 = 0) $v 0 = $v 0 + $t 0; if ($t 0 < 0 & $t 1 != 0) $v 1= $v 1+ $t 0; } return;

Exercise 5. 4 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2. globl

Exercise 5. 4 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2. globl SUM. text Comments li li $v 0, 0 $v 1, 0 # Initialize summing registers lw andi addi bltz bnez add b $t 0, 0($a 0) $t 2, $t 0, 1 $a 0, 4 $t 0, NEG $t 2, CHK $v 0, $t 0 CHK # Get a value from the array # Extract LSB # Increment array pointer # If the value is negative go to NEG # If the value is odd go to CHK # Add value to positive even sum # Branch to CHK beqz add $t 2, CHK $v 1, $t 0 # If value is even go to CHK # Add value to negative odd sum addi bgtz jr $a 1, -1 $a 1, LOOP $ra # Decrement loop counter # If loop counter > 0 go to Loop # Return PENO: LOOP: NEG: CHK:

Exercise 5. 5 Write a function SUM(N) to find the sum of the integers

Exercise 5. 5 Write a function SUM(N) to find the sum of the integers from 1 to N, making use the multiplication and shifting operations. The value N will be passed to the procedure in $a 0 and the result will be returned in the $v 0 register. Write a MIPS assembly language main program that will call the Sum function five times each time passing a different value to the function for N, and printing the results. The values for N are defined below: . data N: . word 9, 10, 32666, 32777, 654321

Exercise 5. 5 (Pseudo Code) Function SUM (a 0: input value, $v 0: output

Exercise 5. 5 (Pseudo Code) Function SUM (a 0: input value, $v 0: output value) $v 0 = $a 0 + 1; $v 0 = $v 0 * $a 0; $v 0 = $v 0 >> 1 return; #$v 0 / 2;

Exercise 5. 5 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2 Comments

Exercise 5. 5 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2 Comments . text SUM: addi mult mflo sra jr $v 0, $a 0, 1 $v 0, $a 0 $v 0, 1 $ra # $v 0 = $a 0 + 1 # $v 0 = $v 0 * $a 0 # Shift right arithmetic # is the quick way to # divide by 2

The Main Program N: main: . data. word. text li la 9, 10, 32666,

The Main Program N: main: . data. word. text li la 9, 10, 32666, 32777, 654321 $s 0, 5 $s 1, N loop: lw addiu jal move li syscall addi bnez li syscall $a 0, 0($s 1) $s 1, 4 SUM $a 0, $v 0, 1 $s 0, -1 $s 0, loop $v 0, 10

Exercise 5. 6 Write a function FIB(N, &array) to store the First N elements

Exercise 5. 6 Write a function FIB(N, &array) to store the First N elements of the Fibonacci sequence into an array in memory. The value N is passed in $a 0, and the address of the array is passed in register $a 1. The first few numbers of the Fibonacci sequence are: 1, 1, 2, 3, 5, 8, 13, . . .

Exercise 5. 6 (Pseudo Code) Mem($a 1) = 1; Mem($a 1 + 4) =

Exercise 5. 6 (Pseudo Code) Mem($a 1) = 1; Mem($a 1 + 4) = 1; for ($a 0 = $a 0 - 2; $a 0 > 0; $a 0 = $a 0 -1) { Mem($a 1+8) = Mem($a 1) + Mem($a 1+4); $a 1 = $a 1 + 4; } return;

Exercise 5. 6 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2 fib:

Exercise 5. 6 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2 fib: li sw sw addi $t 0, 1 $t 0, 0($a 1) $t 0, 4($a 1) $a 0, -2 lw lw add sw addi bgtz jr $t 0, 0($a 1) $t 1, 4($a 1) $t 0, $t 1 $t 0, 8($a 1) $a 1, 4 $a 0, -1 $a 0, loop $ra loop: Comments

Exercise 5. 7 Write a function that receives 3 integer words in registers $a

Exercise 5. 7 Write a function that receives 3 integer words in registers $a 0, $a 1, & $a 2, and returns them in ordered form with the minimum value in $a 0 and the maximum value in $a 2.

Exercise 5. 7 (Pseudo Code) Function Order($a 0, $a 1, $a 2); If ($a

Exercise 5. 7 (Pseudo Code) Function Order($a 0, $a 1, $a 2); If ($a 0 > $a 1) exchange $a 0 and $a 1; if ($a 1 > $a 2) exchange $a 1 and $a 2 else return; If ($a 0 > $a 1) exchange $a 0 and $a 1; return; .

Exercise 5. 7 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2. text

Exercise 5. 7 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2. text order: ble move $a 0, $a 1, next $t 0, $a 1, $a 0, $t 0 ble move $a 1, $a 2, done $t 0, $a 2, $a 1, $t 0 ble move jr $a 0, $a 1, done $t 0, $a 1, $a 0, $t 0 $ra next: done: Comments

Exercise 5. 8 Write the complete assembly language program, including data declarations, that corresponds

Exercise 5. 8 Write the complete assembly language program, including data declarations, that corresponds to the following C code fragment. Make use of the fact that multiplication and division by powers of 2 can be performed most efficiently by shifting. int main() { int K, Y ; int Z[50] ; Y = 56 ; K = 20 ; Z[K] = Y - 16 * ( K / 4 + 210) ; }

Exercise 5. 8 (MIPS Assembly Language) Label K: Y: Z: main: Op-Code Dest. S

Exercise 5. 8 (MIPS Assembly Language) Label K: Y: Z: main: Op-Code Dest. S 1, S 2. globl main. data. space 4. space 200. text la t 3, K li $t 0, 56 sw $t 0, 4($t 3) li $t 1, 20 sw $t 1, 0($t 3) sra $t 1, 2 addi $t 1, 210 sll $t 1, 4 sub $t 2, $t 0, $t 1 lw $t 1, 0($t 3) sll $t 1, 2 addu $t 1, $t 3 sw $t 2, 8($t 1) Comments # Y= 56 # K= 20 # K/4 + 210 # --- x 16 # t 2= Y – 16 * (K / 4 + 210) # t 1=K # scale # t 1= & Z[k] - 8 # Z[K]= Y-16*(k/4+210)

Exercise 5. 9 Write a function to search through an array “X” of “N”

Exercise 5. 9 Write a function to search through an array “X” of “N” words to find the minimum and maximum values. The address of the array will be passed to the function using register $a 0, and the number of words in the array will be passed in register $a 1. The minimum and maximum values are returned in registers $v 0, & $v 1.

Exercise 5. 9 (Pseudo Code) Max. Min ($a 0: address, $a 1: number of

Exercise 5. 9 (Pseudo Code) Max. Min ($a 0: address, $a 1: number of words) $v 0 = Mem($a 0); $v 1 = $v 0; $a 1 = $a 1 - 1; While ($a 1 > 0) {$a 0 = $a 0 + 4; $t 0 = Mem($a 0); if ($t 0 < $v 0) $v 0 = $t 0; else if ($t 0 > $v 1) $v 1 = $t 0; $a 1= $a 1 - 1; } return;

Exercise 5. 9 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2 Max.

Exercise 5. 9 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2 Max. Min: lw move addi blez $v 0, 0($a 0) $v 1, $v 0 $a 1, -1 $a 1, ret addi lw bge move b $a 0, 4 $t 0, 0($a 0) $t 0, $v 0, next $v 0, $t 0 chk ble move $t 0, $v 1, chk $v 1, $t 0 addi bgtz $a 1, -1 $a 1, loop jr $ra loop: next: chk: ret: Comments

Exercise 5. 10 Write a function to find the sum of the main diagonal

Exercise 5. 10 Write a function to find the sum of the main diagonal elements in a two dimensional N by N array of 32 bit words. The address of the array and the size N are passed to the function in registers $a 0 and $a 1 respectively. The result is returned in $v 0. The values in registers $a 0 and $a 1 should not be modified by this function. Calculate the number of clock cycles required to execute your algorithm, assuming N=4

Exercise 5. 10 (Pseudocode) $v 0 = Mem($a 0); $t 1 = $a 0;

Exercise 5. 10 (Pseudocode) $v 0 = Mem($a 0); $t 1 = $a 0; $t 3=($a 1+1) * 4; for ($t 0 = $a 1 -1; $t 0 > 0, $t 0= $t 0 -1) {$t 1= $t 1+ $t 3; $v 0= $v 0 + Mem($t 1) } return

Exercise 5. 10 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2 Comments

Exercise 5. 10 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2 Comments . text mdsum: lw move addi sll addi blez loop: add lw addi bgtz return: jr $v 0, 0($a 0) # v 0 = first element $t 1, $a 0 $t 3, $a 1, 1 # compute offset $t 3, 2 # multiply by 4 $t 0, $a 1, -1 # init. loop count $t 0, return $t 1, $t 3 # calc. next address $t 2, 0($t 1) # t 2=Mem(t 1) $v 0, $t 2 # add to sum $t 0, -1 # decrement loop count $t 0, loop $ra

Exercise 5. 11 Write a function to find the determinant of a two by

Exercise 5. 11 Write a function to find the determinant of a two by two matrix (array). The address of the array is passed to the function in registers $a 0 and the result is returned in $v 0. The value in register $a 0 should not be modified by this function. Calculate the number of clock ticks required to execute your algorithm.

Exercise 5. 11 (Pseudocode) $v 0 = Mem($a 0) * Mem($a 0+12) Mem($a 0+4)

Exercise 5. 11 (Pseudocode) $v 0 = Mem($a 0) * Mem($a 0+12) Mem($a 0+4) * Mem($a 0+8); Return

Exercise 5. 11 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2 .

Exercise 5. 11 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2 . globl. text determ 2: lw lw mult mflo sub return: jr determ 2 $t 0, 0($a 0) $t 1, 12($a 0) $t 1, $t 0 $v 0 $t 0, 4($a 0) $t 1, 8($a 0) $t 1, $t 0 $v 0, $t 0 $ra Comments

Exercise 5. 12 Write a MIPS assembly language function that accepts a binary number

Exercise 5. 12 Write a MIPS assembly language function that accepts a binary number in register $a 0 and returns a value corresponding to the number of one’s in the binary number.

Exercise 5. 12(Pseudocode) $v 0 = 0; while ($a 0 = !0) { $t

Exercise 5. 12(Pseudocode) $v 0 = 0; while ($a 0 = !0) { $t 0 = $a 0 & 1; $a 0 = $a 0 >> 1; $v 0 = $v 0 + $t 0; } Return

Exercise 5. 12 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2 Comments

Exercise 5. 12 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2 Comments . globl count. text count: li $v 0, 0 while: andi $t 0, $a 0, 1 srl $a 0, 1 add $v 0, $t 0 bnez $a 0, while jr $ra

Exercise 5. 13 Translate the following pseudocode expression to MIPS assembly language code. Include

Exercise 5. 13 Translate the following pseudocode expression to MIPS assembly language code. Include code to insure that there is no array bounds violation when the store word (sw) instruction is executed. Note that the array “zap” is an array containing 50 words, thus the value in register $a 0 must be in the range from 0 to 196. Include code to insure that the value in register $a 0 is a word address offset into the array “zap. ” If an array bounds violation is detected or the value in register $a 0 is not a word address offset then branch to the label “Error. ” zap: . data. space 200. text. . . zap[$a 0] = $s 0

Exercise 5. 13 (Pseudocode) $t 0 = $a 0 & 3; If ($t 0

Exercise 5. 13 (Pseudocode) $t 0 = $a 0 & 3; If ($t 0 != 0 ) go to Error; if ($a 0 < 0) go to Error if ($a 0 > 196) go to Error $t 0 = &zap $a 0 = $a 0 + $t 0 Mem($a 0) = $s 0;

Exercise 5. 13 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2 Comments.

Exercise 5. 13 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2 Comments. data zap: . space 200. text andi $t 0, $a 0, 3 bnez $t 0, Error bltz $a 0, Error li $t 0, 196 bgt $a 0, $t 0, Error la $t 0, zap add $a 0, $t 0 sw $s 0, 0($a 0)

Exercise 5. 14 Write a function to search through an array “X” of “N”

Exercise 5. 14 Write a function to search through an array “X” of “N” words to find how many of the values are evenly divisible by four. The address of the array will be passed to the function using register $a 0, and the number of words in the array will be passed in register $a 1. Return the results in register $v 0.

Exercise 5. 14 (Pseudocode) $v 0 = 0; $t 3 = 3; For (

Exercise 5. 14 (Pseudocode) $v 0 = 0; $t 3 = 3; For ( ; $a 1 > 0; $a 1= $a 1 - 1) { $t 2 = Mem ($a 0); $a 0 = $a 0 + 4; $t 0 = $t 2 & t 3; If ($t 0 == 0 ) $v 0 = $v 0 + 1; } return

Exercise 5. 14 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2 Div

Exercise 5. 14 (MIPS Assembly Language) Label Op-Code Dest. S 1, S 2 Div 4: li li b $v 0, 0 $t 3, 3 skip lw addi and bnez addi $t 2, 0($a 0) $a 0, 4 $t 0, $t 2, $t 3 $t 0, skip $v 0, 1 addi bgez jr $a 1, -1 $a 1, loop $ra loop: skip: Comments