Skip to main content
Commonmark migration
Source Link

#x86-64 Machine Code, 14 bytes

x86-64 Machine Code, 14 bytes

8D 3C 7F 8D 14 92 8D 04 B7 01 D0 29 C8 C3

A function following the System V AMD64 calling convention (ubiquitous on Gnu/Linux systems) that takes four integer parameters:

  • EDI = num_of_3_par_holes
  • ESI = num_of_4_par_holes
  • EDX = num_of_5_par_holes
  • ECX = difficulty_rating

It returns a single value, the standard scratch, in the EAX register.

Ungolfed assembly mnemonics:

; int ComputeStandardScratch(int num_of_3_par_holes,
;                            int num_of_4_par_holes,
;                            int num_of_5_par_holes,
;                            int difficulty_rating);
lea   edi, [rdi+rdi*2]    ; EDI = num_of_3_par_holes * 3
lea   edx, [rdx+rdx*4]    ; EDX = num_of_5_par_holes * 5
lea   eax, [rdi+rsi*4]    ; EAX = EDI + (num_of_4_par_holes * 4)
add   eax, edx            ; EAX += EDX
sub   eax, ecx            ; EAX -= difficulty_rating
ret                       ; return, leaving result in EAX

Just a simple translation of the formula. What's interesting is that this is essentially the same code that you would write when optimizing for speed, too. This really shows the power of the x86's LEA instruction, which is designed to load an effective address, but can do addition and scaling (multiplication by low powers of 2) in a single instruction, making it a powerful multi-purpose arithmetic workhorse.

#x86-64 Machine Code, 14 bytes

8D 3C 7F 8D 14 92 8D 04 B7 01 D0 29 C8 C3

A function following the System V AMD64 calling convention (ubiquitous on Gnu/Linux systems) that takes four integer parameters:

  • EDI = num_of_3_par_holes
  • ESI = num_of_4_par_holes
  • EDX = num_of_5_par_holes
  • ECX = difficulty_rating

It returns a single value, the standard scratch, in the EAX register.

Ungolfed assembly mnemonics:

; int ComputeStandardScratch(int num_of_3_par_holes,
;                            int num_of_4_par_holes,
;                            int num_of_5_par_holes,
;                            int difficulty_rating);
lea   edi, [rdi+rdi*2]    ; EDI = num_of_3_par_holes * 3
lea   edx, [rdx+rdx*4]    ; EDX = num_of_5_par_holes * 5
lea   eax, [rdi+rsi*4]    ; EAX = EDI + (num_of_4_par_holes * 4)
add   eax, edx            ; EAX += EDX
sub   eax, ecx            ; EAX -= difficulty_rating
ret                       ; return, leaving result in EAX

Just a simple translation of the formula. What's interesting is that this is essentially the same code that you would write when optimizing for speed, too. This really shows the power of the x86's LEA instruction, which is designed to load an effective address, but can do addition and scaling (multiplication by low powers of 2) in a single instruction, making it a powerful multi-purpose arithmetic workhorse.

x86-64 Machine Code, 14 bytes

8D 3C 7F 8D 14 92 8D 04 B7 01 D0 29 C8 C3

A function following the System V AMD64 calling convention (ubiquitous on Gnu/Linux systems) that takes four integer parameters:

  • EDI = num_of_3_par_holes
  • ESI = num_of_4_par_holes
  • EDX = num_of_5_par_holes
  • ECX = difficulty_rating

It returns a single value, the standard scratch, in the EAX register.

Ungolfed assembly mnemonics:

; int ComputeStandardScratch(int num_of_3_par_holes,
;                            int num_of_4_par_holes,
;                            int num_of_5_par_holes,
;                            int difficulty_rating);
lea   edi, [rdi+rdi*2]    ; EDI = num_of_3_par_holes * 3
lea   edx, [rdx+rdx*4]    ; EDX = num_of_5_par_holes * 5
lea   eax, [rdi+rsi*4]    ; EAX = EDI + (num_of_4_par_holes * 4)
add   eax, edx            ; EAX += EDX
sub   eax, ecx            ; EAX -= difficulty_rating
ret                       ; return, leaving result in EAX

Just a simple translation of the formula. What's interesting is that this is essentially the same code that you would write when optimizing for speed, too. This really shows the power of the x86's LEA instruction, which is designed to load an effective address, but can do addition and scaling (multiplication by low powers of 2) in a single instruction, making it a powerful multi-purpose arithmetic workhorse.

Source Link
Cody Gray
  • 3.5k
  • 16
  • 28

#x86-64 Machine Code, 14 bytes

8D 3C 7F 8D 14 92 8D 04 B7 01 D0 29 C8 C3

A function following the System V AMD64 calling convention (ubiquitous on Gnu/Linux systems) that takes four integer parameters:

  • EDI = num_of_3_par_holes
  • ESI = num_of_4_par_holes
  • EDX = num_of_5_par_holes
  • ECX = difficulty_rating

It returns a single value, the standard scratch, in the EAX register.

Ungolfed assembly mnemonics:

; int ComputeStandardScratch(int num_of_3_par_holes,
;                            int num_of_4_par_holes,
;                            int num_of_5_par_holes,
;                            int difficulty_rating);
lea   edi, [rdi+rdi*2]    ; EDI = num_of_3_par_holes * 3
lea   edx, [rdx+rdx*4]    ; EDX = num_of_5_par_holes * 5
lea   eax, [rdi+rsi*4]    ; EAX = EDI + (num_of_4_par_holes * 4)
add   eax, edx            ; EAX += EDX
sub   eax, ecx            ; EAX -= difficulty_rating
ret                       ; return, leaving result in EAX

Just a simple translation of the formula. What's interesting is that this is essentially the same code that you would write when optimizing for speed, too. This really shows the power of the x86's LEA instruction, which is designed to load an effective address, but can do addition and scaling (multiplication by low powers of 2) in a single instruction, making it a powerful multi-purpose arithmetic workhorse.