29 January 2018
CS 351: Computer Engineering I
Code Generation Tutorial Question
1/29/2018 Dr. E. Kalinga|CSE|CoICT|UDSM 1
1. Assume i and c maps $s1 and $s2 respectively. Base
register to be $s6 for array b and $s5 for array a.
a) What is the MIPS assembly code. For clarity
provide comments.
for (i = 0; i < 100; i = i+1)
{
a[i] = b[i] + c;
}
b) What is the equivalent machine language
2. Given an instruction for MIPS RISC machine in
machine language, write the equivalent MIPS
assembly language. Describe your answer.
10101100000010010000000000001001
1/29/2018 Dr. E. Kalinga|CSE|CoICT|UDSM 2
Dr. E. Kalinga|CSE|CoICT|UDSM 1
29 January 2018
4. What is the MIPS
3. What is the MIPS assembly code.
assembly code. …
if (a <5)
while (save[i] < k) b = c – a - 25;
{ else if (a==5)
a = b + c – d; b = c + a + 25;
i = i + j; else
c = a+5;
}
a = b + c;
…
1/29/2018 Dr. E. Kalinga|CSE|CoICT|UDSM 3
5. Write the corresponding MIPS assembly code
for the following C segment. Assume
variables with associations are as follows: x, y
and z to $s1, $s2, $s3 and $s4 respectively.
if (x <= 10)
{
y = x+5;
z = (2+y) – (3+x);
}
1/29/2018 Dr. E. Kalinga|CSE|CoICT|UDSM 4
Dr. E. Kalinga|CSE|CoICT|UDSM 2
29 January 2018
6. Assume that “a” is an array of 100 elements
whose base register is in register $s3. Variable
with associations is as follows: i, n and sum to
$s1, $s2 and $s4 respectively. Using the following
C segment
for(i = 0; i < n; i++)
{
sum += a[i];
}
a) Write the corresponding MIPS assembly code,
including necessary comments within the code
b) Provide a Machine code representation of the
MIPS assembly code created in question i above.
(Use equivalent decimal numbers)
1/29/2018 Dr. E. Kalinga|CSE|CoICT|UDSM 5
7. Given the following code segment in MIPS:
………………………..
X = 1;
do
{
X = X + 1;
}while (X < 10);
………………………..
a) Show MIPS assembly language code that would
implement the given high level language code.
Assume x corresponds to $S1. Add comments
for each assembly statement provided:
b) Write the equivalent machine language (end up
with decimal numbers)
1/29/2018 Dr. E. Kalinga|CSE|CoICT|UDSM 6
Dr. E. Kalinga|CSE|CoICT|UDSM 3