kernel-asmg.asm 12 KB

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