Load code into memory and jmp to it?

Programming, for all ages and all languages.
Post Reply
hyaku

Load code into memory and jmp to it?

Post 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!
AR

Re:Load code into memory and jmp to it?

Post 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.
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Load code into memory and jmp to it?

Post 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 ?)
Only Human
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re:Load code into memory and jmp to it?

Post by bubach »

Neo wrote:you would have to discard all of this code anyway
Why would he need to do that? :P
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Load code into memory and jmp to it?

Post by Neo »

Because i don't think he would need to re-init the system after his kernel is loaded in.
Only Human
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:Load code into memory and jmp to it?

Post 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.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Load code into memory and jmp to it?

Post 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)
Only Human
Post Reply