CSE3101 Fall 2004 Exam #1, open book, open notes. Name ___________________ 1. What is the difference between real mode and protected mode? (10 pts) ANSWER: Real mode has a 1 MB address space, 16-bit pointers, and full access to all instructions. It is intended for single-tasking operating systems. Protected mode has a 4 GB address space and 32-bit pointers. Programs are assignmed memory segments with read, write, and execute permissions. Access outside these segments is trapped by the operating system. Only the operating system may modify assigned segments using priveliged instructions. 2. Which mode does Windows use? (5 pts) ANSWER: protected 3. Which are valid MASM statements? Circle Yes or No (2 pts each). ANSWERS x db 10 dup(?) Yes y dd 1,2,3 Yes mov x, y No, can't address 2 memory locations mov 20, al No, move is right to left mov [esi+ebx*8+offset x], al Need "mov byte ptr [esi..." (I accepted either answer) 4. Translate the following C function to MASM (20 pts). int f(int a, int b) {return a+b;} ; ANSWER public _f ; needed if called from C _f: mov eax, [esp+4] ; a add eax, [esp+8] ; b ret ; result returned in eax 5. Write MASM equivalent to the following C code. Assume x and y are arrays as in question 3 (25 pts). y[1] = f(3, (x[5] << 4) - 8); ; ANSWER mov eax, [x+5] ; gets 4 bytes, x[5]...x[8] and eax, 0ffh ; don't need x[6]...x[8] so set to 0 shl eax, 4 ; x[5] << 4 sub eax, 8 ; (x[5] << 4) - 8 push eax ; pass arguments right to left push 3 call _f add esp, 8 ; In C, caller pops arguments off stack mov [y+4], eax ; result from f 6. How many times does this loop? (10 pts) L1: mov ecx, 10h ANSWER: forever (note position of label) dec ecx jnz L1 7. If a, b, and c are 32-bit unsigned numbers (defined with dd in MASM), write MASM code equivalent to a = a*b/c; such that the 64-bit intermediate result a*b does not overflow. You may assume c > b (10 pts). ; ANSWER mov eax, [a] mul [b] ; a*b in edx:eax as 64 bit number div [c] ; divide edx:eax / c, put quotient in eax, remainder in edx mov [a], eax ; don't forget to put result in a 8. Assume a and b are 64-bit unsigned numbers in little-endian format (defined with dq). Put a+b into edx:eax (10 pts). ; ANSWER mov eax, [a] ; low 32 bits of a mov edx, [a+4] ; high 32 bits add eax, [b] ; add low 32 bits of b adc edx, [b+4] ; add high 32 bits + carry