46dd2d8cd365f950bedb9c4a3f4004ada8e32e2c
[mes.git] / lib / linux / crt0.c
1 /* -*-comment-start: "//";comment-end:""-*-
2  * GNU Mes --- Maxwell Equations of Software
3  * Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
4  *
5  * This file is part of GNU Mes.
6  *
7  * GNU Mes is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or (at
10  * your option) any later version.
11  *
12  * GNU Mes is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Mes.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 // no clue what crt0.o is and why gcc-2.6.3 needs it
22
23 // instead of calling main, it seems to call either _main or ___main,
24 // let's try _main first
25
26 char **environ = 0;
27 int _main (int argc, char *argv[]);
28
29 #if __GNUC__
30
31 void
32 _start ()
33 {
34   asm (
35        "mov     %%ebp,%%eax\n\t"
36        "addl    $4,%%eax\n\t"
37        "movzbl  (%%eax),%%eax\n\t"
38        "addl    $3,%%eax\n\t"
39        "shl     $2,%%eax\n\t"
40        "add     %%ebp,%%eax\n\t"
41        "movl    %%eax,%0\n\t"
42        : "=environ" (environ)
43        : //no inputs ""
44        );
45   asm (
46        "mov     %%ebp,%%eax\n\t"
47        "addl    $8,%%eax\n\t"
48        "push    %%eax\n\t"
49
50        "mov     %%ebp,%%eax\n\t"
51        "addl    $4,%%eax\n\t"
52        "movzbl  (%%eax),%%eax\n\t"
53        "push    %%eax\n\t"
54
55        "call    _main\n\t"
56
57        "mov     %%eax,%%ebx\n\t"
58        "mov     $1,%%eax\n\t"
59        "int     $0x80\n\t"
60        "hlt      \n\t"
61        :
62        );
63 }
64
65 #elif __MESC__
66
67 int
68 _start ()
69 {
70   asm ("mov____%ebp,%eax");
71   asm ("add____$i8,%eax !4");
72
73   asm ("movzbl_(%eax),%eax");
74   asm ("add____$i8,%eax !3");
75
76   asm ("shl____$i8,%eax !0x02");
77   asm ("add____%ebp,%eax");
78   asm ("mov____%eax,0x32 &environ");
79
80   asm ("mov____%ebp,%eax");
81   asm ("add____$i8,%eax !8");
82   asm ("push___%eax");
83
84   asm ("mov____%ebp,%eax");
85   asm ("add____$i8,%eax !4");
86   asm ("movzbl_(%eax),%eax");
87   asm ("push___%eax");
88
89   _main ();
90
91   asm ("mov____%eax,%ebx");
92   asm ("mov____$i32,%eax %1");
93   asm ("int____$0x80");
94   asm ("hlt");
95 }
96
97 #endif