0

I am trying to develop a simple boot sector. Including another assembly file is producing an error.Here is my code:

boot_sect.asm

mov ah,0x0e
org 0x7c00

mov bx,HELLO
call print_bx
call print_bx

jmp $

print_bx:
   pusha
   call printing_loop
   mov al,0xa
   int 0x10
   mov al,0xd
   int 0x10
   popa
   ret

printing_loop:
   mov al,[bx]
   int 0x10
   add bx,0x01
   cmp byte [bx],0x00
   jne printing_loop
   ret

HELLO:
   db 'Hello World',0

times 510-($-$$) db 0

dw 0xaa55

This works perfectly and produces the output:

Hello World
Hello World 

But when I add the line %include "print_al.asm" in the beginning,the output produced is

U

No errors are shown. I run the code using

nasm boot_sect.asm -f bin -o boot_sect.bin
qemu-system-x86_64 boot_sect.bin

What am I doing wrong?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
ADP
  • 107
  • 10
  • 1
    The `org` directive must be the first thing in the file. – fuz Jun 19 '21 at 07:48
  • 1
    You haven't shown where you added that `%include` line. If it's in the path of execution from the top of the file, note that execution will fall into the code you `%include`, which if it contains a `ret` will probably mean it doesn't reach those `call` instructions near the top. Use `ndisasm` on your final binary, or single-step your bootloader with a debugger (qemu+GDB, or BOCHS) – Peter Cordes Jun 19 '21 at 12:01
  • 2
    @fuz: I tested, and `org` before or after the `mov ah, 0x0e` doesn't change the addresses used. I had wondered if it would tell NASM that the load address of the code *after* the first 2-byte instruction will be `0x7c00`, but that doesn't seem to be the case. `mov bx, HELLO` assembles to `BB28 7C` either way, with HELLO: at `0028` relative to the top of the file. The code does seem to be missing a DS=0 to match that origin setting, but apparently it happens to work with the BIOS inside QEMU. Instead, the problem is probably execution falling into the `%include`d code. – Peter Cordes Jun 19 '21 at 12:09
  • @PeterCordes Thank you..it worked when I moved ```%include...``` after ```jmp $``` – ADP Jun 19 '21 at 15:18

0 Answers0