Sally the Programmer is writing her CS 17 homework assignmen
Sally the Programmer is writing her CS 17 homework assignment. The assignment asks her to write a function which calculates some values. She has written the following function: procedure bar( data : int16 ); @nodisplay; @noframe; static iTemp : int16; iReturnAddress : dword; begin bar; // retrieve return address from the stack pop( iReturnAddress ); // retrieve 16 bits of padding from the stack pop( iTemp ); // retrieve actual parameter from the stack pop( data ); // do somework... mov( 0, EAX ); mov( 0, EBX ); mov( 0, ECX ); mov( 0, EDX ); inc( EAX ); add( EBX, EAX ); inc( EBX ); add( EBX, ECX ); inc( EDX ); ret(); end bar; Sally the Programmer thinks her programming job is finished. She offers you her code, believing it is working perfectly. You use it but report that your driver code which calls her code doesn’t seem to be working correctly. Why? What needs to be done to Sally’s code to make it work properly? (HINT: There are two major problems in the code shown above...)
Solution
Factorial PROC push ebp mov ebp,esp mov eax,[ebp+8] cmp eax,0 ja L1 mov eax,1 jmp L2 L1:dec eax push eax call Factorial ReturnFact: mov ebx,[ebp+8] mul ebx L2:pop ebp ret 4 Factorial ENDP