mov si, offset msg 中的 FASM vc MASM 转换问题
刚刚使用相同的代码(almos)对 MASM 和 FASM 进行了第一次测试,但我遇到了麻烦。唯一的区别是,为了只生成 104 个字节,我需要写入 FASM 中的 MBR,我将 org 7c00h 放在 MASM 0h 中。
问题在于
mov si, offset msg
,在第一种情况下将其转写为 44 7C (7c44h),而使用 masm 则将其转写为 44 00 (0044h)!但就在我在 MASM 中将 org 7c00h 更改为 org 0h 时。否则,它将生成从 0 到 7dff 的整个段。
我该如何解决它?
或者简而言之,如何使 MASM 生成一个以 7c00h 开头作为第一个字节且后续跳转保持相对于 7c00h 的二进制文件?
.model TINY
.code
org 7c00h ; Boot entry point. Address 07c0:0000 on the computer memory
xor ax, ax ; Zero out ax
mov ds, ax ; Set data segment to base of RAM
jmp start ; Jump to the first byte after DOS boot record data
; ----------------------------------------------------------------------
; DOS boot record data
; ----------------------------------------------------------------------
brINT13Flag db 90h ; 0002h - 0EH for INT13 AH=42 READ
brOEM db 'MSDOS5.0' ; 0003h - OEM name & DOS version (8 chars)
brBPS dw 512 ; 000Bh - Bytes/sector
brSPC db 1 ; 000Dh - Sectors/cluster
brResCount dw 1 ; 000Eh - Reserved (boot) sectors
brFATs db 2 ; 0010h - FAT copies
brRootEntries dw 0E0h ; 0011h - Root directory entries
brSectorCount dw 2880 ; 0013h - Sectors in volume, < 32MB
brMedia db 240 ; 0015h - Media descriptor
brSPF dw 9 ; 0016h - Sectors per FAT
brSPH dw 18 ; 0018h - Sectors per track
brHPC dw 2 ; 001Ah - Number of Heads
brHidden dd 0 ; 001Ch - Hidden sectors
brSectors dd 0 ; 0020h - Total number of sectors
db 0 ; 0024h - Physical drive no.
db 0 ; 0025h - Reserved (FAT32)
db 29h ; 0026h - Extended boot record sig
brSerialNum dd 404418EAh ; 0027h - Volume serial number (random)
brLabel db 'OSAdventure' ; 002Bh - Volume label (11 chars)
brFSID db 'FAT12 ' ; 0036h - File System ID (8 chars)
;------------------------------------------------------------------------
; Boot code
; ----------------------------------------------------------------------
start:
mov si, offset msg
call showmsg
hang:
jmp hang
msg db 'Loading...',0
showmsg:
lodsb
cmp al, 0
jz showmsgd
push si
mov bx, 0007
mov ah, 0eh
int 10h
pop si
jmp showmsg
showmsgd:
retn
; ----------------------------------------------------------------------
; Boot record signature
; ----------------------------------------------------------------------
dw 0AA55h ; Boot record signature
END
just did my first test with MASM and FASM with the same code (almos) and I falled in trouble. The only difference is that to produce just the 104 bytes I need to write to MBR in FASM I put org 7c00h and in MASM 0h.
The problem is on the
mov si, offset msg
that in the first case transletes it to 44 7C (7c44h) and with masm translates to 44 00 (0044h)! but just when I change org 7c00h to org 0h in MASM. Otherwise it will produce the entire segment from 0 to 7dff.
how do I solve it?
or in short, how to make MASM produce a binary that begins at 7c00h as it first byte and subsequent jumps remain relative to 7c00h?
.model TINY
.code
org 7c00h ; Boot entry point. Address 07c0:0000 on the computer memory
xor ax, ax ; Zero out ax
mov ds, ax ; Set data segment to base of RAM
jmp start ; Jump to the first byte after DOS boot record data
; ----------------------------------------------------------------------
; DOS boot record data
; ----------------------------------------------------------------------
brINT13Flag db 90h ; 0002h - 0EH for INT13 AH=42 READ
brOEM db 'MSDOS5.0' ; 0003h - OEM name & DOS version (8 chars)
brBPS dw 512 ; 000Bh - Bytes/sector
brSPC db 1 ; 000Dh - Sectors/cluster
brResCount dw 1 ; 000Eh - Reserved (boot) sectors
brFATs db 2 ; 0010h - FAT copies
brRootEntries dw 0E0h ; 0011h - Root directory entries
brSectorCount dw 2880 ; 0013h - Sectors in volume, < 32MB
brMedia db 240 ; 0015h - Media descriptor
brSPF dw 9 ; 0016h - Sectors per FAT
brSPH dw 18 ; 0018h - Sectors per track
brHPC dw 2 ; 001Ah - Number of Heads
brHidden dd 0 ; 001Ch - Hidden sectors
brSectors dd 0 ; 0020h - Total number of sectors
db 0 ; 0024h - Physical drive no.
db 0 ; 0025h - Reserved (FAT32)
db 29h ; 0026h - Extended boot record sig
brSerialNum dd 404418EAh ; 0027h - Volume serial number (random)
brLabel db 'OSAdventure' ; 002Bh - Volume label (11 chars)
brFSID db 'FAT12 ' ; 0036h - File System ID (8 chars)
;------------------------------------------------------------------------
; Boot code
; ----------------------------------------------------------------------
start:
mov si, offset msg
call showmsg
hang:
jmp hang
msg db 'Loading...',0
showmsg:
lodsb
cmp al, 0
jz showmsgd
push si
mov bx, 0007
mov ah, 0eh
int 10h
pop si
jmp showmsg
showmsgd:
retn
; ----------------------------------------------------------------------
; Boot record signature
; ----------------------------------------------------------------------
dw 0AA55h ; Boot record signature
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我稍微修改了你的代码以与 FASM 一起使用。希望这有帮助。根据 MS 服务条款,您不允许使用 MASM 制作操作系统。所以不建议这样做然后在公开聊天中做广告。但 FASM 效果很好。这是您的代码“已修复”,以便您可以在 FASM 中对其进行编译。
使用 FASM 1.6+ 对其进行编译,它将使自己成为您命名的文件的名称,并使其成为 BIN 文件。我使用 PowerISO 来制作 CD 映像,它允许您提取 BIN 文件,以便使 CD 可启动。 (提示:它会显示您只选择 BIF 文件,只需选择 . 并选择 BIN 文件即可。)然后使用免费程序 VM VirtualBox 挂载并测试 CD。 :-)
I altered your code a little bit to work with FASM. Hope this helps. According to the MS Terms of service your not allowed to make an OS with MASM. So its not recommended to do that and then advertise it in open chat. But FASM works great. Here is your code "fixed" so that you can compile it in FASM.
Compile that with FASM 1.6+ and it will make itself the name of the file you name it as well as make it into a BIN file. I use PowerISO to make CD images and it allows you to pull in a BIN file so you can make the CD bootable. ( HINT : It will show that only BIF files are your choice, just choose . and choose the BIN file anyways and there you go. ) Then use the free program VM VirtualBox to mount and test the CD. :-)
我没有方便的 MASM 文档和/或自己的源代码,但我认为您必须定义一个 SEGMENT AT 07C00 (又名绝对段)。并始终在最后添加 ENDS...
现在您检查了 MASM 运行生成的实际 bin 代码了吗?因为 MASM 列表显示的十六进制值不一定与其实际生成的值相同。
按照您定义的方式,您创建了一个可重定位的代码段,其中的代码从段中的 07C00 开始。现在您需要一个链接来创建实际的二进制文件,并且链接的代码可能是正确的 - 或几乎正确:链接器可能在绝对目标模块中生成从 0000 到 07C00 的零。
顺便说一句,您需要链接到一个垃圾箱。不确定链接到“.com”是否可以。您使用什么 16 位链接器?我使用 Digital Mars Optasm(您可以在他们的免费 C 编译器包中免费下载)。
I don't have my MASM docs and/or own source code handy, but I think you have to define a SEGMENT AT 07C00 (aka absolute segment). And consistently add an ENDS in the end...
Now did you check the actual bin code that your MASM run generated? Because the hex values that the MASM listing shows are not necessarily identical to what it actually generated.
The way you defined it, you created a relocatable segment of code with code that start at 07C00 in the segment. Now you need a link to create the actual binary, and the linked code could be right - or nearly right: It could be that the linker generates zeroes from 0000 to 07C00 in the absolute target module.
You need to link to a bin, btw. Not sure linking to a ".com" will do it. What 16-bit linker do you use? I use Digital Mars Optasm (that you can freely download in their free C compiler package).