gpt4 book ai didi

assembly - 如何使用 Bochs 运行汇编代码?

转载 作者:行者123 更新时间:2023-12-03 06:43:20 25 4
gpt4 key购买 nike

我想使用 Bochs 作为 8086 模拟器。是否有捷径可寻?我想要的是像 emu8086 ( http://www.emu8086.com/ ) 这样的东西。

最佳答案

如果程序的初始部分适合 512 字节,并且您不介意将自己限制在 BIOS 调用、in/out 指令以及写入 magic 中I/O 的内存位置...那么是的!

假设您正在使用 NASM,这是一个愚蠢的示例...(警告:我的 16 位汇编技能不是很好,而且有点生疏,所以它可能不是最好的代码。)

[org 7c00h]              ; BIOS will load us to this address

mov ax, 0b800h ; Console memory is at 0xb8000; set up a segment
mov es, ax ; for the start of the console text.

;
; Let's clear the screen....
;

xor di, di ; Start at beginning of screen
mov cx, 80*25 ; Number of chars in the screen
mov al, ' ' ; Space character
mov ah, 0fh ; Color (white on black)
repne stosw ; Copy!

;
; Write an 'a' to the screen...
;

mov byte [es:0], 'a' ; Write an 'a'

sleep:
hlt ; Halts CPU until the next external interrupt is fired
jmp sleep ; Loop forever

times 510-($-$$) db 0 ; Pad to 510 bytes
dw 0aa55h ; Add boot magic word to mark us as bootable

然后你可以组装:

nasm foo.asm

并将其写入软盘镜像,如下所示:(假设是 Unix 类型的系统...)

$ dd if=/dev/zero of=floppy.img bs=512 count=2880
$ dd if=foo of=floppy.img conv=notrunc

现在您可以在 Bochs 中启动该软盘镜像(或者,如果您将其写入软盘,则在真正的 PC 上运行它),并且它应该在屏幕上写入一个“a”。

请注意,这通常仅在您编写引导加载程序或操作系统时才有用...但尝试起来很有趣,尤其是在您学习时。

更新:我阅读了 emu8086 网站...似乎有点面向 x86 的嵌入式使用而不是 PC。看起来它有一些有趣的功能来模拟硬件。如果您对 PC 不感兴趣,那么 Bochs 就不会引起您的兴趣。如果这不是您想要做的,我同意评论者建议使用 emu8086 本身的观点。

如果您对 PC 感兴趣,但想要一些东西来逐步执行您的程序...我经常使用 qemu 来实现此目的。它的调试标志(请参阅 -d 下的联机帮助页)足以在汇编级别观察 x86 程序的执行状态。 (我什至发现它对于调试用 C 编写的操作系统内核非常有用,只要您仔细查看 C 编译器生成的内容。)

关于assembly - 如何使用 Bochs 运行汇编代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6142925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com