crt1.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include <mes/lib-mini.h>
  21. // int main (int argc, char *argv[]);
  22. // gcc x86_64 calling convention:
  23. // rdi, rsi, rdx, rcx, r8, r9, <stack0>, <stack1>
  24. // *INDENT-OFF*
  25. void
  26. _start ()
  27. {
  28. asm (
  29. "mov $0,%%eax\n\t"
  30. "mov %%eax,%0\n"
  31. : "=r" (__stdin)
  32. : //no inputs ""
  33. );
  34. asm (
  35. "mov $1,%%eax\n\t"
  36. "mov %%eax,%0\n"
  37. : "=r" (__stdout)
  38. : //no inputs ""
  39. );
  40. asm (
  41. "mov $2,%%eax\n\t"
  42. "mov %%eax,%0\n"
  43. : "=r" (__stderr)
  44. : //no inputs ""
  45. );
  46. asm (
  47. "mov %%rbp,%%rax\n\t"
  48. "add $8,%%rax\n\t"
  49. "mov (%%rax),%%rax\n\t"
  50. "add $3,%%rax\n\t"
  51. "shl $3,%%rax\n\t"
  52. "add %%rbp,%%rax\n\t"
  53. "mov %%rax,%0\n\t"
  54. "mov %%rax,%%rdx\n\t"
  55. : "=r" (environ)
  56. : //no inputs ""
  57. );
  58. asm (
  59. "mov %rbp,%rax\n\t"
  60. "add $16,%rax\n\t"
  61. "mov %rax,%rsi\n\t"
  62. "mov %rbp,%rax\n\t"
  63. "add $8,%rax\n\t"
  64. "mov (%rax),%rax\n\t"
  65. "mov %rax,%rdi\n\t"
  66. "call main\n\t"
  67. "mov %rax,%rdi\n\t"
  68. "mov $0x3c,%rax\n\t"
  69. "syscall \n\t"
  70. "hlt \n\t"
  71. );
  72. }