2

I've recently been researching Operating Systems, the boot process, and NASM. On my journeys I ran into a piece of useful bootstrapping code which I partially understand and have tested via a virtual floppy disk. My basic question is to what some of these lines I don't understand do. I've commented what I think the lines do, and any corrections or confirmations would be much appreciated.

; This is NASM

        BITS 16                 ; 16 bits!

start:                          ; Entry point
        mov ax, 07C0h           ; Move the starting address (after this bootloader) into 'ax'
        add ax, 288             ; Leave 288 bytes before the stack beginning for some reason
        mov ss, ax              ; Show 'stack segment' where our stack starts
        mov sp, 4096            ; Tell 'stack pointer'  that our stack is 4K in size

        mov ax, 07C0h           ; Use 'ax' as temporary variable for setting 'ds'
        mov ds, ax              ; Set data segment to where we're loaded


        mov si, text_string     ; Put string position into SI (the reg used for this!)
        call print_string       ; Call our string-printing routine

        jmp $                   ; Jump here - infinite loop!

        text_string db 'This is my cool new OS!', 0 ; Our null terminated string
                                                    ; For some reason declared after use


print_string:                   ; Routine: output string in SI to screen
        mov ah, 0Eh             ; I don't know what this does..
                                ; Continue on to 'repeat'
.repeat:
        lodsb                   ; Get character from DS:SI into AL
        cmp al, 0               ; If end of text_string
        je .done                ; We're done here
        int 10h                 ; Otherwise, print the character (What 10h means)
        jmp .repeat             ; And repeat

.done:
        ret

        times 510-($-$$) db 0   ; Pad remainder of boot sector with 0s
        dw 0xAA55               ; The standard PC 'magic word' boot signature

Thanks,

Joe

Siguza
  • 21,155
  • 6
  • 52
  • 89

2 Answers2

1

Your comments are largely correct.

mov ah,0Eh

This sets a parameter to the BIOS interrupt call:

int 10h

See here for more details, but essentially the call to 10h expects an operation in ah and data for the operation in al.

The segment registers cannot be loaded directly and can only load from a register, thus the use of ax as a 'temporary variable.'

The 288 bytes added to the base stack pointer are actually not bytes at all. Addresses loaded into the segment registers are actually pointers to 16-byte blocks, so to convert the number to its actual address, shift it left by 4-bits. That means that the address 07C0h is actually referring to 7C00h, which is where your bootloader code is placed. 288 is 120h in hex, and so the actual location of the stack is really 7C00h + 1200h = 8E00h.

Also, you use words like "show" and "tell" which are fine, but it's better to think of defining the stack by setting ss and sp as opposed to reporting where it is at... I hope that makes sense.

benaryorg
  • 915
  • 1
  • 8
  • 21
Dougvj
  • 6,409
  • 2
  • 23
  • 18
  • Oh I see, so 4608 bytes are actually being added to 'ax'. Presumably 512 of this is for the script/program itself, and then 4096 of this is used for something. Is the 4096 used as a disk buffer or is it for the stack itself -- is 'ss' being set to the beginning or the end of the stack? –  Jun 01 '12 at 17:18
  • @Joesavage1 SS defines the base of the stack and SP defines where in the stack we are at. When the stack is empty, SP points to the highest offset from SS. That means that SP is actually decremented when things are pushed onto the stack and incremented when they are popped off. SP Always points to the last thing pushed onto the stack – Dougvj Jun 01 '12 at 17:23
  • So you're saying it indicates the _start_ of the stack?! If so, why are we passing it a location ('ax') 4096 bytes away from where the storage of the code itself is? (4608 from the entry point of the thing) What're those 4096 bytes for if they aren't for the stack? Also, do you know why 'ds' is being set back to 07C0h [7C00h] after the stack stuff is done? ("Set the data segment to where we're loaded") –  Jun 01 '12 at 17:27
  • @Joesavage1 Sorry I hit enter prematurely on the last comment. The stack can be located pretty much arbitrarily, perhaps the code author wanted some space for some other purpose. I don't think its strictly necessary, if you move the stack down it should be exactly the same. DS is set to where you are loaded at because the constants (eg text_string) are referenced from there. If you want to use a special separate segment, you would have to tell NASM. – Dougvj Jun 01 '12 at 17:33
  • Right, ok. So that 4096 is basically just random space, got it. My issue with the DS is that surely we've already set 512 B code segment as well as that random space and the 4K stack over from 07C0h [7C00h], hence surely setting the DS (where we want to store the string) there would mean that the string storage would overwrite our 512 B section and all the other stuff we just made? –  Jun 01 '12 at 17:42
  • No, DS simply tells the processor where to start referencing data from, and in this example it is the exact same place as where the code is stored. The loadsb instruction will grab the data from DS:SI as shown in the comment. Data must be set to the same as the code segment in this example because NASM will auto-magically place the offset from the beginning of the code to the string constant in the mov ax, text_string instruction. 8086 instructions as these are very low level, if there were something being overwritten you would know because it would be in a loop. The two segments can overlap. – Dougvj Jun 01 '12 at 17:52
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12041/discussion-between-dougvj-and-joesavage1) – Dougvj Jun 01 '12 at 17:56
0
 mov ah, 0Eh             ; I don't know what this does..

Loading 0eh into ah sets up the int 10h function Teletype output, which will print the character in al to the screen.

Your .repeat loop will then load each character from text_string into al and call int 10h.

Anders Lindahl
  • 41,582
  • 9
  • 89
  • 93
  • I'm not that much of a boot loader person. :-) This question has answers explaining the setup: http://stackoverflow.com/questions/3231607/stack-segment-in-the-mikeos-bootloader – Anders Lindahl Jun 01 '12 at 16:17