Created
September 4, 2025 09:12
-
-
Save C00kiie/c4a071d09860416b18c7935b829e2db6 to your computer and use it in GitHub Desktop.
calculate factorial value using c preprocessor/macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .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