Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---
- layout: default
- title: CSCI 2500 Quiz 3
- description: CSCI 2500 Quiz 3
- date: October 13, 2017
- header-includes:
- - \usepackage{listings}
- - \lstset{basicstyle=\ttfamily}
- ---
- ### NAME and RCS ID:\underline{\hspace{4.0in}}
- ### YOUR EMAIL IS (RCS ID) @ RPI.EDU
- ### THIS QUIZ IS CLOSED BOOK, CLOSED NOTES, and NO CALCULATOR.
- ### STAY AWAY FROM THE MARGINS AS THEY MAY GET TRIMMED OFF.
- \pagebreak
- # 1. MIPS Design Principles [15 points]
- Name three (and only three!) out of the four MIPS design principles.
- For each principle indicate an example within the MIPS instruction set architecture of how it is used.
- 1. Simplicity favors regularity
- 2. Smaller is faster
- 3. Make the common case fast
- Sisc vs risc
- 4. Good design demands good compromises
- Layers of software/hardware
- Compiler, assembler, hardware
- \pagebreak
- # 2. Matrix Indexing [25 points]
- ~~~
- int** matrix = (int**)malloc(sizeof(int*) * 4);
- for (int i = 0; i < 4; i++) {
- matrix[i] = (int*)malloc(sizeof(int) * 4);
- }
- ~~~
- The above code is different than the way that matrices were supported in our homework.
- There our memory was allocated in a 16-element one-dimensional array but we simulated a 4x4 array.
- Given the approach above, how would we go about setting `matrix[1][3]` to 14?
- assume matrix[0] is in $s0
- compute offset = 1 * 4
- load i nto (la)$t1, ($s0)
- add 4 * offset
- compute offset2 = 3 * 4
- load into $t2 = $t1 + offset 2
- \pagebreak
- # 3. MIPS Branches [25 points]
- ## 3.1 [10 points]
- Recall our discussion of MIPS branch instructions.
- The code below is completely valid but may not always work.
- Explain why.
- **Hint: refer to the image below.**
- ~~~
- foo:
- beq $t0, $t1, bar
- add $t2, $t3, $t4
- ...
- bar:
- add $t5, $t6, $t7
- ~~~
- if there are too many lines between foo and bar
- if the distance is greater than the number of bits (16 from the immediate)
- ![Instruction Reference](InstructionsCard.PNG)
- \vspace{1.5in}
- ## 3.2 [15 points]
- How can you rewrite this code to prevent this problem?
- foo:
- beq $t0, $t1, half_bar
- add $t2, $t3, $t4
- ...
- #after you run out of your 16 bits#
- j foo_continue
- half_bar: j bar
- foo_continue: do the rest of foo here
- bar:
- add $t5, $t6, $t7
- \pagebreak
- # 4. Convert C to MIPS [35 points]
- From the man page: The `strchr()` function locates the first occurrence of `c` (converted to a
- `char`) in the string pointed to by `s`.
- The functions `strchr()` returns a pointer to the located character, or `NULL` if the character does not appear in the string.
- Assume `NULL` = 0.
- Convert the following function from C to MIPS assembly.
- It may be helpful to use basic blocks to outline your program (especially your loop).
- You are allowed to use MIPS pseudo-instructions e.g., `blt`, `bgt`, etc.
- Additionally, assume the array always has at least one element.
- Per convention, `s` will be in `$a0` and `c` will be in `$a1`.
- Your return value should be placed in `$v0`.
- ~~~
- char *mystrchr(char *s, int c) {
- while (*s != (char) c) {
- if (!*s++) {
- return NULL;
- }
- }
- return (char *)s;
- }
- ~~~
- \pagebreak
- \centerline{\bf Extra space page. Please denote which problem you are answering here.}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement