assembler help

Posted By: Anonymous

assembler help - 04/14/10 15:57

hey,

is there anyone who knows assembler and can help me with a problem (bootloader)?

Thanks,
Fear
Posted By: Joey

Re: assembler help - 04/14/10 19:13

assembler is a wide topic. what problem do you have?
Posted By: Anonymous

Re: assembler help - 04/14/10 19:26

i dont know if the problem is in the bootloader or in the bootable floppy image,
but i created a bootload which should load a program, but the program doesn't get loaded.
Posted By: MichaelGale

Re: assembler help - 04/14/10 19:52

Have you verified that your bootloader is exactly 512 bytes long and located in the first sector on your floppy?
Is your computer / virtual machine set to boot from the floppy?
What happens if you run your bootloader?
Posted By: Anonymous

Re: assembler help - 04/14/10 20:02

yes
yes

The bootloader does everything he should except loading/jumping to the program.

Do you need some code?
Posted By: MichaelGale

Re: assembler help - 04/14/10 20:04

I suppose so; otherwise it's hard to tell what is going wrong.
Posted By: Anonymous

Re: assembler help - 04/14/10 20:48

Thats the bootloader

Click to reveal..
Code:
org 0x7C00		;start address of the bootloder that will load the kernel

xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
mov sp, ax


mov [bootdrive], dl 	;bootdrive stored by BIOS in DL.
                        ;we save it to [bootdrive] to play for safety.


mov si, strLoad		;show loading message
call printStr


;-----------------------

loadKernel:
  mov si, strDot	;show loading progress
  call printStr

  xor ax, ax         	;mov ax, 0 => function "reset"
  mov dl, [bootdrive] 	;boot drive
  int 0x13   		;reset floppy drivers
  jc loadKernel     	;CF set? try again

  mov si, strDot
  call printStr

			;set parameters for reading function
  mov ah, 0x2          	;read sectors 
  mov al, 10          	;read 10 sectors
  mov ch, 0          	;cylinder = 0
  mov cl, 2          	;sector = 2
  mov dh, 0          	;head = 0
  mov dl, [bootdrive] 	;boot drive
  mov bx, 0x8000	;start address of kernel

  int 0x13         	;read sectors
  jc loadKernel     	;CF set? try again

  mov si, strDot
  call printStr
  
  mov si, strDone
  call printStr

  jmp 0x8000

;-----------------------

printStr:
  mov ah, 0x0E      	;teletype output
.loop:   
  lodsb             	;grab a byte from SI
  test al, al       	;NULL?
  jz .done          	;if NULL exit
  int 0x10          	;print out the character!
  jmp .loop
.done:
  ret

;-----------------------

bootdrive db 0    	;boot drive, 0 = floppy
strLoad db "Loading kernel", 0
strDot db ".", 0
strDone db "[DONE]", 13, 10, 0
    
times 510-($-$$) hlt
db 0x55
db 0xAA



and thats the kernel:

Code:
[BITS 32]
[global kernelMain]
[extern _main]

kernelMain:
    jmp 0xffff:0x0000  ;reset pc
    jmp $



the c kernel is just a main function. At this stage i dont call it.

The linker script:
Code:
ENTRY(kernelMain)
SECTIONS
{
  .text  0x8000 : {
    *(.text)
  }
  .data  : {
    *(.data)
  }
  .bss  :  { 					
    *(.bss)
  }
}




and the program to create the floppy image:
Code:
char boot_buf[512];
	char kernel_buf[1024];
	char space_buf[1474560-512-1024] = {0};
	int floppy_desc, file_desc;

	file_desc = open("boot.bin", O_RDONLY);

        read(file_desc, boot_buf, 512);
        close(file_desc);

        floppy_desc = open("floppy.img", O_RDWR);

        lseek(floppy_desc, 0, SEEK_SET);
        write(floppy_desc, boot_buf, 512);

        file_desc = open("kernel.bin", O_RDONLY);
        read(file_desc, kernel_buf, 1024);
        close(file_desc);

        lseek(floppy_desc, 512, SEEK_SET);
        write(floppy_desc, kernel_buf, 1024);

	lseek(floppy_desc, 1024+512, SEEK_SET);
        write(floppy_desc, space, 1474560-512-1024);

        close(floppy_desc);



Posted By: Joey

Re: assembler help - 04/14/10 21:21

i'm not quite sure about all that absolute addressing stuff and asm linking but could it be that you're jumping into the text section of your kernel and not to the program itself?

edit: nevermind, i mixed data and text. (can't they call it code and data?)
Posted By: MichaelGale

Re: assembler help - 04/14/10 21:23

You are trying to run a 32-bit program in real mode which is something you can't do. You need to switch to protected mode if you want to run your 32-bit kernel.
Posted By: Anonymous

Re: assembler help - 04/15/10 14:29

mhh it doesnt work, also when i switch the program to 16bit.
But i have another problem. I used fat_imgen to generate the image and then inserted the bootloader and the kernel. But i changed it so it creates the image with the file above, but now it doesnt even boots.
Posted By: MichaelGale

Re: assembler help - 04/15/10 22:15

Not surprised it doesn't boot. I assume (based on the name of the tool you have used) that it formats the image using a FAT (either FAT12, FAT16 or FAT32) filesystem and then inserts your bootloader and kernel as regular files which means the bootloader won't be located in the first sector of the image. Even if it should insert the bootloader at the correct position, your kernel will probably end up as file and your bootloader does not contain the required code to parse the FAT which would be required in that scenario.

You should stick to the program you first wrote because that is easier. I also recommend that you first compile your kernel without the linker stuff (and consequently without the bit written in C). Just write a simple 16 bit kernel in assembler that prints a string to the screen so you can test whether it gets called. Once you get that working, try to enter protected mode and then you can load a program written in C/C++.
Posted By: Anonymous

Re: assembler help - 04/17/10 15:21

ok i used only the two asm files. When i compile them and copy them together then it works but if i use my createFloppy file then it doesnt boot.

Thanks for you help,
Fear
Posted By: Anonymous

Re: assembler help - 04/20/10 16:32

it also doesnt work if i use a real floppy. Any idea?
Posted By: Anonymous

Re: assembler help - 04/21/10 20:27

I try to do it now with the win32 api, but i cant get it to work. This is my code:

Code:
HANDLE hFloppy = CreateFile("\\\\.\\A:", GENERIC_ALL, 0, NULL, OPEN_EXISTING, FILE_SHARE_WRITE, NULL);
 WriteFile(hFloppy, bootBuf, 512, NULL, NULL);
 CloseHandle(hFloppy);


Posted By: Anonymous

Re: assembler help - 04/21/10 21:01

it works now. I just had to set administrative rights for the exe, windows 7...
I can write the bootloader into sector 0 with the c program, but it doesnt wrote the kernel into sector 1.
Posted By: Anonymous

Re: assembler help - 04/22/10 15:10

found a program that works:

http://uranus.chrysocome.net/linux/rawwrite-old.htm

Thanks to everyone.

EDIT: Forgott about the edit function wink
© 2024 lite-C Forums