Docsity

MIPS Assembly Instructions and Code Optimization, Lecture notes of Architecture

Toggle sidebar
bg1
180
Chapter 2Instructions: Language
of
the
Computer
they are quite convenient
and
result in
more
readable code (for example, the 1i
and
rna
ve instructions).
If
you
choose to use pseudoinstructions for these reasons,
please
add
asentence
or
two to your solution stating which pseudoinstructions
you
have used
and
why.
Exercise
2.1
The
following problems deal with translating from Cto MIPS. Assume
that
the
variables 9,
h,
i
,and
jare given
and
could be considered 32-bit integers
as
declared
in
a C program.
~
f=g+h+ +
j;
~
f=g+(h +
5);
2.1.1
[5] <2.2> For the Cstatements above, what
is
the
corresponding MIPS
assembly code? Use a
minimal
number
of
MIPS assembly instructions.
2.1.2
[5]
<2.2> For the Cstatements above,
how
many
MIPS assembly instruc-
tions are needed to
perform
the Cstatement?
2.1.3
[5]
<2.2>
If
the variables
f,
9,
h,
i,
and
jhave values
1,
2,
3,
4,
and
5,
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
jare 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
2.1.4
[5] <2.2> For the MIPS statements above,
what
IS acorresponding
Cstatement?
2.1.5
[5]
<2.2>
If
the variables
f,
9,
h,
and ihave values
1,
2,
3,
and
4,
respectively,
what
is
the
end
value
of
f?
Exercise
2.2
The following problems deal with translating from Cto MIPS. Assume that the
variables 9,
h,
i , and jare given and could be considered 32-bit integers
as
declared
in a C program.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a

Partial preview of the text

Download MIPS Assembly Instructions and Code Optimization and more Lecture notes Architecture in PDF only on Docsity!

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.

Exercise 2.

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.

~ f = g + h + + j;

~ f = g + (h + 5);

2.1.1 [5] <2.2> For the C statements above, what is the corresponding MIPS

assembly code? Use a minimal number of MIPS assembly instructions.

2.1.2 [5] <2.2> For the C statements above, how many MIPS assembly instruc-

tions are needed to perform the C statement?

2.1.3 [5] <2.2> If the variables f, 9, h, i, and j have values 1, 2, 3, 4, and 5,

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

2.1.4 [5] <2.2> For the MIPS statements above, what IS a corresponding

C statement?

2.1.5 [5] <2.2> If the variables f, 9, h, and i have values 1, 2, 3, and 4, respectively,

what is the end value of f?

Exercise 2.

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

~f~f+f+i;

b. f = 9 + (j + 2);

2.2.1 [5] <2.2> For the C statements above, what is the corresponding MIPS

assembly code? Use a minimal number of MIPS assen1bly instructions.

2.2.2 [5] <2.2> For the C statements above, how many MIPS assembly instruc-

tions are needed to perform the C statement?

2.2.3 [5] <2.2> If the variables f, g, h, and i have values 1, 2, 3, and 4, respectively,

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.

[g

. add f. f, h

b. sub f. $ O. f

. addi f, f, 1

2.2.4 [5] <2.2> For the MIPS statements above, what is a corresponding C

statement?

2.2.5 [5] <2.2> If the variables f, g, h, and i have values 1,2,3, and 4, respectively,

what is the end value of f?

Exercise 2.

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.

~ f ~ f + 9 + h + i + j + 2;

b. f = 9 - (f + 5);

2.3.1 [5] <2.2> For the C statements above, what is the corresponding MIPS

assembly code? Use a lninimal number of MIPS asselnbly instructions.

2.3.2 [5] <2.2> For the C staten1ents above, how many MIPS assembly instruc-

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)

2.4.4 [10] <2.2, 2.3> For the MIPS assembly instructions above, what is the

corresponding C staten1ent?

2.4.5 [5] <2.2, 2.3> For the MIPS asselnbly instructions above, rewrite the

assembly code to minimize the number of MIPS instructions (if possible) needed to carry out the same function.

2.4.6 [5] <2.2,2.3> How many registers are needed to carry out the MIPS assem-

bly as written above? If you could rewrite the code above, what is the minimal number of registers needed?

Exercise 2.

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

2.5.1 [10] <2.2,2.3> For the memory locations in the table above, write C code

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

C variable called Ar ray, which is an array of type i n t. Assume that this particular

machine is a byte-addressable machine and a word consists of 4 bytes.

2.5.2 [10] <2.2, 2.3> For the memory locations in the table above, write MIPS

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

base address of Ar ray is stored in register $ s 6.

183

184 Chapter 2 Instructions: Language of the Computer

2.5.3 [5] <2.2,2.3> To sort the array above, how many instructions are required

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.

~ Ox

b. OxbeadfOOd

2.5.4 [5] <2.3> Translate the hexadecilnal numbers above into decimal.

