Homework Assignment 3 - Kevin Burger`s Website
Transcription
Homework Assignment 3 - Kevin Burger`s Website
CSE/EEE 230 Computer Organization and Assembly Language Homework 3 :: 75 pts 1 Instructions You may work in pairs with a partner on this assignment if you wish or you may work alone. If you work with a partner, only submit one zip archive with both of your names in the PDF document and source code file; you will each earn the same number of points. Your zip archive must be uploaded to Blackboard by the assignment deadline. Section 3 describes what to submit and by when. 2 Exercises 1. What would be the encoding of this instruction: xor $s7, $a3, $t5? Write your answer as an 8-hexdigit integer, e.g., 0x1234_5678, and for full credit explain how your answer was obtained. 2. What would be the encoding of this instruction: ori $s1, $zero, 21345? Write your answer as an 8-hexdigit integer, e.g., 0x123_45678, and for full credit explain how your answer was obtained. 3. Decode this instruction: 0x3C01_1001. If the assembly language instruction contains any numbers write them in decimal and for full credit explain how your answer was obtained. Hint: Tables A.2 and A.3 in the MIPS Architecture for Programmers Vol II-A reference manual (on the course website) may be helpful. 4. Decode this instruction: 0x0011_4B43. If the assembly language instruction contains any numbers write them in decimal and for full credit explain how your answer was obtained. 5. Name your assembly language source code file hw03_01.s. Write a complete MIPS assembly language program that will display a prompt string asking the user to enter an integer n. The program shall use a loop to compute and then display the sum (13 - 1) + (23 - 2) + (33 - 3) + ... + (n3 - n). Sample input and output, Enter n (>= 1)? 8 Result = 1260 Here is the pseudocode, Function main() SysPrintStr("Enter n (>= 1)>? " n = SysReadInt() sum = 0 For i = 1 to n Do sum += i^3 - i End For SysPrintStr("Result = ") SysPrintInt(sum) SysExit() End Function main I would suggest that if you feel intimidated by the assembly language code, that you first rewrite this pseudocode replacing the for loop with a while loop. Then rewrite that pseudocode replacing the while loop with an if statement and gotos. The unstructured pseudocode with if statements and gotos maps directly to the assembly language code. You may optimize the code as much as you wish, as long as you still use a loop to compute the result 1. 6. Name your assembly language source code file hw03_02.s. Let n be an integer that is greater than 1. If n is even, we change n to be n / 2; if n is odd, we change n to be 3n + 1. We repeat this procedure until n reaches 1. For example, let n = 6, 6, 3, 10, 5, 16, 8, 4, 2, 1 1 There may be a closed-form formula to compute this sum, but the objective of the question is for you to demonstrate that you can write a loop in assembly language. (c) Kevin R. Burger :: Computer Science & Engineering :: Arizona State University Page 1 CSE/EEE 230 Computer Organization and Assembly Language Homework 3 :: 75 pts The Collatz conjecture2 states for any natural number, this Collatz sequence will eventually reach 1 (this is called a conjecture, because it has not been proven). The total stopping time is the number of iterations of applying the rule (divide by 2 or multiply by 3 and add 1) until n reaches 1. For n = 6, we applied the rule 8 times, so for n = 6 the total stopping time is 8. For this exercise, you are to write a complete MIPS assembly language program that will ask the user to enter n. The program shall generate and display the Collatz sequence, stopping when n reaches 1. The program shall also display the total stopping time. Sample input and output, Enter n (>= 1)? 27 27 82 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1 The total stopping time is 111 Here is the pseudocode, Function main() SysPrintStr("Enter n (>= 1)>? " n = SysReadInt() stop = 0 SysPrintInt(n) SysPrintChar(' ') While (n != 1) Do If (n % 2 == 0) Then n = n / 2 Else n = 3n + 1 End If ++stop SysPrintInt(n) SysPrintChar(' ') End While SysPrintStr("\nThe total stopping time is ") SysPrintInt(stop) SysExit() End Function main 7. Name your assembly language source code file hw03_03.s. A global array of 4-byte integers can be defined in the data segment using the .space directive, .data x: .space 68 # int x[17] = { 0 }; Once the array x is defined, we can access the elements of x using lw and sw, e.g., la lw sw $s0, x $t0, 12($s0) $t0, 64($s0) # $s0 = &x # $t0 = x[3] # x[16] = x[3] Note that the element at index i is at an offset of 4 × i from the address of x. Arrays are commonly manipulated using loops. Suppose we wish to initialize the elements of x so x[i] = i, i.e., x[0] is initialized to 0, x[1] is initialized to 1, ..., x[16] is initialized to 16. The pseudocode would be, For i = 0 to 16 Do x[i] = i End For 2 https://en.wikipedia.org/wiki/Collatz_conjecture (c) Kevin R. Burger :: Computer Science & Engineering :: Arizona State University Page 2 CSE/EEE 230 Computer Organization and Assembly Language Homework 3 :: 75 pts This for loop can be rewritten as a while loop, i = 0 While (i < 17) Do x[i] = i ++i End For Which can be rewritten using an if statement and gotos, i = 0 loop_begin: if (i >= 17) goto end_loop; x[i] = i; ++i; goto loop_begin; end_loop: Which maps quite nicely into assembly language, # Load address of x into $s0 la $s0, x # $s0 = &x # Load constant 17 into $t1 addi $t1, $zero, 17 # $t1 = 17 # i = 0; add # i = 0 $s1, $zero, $zero loop_begin: # If (i >= 17) goto end_loop; bge $s1, $t1, end_loop # if i >= 17 goto end_loop # x[i] = i; sll $t2, $s1, 2 add $t2, $s0, $t2 sw $s1, 0($t2) # $t2 = 4 x i # $t2 = &x + 4 x i (this is the addr of x[i]) # x[i] = i # ++i addi # ++i $s1, $s1, 1 # goto loop_begin; j loop_begin end_loop: For this exercise, you are to define an array named x of 4-byte integers in the data segment with 17 elements. The program shall then implement the following pseudocode to initialize the elements of x. const int TWO_TO_31_MINUS_1 = 0x7FFF_FFFF int a = 1947, b = 1111, m = 32707, seed = 1792, t Function main() For i = 16 downto 0 Do x[i] = 0 For j = 1 to 23 Do t = a - seed x[i] /= 2 If (t < 0) Then x[i] += TWO_TO_31_MINUS_1 t += m End if seed = b; b = a; a = t End For (c) Kevin R. Burger :: Computer Science & Engineering :: Arizona State University Page 3 CSE/EEE 230 Computer Organization and Assembly Language Homework 3 :: 75 pts SysPrintInt(x[i]) SysPrintChar(' ') End For SysExit() End Function main The correct output from the program should be: -288922625 1850002942 -2003060226 991671295 1226978302 -613639169 -843658753 1684828670 -645342721 286493183 -287072769 573693439 -485350401 -969166337 -1155231234 1840367614 -1233684482 Important note: you will get an arithmetic overflow exception in some cases when performing the addition x[i] += TWO_TO_31_MINUS_1 if you use the MIPS add instruction. In this program, use the MIPS addu and subu instructions, which perform the same operations as add and sub, but do not generate overflow exceptions. 3 Submission Instructions Type your answers to Exercises 1–4 in a document and convert the document to PDF. If you work with a partner, be sure to put both of your names in the document. To convert your document into PDF format, Microsoft Office versions 2008 and newer will export the document into PDF format by selecting the proper menu item from the File menu. The same is true of Open Office and Libre Office. THERE WILL BE A 25% PENALTY FOR NOT SUBMITTING YOUR SOLUTION IN PDF FORMAT. FURTHERMORE, IF YOU SUBMIT A DOCUMENT WHICH IS NOT IN PDF FORMAT AND THE GRADER CANNOT OPEN THE DOCUMENT BECAUSE IT IS IN SOME WEIRD FORMAT, YOU WILL BE GIVEN A SCORE OF 0 ON EXERCISES 1–4. Then, create an empty folder named 230-s15-h03-asurite where asurite is your ASURITE user name (this is not your ASU id number, e.g., mine is kburger2). If you worked with a partner, name the folder 230-s15-h03-asurite1-asurite2 where asurite1 and asurite2 are the ASURITE user names of both partners. Copy the PDF document and your three .s assembly language source code files to this folder. Next, create a zip archive of the folder naming the zip archive 230-s15h03-asurite.zip or 230-s15-h03-asurite1-asurite2.zip. Upload the zip archive to Blackboard using the homework submission link by the deadline which is 4:00am Fri 20 Feb (Note: this is four o'clock in the morning on Friday, which for some of you, will be very late Thursday night). Consult the online syllabus for the late and academic integrity policies. (c) Kevin R. Burger :: Computer Science & Engineering :: Arizona State University Page 4