4

I have a very simple boot loader in assembly that is supposed to print a string. I've tried looking up many different ways to print a string in assembly but none of them have worked for me. this is my most recent attempt that I've seen in a couple of tutorials but it doesn't work. it prints a T rather than the message.

mov si, message       ;The message location *you can change this*
    call print            ;CALL tells the pc to jump back here when done

    print:
      mov ah, 0Eh         ;Set function

    .run:
      lodsb               ;Get the char
    ; cmp al, 0x00        ;I would use this but ya know u dont so use:
      cmp al, 0           ;0 has a HEX code of 0x48 so its not 0x00
      je .done            ;Jump to done if ending code is found
      int 10h             ;Else print
      jmp .run            ; and jump back to .run

    .done:
      ret                 ;Return

    message           db  'Hello, world', 0 ;IF you use 0x00
    ;message          db  'Hello, world', 0x00

jmp $

times 510-($-$$) db 0
dw 0xaa55
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • thank you, i added org 0x7c00 to the beggining and i now prints hello world but with two random characters at the end. do you know how to fix this – Valarie Brega Jul 01 '19 at 17:46
  • 1
    You need an `org 0x7c00` at the top since bootloaders are loaded at 0x0000:0x7c00 and you then need to zero DS (You can use `xor ax, ax` `mov ds, ax`. You should also put an infinite loop after `call print` so that your bootloader doesn't fall into the code after. which causes the code in print to be executed again with gibberish. You can do that with `jmp $` just after `call print` – Michael Petch Jul 01 '19 at 17:46
  • ah got it thanks again – Valarie Brega Jul 01 '19 at 17:48
  • [This link](https://www.apriorit.com/dev-blog/66-develop-boot-loader) shows how to load sector 0 then clear screen & print text via interrupts (from section 2.3). This one shows how to write a [bootloader that launches an app](https://interrupt.memfault.com/blog/how-to-write-a-bootloader-from-scratch) , which could be an OS instead. The [basic idea](http://3zanders.co.uk/2017/10/13/writing-a-bootloader/) is to load the first 512 bytes into memory, of which the last two bytes are 0xAA55. This signals BIOS to jump to location 0x7C00 (effectively transferring control to the bootloader). – Zimba Jan 22 '21 at 16:03

0 Answers0