Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

60%
+1 −0
Challenges Find n Niven Numbers

x86-64 machine code, 36 bytes 31 C0 8D 48 0A FF C0 50 99 89 17 48 F7 F1 01 17 99 85 C0 75 F6 58 50 F7 37 58 85 D2 75 E7 AB FF CE 75 E2 C3 Try it online! Following the standard calling conventi...

posted 1y ago by m90‭

Answer
#1: Initial revision by user avatar m90‭ · 2022-09-03T09:35:10Z (over 1 year ago)
# x86-64 machine code, 36 bytes
    31 C0 8D 48 0A FF C0 50 99 89 17 48 F7 F1 01 17 99 85 C0 75 F6 58 50 F7 37 58 85 D2 75 E7 AB FF CE 75 E2 C3
[Try it online!](https://tio.run/##TZLBTuMwEIbP8VMMWSHZJKDSIg7tlgtcV4sqpEVqezC203jl2sFOuy5VX53smFDoxfbM/P83k1FE01yuhOg6HtZAYZZTcrUy7oUbqEg1zqLzoHgs00EyozgogdHc81hcD5bEjDNtRV9tNqEGn15CvpJs7baok3qJZhmJRqnUW/ACBVzK01pvaFVoT5r9tW@gDWJd01O/@Ynz8Of37AEen2Y96ETXc2T8ZCcOYkLrgkSrwmmD/kp71RKWA5sQFVvlLeT3Oey3TkuoqLYtXPgS0h3Z5EDID/xas5EKfoZWandV3xGSqmuuLWVkT7IPLUxhUMIuXZM@5VXYmHY@Gi4xUdE@LGE0ZCnGLdNejaazKabxURSMZFnj0V7R/FxDXh4xu2WyHUsLu7C/uKi1VSCcVOM8VeNnd@lgf1TC@WD4jJjdlNKNDXpllQRRc3/BKjaPRYHcA/yrtVFpIJxkEO9Hp72A4iAvO9wxW1gk4VrIoeveRWX4KnSX69sbPPBvmqJemf8 "C++ (gcc) – Try It Online")

Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes in RDI an address of an array of `n` 32-bit integers to be filled in with the Nuven numbers, and takes `n` in ESI.

In assembly:
````
f:	xor eax, eax        # Set EAX to 0. EAX will hold the currently tried number.
	lea ecx, [rax+10]   # Set ECX to 10.
l:	inc eax             # Increase EAX by 1.
	push rax            # Push RAX (64-bit register containing EAX) onto the stack.
	cdq                 # Sign-extend, making EDX 0 provided EAX hasn't overflowed.
	mov [rdi], edx      # Place 0 at address RDI.
il:	div rcx             # Divide EDX:EAX (equal to EAX, as EDX is 0) by 10.
                            #  The quotient goes in EAX and the remainder goes in EDX.
	add [rdi], edx      # Add the remainder to the value at address RDI.
	cdq                 # Sign-extend, making EDX 0 provided EAX hasn't overflowed.
	test eax, eax       # Check the value in EAX.
	jnz il              # Jump back, to repeat the division loop, if it's nonzero.
	pop rax             # Restore the value of RAX from the stack.
	push rax            # Push it onto the stack again.
	div DWORD PTR [rdi] # Divide EDX:EAX (equal to EAX, as EDX is 0) by
                            #  the value at address RDI, which is the digit sum.
                            #  The quotient goes in EAX and the remainder goes in EDX.
	pop rax             # Restore the value of RAX from the stack.
	test edx, edx       # Check the value of the remainder in EDX.
	jnz l               # Jump back, repeating the outer loop, if it's nonzero.
                            #  (Otherwise, the value in EAX is a Niven number.)
	stosd               # Put EAX's value at address RDI, advancing the pointer.
	dec rsi             # Subtract 1, counting down from the input number in ESI.
	jnz l               # Jump back, repeating the outer loop, if it's nonzero.
	ret                 # Return.
````