Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assembly Questions תרגול 12.

Similar presentations


Presentation on theme: "Assembly Questions תרגול 12."— Presentation transcript:

1 Assembly Questions תרגול 12

2 Q1 Consider the following assembly representation of a function foo containing a for loop

3 Q1 Fill in the blanks to provide the functionality of the loop
int foo(int a) { int i; int result = _____________; for( ________; ________; i++) __________________; } return result;

4 Answer int foo(int a) { int i; int result = a + 2;
for (i=0; i < a; i++) result += (i + 5); result *= (i + 3); } return result;

5 Q2 Which of the functions compiled into the assembly code shown?

6 Q3

7 Q3 Is the variable val stored on the stack? If so, at what byte offset(relative to %ebp) is it stored, and why is it necessary to store it on the stack? answer: yes, -4, Need to pass pointer to it to recursive call Is the variable val2 stored on the stack? If so, at what byte offset(relative to %ebp) is it stored, and why is it necessary to store it on the stack? answer: no

8 Q3 What (if anything) is stored at -24(%ebp)?If something is stored there, why is it necessary to store it? answer: the value of %ebx is saved here, because %ebx is a callee-save register What (if anything) is stored at -8(%ebp)?If something is stored there, why is it necessary to store it? answer: nothing is stored here.

9 Q4 Optimized the following Assembly code as much as possible:
movl $0, %eax movl $0, %edx movl 8(%ebp), %ebx .L1: cmpl %eax, %ebx jle .L2 movl 12(%ebp), %ecx addl %eax, %ecx addl %ecx, %edx incl %eax jmp .L1 .L2: movl %edx, %eax Optimized the following Assembly code as much as possible:

10 Q4 The small and obvious things: The big improvement:
Replace all movl $0 with xorl. Instead of using both %eax and %ebx – we can initialized %eax to 8(%ebp) and reduce it till we get zero (save the need in a saved register!) We can read the value of 12(%ebp) outside the loop and increased it by one instead of adding the index. The big improvement: Notice we are calculating the sum of an Arithmetic Series. Therefore, instead of using loops we can just calculate the formula – a much more efficient solution!

11 Q5 We are sending an ASCII file over the net from computer A to computer B. Will it always be possible for computer B to read the file while knowing nothing on computer A? Same as before only now we are sending a different file coded using the utf-8 coding?

12 Q5 Yes. In ASCII files each character is coded using one byte. Therefore, computer B doesn’t care if computer A use big or little endians. No. In UTF-8, some of the characters need more then one byte and then computer B must know if computer A use big or little endians.

13 Q6 Computer A runs program gcc in 3.2 seconds. Computer B runs the same program in 2.9 seconds. Which computer has better performance and by how much? What about computer C, which runs the program in 3.1 seconds?

14 Q6 Performance is relative so we want to measure PB/PA which is the inverse of CPUTA/CPUTB = 3.2/2.9 = Thus the performance of computer B is 10% better that computer A. Computer C is 3% (3.2/3.1 = 1.03) better than computer A. And computer B is 7% (3.1/2.9 = 1.07) faster than computer C.

15 Q7 You are given the next number in binary representation:
What is its value if it is: An unsigned int. A float.

16 Q7 float = (-1)1*( )*2( ) = ( )*21 = -1.25*2 = -2.5 unsigned = = ( ) = = 3,223,322,624

17 Q8 Computer A has 2 cache’s levels L1 and L2. In L1 hit ratio is 95% and hit time is one cycle. In L2 hit ratio is 92% and hit time is 4 cycles. The miss penalty of accessing memory is 12 cycles. What is the average memory access time (AMAT)? What should have happened so the AMAT value will be one?

18 Q8 The average memory access time is: AMAT = (hit ratio * hit time) + (miss ratio * miss penalty) AMAT = 0.95*1 + (1-0.95) * AMAT of L2 AMAT = *(0.92* *12) = 1.182 For the AMAT to be 1.0 the L1 hit ratio must be 100%, or the hit time of L2 and the miss penalty should be 1.

19 Q9 Answer: many possible solutions
Transfer the next C function info Assembly code: (don’t forget comments!) void swap(int* a, int* b) { int temp; temp = *a; *a = *b; *b = temp; } Answer: many possible solutions

20 Q10 What is the MIPS measurement?
What does the spatial locality principal say? What do we use MUXes for? What is the ALU?

21 Q10 MIPS: A measurement used to compare computers. Note that it is not efficient since it ignores the IC value. Therefore it cannot be used to compare between computers with different IS. The spatial locality principal says that if you accessed information in the memory you will probably access information that sitting next to it in the memory very soon. We use MUXes to select between different input lines. The Arithmetical Logical Unit is used to execute logical and additive instructions.


Download ppt "Assembly Questions תרגול 12."

Similar presentations


Ads by Google