kernel-asmg.asm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. ;; This file is part of asmc, a bootstrapping OS with minimal seed
  2. ;; Copyright (C) 2018 Giovanni Mascellani <gio@debian.org>
  3. ;; https://gitlab.com/giomasce/asmc
  4. ;; This program 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. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. str_platform_g_compile:
  15. db 'platform_g_compile'
  16. db 0
  17. str_platform_setjmp:
  18. db 'platform_setjmp'
  19. db 0
  20. str_platform_longjmp:
  21. db 'platform_longjmp'
  22. db 0
  23. str_init_compile_main:
  24. db 'Will now compile main.g...'
  25. db NEWLINE
  26. db 0
  27. str_init_launch_main:
  28. db 'Will now call main!'
  29. db NEWLINE
  30. db 0
  31. str_main_g:
  32. db 'main.g'
  33. db 0
  34. str_main:
  35. db 'main'
  36. db 0
  37. ;; void platform_g_compile(char *filename)
  38. platform_g_compile:
  39. ;; Prepare to write in memory
  40. mov eax, write_mem_ptr
  41. mov ecx, heap_ptr
  42. mov edx, [ecx]
  43. mov [eax], edx
  44. ;; Load the parameter and save some registers
  45. mov eax, [esp+4]
  46. push edx
  47. push edx
  48. ;; Open the file
  49. push eax
  50. call platform_open_file
  51. add esp, 4
  52. pop edx
  53. ;; Assemble the file
  54. push 0
  55. push edx
  56. push 0
  57. push eax
  58. call compile
  59. add esp, 16
  60. ;; Actually allocate used heap memory, so that new allocations will
  61. ;; not overwrite it
  62. mov eax, write_mem_ptr
  63. mov ecx, heap_ptr
  64. mov edx, [eax]
  65. sub edx, [ecx]
  66. push edx
  67. call platform_allocate
  68. add esp, 4
  69. ;; Assert that the allocation gave us what we expected
  70. pop edx
  71. cmp edx, eax
  72. jne platform_panic
  73. ;; Discard temporary labels
  74. call discard_temp_labels
  75. ret
  76. discard_temp_labels:
  77. push esi
  78. push edi
  79. push ebx
  80. ;; Use esi for the source index and edi for the dst index
  81. mov esi, 0
  82. mov edi, 0
  83. discard_temp_labels_loop:
  84. ;; Check for termination
  85. mov eax, symbol_num
  86. cmp [eax], esi
  87. je discard_temp_labels_end
  88. ;; Check if the symbol is a temp label
  89. mov ecx, symbol_names_ptr
  90. mov ecx, [ecx]
  91. mov eax, MAX_SYMBOL_NAME_LEN
  92. mul esi
  93. add eax, ecx
  94. cmp BYTE [eax], DOT
  95. je discard_temp_label_loop_end
  96. ;; If the two pointers are equal, do nothing
  97. cmp esi, edi
  98. je discard_temp_label_update_dest
  99. ;; Copy name
  100. mov ebx, eax
  101. mov eax, MAX_SYMBOL_NAME_LEN
  102. mul edi
  103. add eax, ecx
  104. push ebx
  105. push eax
  106. call strcpy
  107. add esp, 8
  108. ;; Copy location
  109. mov ecx, symbol_locs_ptr
  110. mov ecx, [ecx]
  111. mov eax, 4
  112. mul esi
  113. add eax, ecx
  114. mov ebx, eax
  115. mov eax, 4
  116. mul edi
  117. add eax, ecx
  118. mov edx, [ebx]
  119. mov [eax], edx
  120. ;; Copy arity
  121. mov ecx, symbol_arities_ptr
  122. mov ecx, [ecx]
  123. mov eax, 4
  124. mul esi
  125. add eax, ecx
  126. mov ebx, eax
  127. mov eax, 4
  128. mul edi
  129. add eax, ecx
  130. mov edx, [ebx]
  131. mov [eax], edx
  132. discard_temp_label_update_dest:
  133. add edi, 1
  134. discard_temp_label_loop_end:
  135. ;; Increment reading pointer and reloop
  136. add esi, 1
  137. jmp discard_temp_labels_loop
  138. discard_temp_labels_end:
  139. ;; Save the new length
  140. mov eax, symbol_num
  141. mov [eax], edi
  142. pop ebx
  143. pop edi
  144. pop esi
  145. ret
  146. start:
  147. ;; Init compiler
  148. call init_g_compiler
  149. call init_g_operations
  150. ;; Add some asmg-specific platform_* calls
  151. push 1
  152. push platform_g_compile
  153. push str_platform_g_compile
  154. call add_symbol
  155. add esp, 12
  156. push 1
  157. push platform_setjmp
  158. push str_platform_setjmp
  159. call add_symbol
  160. add esp, 12
  161. push 2
  162. push platform_longjmp
  163. push str_platform_longjmp
  164. call add_symbol
  165. add esp, 12
  166. ;; Log
  167. push str_init_compile_main
  168. push 1
  169. call platform_log
  170. add esp, 8
  171. ;; Assemble main.asm
  172. push str_main_g
  173. call platform_g_compile
  174. add esp, 4
  175. ;; Log
  176. push str_init_launch_main
  177. push 1
  178. call platform_log
  179. add esp, 8
  180. ;; Set EBP to zero, so that stack traces originating from the G code
  181. ;; are cut
  182. push ebp
  183. mov ebp, 0
  184. ;; Enable PMC
  185. call enable_pmc
  186. ;; Call main
  187. push 0
  188. push str_main
  189. call platform_get_symbol
  190. add esp, 8
  191. call eax
  192. pop ebp
  193. ret
  194. ret:
  195. ret
  196. str_equal:
  197. db '='
  198. db 0
  199. equal:
  200. mov eax, [esp+4]
  201. mov ecx, [esp+8]
  202. mov [ecx], eax
  203. ret
  204. str_equalc:
  205. db '=c'
  206. db 0
  207. equalc:
  208. mov eax, [esp+4]
  209. mov ecx, [esp+8]
  210. mov [ecx], al
  211. ret
  212. str_param:
  213. db 'param'
  214. db 0
  215. param:
  216. mov eax, [esp+4]
  217. mov edx, 4
  218. mul edx
  219. add eax, 8
  220. add eax, ebp
  221. mov eax, [eax]
  222. ret
  223. str_plus:
  224. db '+'
  225. db 0
  226. plus:
  227. mov eax, [esp+8]
  228. add eax, [esp+4]
  229. ret
  230. str_minus:
  231. db '-'
  232. db 0
  233. minus:
  234. mov eax, [esp+8]
  235. sub eax, [esp+4]
  236. ret
  237. str_un_minus:
  238. db '--'
  239. db 0
  240. un_minus:
  241. mov eax, [esp+8]
  242. neg eax
  243. ret
  244. str_times:
  245. db '*'
  246. db 0
  247. times_op:
  248. mov eax, [esp+8]
  249. imul DWORD [esp+4]
  250. ret
  251. str_over:
  252. db '/'
  253. db 0
  254. over:
  255. mov edx, 0
  256. mov eax, [esp+8]
  257. idiv DWORD [esp+4]
  258. ret
  259. str_mod:
  260. db '%'
  261. db 0
  262. mod:
  263. mov edx, 0
  264. mov eax, [esp+8]
  265. idiv DWORD [esp+4]
  266. mov eax, edx
  267. ret
  268. str_eq:
  269. db '=='
  270. db 0
  271. eq:
  272. mov eax, 1
  273. mov ecx, [esp+8]
  274. cmp ecx, [esp+4]
  275. je ret
  276. mov eax, 0
  277. ret
  278. str_neq:
  279. db '!='
  280. db 0
  281. neq:
  282. mov eax, 1
  283. mov ecx, [esp+8]
  284. cmp ecx, [esp+4]
  285. jne ret
  286. mov eax, 0
  287. ret
  288. str_l:
  289. db '<'
  290. db 0
  291. l:
  292. mov eax, 1
  293. mov ecx, [esp+8]
  294. cmp ecx, [esp+4]
  295. jl ret
  296. mov eax, 0
  297. ret
  298. str_le:
  299. db '<='
  300. db 0
  301. le:
  302. mov eax, 1
  303. mov ecx, [esp+8]
  304. cmp ecx, [esp+4]
  305. jle ret
  306. mov eax, 0
  307. ret
  308. str_g:
  309. db '>'
  310. db 0
  311. g:
  312. mov eax, 1
  313. mov ecx, [esp+8]
  314. cmp ecx, [esp+4]
  315. jg ret
  316. mov eax, 0
  317. ret
  318. str_ge:
  319. db '>='
  320. db 0
  321. ge:
  322. mov eax, 1
  323. mov ecx, [esp+8]
  324. cmp ecx, [esp+4]
  325. jge ret
  326. mov eax, 0
  327. ret
  328. str_shl:
  329. db '<<'
  330. db 0
  331. shl:
  332. mov ecx, [esp+4]
  333. mov eax, [esp+8]
  334. shl eax, cl
  335. ret
  336. str_shr:
  337. db '>>'
  338. db 0
  339. shr:
  340. mov ecx, [esp+4]
  341. mov eax, [esp+8]
  342. shr eax, cl
  343. ret
  344. str_and:
  345. db '&'
  346. db 0
  347. and:
  348. mov eax, [esp+8]
  349. and eax, [esp+4]
  350. ret
  351. str_or:
  352. db '|'
  353. db 0
  354. or:
  355. mov eax, [esp+8]
  356. or eax, [esp+4]
  357. ret
  358. str_xor:
  359. db '^'
  360. db 0
  361. xor:
  362. mov eax, [esp+8]
  363. xor eax, [esp+4]
  364. ret
  365. str_not:
  366. db '~'
  367. db 0
  368. not:
  369. mov eax, [esp+4]
  370. not eax
  371. ret
  372. str_land:
  373. db '&&'
  374. db 0
  375. land:
  376. mov eax, 0
  377. cmp DWORD [esp+8], 0
  378. je ret
  379. cmp DWORD [esp+4], 0
  380. je ret
  381. mov eax, 1
  382. ret
  383. str_lor:
  384. db '||'
  385. db 0
  386. lor:
  387. mov eax, 1
  388. cmp DWORD [esp+8], 0
  389. jne ret
  390. cmp DWORD [esp+4], 0
  391. jne ret
  392. mov eax, 0
  393. ret
  394. str_lnot:
  395. db '!'
  396. db 0
  397. lnot:
  398. mov eax, 0
  399. cmp DWORD [esp+4], 0
  400. jne ret
  401. mov eax, 1
  402. ret
  403. str_deref:
  404. db '**'
  405. db 0
  406. deref:
  407. mov eax, [esp+4]
  408. mov eax, [eax]
  409. ret
  410. str_derefc:
  411. db '**c'
  412. db 0
  413. derefc:
  414. mov edx, [esp+4]
  415. mov eax, 0
  416. mov al, [edx]
  417. ret
  418. str_inb:
  419. db 'inb'
  420. db 0
  421. inb:
  422. mov edx, [esp+4]
  423. mov eax, 0
  424. in al, dx
  425. ret
  426. str_inw:
  427. db 'inw'
  428. db 0
  429. inw:
  430. mov edx, [esp+4]
  431. mov eax, 0
  432. in ax, dx
  433. ret
  434. str_ind:
  435. db 'ind'
  436. db 0
  437. ind:
  438. mov edx, [esp+4]
  439. mov eax, 0
  440. in eax, dx
  441. ret
  442. str_outb:
  443. db 'outb'
  444. db 0
  445. outb:
  446. mov eax, [esp+4]
  447. mov edx, [esp+8]
  448. out dx, al
  449. ret
  450. str_outw:
  451. db 'outw'
  452. db 0
  453. outw:
  454. mov eax, [esp+4]
  455. mov edx, [esp+8]
  456. out dx, ax
  457. ret
  458. str_outd:
  459. db 'outd'
  460. db 0
  461. outd:
  462. mov eax, [esp+4]
  463. mov edx, [esp+8]
  464. out dx, eax
  465. ret
  466. str_itoa:
  467. db 'itoa'
  468. db 0
  469. str_frame_ptr:
  470. db '__frame_ptr'
  471. db 0
  472. frame_ptr:
  473. mov eax, ebp
  474. ret
  475. str_max_symbol_name_len:
  476. db '__max_symbol_name_len'
  477. db 0
  478. max_symbol_name_len:
  479. mov eax, MAX_SYMBOL_NAME_LEN
  480. ret
  481. str_symbol_table_len:
  482. db '__symbol_table_len'
  483. db 0
  484. symbol_table_len:
  485. mov eax, SYMBOL_TABLE_LEN
  486. ret
  487. str_symbol_num:
  488. db '__symbol_num'
  489. db 0
  490. symbol_num_func:
  491. mov eax, symbol_num
  492. mov eax, [eax]
  493. ret
  494. str_symbol_names:
  495. db '__symbol_names'
  496. db 0
  497. symbol_names_func:
  498. mov eax, symbol_names_ptr
  499. mov eax, [eax]
  500. ret
  501. str_symbol_locs:
  502. db '__symbol_locs'
  503. db 0
  504. symbol_locs_func:
  505. mov eax, symbol_locs_ptr
  506. mov eax, [eax]
  507. ret
  508. str_symbol_arities:
  509. db '__symbol_arities'
  510. db 0
  511. symbol_arities_func:
  512. mov eax, symbol_arities_ptr
  513. mov eax, [eax]
  514. ret
  515. str_ret_instr:
  516. db '__ret_instr'
  517. db 0
  518. ret_instr:
  519. call read_ret_instr
  520. ret
  521. init_g_operations:
  522. push 2
  523. push equal
  524. push str_equal
  525. call add_symbol
  526. add esp, 12
  527. push 2
  528. push equalc
  529. push str_equalc
  530. call add_symbol
  531. add esp, 12
  532. push 1
  533. push param
  534. push str_param
  535. call add_symbol
  536. add esp, 12
  537. push 2
  538. push plus
  539. push str_plus
  540. call add_symbol
  541. add esp, 12
  542. push 2
  543. push minus
  544. push str_minus
  545. call add_symbol
  546. add esp, 12
  547. push 1
  548. push un_minus
  549. push str_un_minus
  550. call add_symbol
  551. add esp, 12
  552. push 2
  553. push times_op
  554. push str_times
  555. call add_symbol
  556. add esp, 12
  557. push 2
  558. push over
  559. push str_over
  560. call add_symbol
  561. add esp, 12
  562. push 2
  563. push mod
  564. push str_mod
  565. call add_symbol
  566. add esp, 12
  567. push 2
  568. push eq
  569. push str_eq
  570. call add_symbol
  571. add esp, 12
  572. push 2
  573. push neq
  574. push str_neq
  575. call add_symbol
  576. add esp, 12
  577. push 2
  578. push l
  579. push str_l
  580. call add_symbol
  581. add esp, 12
  582. push 2
  583. push le
  584. push str_le
  585. call add_symbol
  586. add esp, 12
  587. push 2
  588. push g
  589. push str_g
  590. call add_symbol
  591. add esp, 12
  592. push 2
  593. push ge
  594. push str_ge
  595. call add_symbol
  596. add esp, 12
  597. push 2
  598. push shl
  599. push str_shl
  600. call add_symbol
  601. add esp, 12
  602. push 2
  603. push shr
  604. push str_shr
  605. call add_symbol
  606. add esp, 12
  607. push 2
  608. push and
  609. push str_and
  610. call add_symbol
  611. add esp, 12
  612. push 2
  613. push or
  614. push str_or
  615. call add_symbol
  616. add esp, 12
  617. push 2
  618. push xor
  619. push str_xor
  620. call add_symbol
  621. add esp, 12
  622. push 1
  623. push not
  624. push str_not
  625. call add_symbol
  626. add esp, 12
  627. push 2
  628. push land
  629. push str_land
  630. call add_symbol
  631. add esp, 12
  632. push 2
  633. push lor
  634. push str_lor
  635. call add_symbol
  636. add esp, 12
  637. push 1
  638. push lnot
  639. push str_lnot
  640. call add_symbol
  641. add esp, 12
  642. push 1
  643. push inb
  644. push str_inb
  645. call add_symbol
  646. add esp, 12
  647. push 1
  648. push inw
  649. push str_inw
  650. call add_symbol
  651. add esp, 12
  652. push 1
  653. push ind
  654. push str_ind
  655. call add_symbol
  656. add esp, 12
  657. push 2
  658. push outb
  659. push str_outb
  660. call add_symbol
  661. add esp, 12
  662. push 2
  663. push outw
  664. push str_outw
  665. call add_symbol
  666. add esp, 12
  667. push 2
  668. push outd
  669. push str_outd
  670. call add_symbol
  671. add esp, 12
  672. push 1
  673. push deref
  674. push str_deref
  675. call add_symbol
  676. add esp, 12
  677. push 1
  678. push derefc
  679. push str_derefc
  680. call add_symbol
  681. add esp, 12
  682. push 1
  683. push itoa
  684. push str_itoa
  685. call add_symbol
  686. add esp, 12
  687. push 0
  688. push frame_ptr
  689. push str_frame_ptr
  690. call add_symbol
  691. add esp, 12
  692. push 0
  693. push max_symbol_name_len
  694. push str_max_symbol_name_len
  695. call add_symbol
  696. add esp, 12
  697. push 0
  698. push symbol_table_len
  699. push str_symbol_table_len
  700. call add_symbol
  701. add esp, 12
  702. push 0
  703. push symbol_num_func
  704. push str_symbol_num
  705. call add_symbol
  706. add esp, 12
  707. push 0
  708. push symbol_names_func
  709. push str_symbol_names
  710. call add_symbol
  711. add esp, 12
  712. push 0
  713. push symbol_locs_func
  714. push str_symbol_locs
  715. call add_symbol
  716. add esp, 12
  717. push 0
  718. push symbol_arities_func
  719. push str_symbol_arities
  720. call add_symbol
  721. add esp, 12
  722. push 0
  723. push ret_instr
  724. push str_ret_instr
  725. call add_symbol
  726. add esp, 12
  727. ret