Page 1 of 1

Load code into memory and jmp to it?

Posted: Sat Apr 16, 2005 8:02 am
by hyaku
Hello!
I'm new to assemnly, but I have made a boot loader! It just displays a string, but I'm wondering how can I load machine code into memory and execute it? I think it can be made like this:

Code: Select all

use16

pop code
jmp ... (to memory address where d3f1 code is loaded and it should execute it!)

code db d3f1
can anyone please show me how to do it!? Thank you!

Re:Load code into memory and jmp to it?

Posted: Sat Apr 16, 2005 9:37 am
by AR
The first thing to understand about low level programming is 'The computer does not distinguish between data and code', the CPU sees it all as binary, all you need to do is load memory off a disk or whatever using BIOS interrupts and jump to it.

I suggest you look at more examples, you should be able to find quite a few around.

Re:Load code into memory and jmp to it?

Posted: Sat Apr 16, 2005 10:11 pm
by Neo
When the computer boots up what it basically does is just load the BIOS which after finishing the preliminary checks of hardware loads the first sector of the bootable media (which is specified in the BIOS settings).
Your bootloader is on the first sector. So your first task would be to get your (primary??) bootloader code to load the rest of the code needed from the disk (remember all this has to be done in 512 bytes, 1 sector).
On the other hand you could use an existing bootloader (like GRUB) and forget all about this hardware initialization stuff, loading the kernel etc.. because later on when you progress to writing an OS (that's where you're headed right?) you would have to discard all of this code anyway (unless you're writing a real mode OS).


(Shouldn't this be in the OS design board ?)

Re:Load code into memory and jmp to it?

Posted: Mon Apr 18, 2005 7:03 am
by bubach
Neo wrote:you would have to discard all of this code anyway
Why would he need to do that? :P

Re:Load code into memory and jmp to it?

Posted: Mon Apr 18, 2005 9:16 pm
by Neo
Because i don't think he would need to re-init the system after his kernel is loaded in.

Re:Load code into memory and jmp to it?

Posted: Tue Apr 19, 2005 1:05 am
by distantvoices
@neo:

"discard the code" is a hard expression for such a little thing like a boot loader.

If he likes to, shall he write a boot loader. it's a good experience in assembly programming (which he 'd need in his OS too) and research techniques. Later on, if he thinks, it's better to use GRUB - shall he use grub then. It's his choice what to do.

and of course, once in kernel land, he needs to init the system to a state he needs: gdt,idt,paging, setting up hardware, browsing the pci config space etc ...

@hyaku:
you tell the bios int 13 to load a binary from sectorx to sectory to a given address and if the loading 's been successful, you jump to that address. If you initialize protected mode and stuff a priori is your choice.

Re:Load code into memory and jmp to it?

Posted: Tue Apr 19, 2005 5:46 am
by Neo
Of course, it is up to him. I did not forbid anyone from doing it. In fact I started out with a bootloader. I was just warning him that once he starts on his bootloader it would distract him from his actual goal (if it is a kernel, that is)