2.5.5 [5] <2.3> Show how the data in the table would be arranged in memory

of a little-endian and a big-endian machine. Assume the data is stored starting at address o.

Exercise 2.

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.

~ f ~ -9 + h + B[ll;

b. f = A[B[g]+l];

2.6.1 [10] <2.2,2.3> For the C statelnents above, what is the corresponding MIPS

assembly code?

2.6.2 [5] <2.2, 2.3> For the C statelnents above, how Inany MIPS assembly

instructions are needed to perform the C statement?

2.6.3 [5] <2.2,2.3> For the C statements above, how many registers are needed

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

2.7.3 [5] <2.4> For the patterns above, what hexadecimal number does it

represent? The following problems explore number conversions from decin1al to signed and unsigned binary numbers.

~ 2147483647 ten b. (^1000) ten

2.7.4 [5] <2.4> For the base ten numbers above, convert to two's complement

binary.

2.7.5 [5] <2.4> For the base ten numbers above, convert to two's complement

hexadecimal.

2.7.6 [5] <2.4> For the base ten numbers above, convert the negated values from

the table to two's complement hexadecimal.

Exercise 2.

The following problems deal with sign extension and overflow. Registers $ sO and

$ s 1 hold the values as shown in the table below. You will be asked to perform a

MIPS operation on these registers and show the result.

a. $sO = (^70000000) si xteen' $sl = OxOFFFFFFFsixteen b. $sO = (^) Ox40000000sixteen, $sl (^) = ox40000000sixteen

2.8.1 [5] <2.4> For the contents of registers $ s 0 and $ s 1 as specified above, what

is the value of $ t 0 for the following assembly code:

add $tO, $sO, $sl

Is the result in $ t 0 the desired result, or has there been overflow?

2.8.2 [5] <2.4> For the contents of registers $ s 0 and $ s 1 as specified above, what

is the value of $ t 0 for the following assembly code:

sub $tO, $sO, $sl

Is the result in $ t 0 the desired result, or has there been overflow?

2.21 Exercises

2.8.3 [5] <2.4> For the contents of registers $s 0 and $s 1 as specified above, what

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

2.8.4 [5] <2.4> Assume that register $ S 0 = Ox70000000 and $s 1 = OxlOOOOOOO.

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?

2.8.6 [5] <2.4> Assume that register $ s 0 = Ox8FFFFFFF and $s 1 = OxDOOOOOOO.

For the table above, will there be overflow?

Exercise 2.

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

2.9.1 [5] <2.4> Assume that register $s 0 = Ox70000000 and $s 1 has the value as

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.

~ add $tO, $tO, $zero

b. lw $tl. 4($53)

2.10.4 [5] <2.4, 2.5> For the instructions above, show the hexadecin1al

representation of these instructions.

2.10.5 [5] <2.5> What type (I-type, R-type) instruction do the instructions

above represent?

2.10.6 [5] <2.5> What is the hexadecimal representation of the opcode, rs,

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?

Exercise 2.

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.

~ OxAEOBFFFC

b. 0 x8008 FFe 0

2.11.1 [5] <2.4,2.5> What binary number does the above hexadecimal number

represent?

2.11.2 [5] <2.4,2.5> What decimal number does the above hexadecimal number

represent?

2.11.3 [5] <2.5> What instruction does the above hexadecimal nun1ber

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

2.11.4 [5] <2.5> What type (I-type, R-type) instruction do the instructions

above represent?

2.11.5 [5] <2.5> What is the MIPS assembly instruction described above?

2.11.6 [5] <2.4, 2.5> What is the binary representation of the instructions

above?

Exercise 2.

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

2.12.1 [5] <2.5> If the instruction set of the MIPS processor is modified, the

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?

2.12.2 [5] <2.5> If the instruction set of the MIPS processor is modified, the

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?

2.12.3 [5] <2.5, 2.10> Why could the suggested change in the table above

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

b. ax 80 a 9 aa 12

2.12.4 [5] <2.5> For the entries above, what is the value of the number in

decimal?

192 Chapter 2 Instructions: Language of the Computer

2.13.5 [5] <2.6> Assume that $ t 0 = OxASASOOOO and $ t 1 = ASASOOOO. What is

the value of $ t 2 after the two instructions in the table?

2.13.6 [5] <2.6> Assume that $ to = OxASASFFFF and $ t 1 = ASASFFFF. What is

the value of $ t 2 after the two instructions in the table?

Exercise 2.

The following figure shows the placement of a bit field in register $ to.

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

the bits "Field" from register $ t 0 and place them into register $ t 1 at the location

indicated in the following table.

a. (^) 31 i-j

I

000 ... 000 I

Field

I

b. (^) 31 14 + i - j bits 14 0

I

000 ... 000

I

Field I 000 ... 000 I

2.14.1 [20] <2.6> Find the shortest sequence of MIPS instructions that extracts

a field from $ to for the constant values i = 22 and j = 5 and places the field into

$ t 1 in the format shown in the data table.

2.14.2 [5] <2.6> Find the shortest sequence of MIPS instructions that extracts a

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.

2.14.3 [5] <2.6> Find the shortest sequence of MIPS instructions that extracts a

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

the bits "Field" from register $ t 0 shown in the figure and place them into register

$ t 1 at the location indicated in the following table. The bits shown as "X XX" are to

remain unchanged.

2.21 Exercises

,..-- a. (^) 31 i-j

I

XXX ... XXX I

Field I

  • b. (^) 31 14 + i - j bits 14 0

I

XXX ... XXX I

Field I

XXX ... XXX I

2.14.4 [20] <2.6> Find the shortest sequence of MIPS instructions that extracts

a field from $ t 0 for the constant values i = 17 and j = 11 and places the field into

$ t 1 in the format shown in the data table.

2.14.5 [5] <2.6> Find the shortest sequence of MIPS instructions that extracts a

field froln $ t 0 for the constant values i = 5 and j = 0 and places the field into $ t 1

in the format shown in the data table.

2.14.6 [5] <2.6> Find the shortest sequence of MIPS instructions that extracts a

field from $ to for the constant values i = 31 and j = 29 and places the field into $ t 1

in the fonnat shown in the data table.

Exercise 2.

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

2.15.1 [5] <2.6> The logical instructions above are not included in the MIPS

instruction set, but are described above. If the value of $ t 2 = OxOOFFA5A5 and the

value of $ t 3 = OxFFFF003C, what is the result in $ t 1?

2.15.2 [10] <2.6> The logical instructions above are not included in the

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.

2.15.3 [5] <2.6> For your sequence of instructions in 2.15.2, show the bit-level

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?

For these problems, the table holds various binary values for register $ to. Given the

value of $ t 0, you will be asked to evaluate the outcome of different branches.

~ OxOOOOlOOO

b. 0 x 20001400

2.16.4 [5] <2.7> Suppose that register $ to contains a value from above. What is

the value of $ t 2 after the following instructions?

slt $t2, $tO, $tO

bne $t2, $zero, ELSE

j DONE

ELSE: addi $t2, $t2, 2

DONE:

2.16.5 [5] <2.6, 2.7> Suppose that register $ t 0 contains a value from above.

What is the value of $ t 2 after the following instructions?

sll $tO, $tO, 2

slt $t2, $tO, $zero

2.16.6 [5] <2.7> Suppose the program counter (PC) is set to Ox2000 0000. Is it

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?

Exercise 2.

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

b. sgt $tl, $t2, $t3 II R[rdJ = (R[rsJ > R[rtJ) ? 1:

2.17.1 [5] <2.7> The table above contains some instructions not included in

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

2.17.2 [5] <2.7> The table above contains some instructions not included in the

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?

2.17.3 [5] <2.7> For each instruction in the table above, find the shortest

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:

2.17.4 [5] <2.7> For the loops written in MIPS assembly above, assume that the

register $ t 1 is initialized to the value 10. What is the value in register $ 5 2 assuming

the $ 5 2 is initially zero?

2.17.5 [5] <2.7> For each of the loops above, write the equivalent C code routine.

Assume that the registers $51, $52, $t1, and $t2 are integers A, B, i, and temp, respectively.

2.17.6 [5] <2.7> For the loops written in MIPS assembly above, assume that the

register $ t 1 is initialized to the value N. How many MIPS instructions are executed?

Exercise 2.

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); }

2.19.1 [15] <2.8> Implement the C code in the table in MIPS assembly. What is

the total number of MIPS instructions needed to execute the function?

2.19.2 [5] <2.8> Functions can often be implemented by compilers "in-line".

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.

2.19.3 [5] <2.8> For each function call, show the contents of the stack after the

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

is (( i nt fun c ( i nt a, i nt b);". The code for function f is as follows:

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); }

2.19.4 [10] <2.8> Translate function f into MIPS assembler, also using the MIPS

calling convention from Figure 2.14. If you need to use registers $ to through $ t 7,

use the lower-numbered registers first.

2.19.5 [5] <2.8> Can we use the tail-call optimization in this function? If no,

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

2.19.6 [5] <2.8> Right before your function f fron1 Problem 2.19.4 returns, what

do we know about contents of registers $ t 5, $ s 3, $ r a, and $ s p? Keep in mind that

we know what the entire function f looks like, but for function fun c we only know its declaration.

Exercise 2.

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

2.20.1 [5] <2.8> The MIPS assembly program above computes the factorial of

a given input. The integer input is passed through register $ a 0, and the result is

returned in register $ vO. In the asselnbly code, there are a few errors. Correct the MIPS errors.

2.20.2 [10] <2.8> For the recursive factorial MIPS progran1 above, assun1e that

the input is 4. Rewrite the factorial progran1 to operate in a nonrecursive manner.

199