Skip to main content
Commonmark migration
Source Link

##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. :-)

##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. :-)

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. :-)

deleted 34 characters in body
Source Link
Cody Gray
  • 3.5k
  • 16
  • 28

##x86-64 Assembly (Windows x64 Calling Convention), 1414 13 bytes

An inefficient (but svelte!) iterative approach (with credit to @Neil for inspiration):

               HowFarAway PROC
89 D0        6A 01     mov    eax, edx    ;push temp = b1
EB 0358              jmp  pop  Start  rax     ; skip multiplication on first; iterationtemp of= loop1
               Loop:
0F AF C2           imul   eax, edx    ; temp *= b
               Start:
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. :-)

##x86-64 Assembly (Windows x64 Calling Convention), 14 bytes

An inefficient (but svelte!) iterative approach:

               HowFarAway PROC
89 D0              mov    eax, edx    ; temp = b
EB 03              jmp    Start       ; skip multiplication on first iteration of loop
               Loop:
0F AF C2           imul   eax, edx    ; temp *= b
               Start:
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. :-)

##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. :-)

added 203 characters in body
Source Link
Cody Gray
  • 3.5k
  • 16
  • 28

##x86-64 Assembly (Windows x64 Calling Convention), 14 bytes

An inefficient (but svelte!) iterative approach:

               HowFarAway PROC
89 D0              mov    eax, edx    ; temp = b
EB 03              jmp    Start       ; skip multiplication on first iteration of loop
               Loop:
0F AF C2           imul   eax, edx    ; temp *= b
               Start:
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. :-)

##x86-64 Assembly (Windows x64 Calling Convention), 14 bytes

               HowFarAway PROC
89 D0              mov    eax, edx    ; temp = b
EB 03              jmp    Start       ; skip multiplication on first iteration of loop
               Loop:
0F AF C2           imul   eax, edx    ; temp *= b
               Start:
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);

##x86-64 Assembly (Windows x64 Calling Convention), 14 bytes

An inefficient (but svelte!) iterative approach:

               HowFarAway PROC
89 D0              mov    eax, edx    ; temp = b
EB 03              jmp    Start       ; skip multiplication on first iteration of loop
               Loop:
0F AF C2           imul   eax, edx    ; temp *= b
               Start:
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. :-)

Source Link
Cody Gray
  • 3.5k
  • 16
  • 28
Loading