x86-64 기계 코드, 18 바이트
97
99
31 D0
29 D0
99
F7 FE
29 D6
A8 01
0F 45 D6
92
C3
이 코드는를 계산하는 x86-64 기계 언어의 함수를 정의합니다 bounce(x, k. 다음과 같은 시스템 V AMD64 호출 규칙 GNU / 유닉스 시스템에서 사용의 x매개 변수는에 전달 EDI그동안, 레지스터 k매개 변수가 전달되는 ESI레지스터. 모든 x86 호출 규칙과 마찬가지로 결과는 EAX레지스터에 반환됩니다 .
C에서 이것을 호출하려면 다음과 같이 프로토 타입을 작성하십시오.
int Bounce(int x, int k);
온라인으로 사용해보십시오!
ungolfed 어셈블리 니모닉 :
; Take absolute value of input 'x' (passed in EDI register).
; (Compensates for the fact that IDIV on x86 returns a remainder with the dividend's sign,
; whereas we want 'modulo' behavior---the result should be positive.)
xchg eax, edi ; swap EDI and EAX (put 'x' in EAX)
cdq ; sign-extend EAX to EDX:EAX, effectively putting sign bit in EDX
xor eax, edx ; EAX ^= EDX
sub eax, edx ; EAX -= EDX
; Divide EDX:EAX by 'k' (passed in ESI register).
; The quotient will be in EAX, and the remainder will be in EDX.
; (We know that EAX is positive here, so we'd normally just zero EDX before division,
; but XOR is 2 bytes whereas CDQ is 1 byte, so it wins out.)
cdq
idiv esi
; Pre-emptively subtract the remainder (EDX) from 'k' (ESI),
; leaving result in ESI. We'll either use this below, or ignore it.
sub esi, edx
; Test the LSB of the quotient to see if it is an even number (i.e., divisible by 2).
; If not (quotient is odd), then we want to use ESI, so put it in EDX.
; Otherwise (quotient is even), leave EDX alone.
test al, 1
cmovnz edx, esi
; Finally, swap EDX and EAX to get the return value in EAX.
xchg eax, edx
ret
첫 번째 섹션 (절대 값을 취함)은 동일하게 작성 될 수 있습니다.
; Alternative implementation of absolute value
xchg eax, edi
neg eax
cmovl eax, edi
정확히 같은 바이트 수입니다 (6). 조건부 이동이 느린 특정 인텔 칩을 제외하고는 성능이 약간 빨라야 합니다.
XCHG물론, MOV코드 골프 를 제외 하고는 상대적으로 느리고 선호되지 않습니다 (피연산자 중 하나가 누산기 인 경우 전자는 1 바이트이지만 레지스터 레지스터 MOV는 항상 2 바이트입니다).