4

I am making a bootloader, but it generates a 513 byte output file whereas it should be 512 bits. Here is boot.asm

[ORG 7C00]
[BITS 16]
mov eax,cr0
or eax,1
mov cr0,eax
[BITS 32]
mov ax,10h
mov ds,ax
mov es,ax
mov fs,ax
mov gs,ax
mov ss,ax
INT 0x10
jmp 0x8000
TIMES 510 - ($ - $$) DB 0
DW 0xAA55

How can I get 512 byte of output?

  • Bits? Did you mean bytes? 510 + 3 = 513 so its not surprising that you get 513 bytes, not 512. – tkausl Mar 12 '19 at 15:54
  • `[ORG 7C00]` need to be `[ORG 0x7C00]` (or `[ORG 7c00h]`) as 7c00 is in HEX. You assemble it with `nasm -f bit boot.asm -o boot.bin` (boot.asm being whatever file name you used). The code you have should already generate exactly 512 bytes (not bits) – Michael Petch Mar 12 '19 at 17:02
  • Even if you assemble this code it won't run. `INT 0x10` won't do anything by itself. The `[bits 32]` directive needs to be just before `mov ax, 10h`. You don't get 32-bit into protected mode just by setting bit 0 of CR0. You need to create a GDT with a code and data descriptor and then do a FAR JMP to set CS to a 32-bit segment selector. – Michael Petch Mar 12 '19 at 17:06
  • Is this the bootloader you are actually using or did you edit this down from your real one? – Michael Petch Mar 12 '19 at 17:16
  • 2
    If it assembled, and if the assembler is generating "flat binary" output file format; then I can't see any reason why the output file wouldn't be 512 bytes (the last 2 lines are correct). I'd guess that either it didn't assemble (because of mistakes) and you're looking at the size of an old file; or the code you've shown isn't the code you're assembling; or the file actually is 512 bytes and you incorrectly think it's not. – Brendan Mar 12 '19 at 19:00

1 Answers1

2

You left out the 0x in from of the 7C00 hex constant in your ORG directive . NASM treats this as an error.

You probably assembled this with YASM, which instead of rejecting your source, produces a 513 byte file. Fixing your source makes both YASM and NASM produce a 512-byte file. This is probably a bug in YASM. Unfortunately YASM hasn't been well maintained recently, so even though it has nicer long NOPs from align directives (not bloating disassembly with many lines of single-byte NOP), you should probably just switch to NASM.

$ yasm boot-buggy.asm && ll boot-buggy
-rw-r--r-- 1 peter peter 513 Mar 13 06:03 boot-buggy
$ nasm boot-buggy.asm && ll boot-buggy
boot-buggy.asm:1: error: expression syntax error
boot-buggy.asm:1: error: No or invalid offset specified in ORG directive.
$ nasm boot-fixed.asm && ll boot-fixed
-rw-r--r-- 1 peter peter 512 Mar 13 06:04 boot-fixed
$ yasm boot-fixed.asm && ll boot-fixed
-rw-r--r-- 1 peter peter 512 Mar 13 06:04 boot-fixed

cmp -l boot-fixed boot-buggy shows that the buggy version has an extra 0 byte as the first byte of the file, then all the rest are the same.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • You know a surprising thing that i am using an android phone and yasm in termux terminal emulator. It doesnt provide nasm. – Gyan Prakash Mar 13 '19 at 15:53
  • 2
    @GyanPrakash :If this answer solved your problem please consider [accepting it as an answer](https://meta.stackexchange.com/a/5235/271768) to mark it as solved,since the solution for YASM is to fix the bug in the `[ORG 7c00]` line to be `[ORG 7c00h]` or `[ORG 0x7c00]` – Michael Petch Mar 13 '19 at 18:41