15-213: Introduction to Computer Systems
Written Assignment 3
This written homework covers Machine Programming (Procedures, Data).
Directions
Complete the question(s) on the following pages with single paragraph answers. These questions
are not meant to be particularly long! Once you are done, submit this assignment on Canvas.
Below is an example question and answer.
Q: Please describe benefits of two's-complement signed integers versus other approaches.
A: Other representations of signed integers (ones-complement and sign-and-magnitude) have
two representations of zero (+0 and −0), which makes testing for a zero result more difficult.
Also, addition and subtraction of two’s complement signed numbers are done exactly the same
as addition and subtraction of unsigned numbers (with wraparound on overflow), which means
a CPU can use the same hardware and machine instructions for both.
Grading
Each assignment will be graded in two parts:
1. Does this work indicate any effort? (e.g. it’s not copied from a homework for another
class or from the book)
2. Three peers will provide short, constructive feedback.
Due Date
This assignment is due on February 4 by 11:59 PM Pittsburgh time (currently
UTC−4). Remember to convert this time to the timezone you currently reside in.
Question #1
The following is an excerpt from a C program.
long foo(long* arg1, long arg2);
long call_foo() {
long arg1 = 1;
long arg2 = 300;
long res = foo(&arg1, arg2);
return res;
}
Below is a snippet from the compiler output for the previous program. Fill in the blanks.
call_foo:
subq
movq
movq
movq
_L2_
movq
movq
call
movq
movq
addq
ret
L1: 300
L2: lea
L3: %rdi
L4: %rsi
L5:%rax
$32, %rsp
$1, 8(%rsp)
_L1_, 24(%rsp)
24(%rsp), %rdx
8(%rsp), _L3_
%rdx, _L4_
%rax, %rdi
foo
%rax, 16(%rsp)
16(%rsp), _L5_
$32, %rsp
Question #2
What are caller- and callee-saved registers? Briefly compare the similarities and
differences between the two.
Caller-saved registers and calle-saved registers relate to an agreed upon
standard that usually varies from architecture to architecture. In x86-64
using AT&T syntax, examples of caller-saved registers include: rax, rcx, rdx,
rsi, and rdi. On the other hand, calle-saved registers include: rbx, rbp, r12,
r13. Caller-saved registers and calle-saved registers are similar in that they
are general purpose registers used for local variables by your CPU.
However, they differ in who ensures the data stored in them is saved if
required. Calle-saved registers have data that MUST be stored in a
temporary variable (usually on the stack) by the calle, say, a function, so
that the original data pre-call can be restored before return. However, in a
caller-saved register, the data must be stored by the caller since a callee is
allowed to use the registers without worry about preserving the data.
Therfore, more temporary data registers like rax are caller saved.
Question #3
Identify the effects of the following assembly instructions:
a)
movq %rdi, %rsi
copies the value stored at address in rdi register into rsi register.
b)
leaq 4(%rbx, %rax, 2), %rcx
computes value in rbx + 2 * value in rax + 4 and stores the value into rcx register (NO
dereferences)
c)
mulq (%rcx), %rax
rax *= value at address in rcx
d)
popq %rbx
rbx = value on stack pointed to by rsp register and then increments rsp by 8 bytes
e)
cmpq %rsi, %r9
jb 676767
jump to address 676767 if r9 (unsigned) < rsi (unsigned)