bootstrap.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of M2-Planet.
  3. *
  4. * M2-Planet is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * M2-Planet is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. // CONSTANT stdin 0
  18. // CONSTANT stdout 1
  19. // CONSTANT stderr 2
  20. // CONSTANT EOF 0xFFFFFFFF
  21. // CONSTANT NULL 0
  22. // CONSTANT EXIT_FAILURE 1
  23. // CONSTANT EXIT_SUCCESS 0
  24. // CONSTANT TRUE 1
  25. // CONSTANT FALSE 0
  26. int fgetc(FILE* f)
  27. {
  28. asm("mov_eax, %3"
  29. "lea_ebx,[esp+DWORD] %4"
  30. "mov_ebx,[ebx]"
  31. "push_ebx"
  32. "mov_ecx,esp"
  33. "mov_edx, %1"
  34. "int !0x80"
  35. "test_eax,eax"
  36. "pop_eax"
  37. "jne %FUNCTION_fgetc_Done"
  38. "mov_eax, %-1"
  39. ":FUNCTION_fgetc_Done");
  40. }
  41. void fputc(char s, FILE* f)
  42. {
  43. asm("mov_eax, %4"
  44. "lea_ebx,[esp+DWORD] %4"
  45. "mov_ebx,[ebx]"
  46. "lea_ecx,[esp+DWORD] %8"
  47. "mov_edx, %1"
  48. "int !0x80");
  49. }
  50. void fputs(char* s, FILE* f)
  51. {
  52. while(0 != s[0])
  53. {
  54. fputc(s[0], f);
  55. s = s + 1;
  56. }
  57. }
  58. FILE* open(char* name, int flag, int mode)
  59. {
  60. asm("lea_ebx,[esp+DWORD] %12"
  61. "mov_ebx,[ebx]"
  62. "lea_ecx,[esp+DWORD] %8"
  63. "mov_ecx,[ecx]"
  64. "lea_edx,[esp+DWORD] %4"
  65. "mov_edx,[edx]"
  66. "mov_eax, %5"
  67. "int !0x80");
  68. }
  69. FILE* fopen(char* filename, char* mode)
  70. {
  71. FILE* f;
  72. if('w' == mode[0])
  73. { /* 577 is O_WRONLY|O_CREAT|O_TRUNC, 384 is 600 in octal */
  74. f = open(filename, 577 , 384);
  75. }
  76. else
  77. { /* Everything else is a read */
  78. f = open(filename, 0, 0);
  79. }
  80. /* Negative numbers are error codes */
  81. if(0 > f)
  82. {
  83. return 0;
  84. }
  85. return f;
  86. }
  87. int close(int fd)
  88. {
  89. asm("lea_ebx,[esp+DWORD] %4"
  90. "mov_ebx,[ebx]"
  91. "mov_eax, %6"
  92. "int !0x80");
  93. }
  94. int fclose(FILE* stream)
  95. {
  96. int error = close(stream);
  97. return error;
  98. }
  99. int brk(void *addr)
  100. {
  101. asm("mov_eax,[esp+DWORD] %4"
  102. "push_eax"
  103. "mov_eax, %45"
  104. "pop_ebx"
  105. "int !0x80");
  106. }
  107. long _malloc_ptr;
  108. long _brk_ptr;
  109. void* malloc(int size)
  110. {
  111. if(NULL == _brk_ptr)
  112. {
  113. _brk_ptr = brk(0);
  114. _malloc_ptr = _brk_ptr;
  115. }
  116. if(_brk_ptr < _malloc_ptr + size)
  117. {
  118. _brk_ptr = brk(_malloc_ptr + size);
  119. if(-1 == _brk_ptr) return 0;
  120. }
  121. long old_malloc = _malloc_ptr;
  122. _malloc_ptr = _malloc_ptr + size;
  123. return old_malloc;
  124. }
  125. int strlen(char* str )
  126. {
  127. int i = 0;
  128. while(0 != str[i]) i = i + 1;
  129. return i;
  130. }
  131. void* memset(void* ptr, int value, int num)
  132. {
  133. char* s;
  134. for(s = ptr; 0 < num; num = num - 1)
  135. {
  136. s[0] = value;
  137. s = s + 1;
  138. }
  139. }
  140. void* calloc(int count, int size)
  141. {
  142. void* ret = malloc(count * size);
  143. if(NULL == ret) return NULL;
  144. memset(ret, 0, (count * size));
  145. return ret;
  146. }
  147. void free(void* l)
  148. {
  149. return;
  150. }
  151. void exit(int value)
  152. {
  153. asm("pop_ebx"
  154. "pop_ebx"
  155. "mov_eax, %1"
  156. "int !0x80");
  157. }