2012-03-01 61 views
1

我有這兩個文件myboot.asm和theirboot.asm(分別列出):試圖瞭解一個二進制文件的大小(NASM)輸出

;---------------------------------------------------------------------- 
; A Simple boot program that prints the string 'Hello World' 
; Author: Matthew Hoggan 2012 
;---------------------------------------------------------------------- 
Output db 'Hello',0x00   ; Output string for bios 

org 0x7c00        ; This is where BIOS loads the bootloader 

entry:         ; Label to Signify entry to program 
    jmp short begin      ; Jump over the DOS boot record data 

; -------------------------------------------- 
; Boot program code begins here 
; -------------------------------------------- 
begin:         ; Label to Signify entry to program 
mov si, Output       ; Get pointer to string 
loop:         ; Top of loop to iterate over string 
    mov al, [si]      ; Move contents of pointer to al 
    or al, al       ; Check if character pointed to is Zero 
    jz hang        ; Zero signifies end of string 
    call print_char      ; Print current char in al 
    jmp loop       ; Repeat 

; -------------------------------------------- 
; Function to print char 
; assume caller has placed char in al 
; -------------------------------------------- 
print_char: 
    mov ah, 0x0e      ; Function to print a character to the screen 
    mov bl, 7       ; color/style to use for the character 
    int 0x10       ; print the character 

hang: 
     jmp  hang     ; just loop forever. 

;--------------------------------------------- 
; Write Zeros up to end of program - 2 then boot signature 
;--------------------------------------------- 
size equ  $ - entry 
     times (512 - size - 2) db 0 
     db  0x55, 0xAA    ;2 byte boot signature 

;---------------------------------------------------------------------- 
; A Simple boot program that prints the string 'Hello World' 
;---------------------------------------------------------------------- 
org 0x7c00        ; This is where BIOS loads the bootloader 

entry:         ; Label to Signify entry to program 
    jmp short begin      ; Jump over the DOS boot record data 
; -------------------------------------------- 
; Boot program code begins here 
; -------------------------------------------- 
begin:         ; Label to Signify entry to program 
    xor ax, ax       ; Zero out ax 
    mov ds, ax       ; Set data segment to base of RAM 
    mov si, msg       ; Get pointer to string 
    call putstr       ; Print the message 
    jmp hang       ; Go to infinite loop 

msg db 'Hello, World',0x00 

putstr:         ; Function to print the string 
    lodsb        ; al = [DS:SI] 
    or al, al       ; Set zero flag if al = 0 
    jz ret        ; Jump to end of function if al = 0 
    mov ah, 0x0e      ; Video function 0Eh (print char) 
    mov bx, 0x0007      ; Color 
    int 0x10 
    jmp putstr 
ret: 
    retn 

hang: 
     jmp  hang     ; just loop forever. 

;--------------------------------------------- 
; Write Zeros up to end of program - 2 then boot signature 
;--------------------------------------------- 
size equ  $ - entry 
     times (512 - size - 2) db 0 
     db  0x55, 0xAA    ;2 byte boot signature 

建設這兩個文件上運行他們hexdump都和上市在目錄中的文件,看看它們的大小顯示:

[email protected]:~/Code/play/asm$ nasm myboot.asm -f bin -o boot.bin && hexdump boot.bin && ls -l && echo "------" && nasm bootloader1.asm -f bin -o boot.bin && hexdump boot.bin && ls -l 
0000000 6548 6c6c 006f 00eb 00be 8a7c 0804 74c0 
0000010 e80c 0003 f4e9 b4ff b30e cd07 e910 fffd 
0000020 0000 0000 0000 0000 0000 0000 0000 0000 
* 
0000200 0000 0000 aa55       
0000206 
total 20 
-rw-r--r-- 1 mehoggan mehoggan 518 2012-02-29 21:57 boot.bin 
-rw-r--r-- 1 mehoggan mehoggan 2290 2012-02-29 20:23 bootloader0.asm 
-rw-r--r-- 1 mehoggan mehoggan 1661 2012-02-29 21:55 bootloader1.asm 
-rw-r--r-- 1 mehoggan mehoggan 1786 2012-02-29 21:49 myboot.asm 
-rw-r--r-- 1 mehoggan mehoggan 1065 2012-02-29 20:14 ourbootloader.asm 
------ 
0000000 00eb c031 d88e 0fbe e87c 0010 1de9 4800 
0000010 6c65 6f6c 202c 6f57 6c72 0064 08ac 74c0 
0000020 b40a bb0e 0007 10cd f1e9 c3ff fde9 00ff 
0000030 0000 0000 0000 0000 0000 0000 0000 0000 
* 
00001f0 0000 0000 0000 0000 0000 0000 0000 aa55 
0000200 
total 20 
-rw-r--r-- 1 mehoggan mehoggan 512 2012-02-29 21:57 boot.bin 
-rw-r--r-- 1 mehoggan mehoggan 2290 2012-02-29 20:23 bootloader0.asm 
-rw-r--r-- 1 mehoggan mehoggan 1661 2012-02-29 21:55 bootloader1.asm 
-rw-r--r-- 1 mehoggan mehoggan 1786 2012-02-29 21:49 myboot.asm 
-rw-r--r-- 1 mehoggan mehoggan 1065 2012-02-29 20:14 ourbootloader.asm 

爲什麼這些文件由6個字節大小不一關閉?

回答

4

退房的彙編代碼出現在最後的小塊:

size equ  $ - entry 
     times (512 - size - 2) db 0 
     db  0x55, 0xAA    ;2 byte boot signature 

的代碼塊計算碼有多大(從entry到當前位置),然後墊出來的總共512帶有零的字節和在最後兩個位置中的簽名0x55 0xAA。那就是:

entry: Some code 
     . 
     . 
     . 
    Some zeroes 
     . 
     . 
     . 
    0x55 0xAA 

那個小組裝塊是指從entry標籤輸出大小0x55 0xAA是總是 512字節。在你的第一個例子中,在entry之前有一個6字節的字符串Hello\0。在你的第二個例子中沒有。因此,第一個程序比第二個程序長六個字節。您可能想要在entry之後和填充塊之前將該字符串移動到某個位置。

如果您在二進制文件上使用hexump -C,則會在第一個二進制文件的頂部看到該字符串。