get_machine_x86.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. ## This file was generated by running:
  2. ## cat functions/exit.c functions/file.c functions/file_print.c functions/malloc.c functions/calloc.c functions/uname.c test/test24/get_machine.c >| ../stage0/stage3/get_machine_x86.c
  3. ## inside stage0's repo
  4. /* Copyright (C) 2016 Jeremiah Orians
  5. * This file is part of stage0.
  6. *
  7. * stage0 is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * stage0 is distributed in the hope that it will be useful,
  13. * but 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 stage0. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. // CONSTANT EXIT_FAILURE 1
  21. // CONSTANT EXIT_SUCCESS 0
  22. void exit(int value)
  23. {
  24. asm("POP_ebx"
  25. "POP_ebx"
  26. "LOAD_IMMEDIATE_eax %1"
  27. "INT_80");
  28. }
  29. /* Copyright (C) 2016 Jeremiah Orians
  30. * This file is part of stage0.
  31. *
  32. * stage0 is free software: you can redistribute it and/or modify
  33. * it under the terms of the GNU General Public License as published by
  34. * the Free Software Foundation, either version 3 of the License, or
  35. * (at your option) any later version.
  36. *
  37. * stage0 is distributed in the hope that it will be useful,
  38. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  39. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40. * GNU General Public License for more details.
  41. *
  42. * You should have received a copy of the GNU General Public License
  43. * along with stage0. If not, see <http://www.gnu.org/licenses/>.
  44. */
  45. // CONSTANT stdin 0
  46. // CONSTANT stdout 1
  47. // CONSTANT stderr 2
  48. // CONSTANT EOF 0xFFFFFFFF
  49. int fgetc(FILE* f)
  50. {
  51. asm("LOAD_IMMEDIATE_eax %3"
  52. "LOAD_EFFECTIVE_ADDRESS_ebx %4"
  53. "LOAD_INTEGER_ebx"
  54. "PUSH_ebx"
  55. "COPY_esp_to_ecx"
  56. "LOAD_IMMEDIATE_edx %1"
  57. "INT_80"
  58. "TEST"
  59. "POP_eax"
  60. "JUMP_NE8 !FUNCTION_fgetc_Done"
  61. "LOAD_IMMEDIATE_eax %-1"
  62. ":FUNCTION_fgetc_Done");
  63. }
  64. void fputc(char s, FILE* f)
  65. {
  66. asm("LOAD_IMMEDIATE_eax %4"
  67. "LOAD_EFFECTIVE_ADDRESS_ebx %4"
  68. "LOAD_INTEGER_ebx"
  69. "LOAD_EFFECTIVE_ADDRESS_ecx %8"
  70. "LOAD_IMMEDIATE_edx %1"
  71. "INT_80");
  72. }
  73. /* Important values needed for open
  74. * O_RDONLY => 0
  75. * O_WRONLY => 1
  76. * O_RDWR => 2
  77. * O_CREAT => 64
  78. * O_TRUNC => 512
  79. * S_IRWXU => 00700
  80. * S_IXUSR => 00100
  81. * S_IWUSR => 00200
  82. * S_IRUSR => 00400
  83. */
  84. FILE* open(char* name, int flag, int mode)
  85. {
  86. asm("LOAD_EFFECTIVE_ADDRESS_ebx %12"
  87. "LOAD_INTEGER_ebx"
  88. "LOAD_EFFECTIVE_ADDRESS_ecx %8"
  89. "LOAD_INTEGER_ecx"
  90. "LOAD_EFFECTIVE_ADDRESS_edx %4"
  91. "LOAD_INTEGER_edx"
  92. "LOAD_IMMEDIATE_eax %5"
  93. "INT_80");
  94. }
  95. FILE* fopen(char* filename, char* mode)
  96. {
  97. FILE* f;
  98. if('w' == mode[0])
  99. { /* 577 is O_WRONLY|O_CREAT|O_TRUNC, 384 is 600 in octal */
  100. f = open(filename, 577 , 384);
  101. }
  102. else
  103. { /* Everything else is a read */
  104. f = open(filename, 0, 0);
  105. }
  106. /* Negative numbers are error codes */
  107. if(0 > f)
  108. {
  109. return 0;
  110. }
  111. return f;
  112. }
  113. int close(int fd)
  114. {
  115. asm("LOAD_EFFECTIVE_ADDRESS_ebx %4"
  116. "LOAD_IMMEDIATE_eax %6"
  117. "INT_80");
  118. }
  119. int fclose(FILE* stream)
  120. {
  121. int error = close(stream);
  122. return error;
  123. }
  124. /* Copyright (C) 2016 Jeremiah Orians
  125. * This file is part of stage0.
  126. *
  127. * stage0 is free software: you can redistribute it and/or modify
  128. * it under the terms of the GNU General Public License as published by
  129. * the Free Software Foundation, either version 3 of the License, or
  130. * (at your option) any later version.
  131. *
  132. * stage0 is distributed in the hope that it will be useful,
  133. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  134. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  135. * GNU General Public License for more details.
  136. *
  137. * You should have received a copy of the GNU General Public License
  138. * along with stage0. If not, see <http://www.gnu.org/licenses/>.
  139. */
  140. #include<stdio.h>
  141. // void fputc(char s, FILE* f);
  142. void file_print(char* s, FILE* f)
  143. {
  144. while(0 != s[0])
  145. {
  146. fputc(s[0], f);
  147. s = s + 1;
  148. }
  149. }
  150. /* Copyright (C) 2016 Jeremiah Orians
  151. * This file is part of stage0.
  152. *
  153. * stage0 is free software: you can redistribute it and/or modify
  154. * it under the terms of the GNU General Public License as published by
  155. * the Free Software Foundation, either version 3 of the License, or
  156. * (at your option) any later version.
  157. *
  158. * stage0 is distributed in the hope that it will be useful,
  159. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  160. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  161. * GNU General Public License for more details.
  162. *
  163. * You should have received a copy of the GNU General Public License
  164. * along with stage0. If not, see <http://www.gnu.org/licenses/>.
  165. */
  166. // CONSTANT NULL 0
  167. void* malloc(int size)
  168. {
  169. asm("STORE_eax_into_ESP_IMMEDIATE8 !4"
  170. "PUSH_eax"
  171. "LOAD_IMMEDIATE_eax %45"
  172. "LOAD_IMMEDIATE_ebx %0"
  173. "INT_80"
  174. "POP_ebx"
  175. "ADD_eax_to_ebx"
  176. "PUSH_eax"
  177. "PUSH_ebx"
  178. "LOAD_IMMEDIATE_eax %45"
  179. "INT_80"
  180. "POP_ebx"
  181. "CMP"
  182. "POP_eax"
  183. "JUMP_EQ8 !FUNCTION_malloc_Done"
  184. "LOAD_IMMEDIATE_eax %-1"
  185. ":FUNCTION_malloc_Done");
  186. }
  187. /* Copyright (C) 2016 Jeremiah Orians
  188. * This file is part of stage0.
  189. *
  190. * stage0 is free software: you can redistribute it and/or modify
  191. * it under the terms of the GNU General Public License as published by
  192. * the Free Software Foundation, either version 3 of the License, or
  193. * (at your option) any later version.
  194. *
  195. * stage0 is distributed in the hope that it will be useful,
  196. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  197. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  198. * GNU General Public License for more details.
  199. *
  200. * You should have received a copy of the GNU General Public License
  201. * along with stage0. If not, see <http://www.gnu.org/licenses/>.
  202. */
  203. // void* malloc(int size);
  204. void* memset(void* ptr, int value, int num)
  205. {
  206. char* s;
  207. for(s = ptr; 0 < num; num = num - 1)
  208. {
  209. s[0] = value;
  210. s = s + 1;
  211. }
  212. }
  213. void* calloc(int count, int size)
  214. {
  215. void* ret = malloc(count * size);
  216. memset(ret, 0, (count * size));
  217. return ret;
  218. }
  219. void free(void* l)
  220. {
  221. return;
  222. }
  223. /* Copyright (C) 2016 Jeremiah Orians
  224. * This file is part of stage0.
  225. *
  226. * stage0 is free software: you can redistribute it and/or modify
  227. * it under the terms of the GNU General Public License as published by
  228. * the Free Software Foundation, either version 3 of the License, or
  229. * (at your option) any later version.
  230. *
  231. * stage0 is distributed in the hope that it will be useful,
  232. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  233. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  234. * GNU General Public License for more details.
  235. *
  236. * You should have received a copy of the GNU General Public License
  237. * along with stage0. If not, see <http://www.gnu.org/licenses/>.
  238. */
  239. struct utsname
  240. {
  241. char sysname[65]; /* Operating system name (e.g., "Linux") */
  242. char nodename[65]; /* Name within "some implementation-defined network" */
  243. char release[65]; /* Operating system release (e.g., "2.6.28") */
  244. char version[65]; /* Operating system version */
  245. char machine[65]; /* Hardware identifier */
  246. };
  247. int uname(struct utsname* unameData)
  248. {
  249. asm("LOAD_EFFECTIVE_ADDRESS_ebx %4"
  250. "LOAD_INTEGER_ebx"
  251. "LOAD_IMMEDIATE_eax %109"
  252. "INT_80");
  253. }
  254. /* -*- c-file-style: "linux";indent-tabs-mode:t -*- */
  255. /* Copyright (C) 2017 Jeremiah Orians
  256. * This file is part of mescc-tools.
  257. *
  258. * mescc-tools is free software: you can redistribute it and/or modify
  259. * it under the terms of the GNU General Public License as published by
  260. * the Free Software Foundation, either version 3 of the License, or
  261. * (at your option) any later version.
  262. *
  263. * mescc-tools is distributed in the hope that it will be useful,
  264. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  265. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  266. * GNU General Public License for more details.
  267. *
  268. * You should have received a copy of the GNU General Public License
  269. * along with mescc-tools. If not, see <http://www.gnu.org/licenses/>.
  270. */
  271. #include <stdio.h>
  272. #include <stdlib.h>
  273. #include <sys/utsname.h>
  274. void file_print(char* s, FILE* f);
  275. /* Standard C main program */
  276. int main()
  277. {
  278. struct utsname* unameData = calloc(1, sizeof(struct utsname));
  279. uname(unameData);
  280. file_print(unameData->machine, stdout);
  281. file_print("\n", stdout);
  282. return EXIT_SUCCESS;
  283. }