question

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
idunno

question

Post by idunno »

can anyone show me how to make an ASM file load like my kernel here:

(start of code)


[BITS 16] ;how many bits our code is
[ORG 0] ;where our code starts


[SEGMENT .text]
    mov ax, 0x0100                          ;location where kernel is loaded (where we should be)
    mov ds, ax
    mov es, ax
    
    cli     ;disable interupts
    mov ss, ax                              ;stack segment
    mov sp, 0xFFFF                          ;stack pointer at 64k limit
    sti             ;enable them again
    


jmp main ;this is here so we can skip the lines below which hold strings/misc stuff


(end of file)

but rather than jumping to my main in my ASM file, jump to my C file and let me do the rest of the kernel in C?  is this possible/? thanks in advanced
Khumba

RE:question

Post by Khumba »

Here's a bootloader that loads sectors 2-18 from the bootdrive and jumps to them. Note that you shouldn't load your kernel to 0x01000 because the BIOS might keep something there, instead try loading it to 0x10000. I left it loading at 0x01000, but if you want to change it change the lines that end with a ;*** to 0x1000 from 0x0100.

[SECTION .text]
[ORG 7c00h]
[BITS 16]

cli
mov [bootdrive], dl   ; Save the bootdrive
mov ax, 0x9000        ; We need a temporary stack
mov ss, ax
mov sp, 0xffff

mov ax, cs            ; Set up the DS
mov ds, ax
sti

mov bp, 4             ; If it fails 4 times, then give up
mov [tryctr], bp

mov ax, 0x211         ; AH=2 Load sector, AL=17 Load 17 sectors
mov cx, 2             ; CH=0 Track 0, CL=2 Sector 2
mov dh, 0             ; DH=0 Head 0, DL=xx Bootdrive
mov bx, 0x0100        ;*** ES:BX=0x01000 Destination
mov es, bx
mov bx, 0

loadLoop:
pusha
int 13h
popa
jnc loadOkay
mov bp, [tryctr]
dec bp
jnz tryAgain
jmp $

tryAgain:
mov [tryctr], bp
jmp loadLoop

loadOkay:
mov dl, [bootdrive]   ; Restore the bootdrive
mov ax, 0x0100        ;*** Push 0x01000 on the stack
push ax
mov ax, 0
push ax
retf                  ; Far return
jmp $                 ; Just incase...
anton

RE:question

Post by anton »

BTW, you don't need to use cli and sti in this case:
cli
mov ss,ax;
mov sp,0xFFFF;
sti;

In the Intel manuals it states that mov ss, ... or mov sp,... block until for the next instuction also:
Anton
anton

RE:question

Post by anton »

If you whont to jump to your C file(or any other binary), you will need to know the point of "start".
And then you write in asm:
jmp "start".

Explenation:
1)write a VERY small asm file to call "init_kernel": goes in head.S
.globl startup_32
startup_32:
jmp init_kernel;
infty:
jmp infty;
2)Write a c program which has a void init_kernel() function
3)your as program should have jmp 0x1000;//you should place your image so that the second
sector will be at that addess.
4)Link the file as follows:
as86 -b boot.bin boot.S
$(GCC) -traditional head.S -o head.o
ls -m elf_i386 -Ttext 0x1000 -e startup_32 head.o <cfile-object>.o -o head.img
pnjcopy -0 binary -R=.note -R=.comment -S head.img head.bin
cat boot.bin head.bin > image

Anton
Khumba

RE:question

Post by Khumba »

I've read that, but it's just an old habit =)
Post Reply