ECE 2035
Programming HW/SW Systems
4 problems, 6 pages
Exam One
Fall 2014
17 September 2014
Your Name (please print clearly) ________________________________________________
This exam will be conducted according to the Georgia Tech Honor Code. I pledge to neither
give nor receive unauthorized assistance on this exam and to abide by all provisions of the
Honor Code.
Signed __________________________________________________________________
1
2
3
4
total
30
20
30
20
100
Instructions: This is a closed book, closed note exam. Calculators are not permitted.
Read each question over before you start to work. If you need to make any assumptions, state
them. If you have a question, raise your hand; do not leave your seat. The meaning of each
question should be clear, but if something does not make any sense to you, please ask for
clarification.
Please work the exam in pencil and do not separate the pages of the exam. If you run out of
room, please continue on the back of the previous page. For maximum credit, show your work.
Good Luck!
1
ECE 2035
Programming HW/SW Systems
4 problems, 6 pages
Exam One
Fall 2014
17 September 2014
Problem 1 (2 parts, 30 points)
Loops
Part A (10 points) Write a for loop in C that copies the elements of vector A into vector B in
reverse order. (So, given vector A below, vector B should contain {17, 6, …, 3, -1, 4}.)
Assume each vector has eight elements. For maximum credit, declare and initialize variables as
needed.
int A[] = {4, -1, 3, …, 6, 17};
int B[8];
Part B (20 points) Write a MIPS code fragment that is equivalent to the code you wrote in Part
A. For maximum credit, include comments and use a minimal number of instructions.
Label
Instruction
Comment
.data
A:
.word 4, -1, 3, …, 6, 17
# int A[]={4,-1,3,…, 6, 17};
B:
.alloc 8
# int B[8];
.text
Exit:
...
# instructions after the loop
2
ECE 2035
Programming HW/SW Systems
4 problems, 6 pages
Exam One
Problem 2 (2 parts, 20 points)
Fall 2014
17 September 2014
Conditionals: Compound Predicates
Part A (10 points) Consider the following C code fragment.
if (x<y) || (x>100)
x = 33;
else
y = 44;
Write a MIPS code fragment that implements this compound if-then-else exactly as C would.
Assume the value of x is held in $2 and y is in $3. Use a minimal number of additional
registers as needed. For maximum credit, include comments.
Label
Instruction
Comment
Part B (10 points) Consider the following MIPS code fragment. If $1, $2, $3, and $4 hold
variables A, B, C, and D, respectively, what is the equivalent 1-line C statement using compound
predicates that this computes? Hint: draw the control flow graph.
Label
Instruction
addi $4, $0, 1
beq
$1, $0, TestC
bne
$2, $0, TestC
j End
TestC:
bne
$3, $0, End
addi $4, $0, 0
End:
Answer:
...
D =
;
3
ECE 2035
Programming HW/SW Systems
4 problems, 6 pages
Exam One
Fall 2014
17 September 2014
Problem 3 (2 parts, 30 points)
Packed Pixel Data
Suppose an image is stored in memory as an array of pixels. As in Homework 2, each pixel is
represented as a triple of 8-bit red, green, and blue color components, packed in the lower 24 bits
of a 32-bit word, as shown here:
31
0
unused
red component
green component
blue component
31
24 23
16 15
8 7
0
Part A (20 points) Write a MIPS code fragment that reads in the red, green, and blue
components of the ith pixel of the image, finds the maximum of these color components, and
stores it in register $11. Assume $1 holds i, which could be any integer 0, 1, 2,...N-1, where N is
the number of pixels in the image. The image is stored in memory starting at base address
labeled Image. Modify only registers $1, $2, $3, $4, $5, and $11.
Label
Instruction
Comment
MaxPixel:
# Compute address of i_th
# pixel and put it into $1
# Read R, G, B components of
# i_th pixel into $2, $3, $4
# Find the max(R, G, B) and
# put it in $11.
4
ECE 2035
Programming HW/SW Systems
Fall 2014
4 problems, 6 pages
Exam One
17 September 2014
Part B (10 points) Suppose we have an image processing application that reads in the pixels in
an image array Image and unpacks the color components as shown in the C fragment below.
Complete the fragment (by filling in the blank) so that it repacks the color components into the
same pixel location but with the red and blue components swapped.
int i, Blue, Green, Red;
for (i=0; i<ImageSize; i++){
/* unpack current color */
Color = Image[i];
Blue = Color & 0xFF;
Green = (Color >> 8) & 0xFF;
Red = (Color >> 16) & 0xFF;
/* Repack the pixel color components with Red and Blue swapped. */
Image[i] = _____________________________________________________;
}
Problem 4 (2 parts, 20 points)
Control Flow
Part A (10 points) What does the following code fragment print?
int i,f=1;
for(i=10; i>0; i--){
if(i == 7)
break;
f = f*i;
}
printf("f = %d\n", f);
Part B (10 points) Suppose the instruction ”jal Foo” is at instruction memory address 3020
and Foo is a label of an instruction at memory address 4016. When this instruction is executed,
what changes occur to the registers. List all registers that are changed (both general purpose and
special purpose) and give their new values.
Register
Value
5
ECE 2035
Programming HW/SW Systems
4 problems, 6 pages
Exam One
MIPS Instruction Set (core)
instruction
add
subtract
add immediate
add unsigned
subtract unsigned
add immediate unsigned
set if less than
set if less than immediate
set if less than unsigned
set if < immediate unsigned
multiply
multiply unsigned
divide
divide unsigned
move from Hi
move from Lo
load upper immediate
and
or
and immediate
or immediate
nor
xor
xor immediate
shift left logical
shift left logical variable
shift right logical
shift right logical variable
shift right arithmetic
shift right arithmetic variable
load word
store word
load byte
load byte unsigned
store byte
branch if equal
branch if not equal
jump
jump register
jump and link
Fall 2014
17 September 2014
example
meaning
arithmetic
add $1,$2,$3
$1 = $2 + $3
sub $1,$2,$3
$1 = $2 - $3
addi $1,$2,100
$1 = $2 + 100
addu $1,$2,$3
$1 = $2 + $3
subu $1,$2,$3
$1 = $2 - $3
addiu $1,$2,100
$1 = $2 + 100
slt $1, $2, $3
if ($2 < $3), $1 = 1 else $1 = 0
slti $1, $2, 100
if ($2 < 100), $1 = 1 else $1 = 0
sltu $1, $2, $3
if ($2 < $3), $1 = 1 else $1 = 0
sltui $1, $2, 100
if ($2 < 100), $1 = 1 else $1 = 0
mult $2,$3
Hi, Lo = $2 * $3, 64-bit signed product
multu $2,$3
Hi, Lo = $2 * $3, 64-bit unsigned product
div $2,$3
Lo = $2 / $3, Hi = $2 mod $3
divu $2,$3
Lo = $2 / $3, Hi = $2 mod $3, unsigned
transfer
mfhi $1
$1 = Hi
mflo $1
$1 = Lo
lui $1,100
$1 = 100 x 2 16
logic
and $1,$2,$3
$1 = $2 & $3
or $1,$2,$3
$1 = $2 | $3
andi $1,$2,100
$1 = $2 & 100
ori $1,$2,100
$1 = $2 | 100
nor $1,$2,$3
$1 = not($2 | $3)
xor $1, $2, $3
$1 = $2 ⊕ $3
xori $1, $2, 255
$1 = $2 ⊕ 255
shift
sll $1,$2,5
$1 = $2 << 5 (logical)
sllv $1,$2,$3
$1 = $2 << $3 (logical), variable shift amt
srl $1,$2,5
$1 = $2 >> 5 (logical)
srlv $1,$2,$3
$1 = $2 >> $3 (logical), variable shift amt
sra $1,$2,5
$1 = $2 >> 5 (arithmetic)
srav $1,$2,$3
$1 = $2 >> $3 (arithmetic), variable shift amt
memory
lw $1, 1000($2)
$1 = memory [$2+1000]
sw $1, 1000($2)
memory [$2+1000] = $1
lb $1, 1002($2)
$1 = memory[$2+1002] in least sig. byte
lbu $1, 1002($2) $1 = memory[$2+1002] in least sig. byte
sb $1, 1002($2)
memory[$2+1002] = $1 (byte modified only)
branch
beq $1,$2,100
if ($1 = $2), PC = PC + 4 + (100*4)
bne $1,$2,100
if ($1 ≠ $2), PC = PC + 4 + (100*4)
jump
j 10000
PC = 10000*4
jr $31
PC = $31
jal 10000
$31 = PC + 4; PC = 10000*4
6
© Copyright 2026 Paperzz