Advertisement
Haydot

Assembly boot sector "Hello world!"

Oct 11th, 2021
2,277
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [bits 16] ;Tell nasm that this code should be written in 16 bits !
  2. [org 0x7c00] ;Tell BIOS where our boot sector should be loaded (the boot sector lays in between the adresses starting from 0x7c00 to 0x7e00)
  3. mov bp, 0x9000 ;Place the start of the stack very far away from BIOS files so that we don't overwrite them
  4. mov sp, bp ;Same thing here
  5. mov bx, msg ;Using the register bx as an argument to our print_string function. We put the value of msg in bx
  6. call print_string ;Call our function for printing strings
  7. jmp $ ;Jump to the current adress AKA hanging in one place
  8. print_string: ;Start of our print string function
  9.     pusha ;Push all registers to the stack
  10. char_loop: ;In this function, we loop through every byte in bx and print it, ending when there are no bytes left
  11.     mov al, [bx] ;Move the next byte into al
  12.     cmp al, 0 ;Check if there is a byte
  13.     jne char_print ;If there is, we print the char
  14.     popa ;If there isn't, we retrieve all registers from the stack
  15.     ret ;And return to our main loop
  16. char_print: ;Printing individual chars
  17.     mov ah, 0x0e ;Enabling BIOS Tele-type mode
  18.     int 0x10 ;Calling BIOS interupt for printing to screen, equvialent to, in python: 'print(al)'; Interupts are special functions in lower-level programming which when called stop all processes on the pc, do what they must do and let the pc continue it's work
  19.     add bx, 1 ;Moving to the next adress AKA next char in bx
  20.     jmp char_loop ;Going back to the char loop
  21. msg: db 'Hello world!', 0 ;Making a variable called 'msg' that holds databytes given to it by the db(declare byte(s)). In it's current state it's a char, not a string, so we have to tell the router when to stop printing, which we do witj the ', 0'
  22. times 510-($-$$) db 0 ;Pad the file with zeros 510 times !
  23. dw 0xaa55 ;The last two bytes tell BIOS that this is a boot sector and that it should boot it !
  24.  
  25. ;All lines that have a '!' at the end of their comments are only read by the assembler and are deleted while assembling.
  26.  
  27.    
  28.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement