创建第一个x86的hello world

bochs生成bochsrc配置文件,并将执行选项修改为”gui_debug”

bxiamge 创建一个硬盘,并设置大小为 16M

将最终产生的脚本添加到bochsrc的配置文件当中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# configuration file generated by Bochs
plugin_ctrl: unmapped=true, biosdev=true, speaker=true, extfpuirq=true, parallel=true, serial=true, iodebug=true
config_interface: textconfig
# modfiy
display_library: x, options="gui_debug"
memory: host=32, guest=32
romimage: file="/usr/local/share/bochs/BIOS-bochs-latest", address=0x00000000, options=none
vgaromimage: file="/usr/local/share/bochs/VGABIOS-lgpl-latest"
# modify
boot: disk
floppy_bootsig_check: disabled=0
# no floppya
# no floppyb
ata0: enabled=true, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14
# modify
ata0-master: type=disk, path="master.img", mode=flat
ata0-slave: type=none
ata1: enabled=true, ioaddr1=0x170, ioaddr2=0x370, irq=15
ata1-master: type=none
ata1-slave: type=none
ata2: enabled=false
ata3: enabled=false
optromimage1: file=none
optromimage2: file=none
optromimage3: file=none
optromimage4: file=none
optramimage1: file=none
optramimage2: file=none
optramimage3: file=none
optramimage4: file=none
pci: enabled=1, chipset=i440fx
vga: extension=vbe, update_freq=5, realtime=1
cpu: count=1, ips=4000000, model=bx_generic, reset_on_triple_fault=1, cpuid_limit_winnt=0, ignore_bad_msrs=1, mwait_is_nop=0
cpuid: level=6, stepping=3, model=3, family=6, vendor_string="GenuineIntel", brand_string=" Intel(R) Pentium(R) 4 CPU "
cpuid: mmx=true, apic=xapic, simd=sse2, sse4a=false, misaligned_sse=false, sep=true
cpuid: movbe=false, adx=false, aes=false, sha=false, xsave=false, xsaveopt=false, smep=false
cpuid: smap=false, mwait=true
print_timestamps: enabled=0
debugger_log: -
magic_break: enabled=0
port_e9_hack: enabled=0
private_colormap: enabled=0
clock: sync=none, time0=local, rtc_sync=0
# no cmosimage
log: -
logprefix: %t%e%d
debug: action=ignore
info: action=report
error: action=report
panic: action=ask
keyboard: type=mf, serial_delay=250, paste_delay=100000, user_shortcut=none
mouse: type=ps2, enabled=false, toggle=ctrl+mbutton
speaker: enabled=true, mode=system
parport1: enabled=true, file=none
parport2: enabled=false
com1: enabled=true, mode=null
com2: enabled=false
com3: enabled=false
com4: enabled=false

利用 bximage 创建扁平格式硬盘映像文件

上述命令用于创建一个16MB大小、扇区大小为512字节的扁平格式硬盘映像文件 master.img。

yes | bximage -q -hd=16 -mode=create -sectsize=512 -imgmode=flat master.img 这个命令的含义如下:

  • yes 是一个命令,用于重复输出 “y”(表示同意)。
  • | 是管道操作符,用于将一个命令的输出作为另一个命令的输入。
  • bximage 是一个用于创建硬盘映像文件的工具。
  • -q 参数表示“安静模式”,在不需要进一步确认的情况下运行命令。
  • -hd=16 参数指定硬盘映像文件的大小为16MB。
  • -mode=create 参数表示创建一个新的硬盘映像文件。
  • -sectsize=512 参数指定扇区大小为512字节。
  • -imgmode=flat 参数表示使用扁平格式作为映像文件的类型。
  • master.img 是要创建的硬盘映像文件的文件名。

利用dd命令将hello.asm加入硬盘当中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[bits 32]

; extern printf
; extern exit

; section .text
; global main
; main:
; push message
; call printf
; add esp, 4

; push 0
; call exit

; section .data
; message db "hello world!!!", 10, 13, 0
; message_end:

section .text
global main
main:

mov eax, 4; write
mov ebx, 1; stdout
mov ecx, message; buffer
mov edx, message_end - message
int 0x80

mov eax, 1; exit
mov ebx, 0; status
int 0x80

section .data
message db "hello world!!!", 10, 13, 0
message_end:
1
dd if=hello.bin of=master.img bs=512 count=1 conv=notrunc

这个命令使用dd工具将hello.bin文件的内容复制到master.img文件中,每次复制512字节,只复制一次,并且不截断目标文件。

下面是对该命令中使用的参数的解释:

  • if=hello.bin:指定输入文件的路径和名称为hello.bin
  • of=master.img:指定输出文件的路径和名称为master.img
  • bs=512:指定每个读取和写入操作的块大小为512字节。
  • count=1:指定只进行一次复制操作。
  • conv=notrunc:表示不截断目标文件,在写入之前保留目标文件的现有内容。

请确保在运行该命令之前,hello.binmaster.img文件都存在,并且目标文件master.img的大小足够容纳源文件hello.bin的内容,否则可能会导致数据丢失或截断。

最终构建项目的 makefile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
%.bin: %.asm
# $< 是输入文件; $@ 是输出文件
nasm $< -o $@

master.img: boot.bin
dd if=boot.bin of=master.img bs=512 count=1 conv=notrunc

.PHONY:bochs
# 因为bochs的执行是需要master.img文件的,所以将它添加到依赖上
bochs:master.img
bochs -q

# 伪目标,每次都会执行,而对于其他目标make之后有可能是 up to date
.PHONY:clean
clean:
rm -rf *.bin

创建第一个x86的hello world
http://example.com/2023/07/28/创建第一个x86的hello-world/
作者
WHC
发布于
2023年7月28日
许可协议