Advertisement
Guest User

Quiz 3

a guest
Oct 11th, 2018
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. ---
  2. layout: default
  3. title: CSCI 2500 Quiz 3
  4. description: CSCI 2500 Quiz 3
  5. date: October 13, 2017
  6. header-includes:
  7. - \usepackage{listings}
  8. - \lstset{basicstyle=\ttfamily}
  9. ---
  10.  
  11. ### NAME and RCS ID:\underline{\hspace{4.0in}}
  12. ### YOUR EMAIL IS (RCS ID) @ RPI.EDU
  13.  
  14. ### THIS QUIZ IS CLOSED BOOK, CLOSED NOTES, and NO CALCULATOR.
  15. ### STAY AWAY FROM THE MARGINS AS THEY MAY GET TRIMMED OFF.
  16.  
  17. \pagebreak
  18.  
  19. # 1. MIPS Design Principles [15 points]
  20.  
  21. Name three (and only three!) out of the four MIPS design principles.
  22. For each principle indicate an example within the MIPS instruction set architecture of how it is used.
  23.  
  24. 1. Simplicity favors regularity
  25. 2. Smaller is faster
  26. 3. Make the common case fast
  27. Sisc vs risc
  28. 4. Good design demands good compromises
  29.  
  30. Layers of software/hardware
  31. Compiler, assembler, hardware
  32.  
  33. \pagebreak
  34.  
  35. # 2. Matrix Indexing [25 points]
  36.  
  37. ~~~
  38. int** matrix = (int**)malloc(sizeof(int*) * 4);
  39. for (int i = 0; i < 4; i++) {
  40. matrix[i] = (int*)malloc(sizeof(int) * 4);
  41. }
  42. ~~~
  43.  
  44. The above code is different than the way that matrices were supported in our homework.
  45. There our memory was allocated in a 16-element one-dimensional array but we simulated a 4x4 array.
  46. Given the approach above, how would we go about setting `matrix[1][3]` to 14?
  47.  
  48. assume matrix[0] is in $s0
  49. compute offset = 1 * 4
  50. load i nto (la)$t1, ($s0)
  51. add 4 * offset
  52. compute offset2 = 3 * 4
  53. load into $t2 = $t1 + offset 2
  54.  
  55. \pagebreak
  56.  
  57. # 3. MIPS Branches [25 points]
  58.  
  59. ## 3.1 [10 points]
  60.  
  61. Recall our discussion of MIPS branch instructions.
  62. The code below is completely valid but may not always work.
  63. Explain why.
  64. **Hint: refer to the image below.**
  65.  
  66. ~~~
  67. foo:
  68. beq $t0, $t1, bar
  69. add $t2, $t3, $t4
  70. ...
  71.  
  72. bar:
  73. add $t5, $t6, $t7
  74. ~~~
  75. if there are too many lines between foo and bar
  76. if the distance is greater than the number of bits (16 from the immediate)
  77.  
  78. ![Instruction Reference](InstructionsCard.PNG)
  79.  
  80. \vspace{1.5in}
  81.  
  82. ## 3.2 [15 points]
  83.  
  84. How can you rewrite this code to prevent this problem?
  85. foo:
  86. beq $t0, $t1, half_bar
  87. add $t2, $t3, $t4
  88. ...
  89. #after you run out of your 16 bits#
  90. j foo_continue
  91.  
  92. half_bar: j bar
  93. foo_continue: do the rest of foo here
  94. bar:
  95. add $t5, $t6, $t7
  96. \pagebreak
  97.  
  98. # 4. Convert C to MIPS [35 points]
  99.  
  100. From the man page: The `strchr()` function locates the first occurrence of `c` (converted to a
  101. `char`) in the string pointed to by `s`.
  102. The functions `strchr()` returns a pointer to the located character, or `NULL` if the character does not appear in the string.
  103. Assume `NULL` = 0.
  104.  
  105. Convert the following function from C to MIPS assembly.
  106. It may be helpful to use basic blocks to outline your program (especially your loop).
  107. You are allowed to use MIPS pseudo-instructions e.g., `blt`, `bgt`, etc.
  108. Additionally, assume the array always has at least one element.
  109. Per convention, `s` will be in `$a0` and `c` will be in `$a1`.
  110. Your return value should be placed in `$v0`.
  111.  
  112. ~~~
  113. char *mystrchr(char *s, int c) {
  114. while (*s != (char) c) {
  115. if (!*s++) {
  116. return NULL;
  117. }
  118. }
  119. return (char *)s;
  120. }
  121. ~~~
  122.  
  123. \pagebreak
  124. \centerline{\bf Extra space page. Please denote which problem you are answering here.}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement