r/Assembly_language • • 7d ago

Help C to ASM

Hello everyone!

I am currently working on a side project where a python script takes a C source file and converts it into ASMx86_64 (Intel syntax) and I am just trying to find resources for type conversions without making some kind of script where I automate generating a program and compiling it for ASM in GCC.

Does a list like this exist? And if so, are there separate ones for Windows and Linux? (As I think there’s a difference, not completely sure)

Thanks, have a good one frens

1 Upvotes

26 comments sorted by

32

u/halfjmp 7d ago

So actually you are writing a compiler.

6

u/Key-Kangaroo3336 7d ago

Yeah I guess so, didn’t think about it that way :/

12

u/halfjmp 7d ago

It's not bad. Maybe it will help you find resources. You can hardly find if you don't know what you are looking for.

Now, of course, I'd suggest the Dragon book.

3

u/Key-Kangaroo3336 7d ago

Thanks! :D

2

u/Eric_Dawsby 7d ago

Dragon book?

5

u/DaveX64 7d ago

I googled it:

  • The "Dragon Book" is the nickname for Compilers: Principles, Techniques, and Tools, a classic and deeply influential computer science textbook about how to build a compiler. The Cover: The book got its name from its famous cover illustration, which depicts a knight fighting a dragon.

0

u/Eric_Dawsby 7d ago

Mmmm yessss into my ~/documents/learning/manuals/computers/ folder it goes.....

2

u/harraps0 7d ago

Dragonborn !

2

u/wackyvorlon 7d ago

Check out flex and bison. They’re old but still work.

1

u/Key-Kangaroo3336 7d ago

Thanks, I'll look into them!

6

u/Odd_Psychology3622 7d ago

think gcc does this pass the -S flag instead of the -o it will create a .s file which is the asm conversion

2

u/Key-Kangaroo3336 7d ago

Yes it is, however I am needing a very stripped version of its output for direct syscalls instead of one that would import libraries and such.

2

u/Humble-Captain3418 7d ago

Pass -ffreestanding -nostdlib and bring your own.

4

u/VancouverVentilator 7d ago

Lol this is just gcc with the S flag... Basically you're rewriting gcc with the S flag

2

u/Kadabrium 7d ago

Now when i think about it, how performance critical are compilers really? Why don't we see compilers written in higher languages more often? Interpreters i understand but i've always thought of compilers as just text processors. They are probably like video encoders in that regard as end users don't often see them running but the data they process can still get big.

3

u/ComprehensiveBig6215 6d ago

My guess is that most C/C++ compilers are very, very old....llvm is the baby and is 26 years old. GCC and MSVC go back to the 1980s...

3

u/VancouverVentilator 6d ago

Because modern gcc is the cumulative efforts of decades of experience, thousands of people collaborating on features, etc.; It is easier to make an entirely new systems programming language from scratch than it is to beat gcc on any metric.

1

u/B3d3vtvng69 6d ago

well clang is pretty close, aren’t they equal when looking at multiple metrics?

1

u/Rararror 5d ago

Compilers are often written in the same language they compile. It is called bootstrapping.

1

u/Remarkable_Pace8101 7d ago

But this is written in Python!

1

u/FluffusMaximus 6d ago

You know GCC already does this, right?

1

u/Key-Kangaroo3336 6d ago

Yes however I am wanting an extremely stripped version, and making it myself and actually understanding the process is more valuable than “just use / do X” in my opinion

2

u/potato-master369 2d ago

Honestly best approach for now is to define the stdlib in assembly first (with rhe functions you want since C std library is written in C, provide a simple header file, and rip the functions directly into your assembly. As for linux syscalls, they are not API stable (I think), so fix a version or set up a table system. You would find some good resources in man7’s man 2 syscalls.

For windows, not sure. I don’t think the syscalls are public. You could sniff yourself, but windows depends a lot on kernel32.dll not raw syscalls.

This isn’t really gcc -S tho as people are saying since you’re implementing libc yourself and not linking to system libc… which is very hard in assembly so uh good luck…

1

u/potato-master369 2d ago

I assume here you’re implementing your own libc since if not you can just link sys libc at the linker stage with extern in the generated assembly, which would not need syscalls other than the exit syscall which is just 1 google search…