Computer Science 03-60-266 Final Examination Wednesday December 13 th 2006 Dr. Alioune Ngom Last Name: First Name: Student Number: INSTRUCTIONS EXAM DURATION IS 3 hours. OPEN NOTES EXAM: lecture notes, textbook, past exam solutions, and lab notes. Calculators (including your brain and fingers) are allowed. READ ALL QUESTIONS CAREFULLY BEFORE ANSWERING. Write all answers and work in the space provided. Use no other paper. WRITE YOUR FINAL ANSWERS WITH A PEN. There are 6 required questions (2 questions/hour). Answer all of them. QUESTION #7 is a bonus question. You are required to solve the first 6 questions. There are 20 pages. Count them before you start. If you need to make any assumptions, state them clearly with your answers. Write your answer neatly. Messy work is very hard to read and may cause you to lose marks. Q#1 Q#2 Q#3 Q#4 Q#5 Q#6 Total Q#7 (Page 2) (Page 4) (Page 6) (Page 8) (Page 10) (Page 12) (Page 14) 16 10 14 10 20 30 100 16 1
Question #1. (16 points) Write the following recursive Greatest Common Divisor program into ASM. public static long GCD(long a, long b) { If (b == 0) return a; Else return GCD(b, a % b); } Function GCD returns a value into EAX. Also, write a piece of the main program code that calls GCD. Assume that the Caller cleans the stack. 2
3
Question #2. (10 marks) Translate the following ASM function into a formula, or a pseudo-code or C, C++, Java. Sofint: PUSH EBP MOV EBP, ESP MOV EBX, [EBP + 8] CMP EBX, 1 JA Continue MOV EAX, 1 JMP Exit Continue: DEC EBX PUSH EBX CALL Sofint ; result in EAX ADD ESP, 4 MOV EBX, [EBP + 8] ADD EAX, EBX Exit: POP EBP RET ; EAX contains result Piece of the program that calls Sofint is.... MOV n, 4 ; n is a double-word PUSH n CALL Sofint ; result in EAX PUTINT EAX.... What does Sofint do? What is the printed value of EAX when n = 4? 4
5
Question #3. (14 marks) 1. Write the hexadecimal content of AX and the values of CF and OF flags, immediately after the execution of the instruction MUL AH, when AX = F8C7h? 2. Write the hexadecimal content of AX and the values of CF and OF flags, immediately after the execution of the instruction IMUL AH, when AX = F8C7h? 3. Write the hexadecimal content of the quotient and remainder registers, or write Divide Overflow if any, immediately after the execution of the instruction DIV BL, when AX = F8C7h and BL = FCh? 4. Write the hexadecimal content of the quotient and remainder registers, or write Divide Overflow if any, immediately after the execution of the instruction IDIV BL, when AX = F8C7h and BL = FCh? 5. Write the hexadecimal content of the destination register and the values of CF and OF flags, immediately after the execution of the instruction ADD AL, BL, when AL = F8h and BL = C7h? 6. Write the hexadecimal content of the destination register and the values of CF and OF flags, immediately after the execution of the instruction SUB AL, BL, when AL =F8h and BL = C7h? 6
7
Question #4. (10 marks) Complete the following data segment and its corresponding memory map.data SpNb1 DD 123.321 ; single-precision floating-point number SpNb2 DD ; single-precision floating-point number ; (fill in its decimal value) 0 1 2 3 0000 0004 3Ch D2h 1Eh F0h If necessary, limit the precisions to 7 digits after the dot. 8
9
Question #5. (20 marks) Write an ASM program which computes the formula x3 ac R = 1.2 cos( ) 4π Your program should use all 5 of the addressing modes shown on page 11 of chapter 10. Please specify an addressing mode by its initials (for instance, CSAM for Classical Stack Addressing Mode, RPAM for Register+Pop Addressing Mode,... ) beside the instruction that is using the addressing mode. See Lecture 10, Page 22, for instructions cos and?. Show all your FPU stack frames as I have done in lab. 10
11
Question #6. (30 marks) 1. How many bytes are contained in the following declaration? Var DD "AB", ABh, 10 DUP(10 DUP("AB"), 10 DUP(ABh), "AB", ABh) 2. Give the hexadecimal value of AH after SAR AH, 3 when AH = -32 initially. 3. Will this jump to Target? Circle Yes or No. MOV AX, 12EFh CMP AX, F1h JS Target 4. Which instructions are illegal? (Circle its letter) (a) PUSH AL (b) MOVZX AX, BX (c) MOV AL, WORD PTR [EBX] (d) ADD [AL], [CH] (e) INC 1Ah 5. The OF flag is updated only by arithmetic instructions. Circle True or False 6. Using CMP on two numbers will set the ZF flag if the operands are equal, but will not update the CF and OF flags. Circle True or False 7. ESP cannot be used to access the parameters of a procedure. Circle True or False 8. If 4 double-words parameters are passed to a procedure, then RET 4 will remove all the 4 parameters from the ASM stack upon returning from the procedure. Circle True or False 9. Upon execution of JMP Next (where label Next is defined in a separate file F1 and specified as public), the CPU will jump to the instruction labeled by Next in file F1. Is this allowed in ASM? Circle True or False. 10. The arithmetic instructions of the FPU, such as FADD, FSUB,..., will update the EFLAGS register upon their execution. Circle True or False 12
13
BONUS Question #7. (?? points) This question is not required. Write a program that uses a for-loop to calculate the first 20 values in the Fibonacci number sequence, {0, 1, 1, 2, 3, 5, 8, 13,...}. That is, your program is non-recursive. Your data segment should at least define an array of byte integers called Fibo. By definition, we have Fibo[0] = 0, Fibo[1] = 1, Fibo[2] = 1 and Fibo[k] = Fibo[k-2] + Fibo[k-1] for k > 2. Register ESI should initially contain the offset address of Fibo. Correct answer to this question will be used to fix your mark, only if it is necessary. 14
15
Space for Scrap Work 16
Space for Scrap Work 17
Space for Scrap Work 18
Space for Scrap Work 19
Space for Scrap Work 20