Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flat Binary format

I am a bit confused about bin formats, I am using the nasm assembler and I don't quite understand how segements and BBS values are enocoded into BIN. How is this format loaded by the system when executed?

Many thanks

like image 484
jimmyzmli Avatar asked Jun 09 '26 16:06

jimmyzmli


1 Answers

bin does not retain any structure nor segments. All code and data goes as is, there are no special headers or descriptors. .bss contents are ignored for bin and resb/resw/resd/etc should be used as placeholder.

Sample code:

bits 16
section .text
org 100h

; copy hw[] to copy[]
mov si, hw
mov di, copy
mov cx, 15
cld
rep movsb

; print copy[]
mov dx, copy
mov ah, 9
int 21h
ret

section .bss

blah db "0123456789abcdef" ; data will be ignored, nasm will warn here

copy resb 15 ; reserve 15 bytes for the text string

section .data

hw db "Hello Wrold!",13,10,"$"

Compiling:

C:\>nasm nsm.asm -fbin -onsm.com
nsm.asm:20: warning: attempt to initialise memory in a nobits section: ignored

Disassemblying:

C:\>ndisasm -b 16 -o 100h nsm.com
00000100  BE1401            mov si,0x114
00000103  BF3401            mov di,0x134
00000106  B90F00            mov cx,0xf
00000109  FC                cld
0000010A  F3A4              rep movsb
0000010C  BA3401            mov dx,0x134
0000010F  B409              mov ah,0x9
00000111  CD21              int 0x21
00000113  C3                ret
00000114  48                dec ax ; this is hw db "Hello Wrold!",13,10,"$"
00000115  656C              gs insb
00000117  6C                insb
00000118  6F                outsw
00000119  205772            and [bx+0x72],dl
0000011C  6F                outsw
0000011D  6C                insb
0000011E  64210D            and [fs:di],cx
00000121  0A24              or ah,[si]

Running on Windows XP (or DOS):

C:\>nsm.com
Hello Wrold!

It is expected by the OS that DOS .COM-style programs have no special structure and the very first byte of the file contains the first instruction that has to be executed.

Please see the NASM documentation for details. I believe you can find all answers to your questions there.

like image 63
Alexey Frunze Avatar answered Jun 11 '26 14:06

Alexey Frunze