Sheet 4
Sheet 4
Sheet 4
Floating point encoding. In this problem, you will work with floating point numbers based on the IEEE
floating point format. We consider two different 6-bit formats:
Format A:
Format B:
For formats A and B, please write down the binary representation for the following (use round-to-even).
Recall that for denormalized numbers, E = 1 − bias. For normalized numbers, E = e − bias.
Page 1
Check Ch: 3.8.8 before solving this problem
Problem 2:
Loops. Consider the following x86-64 assembly function:
loop:
# on entry: a in %rdi, n in %esi
movl $0, %r8d
movl $0, %ecx
testl %esi, %esi
jle .L3
.L6:
movl (%rdi,%rcx,4), %edx
leal 3(%rdx), %eax
testl %edx, %edx
cmovns %edx, %eax
sarl $2, %eax
addl %eax, %r8d
addq $1, %rcx
cmpl %ecx, %esi
jg .L6
.L3:
movl %r8d, %eax
ret
• You may only use the C variable names n, a, i and sum, not register names.
sum = _____;
sum += ______________;
return ____________;
}
Page 2
Problem 3:
Stack discipline. Consider the following C code and its corresponding 32-bit x86 machine code. Please
complete the stack diagram on the following page.
int fact(int n) {
if (n == 1)
return n;
else
return n * fact(n-1);
}
080483a4 <fact>:
80483a4: 55 push %ebp
80483a5: 89 e5 mov %esp,%ebp
80483a7: 53 push %ebx
80483a8: 83 ec 04 sub $0x4,%esp
80483ab: 8b 5d 08 mov 0x8(%ebp),%ebx
80483ae: 83 fb 01 cmp $0x1,%ebx
80483b1: 74 0e je 80483c1 <fact+0x1d>
80483b3: 8d 43 ff lea 0xffffffff(%ebx),%eax
80483b6: 89 04 24 mov %eax,(%esp)
80483b9: e8 e6 ff ff ff call 80483a4 <fact>
80483be: 0f af d8 imul %eax,%ebx
80483c1: 89 d8 mov %ebx,%eax
80483c3: 83 c4 04 add $0x4,%esp
80483c6: 5b pop %ebx
80483c7: 5d pop %ebp
80483c8: c3 ret
Page 3
A. Draw a detailed picture of the stack, starting with the caller invoking fact(4), and ending immediately
before the call instruction that invokes fact(2).
• The stack diagram should begin with the argument for fact that the caller has placed on the stack.
To help you get started, we have given you the first one.
• Use the actual values for function arguments, rather than variable names. For example, use 3 or 2
instead of n.
• For callee-saved registers that are pushed to the stack, simply note the register name (e.g, %ebx).
• Always label %ebp and give its value when it is pushed to the stack, e.g., old %ebp: 0xffff1400.
B. What is the final value of %ebp, immediately before execution of the instruction that calls fact(2)?
%ebp=0x_____________________
C. What is the final value of %esp, immediately before execution of the instruction that calls fact(2)?
%esp=0x____________________
Page 4