0% found this document useful (0 votes)
29 views4 pages

Sheet 4

Uploaded by

wejebil523
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)
29 views4 pages

Sheet 4

Uploaded by

wejebil523
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/ 4

Problem 1:

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:

• There is one sign bit s.

• There are k = 3 exponent bits. The bias is 2k−1 − 1 = 3.

• There are n = 2 fraction bits.

Format B:

• There is one sign bit s.

• There are k = 2 exponent bits. The bias is 2k−1 − 1 = 1.

• There are n = 3 fraction bits.

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.

Value Format A Bits Format B Bits

One 0 011 00 0 01 000


Three
7/8
15/8

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

Fill in the blanks of the corresponding C code.

• You may only use the C variable names n, a, i and sum, not register names.

• Use array notation in showing accesses or updates to elements of a.

int loop(int a[], int n)


{
int i, sum;

sum = _____;

for (i = ____________; ____________; ____________) {

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.

Value of %ebp when fact(4) is called: 0xffffd848


Return address in function that called fact(4): 0x080483e6

Stack The diagram starts with the


addresss argument for fact(4)
+-----------------------------------+
0xffffd830 | 4 |
+-----------------------------------+
0xffffd82c | |
+-----------------------------------+
0xffffd828 | |
+-----------------------------------+
0xffffd824 | |
+-----------------------------------+
0xffffd820 | |
+-----------------------------------+
0xffffd81c | |
+-----------------------------------+
0xffffd818 | |
+-----------------------------------+
0xffffd814 | |
+-----------------------------------+
0xffffd810 | |
+-----------------------------------+

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

You might also like