; afact.asm: assembly program to be linked with cfact.c .386p ; 386 protected mode .model flat, C ; flat memory model, C calling convention .stack .data ; formats will be used to call printf ; c strings terminate in a 0 byte formats db "the factorial of 6 is %d", 13, 10, 0 .code ; declare printf and external function extrn printf: proc ; must use masm procedure syntax factorial proc push ebp mov ebp,esp push ecx push edx ; edx implicitly used in multiplication mov ecx,[ebp+8] ; move param into a register mov eax, 1 ; accumulator lp0: mul ecx ; eax = eax * ecx (edx ignored) dec ecx cmp ecx,2 ; no need to multiply by 1 jge lp0 ; at this point eax contains n!, the return value ; prepare for printf: (save eax, for printf returns a value!) push eax ; second parameter to printf mov ecx, offset formats ; address of format string push ecx ; first parameter to printf call printf pop ecx ; deallocate parameters pop eax pop edx pop ecx mov esp,ebp pop ebp ret factorial endp sumarray proc ; compute sum of array push ebp mov ebp,esp push ecx push ebx mov eax,0 ; accumulator mov ecx,[ebp+12] ; size of array mov ebx,[ebp+8] ; adress of array lp1: add eax,[ebx] add ebx,4 ; int is 4 bytes loop lp1 ; return value now in eax pop ebx pop ecx mov esp,ebp pop ebp ret sumarray endp end