刚刚用相同的代码 (almos) 对 MASM 和 FASM 进行了第一次测试,但我遇到了麻烦。唯一的区别是,为了仅生成我需要在 FASM 中写入 MBR 的 104 个字节,我将 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
最佳答案
我稍微修改了您的代码以使用 FASM。希望这可以帮助。根据 MS 服务条款,您不得使用 MASM 制作操作系统。所以不建议这样做,然后在公开聊天中做广告。但是 FASM 效果很好。这是您的代码“固定”,以便您可以在 FASM 中编译它。
use16
format binary
org 7c00h ; Boot entry point. Address 07c0:0000 on the computer memory
somelabel:
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 'FASMv1.6' ; 0003h - OEM name & LOS 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 404F18EAh ; 0027h - Volume serial number (random)
brLabel db 'FASM_DISK_1' ; 002Bh - Volume label (11 chars)
brFSID db 'FAT12 ' ; 0036h - File System ID (8 chars)
;-------------------------------------------
; Boot code
; ------------------------------------------
msg1 db ' This is a test of FASM 1.6',0
start:
mov ax,msg1
MOV si,ax
display11:
lodsb
test al, al
jnz disp0
jmp finish
disp0:
mov ah, 0xE
mov bx, 7
int 10h
jmp display11
finish:
jmp $ ;This tells times to compare the end here with the
;beginning up there called somelabel ( NOTE : entry by
;itself is not allowed because FASM uses it. )
; ------------------------------------
; Boot record signature
; ------------------------------------
size equ $ + somelabel
times (512 - size - 2) db 0 ;needed to padd the first 512 sector with 0's
;AFTER the jmp $ command. ( size equ $+entry )
;then is takes size away from 512 as well as
;takes 2 bytes away for the boot sig and your done.
dw 0AA55h ; Boot record signature
用 FASM 1.6+ 编译它,它会使自己成为你命名的文件的名称,并将它变成一个 BIN 文件。我使用 PowerISO 制作 CD 镜像,它允许您提取 BIN 文件,以便您可以使 CD 可启动。 (提示:它会显示您只选择 BIF 文件,只需选择 . 并选择 BIN 文件即可。)然后使用免费程序 VM VirtualBox 挂载和测试 CD。 :-)
关于assembly - mov si, offset msg 中的 FASM vc MASM 翻译问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2978998/