Skip to content

Instantly share code, notes, and snippets.

@C00kiie
Created September 4, 2025 09:12
Show Gist options
  • Select an option

  • Save C00kiie/c4a071d09860416b18c7935b829e2db6 to your computer and use it in GitHub Desktop.

Select an option

Save C00kiie/c4a071d09860416b18c7935b829e2db6 to your computer and use it in GitHub Desktop.
calculate factorial value using c preprocessor/macro
#include <stdio.h>
// i was playing with macros a bit, and thought it's a bit funny to make the preprocessor do the math instead of
// the actual source code
// ironically, this will insert the factorial(x) value directly as a number in assembly
// you can take a look in the second main.asm file
#define init(x) ((x) == 1 ? 1 : (x) * step((x)-1))
#define step(x) ((x)*(x-1))
#define factorial(x) init((x)+1)
int main(){
int res = factorial(5);
printf("%d",res);
return 0;
}
.LC0:
.string "%d"
main:
push rbp
mov rbp, rsp
sub rsp, 16
mov DWORD PTR [rbp-4], 120
mov eax, DWORD PTR [rbp-4]
mov esi, eax
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call printf
mov eax, 0
leave
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment