


































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



































180 Chapter 2 Instructions: Language of the Computer
they are quite convenient and result in more readable code (for example, the 1 i and rna ve instructions). If you choose to use pseudoinstructions for these reasons, please add a sentence or two to your solution stating which pseudoinstructions you have used and why.
The following problems deal with translating from C to MIPS. Assume that the variables 9, h, i ,and j are given and could be considered 32-bit integers as declared in a C program.
assembly code? Use a minimal number of MIPS assembly instructions.
tions are needed to perform the C statement?
respectively, what is the end value of f?
The following problems deal with translating from MIPS to C. Assume that the variables 9, h, i ,and j are given and could be considered 32-bit integers as declared in a C program.
~ add f. g, h b.. add 1 f. f. 1 add f, g, h
C statement?
what is the end value of f?
The following problems deal with translating from C to MIPS. Assume that the variables 9, h, i , and j are given and could be considered 32-bit integers as declared in a C program.
2.21 Exercises
b. f = 9 + (j + 2);
assembly code? Use a minimal number of MIPS assen1bly instructions.
tions are needed to perform the C statement?
what is the end value of f?
The following problems deal with translating from MIPS to C. For the following exercise, assume that the variables g, h, i , and j are given and could be considered 32-bit integers as declared in a C program.
b. sub f. $ O. f
. addi f, f, 1
statement?
what is the end value of f?
The following problems deal with translating from C to MIPS. Assume that the variables g, h, i, and j are given and could be considered 32-bit integers as declared in a C program.
b. f = 9 - (f + 5);
assembly code? Use a lninimal number of MIPS asselnbly instructions.
tions are needed to perform the C statement?
181
2.21 Exercises
a. add^ $50.^ $50,^ $ add $50. $50, $ add $50. $50, $ add $50. $50, $ b.^1 W^ $50,^ 4($56)
corresponding C staten1ent?
assembly code to minimize the number of MIPS instructions (if possible) needed to carry out the same function.
bly as written above? If you could rewrite the code above, what is the minimal number of registers needed?
In the following problems, we will be investigating memory operations in the context of a MIPS processor. The table below shows the values of an array stored in melnory.
a. Address Data 12 1 8 6 4 4 0 2 b. Address Data 16 1 12 2 8 3 4 4 0 5
to sort the data from lowest-to-highest, placing the lowest value in the smallest memory location shown in the figure. Assume that the data shown represents the
machine is a byte-addressable machine and a word consists of 4 bytes.
code to sort the data from lowest-to-highest, placing the lowest value in the small- est Inemory location. Use a minimum number of MIPS instructions. Assume the
183
184 Chapter 2 Instructions: Language of the Computer
for the MIPS code? If you are not allowed to use the immediate field in 1 wand 5 W instructions, how many MIPS instructions do you need?
The following problems explore the translation of hexadecimal numbers to other number formats.
b. OxbeadfOOd
of a little-endian and a big-endian machine. Assume the data is stored starting at address o.
The following problems deal with translating from C to MIPS. Assume that the variables f, g, h, i, and j are assigned to registers $50, $51, $52, $53, and $54, respectively. Assume that the base address of the arrays A and Bare in registers $ 5 6 and $ 57, respectively.
b. f = A[B[g]+l];
assembly code?
instructions are needed to perform the C statement?
to carry out the C statement using MIPS assembly code?
The following problems deal with translating from MIPS to C. Assume that the variables f, g, h, i, and j are assigned to registers $50, $51, $52, $53, and $54, respectively. Assume that the base address of the arrays A and Bare in registets $ 5 6 and $ 5 7, respectively.
186 Chapter 2 Instructions: Language of the Computer
represent? The following problems explore number conversions from decin1al to signed and unsigned binary numbers.
~ 2147483647 ten b. (^1000) ten
binary.
hexadecimal.
the table to two's complement hexadecimal.
The following problems deal with sign extension and overflow. Registers $ sO and
MIPS operation on these registers and show the result.
a. $sO = (^70000000) si xteen' $sl = OxOFFFFFFFsixteen b. $sO = (^) Ox40000000sixteen, $sl (^) = ox40000000sixteen
add $tO, $sO, $sl
sub $tO, $sO, $sl
2.21 Exercises
is the value of $t 0 for the following assembly code:
add $tO, $sO, $sl add $tO, $tO, $sO Is the result in $t 0 the desired result, or has there been overflow? In the following problems, you will perform various MIPS operations on a pair of registers, $ s 0 and $s 1. Given the values of $s 0 and $s 1 in each of the questions below, state if there will be overflow.
a. add $sO, $sO, $sl b. sub $sO, $sO, $sl sub $sO, $sO, $sl
For the table above, will there be overflow?
2.8.5 [5] <2.4> Assume that register $ S 0 = Ox40000000 and $s 1 = Ox20000000. For the table above, will there be overflow?
For the table above, will there be overflow?
The table below contains various values for register $ S 1. You will be asked to evaluate if there would be overflow for a given operation.
a. (^2147483647) ten b. Ox 0000 000 Os i xteen
given in the table. If the instruction: add $s 0, $s 0, $s 1 is executed, will there be overflow?
2.9.2 [5] <2.4> Assume that register $s 0 = Ox80000000 and $s 1 has the value as given in the table. If the instruction: sub $s 0, $s 0, $s 1 is executed, will there be overflow?
187
2.21 Exercises
In the following problems, the data table contains MIPS instructions. You will be asked to translate the entries into the bits of the opcode and determine what is the MIPS instruction format.
b. lw $tl. 4($53)
representation of these instructions.
above represent?
and rt fields in this instruction? For R-type instructions, what is the hexadecimal representation of the rd and funct fields? For I-type instructions, what is the hexadecimal representation of the immediate field?
In the following problems, the data table contains bits that represent the opcode of an instruction. You will be asked to translate the entries into asselnbly code and determine what format of MIPS instruction the bits represent.
b. 0 x8008 FFe 0
represent?
represent?
represent?
In the following problems, the data table contains the values of various fields of MIPS instructions. You will be asked to detern1ine what the instruction is, and find the MIPS format for the instruction.
189
190 Chapter 2 Instructions: Language of the Computer
a. op=O, rs=1, rt=2, rd=3, shamt=O, funct= b. op=Ox2B, rs=Ox10, rt=Ox5, const=Ox
above represent?
above?
In the following problems, the data table contains various modifications that could be made to the MIPS instruction set architecture. You will investigate the impact of these changes on the instruction format of the MIPS architecture.
a. 8 registers b. 10 bit immediate constants
instruction format must also be changed. For each of the suggested changes above, show the size of the bit fields of an R-type format instruction. What is the total nUlnber of bits needed for each instruction?
instruction format must also be changed. For each of the suggested changes above, show the size of the bit fields of an I-type format instruction. What is the total number of bits needed for each instruction?
decrease the size of a MIPS assembly progran1? Why could the suggested change in the table above increase the size of a MIPS assembly program?
In the following problems, the data table contains hexadecimal values. You will be asked to determine what MIPS instruction the value represents, and find the MIPS instruction format.
rIJ OxOl
decimal?
192 Chapter 2 Instructions: Language of the Computer
31 0 I__ --'-_Field---------'---- __I 31- i bits i - j bits j bits
In the following problems, you will be asked to write MIPS instructions to extract
indicated in the following table.
a. (^) 31 i-j
000 ... 000 I
Field
b. (^) 31 14 + i - j bits 14 0
000 ... 000
Field I 000 ... 000 I
$ t 1 in the format shown in the data table.
field frOITI $ t 0 for the constant values i = 4 and j = 0 and places the field into $ t 1 in the format shown in the data table.
field from $ t 0 for the constant values i = 31 and j = 28 and places the field into $ t 1 in the format shown in the data table.
In the following problems, you will be asked to write MIPS instructions to extract
remain unchanged.
2.21 Exercises
,..-- a. (^) 31 i-j
I
XXX ... XXX I
Field I
I
XXX ... XXX I
Field I
XXX ... XXX I
in the format shown in the data table.
in the fonnat shown in the data table.
For these problems, the table holds SOlne logical operations that are not included in the MIPS instruction set. How can these instructions be implemented?
a. andn $tl, $t2, $t3 II bit-wise AND of $t2, !$t b. xnor $tl, $t2, $t3 II bit-wise exclusive-NOR
value of $ t 3 = OxFFFF003C, what is the result in $ t 1?
MIPS instruction set, but can be synthesized using one or more MIPS assembly instructions. Provide a minimal set of MIPS instructions that may be used in place of the instructions in the table above.
representation of each instruction.
Various C-Ievellogical statements are shown in the table below. In this exercise, you will be asked to evaluate the statements and implement these C statements using MIPS asselnbly instructions.
193
2.21 Exercises
address as shown in the data table above? Is it possible to use the branch-on-equal (beq) MIPS assembly instruction to set the PC to the address as shown in the data table above?
b. 0 x 20001400
possible to use the jump (j) MIPS assembly instruction to set the PC to the address as shown in the data table above? Is it possible to use the branch-on-equal (beq) MIPS assembly instruction to set the PC to the address as shown in the data table above?
For these problems, several instructions that are not included In the MIPS instruction set are shown.
a. abs $t2. $t3 (^) II R[rdJ = (^) IR[rtJI
the MIPS instruction set and the description of each instruction. Why are these instructions not included in the MIPS instruction set.
195
196 Chapter 2^ Instructions: Language^ of^ the^ Computer
MIPS instruction set and the description of each instruction. If these instructions were to be implemented in the MIPS instruction set, what is the most appropriate instruction fonnat?
sequence of MIPS instructions that performs the same operation.
For these problems, the table holds MIPS assembly code fragments. You will be asked to evaluate each of the code fragments, familiarizing you with the different MIPS branch instructions.
a. LOOP: slt^ $t2.^ $0.^ $t bne $t2.^ $zero.^ ELSE j DONE ELSE: addi $s2. $s2.^2 subi $t1. $tl,^1 j LOOP DONE: b. LOOP: addi $t2. $0. OxA LOOP2: addi $s2. $s2. 2 subi $t2. $t2. 1 bne $t2, $0, LOOP subi $t1, $tl, 1 bne $tl, $0, LOOP DONE:
the $ 5 2 is initially zero?
Assume that the registers $51, $52, $t1, and $t2 are integers A, B, i, and temp, respectively.
For these problems, the table holds some C code. You will be asked to evaluate these C code statements in MIPS assembly code.
a. for ( i =0 ; i<10; i++) a += b; b. while ( a < 10) { D[a] = b + a; a += 1; I
198 Chapter 2 Instructions: Language of the Computer
a. int compareCint a, int b) { if CsubCa, b) )=^ 0) return 1; else return 0; } int sub Ci nt a, int b) { return a-b; } b. int fib_iterCint^ a,^ int^ b,^ int^ n)^ { ifCn == 0) return b; else return fib _iterCa+b, a,^ n-I); }
the total number of MIPS instructions needed to execute the function?
An in-line function is when the body of the function is copied into the program space, allowing the overhead of the function call to be eliminated. Implement an "in-line" version of the C code in the table in MIPS assembly. What is the reduction in the total number of MIPS assembly instructions needed to complete the function? Assume that the C variable n is initialized to 5.
function call is made. Assume the stack pointer is originally at addresss Ox7ffffffc, and follow the register conventions as specified in Figure 2.11.
The following three problems in this exercise refer to a function f that calls another function fun c. The code for C function fun c is already compiled in another module using the MIPS calling convention fron1 Figure 2.14. The function declaration for fun c
a. int fCint a, int b, int c) { return funcCfuncCa ,b) ,c); } b. int fCint a, int b, int c) { return funcCa,b)+funcCb,c); }
use the lower-numbered registers first.
explain why not. If yes, what is the difference in the number of executed instructions in f with and without the optimization?
2.21 Exercises
we know what the entire function f looks like, but for function fun c we only know its declaration.
This exercise deals with recursive procedure calls. For the following problems, the table has an assembly code fragment that cOlnputes the factorial of a number. However, the entries in the table have errors, and you will be asked to fix these errors.
a. FACT:^ addi^ $sp,^ $sp,^ - sw $ra , 4($sp) sw $aO, O($sp) s 1t i $tO, $aO, (^1) beq $tO,^ $0,^ L addi $vO,^ $0,^1 addi $sp,^ $sp,^8 jr $ra L1: addi $aO,^ $aO,^ - j a1 FACT lw $aO, 4($sp) lw $ra, O($sp) addi $sp, $sp, 8 mul $vO, $aO, $vO j r $ra b. FACT: addi $sp, $sp, - sw $ra, 4($sp) sw $aO, O($sp) slti $tO, $aO, 1 beq $tO, $0, L addi $vO, $0, 1 addi $sp, $sp, (^8) jr $ra L1: addi $tO,^ $tO,^ - j a1 FACT lw $aO,^ 4($sp) lw $ra,^ O($sp) addi $sp, $sp, 8 mul $vO, $aO, $vO j r $ra
returned in register $ vO. In the asselnbly code, there are a few errors. Correct the MIPS errors.
the input is 4. Rewrite the factorial progran1 to operate in a nonrecursive manner.
199