1

I'm using NASM on Windows. I have the following simple assembly code in NASM-syntax:

section .data
    cha db 'A'

section .text
global _start
_start:
    mov ah, 0xa
    mov al, [cha]
    mov cx, 1h
    int 10h

times 510-($-$$) db 0
dw 0xaa55

I generate my bootloader with this command:

nasm bootloader.asm -f bin -o bootloader.bin

I use the ROM-BIOS built-in interrupt to print a character in the screen. The problem is that when I execute the program, it doesn't print "A" but "S". I tried defining different letters like "H" but "S" was being displayed. What am I doing wrong?

Note: The two ending lines of the code are just because I run the program in QEMU. In other words they are meaningless for the execution of the code but meaningful for the QEMU to run it!

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • 2
    If you are making a bootloader, you need to setup the segment registers and properly set the origin of your code. Do you have any experience with that? [Michael Petch](https://stackoverflow.com/users/3857942/michael-petch) gave an excellent question on this, full of advice, sadly I can't recall it right now. – Margaret Bloom Jun 20 '17 at 15:45
  • @MargaretBloom The problem isn't that. When I replace "db 'A'" with "resb 1" and I move 0x41 in "cha" variable later in the code it works fine. Why isn't that working? – Anastassis Kapetanakis Jun 20 '17 at 15:50
  • 3
    That can very well be because you did not set up segment registers. It works from code because you are just overwriting random memory (not that you reserved) and then you are reading the same place. PS: learn to use a debugger, and avoid real mode and bootloaders unless forced to, at least initially. – Jester Jun 20 '17 at 15:53
  • 1
    Also, your code doesn't "terminate" - it doesn't hang, the execution will continue through. Judging by the source it seems you are mixing Linux assembly programming and bootloader programming. That's why I asked if "segment registers" and "origin" rang any bells :) – Margaret Bloom Jun 20 '17 at 16:00
  • Besides the segment issue (you can see my [Bootloader tips](https://stackoverflow.com/questions/32701854/boot-loader-doesnt-jump-to-kernel-code/32705076#32705076)) I'd also have to ask how you assemble and link this to a final binary. What command line are you using, what is your linker script (if you have one). We can't tell what origin point you used from what you show. – Michael Petch Jun 20 '17 at 16:02
  • @MichaelPetch I use Nasm assembler with the command: nasm bootloader.asm -f bin -o bootloader.bin – Anastassis Kapetanakis Jun 20 '17 at 16:08
  • 1
    Well that in itself is a problem. `-f bin` with this code using sections like this will place the data section after the text section beyond the boot signature. That will not be loaded in memory (since it will be outside the first 512 byte sector). So you should probably consider removing the `data` section and placing your string in the text section after your code and before the boot signature. You will also want to set the origin point to 0x7c00 with (`org 0x7c00`) at the top of the code. Your lack of an origin point will default to 0 when the code is expected to be running at offset 0x7c00 – Michael Petch Jun 20 '17 at 16:10
  • Of course after `int 10h` you'll want to go in some sort of infinite loop so you don't execute semi-random data in memory. That can be done with a simple `jmp $` right after `int 10h` – Michael Petch Jun 20 '17 at 16:26

0 Answers0