Oh, this looks like fun! Since I agree with zacs7 over the homework aspect of this, I won't provide you with C code. Sorry, but here's an assembly version of it that works with ye olde Borland's command line tools.... turbo assembly ftw:
Yeah, I know.... I'm rusty.
push ebp ; Create stack frame - step 1.0
mov ebp, esp ; Create stack frame - step 1.1
mov eax, [ebp + 8] ; First param to EAX... eax = n now.
or eax, eax ; if eax is 0...
jz procfdone ; We're done. Return 0.
test eax, 1 ; Is EAX even or odd?
jz evennum ; If ZF is set, then the number is even
dec eax ; eax--;
shr eax, 1 ; eax / 2, quick.
push eax ; Pass (n - 1) / 2 as param
call _f ; Pass to f()
add esp, 4 ; Cleanup stack
inc eax ; eax++;
jmp procfdone ; We're finished! ;)
shr eax, 1 ; Fast divide
push eax ; Pass n / 2 to function
call _f ; call _f with (n/2)
add esp, 4 ; Clean up stack.
procfdone: ; EAX will have return value by now.
pop ebp ; Take down stack frame - step 1.0 and 1.1
ret ; Manual return from function/procedure.
But it does work. Could even assemble it into a .obj file and use it in a C program with the Borland stuff.
And btw, if you're lying about homework, you're being stupid.