【ubuntu_nasm】:汇编语言----搭建开发环境(intel_x8664_cpu+ubuntu+nasm+ld+gcc+qemu+gdb)
【ubuntu_nasm】:汇编语言----搭建开发环境(intel_x8664_cpu+ubuntu+nasm+ld+gcc+qemu+gdb)
一、基本说明
1、 汇编语言的的实验环境
---- cpu: intel_x8664_cpu
---- os: ubuntu26.04(Linux ubuntu 7.0.0-15-generic #15-Ubuntu SMP PREEMPT_DYNAMIC Wed Apr 22 16:06:43 UTC 2026 x86_64 GNU/Linu)
---- 汇编工具: nasm
---- 链接工具1: ld(LD)
---- 链接工具2: gcc(GCC)
---- 测试环境: qemu(qemu-system-x86_64)
---- 调试工具: gdb(GDB)
2、 常用文档:
---- intel_x8664_cpu_docs【 https://www.intel.com/content/www/us/en/developer/tools/documentation.html 】
---- intel_x8664_cpu_man_all: https://www.intel.com/content/www/us/en/content-details/916575/intel-64-and-ia-32-architectures-software-developer-s-manual-combined-volumes-1-2a-2b-2c-2d-3a-3b-3c-3d-and-4.html
---- intel_docs: https://www.intel.com/content/www/us/en/developer/tools/documentation.html
---- nasm_docs【 https://www.nasm.us/docs.html 】
---- html_docs: https://www.nasm.us/docs/3.01/
---- pdf__docs: https://www.nasm.us/xdoc/3.01/nasmdoc.pdf
---- docs: https://www.nasm.us/docs.html
---- qemu_docs【 https://www.qemu.org/documentation/ 】
---- html_docs: https://www.qemu.org/docs/master/
---- docs: https://www.qemu.org/documentation/
3、 测试代码
---- 基本说明: 本段代码来自于AI,能运行,(gdb)结果是cr0=0x10(ET)
---- 实验环境: intel_x8664_cpu + ubuntu + nasm + ld + gcc + qemu-system-x86_64 + gdb
---- 编译命令: nasm -f bin -o boot.bin boot.asm
---- qemu设置: qemu-system-x86_64 -S -gdb tcp::1234 -drive format=raw,file=boot.bin
[wit@ubuntu:src]$ cat qemu_run
#!/usr/bin/env bash
function qemu_gdb()
{
qemu-system-x86_64 -S -gdb tcp::1234 -drive format=raw,file=boot.bin
}
function qemu_test()
{
qemu-system-x86_64 -drive format=raw,file=boot.binqemu-system-x86_64 -drive format=raw,file=boot.bin
}
#qemu_gdb
qemu_gdb
#qemu_test
#qemu_test
---- GDB操作:
---- target remote localhost:1234 # 连接 QEMU
---- set architecture i386 # 设置架构为 x86 (因为刚开始是实模式)
---- b *0x7c00 # 在引导入口点设断点
---- c # 继续运行
---- info registers cr0 #此时你可以在 GDB 中使用 info registers cr0 来实时查看 CR0 寄存器的值。
--- si + info registers exa cr0 # 单步调试
# GDB单步调试
si
# 查看寄存器的值:eax, cr0
info registers eax cr0
[wit@ubuntu:src]$ cat boot.asm
[BITS 16]
[ORG 0x7C00]
start:
cli ; 关中断
lgdt [gdt_desc] ; 加载 GDT
; 切换到保护模式
mov eax, cr0
or eax, 1 ; 设置 PE 位
mov cr0, eax
; 远跳转刷新流水线
jmp 0x08:code32
[BITS 32]
code32:
mov ax, 0x10 ; 设置数据段
mov ds, ax
mov ss, ax
mov esp, 0x90000 ; 设置栈顶
; 此时可以检查 CR0 确认状态
mov eax, cr0 ; eax 中现在包含 CR0 的值,PE 位应为 1
; 死循环,防止跑飞
hlt
jmp $
; GDT 定义
gdt_start:
dq 0x0000000000000000 ; 空描述符
gdt_code:
dw 0xFFFF ; Limit (low)
dw 0x0000 ; Base (low)
db 0x00 ; Base (mid)
db 10011010b ; Access byte (Present, Ring 0, Code, Executable)
db 11001111b ; Flags + Limit (high)
db 0x00 ; Base (high)
gdt_data:
dw 0xFFFF
dw 0x0000
db 0x00
db 10010010b ; Access byte (Present, Ring 0, Data, Writable)
db 11001111b
db 0x00
gdt_end:
gdt_desc:
dw gdt_end - gdt_start - 1 ; GDT 界限
dd gdt_start ; GDT 基地址
times 510-($-$$) db 0
dw 0xAA55
二、汇编语言的实验环境(INTEL_X8664_CPU + UBUNTU26.04 + NASM3.01 + LD + GCC + QEMU + GDB)
1、 nasm安装:
[wit@ubuntu:tmp]$ sudo apt install nasm
2、 qemu安装
[wit@ubuntu:tmp]$ sudo apt sudo apt install qemu-system-x86 qemu-system-gui
三、参考资料
1.0、 intel_docs -- https://www.intel.com/content/www/us/en/developer/tools/documentation.html
1.1、 intel_x8664_cpu_docs -- https://www.intel.com/content/www/us/en/content-details/916575/intel-64-and-ia-32-architectures-software-developer-s-manual-combined-volumes-1-2a-2b-2c-2d-3a-3b-3c-3d-and-4.html
2、 nasm -- https://www.nasm.us/
3、 qemu -- https://www.qemu.org/
4、 百度AI -- chat.baidu.com
.
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章版权归属【原创作者】; 转载或引用时【请保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/20066520

浙公网安备 33010602011771号