Author Topic: 16 Bit Code And Bootloaders!  (Read 6904 times)

Offline RagingGrim

  • Jr. Member
  • *
  • Posts: 28
16 Bit Code And Bootloaders!
« on: December 02, 2015, 07:01:09 PM »
Been a while since I posted on here!

I've got a small question regarding bootloaders :)
Stage One
Code: [Select]
BITS 16
ORG 0x7C00 ;Constant - memorise it
;;INITIAL HELLO
mov [DriveNum] , dl
mov esi , MsgHello
call print

mov ah, 2
mov al, 1
mov dl , [DriveNum]
mov ch , 0
mov dh , 0
mov cl , 2
mov bx , 0x1000

int 0x13
cmp ah , 0
JNE err
mov esi,Debug_MsgOK

mov esi,MsgPassingControl
call print
JMP 0x1000 ;; Jump To Stage 2

;;CODE END

;;ROUTINES START
err:
mov esi,MsgErr
call esi
call hang



asciToDigit:
 and eax,0b11001111
 ret

digitToAsci:
 xor eax,0b00110000
 ret

hang:
 JMP $

strlen:
mov ebx,eax
strlen_inc:
cmp byte [ebx],0 ;;Min register use - Not least space
JE strlen_done
inc ebx
JMP strlen_inc
strlen_done:
sub ebx,eax
mov eax,ebx
ret

strreverse: ;;eax source - ebx - strlen - edi - dest
xor edx, edx
sub edi,1
mov esi,eax
add eax , ebx
strreverse_loop:
cmp esi,eax
JG strreverse_done
mov dl , byte [eax]
mov byte [edi] , dl
inc edi
dec eax
JMP strreverse_loop
strreverse_done:
ret

asciToInt:
xor eax,eax
xor esi,esi
mov edi , 10
asciToInt_inc:
cmp esi,ebx
JE asciToInt_Done ;;Probably not the most efficient code nor am I sure it's working properly.
inc esi
mov dl , byte [ecx]
and dl,0b11001111
add al , dl
mul edi
inc ecx
JMP asciToInt_inc
asciToInt_Done:
idiv edi
ret

print:
 mov al , [si] ;Letter
 inc esi ;; Increment Pointer
 cmp al , 0
 JE print_done

 mov ah , 0x0E ;Output Mode
 mov bh , 0    ;Page
 mov bl , 0x07 ;Colour
 int 0x10   ;Interrupt
 JMP print
 print_done:
 ret
;;ROUTINES END

;;DECLARATIONS START

DriveNum dd 0
MsgHello db "Bootloader - Stage 1 V1.2",0xA,13,0
Debug_MsgOK db "OK",0xA,13,0
Debug_MsgNO db "NO",0xA,13,0
MsgErr db "Err",0xA,13,0
MsgPassingControl db "Passing Control To Stage II",0xA,13,0
MsgDriveReady db " Disk is ready . . .",0xA,13,0
Temp db 0,0,0,0,0,0,0,0,0,0,0
TIMES 510 - ( $ - $$ ) db 0 ; Constant memorise it
DW 0xAA55  ; Constant memorise it

Stage Two
Code: [Select]
BITS 16
ORG 0x0
start:
mov esi , MsgHello
call print
JMP $

print:
mov AL , [si] ;Letter
inc esi ;; Increment Pointer
cmp AL , 0
JE done

mov AH , 0x0E ;Output Mode
mov BH , 0    ;Page
mov BL , 0x07 ;Colour
int 0x10   ;Interrupt
JMP print
done:
ret
MsgHello db "Bootloader Stage II",0xA,13,0

So my questions are the following :

1.) I read that BIOS passes the drive that was used to boot from into dl - is that applicable everywhere?
2.) I'm trying to read a sector from a pendrive and then jump to the second part of the bootloader but it doesn't seem to work with any combination of parameters , any advise ?

Compiling with
Code: [Select]
nasm [filename] -f bin - o [new]Writing To Floppy With
Code: [Select]
dd if=Bootloader.bin of=/dev/sdb
dd if=Bootloader_StageTwo.bin of=/dev/sdb bs=512 seek=1
Dumping the pendrive everything seemed fine. I print the messages just fine up and till stage two ; Any help is appreciated!

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: 16 Bit Code And Bootloaders!
« Reply #1 on: December 03, 2015, 01:02:27 AM »
I'll try to look at this in more detail and get back to you. As a first wild-asmed guess, try org 0x1000 for your second stage - that's where you're loading it.

Later,
Frank


Offline RagingGrim

  • Jr. Member
  • *
  • Posts: 28
Re: 16 Bit Code And Bootloaders!
« Reply #2 on: December 03, 2015, 06:10:15 AM »
Thanks Frank I fixed it!

This morning I edited the org to 0x1000 on stage II changed the drive number to 0x81 which did the trick!