assembly - Why does my bootloader crash after adding this line? -


i following nick blundell's tutorials boot sector programming (https://www.cs.bham.ac.uk/~exr/lectures/opsys/10_11/lectures/os-dev.pdf , https://www.youtube.com/watch?v=yvzhgro7hl4). code works fine in qemu emulator, when run on physical machine crash whenever begin reference segment registers. teachers @ school not familiar low level programming , cannot me. here bootloader, here have annotated lines cause crash string crash (note: when crash, goes on load os next disk. loading code external hdd) :

[bits 16] [org 0x7c00]          mov bp, 0xffff mov sp, bp mov ax, 0x0000 mov ds, ax ;; mov es, ax ;; crash ;; mov ss, ax ;; crash  mov si, boot_msg call print_string call print_newline  mov si, init_seg_msg call print_string call print_newline  ;; mov dx, ds ;; crash ;; call print_hex ;; call print_newline  ;;mov dx, cs ;; crash ;;call print_hex ;;call print_newline  ;;mov dx, es ;; crash ;;call print_hex ;;call print_newline  ;;mov dx, ss ;; crash ;;call print_hex ;;call print_newline  ;; mov dl, 0x80         ;; disk kernel ;; mov cl, 3            ;; start sect ;; mov al, 1            ;; num sect ;; mov bx, 0x7ef0       ;; ram addr ;; call load_kernel  ;; mov si, kern_msg ;; call print_string ;; call print_newline  ;;  call switch_to_pm  jmp $  %include "print.asm" %include "print_hex.asm" %include "disk.asm" %include "pm.asm"  [bits 32] pm :     mov esi, pm_msg     call print_string_pm     jmp 0x7ef0     jmp $ [bits 16]  boot_msg : db 'booted 16-bit 0x7c00',0 kern_msg : db 'loaded kernel es 0x7ef0',0 pm_msg : db 'switched 32-bit mode',0 init_seg_msg : db 'init segment registers',0  times 510-($-$$) db 0 dw 0xaa55                   ` 

i sure have fundamental misunderstanding, appreciated. here printing routines:

print_string :     push ax     _loop :         lodsb         cmp al, 0         je _end          mov ah, 0x0e         int 0x10         jmp _loop     _end :         pop ax         ret  print_hex :     mov si, hex_template      mov bx, dx     shr bx, 12     mov bx, [bx+hexabet]     mov [hex_template+2], bl      mov bx, dx      ;; bx -> 0x1234     shr bx, 8       ;; bx -> 0x0012     , bx, 0x000f  ;; bx -> 0x0002     mov bx, [bx+hexabet]     mov [hex_template+3], bl      mov bx, dx           shr bx, 4     , bx, 0x00f        mov bx, [bx+hexabet]     mov [hex_template+4], bl      mov bx, dx           , bx, 0x0f         mov bx, [bx+hexabet]     mov [hex_template+5], bl      call print_string     ret       hex_template : db '0x???? ',0     hexabet : db '0123456789abcdef'  print_newline :     pusha     mov ah, 0x0e     mov al, 0x0d     int 0x10     mov al, 0x0a     int 0x10     popa     ret 

bios's have peculiarity them in state of cs unknown. in bochs , qemu cs = 0, therefore code origin of 0x7c00 work. real hardware may possibly pass cs = 0x7c0, without appropriate far jump @ beginning of code calls near absolute functions skewed 0x7c00 bytes origin set 0x7c00.

solutions:

    org    0x7c00      jmp    0:begin                   ; far jump cs = 0 begin:     mov    ax, cs     mov    ds, ax     mov    es, ax 

or

    org    0     jmp    0x7c0:0                   ; far jump cs = 0x7c0 

this what's happening , crash coming @ call print_string looking code @ 0xf8??.


Comments