## Assignment Two

### 1.(12 marks Perform the following conversions. (Note: for part (c), the “base 36” digits are 0, 1, 2, …, 9, A, B, C, …, Z. Also, do not attempt to do part (b) or part (c) by “brute force”.)  计算机组织与体系结构代写

(a) 21710 = ?7    (c) CMPT36 = ?6   (e) 1101102 = ?6

(b) 101111101011102 = ?16     (d) A916 = ?5 (f) 3647 = ?10  (f) 3647 = ?10

2.(6 marks) Represent the following signed numbers in 8 bit 2’s complement.

(a) -12810    (b) 12610    (c) -8310

### 3.(6 marks) Evaluate the following expressions, in which each operand is an 8 bit 2’s complement number, using the standard binary addition algorithm (evaluate the subtraction in part (a) by negating then adding).

As well as showing each result in 8 bit 2’s complement, also convert and express each as a signed decimal number. Note that whenever there is signed overflow your answer will not be correct. For which expression(s) does this occur?  计算机组织与体系结构代写

(a) 11111111 – 01111111     (b) 10111010 + 11000010      (c) 10111110 + 11010111

4.(10 marks) Outline the machine language instruction format (i.e., describe how the machine language instruction is divided into fields and state what each field is used for),and give the decimal value of each field, for each instruction in the following block of code. Assume that the name “proc” represents memory address 400010. (NOTE: for the bne and jal instructions, look carefully at the descriptions in the lecture notes for how branch and j format instructions are represented in machine language. Also be aware that when run using the default settings, QtSpim does not actually calculate the correct numeric constant field for branches, and so it will not show the correct machine code for “bne \$s0,\$t5, loop”. As described in the lecture notes and the text, the offset to the branch destination is measured in words, and calculated from the instruction after the branch instruction.)

loop: lw \$s0,0(\$s3)

subu \$s3,\$s3,\$t1

bne \$s0, \$t5, loop

andi \$t2,\$s4,20

jal proc

### 5.(10 marks) Write a MIPS procedure that takes as its three parameters the starting address of an integer array, and two positive integers low and high, and returns the sum of the low’th through the high’th array elements.  计算机组织与体系结构代写

For example, for the array 3, 8, -1, 5, 4, 3, 9 and low and high values of 2 and 4, the return value should be 12 (since the sum of the 2nd through the 4th array elements is 8 + -1 + 5 = 12). Also, write a main program to test your procedure. Your main program should input the number of integers n in the array from the user (you may assume that n is at most 25), input the n integers, and then input the integers low and high. Your main program should then invoke your procedure, output the return value, and terminate. Your code must use the “standard” conventions covered in class for passing parameters and returning results.

6.(10 marks) Write a MIPS procedure that takes as its two parameters the starting address of a (zero-terminated) string, and a character c, and removes all instances of the character from the string. Also, write a main program to test your procedure. Your main program should input a string from the user (you can assume that the string will be at most 40 characters, not including the zero byte delimiter), and then prompt for input of a character c计算机组织与体系结构代写

If the character c that the user inputs is the newline character (ascii code 10), your program should terminate. Otherwise, your program should invoke your procedure,output the modified string, and then prompt for input of another character to be removed from the string, continuing in this manner until the input character is the newline character.Your code must use the “standard” conventions covered in class for passing parameters and returning results.

### 7.(10 marks) One of the lesser-known mathematical functions is what we will call here the cmpt215 function. This is a function of three nonnegative integer variables i, j, and k, that can be defined recursively as follows:

cmpt215 (0, 0, 0) = 1

for all nonnegative integers i, j, k, such that at least one of i, j, k is nonzero.Here |𝑛| denotes the absolute value of n, and ⌊ ⌋ denotes the integer result when n is divided by 2, discarding any remainder. Some examples of cmpt215 function values are as follows: cmpt215 (0, 1, 0) = 7, cmpt215 (2, 1, 5) = 93, cmpt215 (20, 10, 50) = 2833,cmpt215 (5, 2, 1) = 65, cmpt215 (10, 50, 20) = 2667, cmpt215 (215, 215, 215) = 58933.  计算机组织与体系结构代写

Write a recursive MIPS procedure cmpt215 implementing this function, and a main program to test it. Your procedure should take as its three parameters nonnegative integers i, j, and k, and should return the function value. Your main program should prompt the user to enter three nonnegative integers i, j, and k. If at least one of the entered integers is negative, your program should terminate. Otherwise, your program should call your cmpt215 procedure, print the returned result, and then repeat by again prompting the user to enter three nonnegative integers. Your code must use the “standard” conventions covered in class for passing parameters and returning results.