##x86-64 Assembly (Windows x64 Calling Convention), 14 13 bytes
x86-64 Assembly (Windows x64 Calling Convention), 14 13 bytes
An inefficient (but svelte!) iterative approach (with credit to @Neil for inspiration):
HowFarAway PROC
6A 01 push 1
58 pop rax ; temp = 1
Loop:
0F AF C2 imul eax, edx ; temp *= b
39 C8 cmp eax, ecx
72 F9 jb Loop ; keep looping (jump) if temp < n
29 C8 sub eax, ecx ; temp -= n
C3 ret ; return temp
HowFarAway ENDP
The above function takes two integer parameters, n
(passed in the ECX
register) and b
(passed in the EDX
register), and returns a single integer result (in the EAX
register). To call it from C, you would use the following prototype:
unsigned HowFarAway(unsigned n, unsigned b);
This is limited to the range of a 32-bit integer. It can be easily modified to support 64-bit integers by using the full long registers, but it would cost more bytes to encode those instructions. :-)