1. Environment Tools
ubuntu 18.04.1
NASM
bochs
2.nasm安装:sudo apt-get install nasm
Bochs installation dependencies compiled library: sudo apt-get install vgabios xorg-dev bochs-sdl bochs bochs-x bochsbios bochs-doc libgtk2.0-dev
Download bochs source, http: //bochs.sourceforge.net/, download the latest version bochs-2.4.6.tar.gz
Extract the source package, unzip and enter the directory bochs-2.6.9, execution
./configure --enable-disasm --enable-debugger
make && make install
3. Write a master boot sector code execution boot.asm, and compiled with the command
nasm boot.asm -o boot.bin
org 07c00h; tells the compiler that the program is loaded into memory address 07c00h mov AX, cs mov ds, AX mov es, AX Call DispStr; call display string subroutine jmp $; infinite loop DispStr: mov AX, bootmessage mov BP, AX; es: bp = string address MOV CX, 16; CX = string length MOV AX, 01301h; AH = 13H, Al = 01h MOV BX, 000CH; page number is 0 (bh = 0) black red (bl = 0Ch , highlighted) mov DL, 0 int 10h; interrupt 10h No. RET bootmessage : db "the Hello, OS World!" Times 510 - ($ - $$) db 0; fill the remaining space, so that the generated binary code exactly 512 bytes DW 0xaa55; end mark the MBR
4. boot.bin dd command written in the floppy disk image disk.img first sector
dd if=boot.bin of=disk.img bs=512 count=1 conv=notrunc
5. Create a configuration file bochsrc bochs start, as follows:
romimage: file=/usr/share/bochs/BIOS-bochs-latest
megs: 32
vgaromimage: file=/usr/share/bochs/VGABIOS-lgpl-latest
floppya:1_44=disk.img, status=inserted
boot:floppy
log: bochsout.txt
mouse: enabled=0
6. Run bochs -f bochsrc, pauses in the BIOS, waiting for debugging
At this point, you can set breakpoints, physical address 0x7c00, bochs use various debugging command to view the operation, the command will be mentioned later
Finally, the interface will appear red string "Hello, OS World!"
7.bochs basic commands roughly divided into the following categories
(1) Breakpoint setting command
vb / vbreak up: Offset
Set instruction breakpoint on a virtual address, which can be in seg and offset 0x hexadecimal number beginning, or decimal, octal, or is the beginning of the 0
lb/lbreak addr
Set breakpoints, addr above the linear address
b/break/pb/pbreak addr
Set a breakpoint on a physical address
info break
Displays current information about all breakpoints
d / of / delete n
Delete a breakpoint
(2) The execution control command
c/cont/continue
Continuous execution
s/step/stepi [count]
Execute count instructions, default is one, will follow the content and interrupt function calls
p/n/next [count]
Execute count instructions, default is 1, but skip function and interrupt call
Ctrl+C
Stop the execution, and return to the command prompt
q/quit/exit
Exit debugging and execution
(3) memory operation command
x /nuf addr
Check the content in memory at a linear address addr
xp /nuf addr
Check the content in memory at the physical address addr
其中参数n,u,f分别表示:
n为显示内存单元的计数值,默认为1
u表示单元大小,默认值为w
b(bytes) 1字节
h(halfwords) 2字节
w(words) 4字节
g(gaintwords) 8字节
f为显示格式,默认值为x
x(hex) 显示为16进制数
d(decimal) 显示为10进制数
u(unsigned) 显示为无符号10进制数
o(octal) 显示为8进制数
t(binary) 显示为2进制数
c(char) 显示为对应的字符
(4)信息显示和CPU寄存器操作命令
r/reg/regs/registers
显示cpu寄存器及其内容
set $reg=val
修改寄存器的内容,除了段寄存器和标志寄存器以外,其它寄存器都可以修改.
creg
所有CR0-CR4寄存器
sreg
CPU全部状态信息,包括各个段选择子(cs,ds等),ldtr,gdtr等
print-stack
打印堆栈情况
info tab
显示页表
(5)反汇编命令
u/disasm/disassemble start end,反汇编给定线性地址范围的指令
u /10反汇编从当前地址开始的10条指令.
bochs的调试功能可以直接看到cup的执行情况,以及各个寄存器和内存单元的内容,对于掌握程序的运行情况是很有好处的。