Chapter 6:增強核心 === :::info 這是讀書筆記 ![book](https://hackmd.io/_uploads/H1rFFZZAkx.jpg =30%x) 作者:鄭鋼 出版社:佳魁資訊股份有限公司 出版日期:2017/05/31 ::: --- # 函數呼叫 Calling convention * cdecl: C declaration, 參數傳遞由右至左,caller 負責清理參數堆疊空間。 # Assembly and C ## System call >unistd.h >https://gist.github.com/duckinator/278652 Why need stdint.h? https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models 有範例可以練習 C and Assembly 相互 call function # 實現自己的列印函數 ## Source Code https://github.com/yifengyou/os-elephant/tree/master/code/c06/c ## Compile > build<span/>.sh ``` # build boot nasm -I boot/inc/ -o out/mbr.bin boot/mbr.S nasm -I boot/inc/ -o out/loader.bin boot/loader.S # build kernel x86_64-linux-gnu-gcc -m32 -c -o out/main.o kernel/main.c x86_64-linux-gnu-ld -m elf_i386 out/main.o -Ttext 0xc0001500 -e main -o out/kernel.bin ``` > gen<span/>.sh ``` dd if=../code/out/mbr.bin of=./sr_hd60m.img bs=512 count=1 conv=notrunc dd if=../code/out/loader.bin of=./sr_hd60m.img bs=512 count=4 seek=2 conv=notrunc dd if=../code/out/kernel.bin of=./sr_hd60m.img bs=512 count=200 seek=9 conv=notrunc ``` ## Result ![截圖 2025-06-30 上午10.21.46](https://hackmd.io/_uploads/rJZkHO1Hlg.png) # Inline Assembly 基本上 x86 assembly 分成 Intel Syntax and AT&T syntax。 https://en.wikipedia.org/wiki/X86_assembly_language inline assembly 是採用 AT&T syntax。 https://wiki.osdev.org/Inline_Assembly 統一都用這種格式來學習。 ![截圖 2025-06-29 下午4.03.18](https://hackmd.io/_uploads/HJD9QORElx.png) 有牽扯到外面C語言的變數,請使用 output oerands and input oerands。 clobbered registers list: 修改 不在 input/output 的 register, or memory。 這部分可以單獨練習,或是至少看得懂在幹嘛。