2

I am trying to learn a simple helloworld bootloader program. referring this link. I have successfully generated the binary file for this assembly code using nasm assembler and run with a emulator bochs and it works fine. But when I did the same thing directly with a hard disk I am not able to print the string to screen.

Please find below the code I have used.

[BITS 16]
[ORG 0x7C00]

MOV SI, HelloString
CALL PrintString
JMP $

PrintCharacter:
    MOV AH, 0x0E
    MOV BH, 0x00
    MOV BL, 0x07
    INT 0x10
    RET

PrintString:
next_character:
    MOV AL, [SI]
    INC SI
    CALL PrintCharacter
    OR AL, AL
    JZ exit_function
    JMP next_character
exit_function:
    RET

HelloString db "Pell",0 

TIMES 510 - ($ - $$) db 0 
DW 0xAA55
skesh
  • 161
  • 2
  • 10
  • 4
    As usual, you forgot to intialize `DS`. – Jester Feb 16 '17 at 14:24
  • Could you please explain what does this DS do.I am beginner in assembly. – skesh Feb 16 '17 at 14:27
  • 1
    I have [Bootloader Tips](http://stackoverflow.com/a/32705076/3857942) in another SO answer. Setting up the segment register like _DS_ may be needed. When you say hard drive do you mean you boot on real hardware? – Michael Petch Feb 16 '17 at 14:29
  • Yes I have tried it on real harddisk using dd command – skesh Feb 16 '17 at 14:30
  • 1
    @skesh DS is the data segment register. Its content is multiplied with 16 and added to every address you use to fetch data. At the beginning of your code, you need to initialize it e.g. to zero by writing something like `xor ax,ax` and then `mov ds,ax`. – fuz Feb 16 '17 at 14:32
  • 1
    Try adding `xor ax,ax` `mov ds,ax` before `MOV SI, HelloString` – Michael Petch Feb 16 '17 at 14:32
  • Thank you Jester,Michael,fus for the help.Initialising the DS register has solved my problem. XOR ax,ax MOV DS,ax MOV SI, HelloString – skesh Feb 17 '17 at 05:15

1 Answers1

1

You need to initialise the segment registers before you do anything else or the program will crash as you cannot access the data.

[BITS 16]
[ORG 0x7C00]

XOR AX, AX
MOV DS, AX

MOV SI, HelloString
CALL PrintString
JMP $

PrintCharacter:
    MOV AH, 0x0E
    MOV BH, 0x00
    MOV BL, 0x07
    INT 0x10
    RET

PrintString:
next_character:
    MOV AL, [SI]
    INC SI
    CALL PrintCharacter
    OR AL, AL
    JZ exit_function
    JMP next_character
exit_function:
    RET

HelloString db "Pell",0 

TIMES 510 - ($ - $$) db 0 
DW 0xAA55
Alex Boxall
  • 541
  • 5
  • 13