Course Review and Midterm Exam Preview

The midterm exam will be in class this Friday, October 17.

You are responsible for understanding:

Here's an example midterm exam, for you to get a feeling for my exam style.  Answers are listed at the bottom of the page.

CS 301 Example Midterm Exam

1.) Convert each assembly code fragment to C++, and each C++ code fragment to assembly:



return new int[10];






push rdi

call print_int

pop rdi

add rdi,1

cmp rdi,10

jle start

int scale2(int x,int y) {

return x*7 + y*114;






(Score: 18 points, six per blank. Each piece of code is separate.)


2.) If “mov eax,DWORD[rcx+4*rdi]” is accessing an array element,


The pointer to the start of the array is ___________________.


The array index is ____________________.


Array element [3] is stored at [ _____________________ ].


Each array element has ______________ bits.

(Score: 12 points.)


3.) Fill in the blanks to make this code return 0x12.

sub rax,rax

mov _______, [ptr]



___ 0xffff12

(Score: 6 points.)


4.) Amazingly, this C++ code returns zero. Why? _____________________

int x=2048, y=2048, z=1024;

return x*y*z;

How could you detect this sort of problem? ____________________________________

How could you get the correct answer? ____________________________________

(6 points. )


5.) What does this code return? ____________

push 123

push 456

pop rax

pop rdx


(4 points.)


6.) True or false!

___ You can always store any value into any scratch register.

___ In 32-bit mode, the 64-bit registers such as rax run half as fast.

___ Push and pop should be used to preserve the value of scratch registers like r9.

___ A value stored in a preserved register will not change after calling a function.

(8 points.)


7.) This code works as is, but why do I need the “pop”? _____________________________________


call myfunction


pop rdx ; ???




mov rax,17

jmp back


How could you make this code cleaner? Sketch your changes above.

(6 points.)


8.) Write some assembly to call the C function “void scratchy(long *data);” with one long of valid memory allocated from the stack or heap.




ANSWER KEY & Suggested Readings


mov rdi,40 ; 4 bytes / int, 10 ints

extern malloc

call malloc

; my return value rax == malloc' return value rax 


void print_ints_to(int param) {

    for (int i=param;i<=10;i++) print_int(i);



global scale2


; edi: parameter 1; esi: parameter 2

imul edi,7

imul esi,114

mov eax,edi

add eax,esi




rcx is the start of this array

rdi is the array index


32 bits per DWORD





(This one is subtle; you want the low 8 bits of the answer.)




Detect with "jo"

Use a 64-bit long to get correct answer


5.) 456



True, scrach registers can be freely used.

False, in 32-bit mode rax does not even exist

True, r9 is a scratch register

True, functions will not change preserved registers.



You need the pop to balance "call myfunction".  It'd be cleaner if myfunction did "ret" instead.



mov rdi,8

extern malloc

call malloc

mov rdi,rax

extern scratchy

call scratchy

; also call free!



For this year's exam, I'm trying to lean more toward conceptual problems than implementation problems: more why, than how.  This means you need to understand both why and how!