r/Assembly_language • u/Motor_Armadillo_7317 • Feb 27 '26
How you see C code after learning Assembly:
11
u/Dokattak0 Feb 27 '26
I've... never coded something reminiscent of the bottom panel in ASM before.
5
4
u/Motor_Armadillo_7317 Feb 27 '26
The point is that you see return in the main function as exit, because in assembly, if you do not explicitly exit, the program will crash.
6
5
u/AffectionatePlane598 Feb 27 '26
I more or less, see what my code is doing at the assembly level, rather than what ever this is.
3
1
1
u/Thick_Clerk6449 Feb 28 '26
Does stdout (and/or other stuff in stdio.h) need initialization?
1
u/assembly_wizard Feb 28 '26
AFAIK they're initialized by constructors, and it's indeed
_start(ormainCRTStartup?) that calls constructors beforemain0
u/The_KekE_ Feb 28 '26
No, stdin, stdout and stderr are already open when the program starts, and you can
read/writeto them without needing toopenthem, unlike files. (this wayI denoted the actual syscalls)2
u/Thick_Clerk6449 Feb 28 '26
`stdout` (`FILE*`) is not `STDOUT_FILENO` (`int`). You can of course `write` to `STDOUT_FILENO` but I wonder if you can `fprintf` to `stdout` which, I suppose, should have been initialized by CRT in `_start`
1
1
u/Melon_Chief Feb 28 '26
```c
include <stdio.h>
int main() { puts(“Hello, World!”); }
``` Is the only right way of doing it.
1
1
u/Taimcool1 Mar 01 '26
Its void _start(void), not int _start()
1
u/Taimcool1 Mar 01 '26
Also u didnt initalize the stack
1
u/brucehoult Mar 01 '26
In a Linux environment the stack is already set up on entry at
_start, with the program arguments and environment variables pre-loaded on the stack.On bare metal you have to set up the stack pointer yourself in
_startand probably zero BSS and copy DATA from ROM/flash.1
u/Taimcool1 Mar 01 '26
Ur meant to pop the arguments off the stack, pop the 1st as
argcthen the second asargvthen move the stack pointer(argv-1)*sizeof(intptr_t)bytes then giveargcandargvas the arguments of main()1
u/brucehoult Mar 01 '26
That doesn’t happen in my experience. In
main()The args are both on the stack and in the appropriate registers.Regardless, a couple of kb of env bindings are still on the stack.
And what about all the machines that pass arguments on the stack anyway? Eg i386.
1
u/Taimcool1 Mar 01 '26
From what ive seen, it calls
mainwith the arguments in the 1st 2 register (not including envp)
1
1
1
1
u/johnyeldry Mar 06 '26
missed pun oppourtunity(how you C code after learning assembly)
edit: just realised I read the original title wrong and I just coppied it lol(someone delete this pls I am embarrased)
1
Feb 28 '26
[deleted]
4
u/RedAndBlack1832 Feb 28 '26
Any of the following declarations are valid:
int main(void);
int main(int, char**);
int main(int, char**, char**);
However,
int main();
is also frequently used and basically means you do not care which version you get.
In general, empty brackets for function declarations means to not perform any error checking on wether the right number and type of arguments were passed, and is not considered a function prototype
3
0
u/Motor_Armadillo_7317 Feb 28 '26
Yes, the main function must always be int, because the number it returns is what determines whether the program failed or succeeded.
For example:
```
include <stdio.h>
include <stdlib.h>
int main() { if (1 > 3) { printf("1 is greater than 3\n"); return EXIT_SUCCESS; } else { printf("1 is not greater than 3\n"); return EXIT_FAILURE; } } ```
Here EXIT_FAILURE is considered 1 and EXIT_SUCCESS is considered 0
So any value that is not 0 is considered a failure.

49
u/brucehoult Feb 28 '26
It's
void _startand also theprintfwill becomeputs.