kernel-asmg.asm 12 KB

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