0% found this document useful (0 votes)
21 views9 pages

Csci2021 Fa23 Midterm 2 Practice Solutions

Uploaded by

sanjna chapsey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views9 pages

Csci2021 Fa23 Midterm 2 Practice Solutions

Uploaded by

sanjna chapsey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CSCI 2021: Machine Architecture and Organization

Fall 2023
Midterm 2 – Practice Questions

1. Assembly Basics
For each snippet of C code below, assume that the variable a is always stored in the a
register (%rax, %eax, etc.) and the variable b is always stored in the b register (%rbx,
%ebx, etc.).

Convert the last line of each C snippet into x86-64 assembly. It is possible to use just
one assembly instruction for each answer. Note that you do not need to know the exact
values stored in a or b.
(a) (2 points)
int a = ...;
int b = ...;
a = a + b;

Solution: addl %ebx, %eax

(b) (2 points)
int a = ...;
int *b = ...;
a = a + *b;

Solution: addl (%rbx), %eax

(c) (2 points)
long a = ...;
long b = ...;
a = a + b;

Solution: addq %rbx, %rax

(d) (2 points)
long a = ...;
long *b = ...;
a = a + *b;

Solution: addq (%rbx), %rax


Page 2 of 9

(e) (2 points)
long a = ...;
long *b = ...;
a = a + b[2];

Solution: addq 16(%rbx), %rax


Page 3 of 9

2. (15 points) C to Assembly


Convert the following C code to x86-64 assembly.
int fib(int num) {
int fib1 = 0;
int fib2 = 1;
int temp;
for (int c = 0; c < num; c++) {
temp = fib2;
fib2 += fib1;
fib1 = temp;
}
return fib2;
}

fib:

movl $0, %ecx # fib1

movl $1, %eax # fib2

movl $0, %edx # c

LOOP:

cmpl %edi, %edx # Compare c to num

jge END

movl %eax, %esi # temp = fib2

addl %ecx, %eax # fib2 += fib1

movl %esi, %ecx # fib1 = temp

addl $1, %edx # c++

jmp LOOP

END:

ret
Page 4 of 9

3. Procedures in Assembly
Consider the following x86-64 assembly code snippet. Assume that the code given below
is one section within a larger program.

Address Instruction
#2040 pushq %rax
#2041 pushq %rcx
#2042 call my_func
#2047 ???
.. ..
. .
my_func:
#3080 pushq %r12
# CHECKPOINT A
#3082 movq $2021, %rbx
#3089 movq $23, %r12
#3096 addq %r12, %rbx
#3099 movq %rbx, %rax
#3102 popq %r12
#3104 ret

(a) (7 points) Assume that, just before the instruction at address #2040 is executed,
the value of %rsp is #8016. Fill in the table below by writing down the value stored
at each memory address as of Checkpoint A in the code snippet.

If the value stored at a location cannot be determined from the given code, cross
out that row of the table. If the value stored at a particular address is equal to
the contents of a register (or, at least, the register’s contents at the time that the
location was modified), write down the name of the register in that row of the table.
Address Value
#8024
XXXX
#8016
XXXX
#8008
%rax
#8000
%rcx
#7992
#2047
#7984
%r12
#7976
XXXX
Page 5 of 9

(b) (3 points) This code snippet contains a violation of the x86-64 calling convention.
Identify where (at which address) the calling convention is violated. How would
you change the code snippet to fix this violation?

Solution: Function my_func uses a callee-save register without saving its origi-
nal value on the stack first. Specifically, it modifies register %rbx in the instruc-
tion at address #3082. To fix this, we can add pushq %rbx before we modify the
register and also add a corresponding popq %rbx before my_func returns.

(c) (3 points) What instructions would you add to the code starting at address #2047
(the location of the ???) to complete the code snippet? Hint: Think about the
x86-64 calling convention and what needs to be done before and after a function is
called.

Solution: The caller needs to restore the values of the caller-save registers
%rax and %rcx after my_func returns. We can add the following instructions to
accomplish this (note the reverse order from the original pushq operations):
popq %rcx
popq %rax
Page 6 of 9

4. Combinational Logic
Consider the following circuit:

b o

c
(a) (8 points) Complete the truth table below detailing the circuit’s output value for
each possible combination of input values.
a b c o
0 0 0
1
0 0 1
0
0 1 0
0
0 1 1
0
1 0 0
0
1 0 1
0
1 1 0
0
1 1 1
1
Page 7 of 9

(b) (3 points) Assume each logic gate incurs a delay of 30 picoseconds between when
it receives new input values and when its output value reflects these new inputs.
What is the maximum total delay of any path through this circuit?

Solution: There are paths in the circuit that go through four gates: from input
b (or c) through a NOT gate, then through two AND gates, and finally through the
OR gate at the end of the circuit. This path has a delay of 4 × 30 ps = 120 ps.

(c) (4 points) What purpose does this circuit serve? In other words, what function
does it compute on inputs a, b, and c?

Solution: The circuit outputs a 1 if a, b, and c are all 0 or all 1. It is checking


for equality among three bits.
Page 8 of 9

5. Pipelining Hazards
Assume we have a CPU with a five-stage pipeline as presented in lecture. Consider the
four x86-64 assembly code snippets below:

A B
addq $4, %rax addq $4, %rax
movq $8, %rcx movq $8, %rcx
movq $15, %r8 addq $16, %r9
addq $16, %r9 addq %rax, %rdx
addq %rax, %rdx

C D
movq 4(%rax), %rcx movq 4(%rax), %rcx
addq $15, %r8 addq %rcx, %rdx
addq %rcx, %rdx movq %r8, %r15

(a) (5 points) Assume that our CPU uses neither forwarding nor stalling. Which of the
assembly code snippets would produce incorrect behavior when executed? Select
all choices below that apply.
⃝ A

B

C

D
⃝ None of the Above
(b) (5 points) Assume that our CPU can forward values to previous pipeline stages
but cannot stall instructions. Which of the assembly code snippets would produce
incorrect behavior when executed? Select all choices below that apply.
⃝ A
⃝ B
⃝ C

D
⃝ None of the Above
(c) (5 points) Assume that our CPU can stall instructions but cannot forward val-
ues. Which of the assembly code snippets would produce incorrect behavior when
executed? Select all choices below that apply.
⃝ A
⃝ B
⃝ C
⃝ D

None of the Above
Page 9 of 9

(d) (5 points) Assume that our CPU can both forward values and stall instructions.
Which of the assembly code snippets would produce incorrect behavior when exe-
cuted? Select all choices below that apply.
⃝ A
⃝ B
⃝ C
⃝ D

None of the Above
(e) (2 points) Even with both forwarding and stalling enabled, one of the code snippets
above will cause hardware utilization to fall below 100% (i.e., some hardware will
not be doing useful work in at least one cycle). Identify this code snippet:
⃝ A
⃝ B
⃝ C

D
(f) (3 points) How would you modify this assembly code so that 100% hardware uti-
lization is achieved? Write your new version below:

Solution:
movq 4(%rax), %rcx
movq %r8, %r15
addq %rcx, %rdx

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy