From: Rod Pemberton on
"James Harris" <james.harris.1(a)googlemail.com> wrote in message
news:00b40b09-bbab-4e18-bd00-8199d4eb3440(a)u22g2000yqf.googlegroups.com...
> On 14 Apr, 16:50, peter <cmk...(a)gmail.com> wrote:
>
> FWIW someone wrote a short ELF loader for an OS. Check out the
> elf_load code at
>
> http://codewiki.wikispaces.com/os+development
>
> though it sounds like you may be beyond that stage.
>

"... it is taken from a working bootloader."
http://groups.google.com/group/alt.os.development/msg/6e4b201530f3fb61?hl=en
http://groups.google.com/group/alt.os.development/msg/e347e423ccc68938?hl=en


Odd, I must not have looked at his code thoroughly when he posted. Maybe
because it was posted to a.o.d. instead of a.l.a...


So, we've got some assembly. Let's rip it up.


"mov WORD [0xb8000],0x0700 + 'E';"
"mov WORD [0xb8002],0x0700 + 'L';"
"mov WORD [0xb8004],0x0700 + 'F';"

One char at time? Why'd he do that?

mov DWORD [0xb8000],' L E'
mov DWORD [0xb8004],' ! F'

Is that byte reversed?... Correct or incorrect for MASM? If it is:

mov DWORD [0xb8000],'L E '
mov DWORD [0xb8004],'! F '


In "ldr_ELF_err"

"cli"
"hlt"

If there's a hardware NMI, the code will restart and enter the code that
follows, which is "skip_err_handler". I.e., an NMI will cause the code to
attempt to load the *bad* ELF. Yikes!!! Maybe that should be followed by
"jmp cli_label" and have the label prior to cli... ?


In "sectionloop":
"push cx"

This is 32-bit code ... "push ecx"? He xor'd the upper to ecx, "xor
ecx,ecx" in "skip_err_handler". AFAICT, ecx's upper isn't corrupted.

Ditto for "mul cx" "pop cx" "or cx,cx" in "sectionloop" and "nextsect" ...
ecx? I.e., 32-bit and 8-bit reg's are native in 32-bit mode. 16-bit reg's
put's in overrides. Slows it down. It looks like there's another one "xor
ax,ax", but that may be needed for part of a calculation... I can't tell.


"cli"
"hlt"
"hang:jmp hang"

See, he did it better there, expecting an NMI, but didn't jump to cli/hlt to
halt again... Cpu full speed ahead! No need to duplicate cli/hlt twice in
the code. I'd use this once, and jump to it for one of the hlt's:

cli_label:
cli
hlt
jmp cli_label


In "zero_memblock"

"a32 rep stosb"

a32 is apparently force use of cx (?)... Is the a32 needed? Again, he
xor'd the upper to ecx, "xor ecx,ecx" in "skip_err_handler". AFAICT, ecx's
upper isn't corrupted.


OK... Is there a reason "rep movsb" wasn't used in "memcopy"? I have no
clue why he did that. Do you? IIRC, I saw someone else do that recently
too...


Interesting... I've looked at quite a few bootloaders and executable
loaders. I've only seen lines like this in my code:

"mov ebp,esp"
"push DWORD 2"
"popfd"


He should load "ss" just prior to:

"mov dword esp,0x90000"

Why? "mov ss" pairs with "mov esp" and on recent cpu's disables inbetween
interruptions. It seems all the PM selectors are setup somewhere else too.


I find this interesting. He saves the kernel entry point. But, ISTM he
only uses the entry point once (obviously). Why'd he save it?

"
mov DWORD eax, [0x10018] ; Store entry point address for
later
mov [krnl_entry], eax
....
mov eax, [krnl_entry] ; Our saved kernel entry point
call eax

....
krnl_entry dd 0

"

Possibly, he could eliminate 3 lines.

I'd probably rewrite it to eliminate the use of 16-bit registers and 16-bit
data, where possible, since we're in 32-bit/8-bit mode.


Rod Pemberton