flat assembler
Message board for the users of flat assembler.

Index > Windows > The best start to a Windows program

Goto page Previous  1, 2
Author
Thread Post new topic Reply to topic
Furs



Joined: 04 Mar 2016
Posts: 2568
Furs 08 May 2018, 12:18
fasmnewbie wrote:
C is not a "Windows" program. It's a high-level language that should appear / behave the same across platforms. Whatever happens behind the scene should not be of concern to users / programmers because they are platform-specific by then. It's the same with C's exit sequence. Whatever happens past the RET is ignorable.
Oh, you're perfectly right but I was thinking of the thread's title when I read your post, didn't realize you were talking about C itself.
Post 08 May 2018, 12:18
View user's profile Send private message Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 555
fasmnewbie 08 May 2018, 19:41
Furs wrote:
fasmnewbie wrote:
C is not a "Windows" program. It's a high-level language that should appear / behave the same across platforms. Whatever happens behind the scene should not be of concern to users / programmers because they are platform-specific by then. It's the same with C's exit sequence. Whatever happens past the RET is ignorable.
Oh, you're perfectly right but I was thinking of the thread's title when I read your post, didn't realize you were talking about C itself.


It works either way. From C to binary, main should be the startup function. OTOH, if you're approaching C from the bottom up, C expects similar startup structure, if you're producing an executable. "main" as the startup function is defined in C99 Specification:
Code:
5.1.2.2.1 Program startup
The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }    

It is also defined in C++98 Standard
Code:
3.6.1 Main function [basic.start.main]
1 A program shall contain a global function called main, which is the designated start of the program. [...]

2 An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation defined. All implementations shall allow both of the following definitions of main:

int main() { /* ... */ }
and

int main(int argc, char* argv[]) { /* ... */ }    

So in hosted environment, as in Windows and Linux, main is a special function assuming these special feature;

1. Global
2. Non-reentrant
3. Not overloaded
4. Should be able to take arguments (from the command lines)
5. Static, as in case of JAVA

My low-level examples here will not work without main as the startup module for both Linux and Windows. Or at least I have to go through non-conventional techniques (re-naming the startup point etc) to produce similar result.

If you're using C in a freestanding environment, then you are free to come up with your own startup function.
Post 08 May 2018, 19:41
View user's profile Send private message Visit poster's website Reply with quote
Melissa



Joined: 12 Apr 2012
Posts: 125
Melissa 09 May 2018, 02:50
I think that in C, main can be recursively called, while in C++ cannot. Can't remember exactly.
Post 09 May 2018, 02:50
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20454
Location: In your JS exploiting you and your system
revolution 09 May 2018, 02:53
Melissa wrote:
I think that in C, main can be recursively called ...
This code would seem to support that:
Code:
main(t,_,a)
char *a;
{return!0<t?t<3?main(-79,-13,a+main(-87,1-_,
main(-86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94, -27+t, a
)&&t == 2 ?_<13 ?main ( 2, _+1, "%s %d %d\n" ):9:16:t<0?t<-72?main(_,
t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+\
,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/\
+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n\
l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#\
n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\
#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/")
:t<-50?_==*a ?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a\
+1 ):0<t?main ( 2, 2 , "%s"):*a=='/'||main(0,main(-61,*a, "!ek;dc \
i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}
    
Compile it and see what you get. It is a good example of how awesomely clear and understandable C code can be. Wink
Post 09 May 2018, 02:53
View user's profile Send private message Visit poster's website Reply with quote
Melissa



Joined: 12 Apr 2012
Posts: 125
Melissa 09 May 2018, 03:03
Beautiful Wink This is art!
Post 09 May 2018, 03:03
View user's profile Send private message Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 555
fasmnewbie 09 May 2018, 07:12
revolution wrote:
Melissa wrote:
I think that in C, main can be recursively called ...
This code would seem to support that:
Code:
main(t,_,a)
char *a;
{return!0<t?t<3?main(-79,-13,a+main(-87,1-_,
main(-86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94, -27+t, a
)&&t == 2 ?_<13 ?main ( 2, _+1, "%s %d %d\n" ):9:16:t<0?t<-72?main(_,
t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+\
,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/\
+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n\
l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#\
n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\
#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/")
:t<-50?_==*a ?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a\
+1 ):0<t?main ( 2, 2 , "%s"):*a=='/'||main(0,main(-61,*a, "!ek;dc \
i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}
    
Compile it and see what you get. It is a good example of how awesomely clear and understandable C code can be. Wink


I guess thats what makes C a powerful language then. Everybody seems to be able to break anything out of it.
Post 09 May 2018, 07:12
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 09 May 2018, 07:50
C was intentionally parsimonious. Most of the dirty tricks usually come from the preprocessor. This particular excerpt is probably some IOCCC winner from years past. EDIT: Seems sources to latest 2018 IOCCC winners were just published a few days ago.
Post 09 May 2018, 07:50
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.