Page 1 of 1

can not execute code loaded in memory

Posted: Sat Mar 11, 2006 12:08 pm
by asmboozer
i read the contents of flopy disk into real memory at 0x7f00,

when i jmp into PM ,

i write
jmp 08h:7f00h+pmmode
[bits32]
pmmode:
mov eax, 10h
mov ds, ax,
mov [ds:0b800000h], 'p'
mov [ds:0b800001h], 0xab
mov [ds:0b800002h], 'm'
mov [ds:0b800003h], 0xab

jmp $


....

btw, the org starts from 0,

if I change
jmp 08:0x7f00+pmmode
to
jmp 08h:0x7c00+pmmode
the code will work,

i wonder why 7f00h wont work although the same code is loaded into the memory at the address 0x7f00h .

Re:can not execute code loaded in memory

Posted: Sat Mar 11, 2006 4:55 pm
by Pype.Clicker
how is your code ORG'ed ? What about deciding that your bootloader will be executing in segment 0 in realmode with appropriated "ORG 0x7c00" statement ? ...

Re:can not execute code loaded in memory

Posted: Sat Mar 11, 2006 6:29 pm
by asmboozer
Pype.Clicker wrote: how is your code ORG'ed ? What about deciding that your bootloader will be executing in segment 0 in realmode with appropriated "ORG 0x7c00" statement ? ...
what do you mean?

it's orged like:
[bits 16]

[org 0]
jmp 0x7c0:start

...

start:
; read flopy disk 512 bytes into memory 0x7f00
;

; lgdt gdtr_desc

;set cr0 register

jmp 08h:0x7f00+pmmode ; since the code said it's orged 0,i need add 0x7c00/0x7f00 to notify the compiler. after set pmmode, the code base address is 0, so jmp 08h:7f00h+pmmode would work as jmp 08h:7c00h+pmmode does.
[bits32]
pmmode:
mov eax, 10h,
mov ds, ax
mov ss, ax,
mov esp, 90000h

;write 'PMODE' into video memory
mov byte [ds:0B8000h], 'P' ;
mov byte [ds:0B8001h], 1Bh
...

jmp $


; i set gdt like these

gdt db 00h ; 00h *NULL*
db 00h ;
db 00h ;
db 00h ;
db 00h ;
db 00h ;
db 00h ;
db 00h ;


dw 0FFFFh ; limit = 4GB *CODE*
dw 0000h ; base address of segment
db 0
db 9Ah ; present, ring 0, application descriptor,
; segment type: CODE, exec-read
db 0CFh ; 32-bit, 4k granularity
db 0


dw 0FFFFh ; limit = 4GB *DATA*
dw 0000h ; base address of segment
db 0
db 92h ; present, ring 0, application descriptor,
; segment type: DATA, read/write
db 0CFh ; 32-bit, 4k granularity
db 0


gdtr_desc dw 23
dd 7c00h + gdt ; Base address of the GDT , i have tried 7f00h + gdt too, but no effect.:(




i have said if I change

jmp 08h:0x7f00+pmmode

to

jmp 08h:0x7c00+pmode

it's ok then, the vmware/boch won't keep on resetting.

since i have loaded this code into 0x7f00-address-started memory,

why it's impossible to do the jmp above?

Re:can not execute code loaded in memory

Posted: Sat Mar 11, 2006 7:05 pm
by Pype.Clicker
what i mean is that for now you assume and enforce that CS=0x7c0 and IP=0 at the start of your bootloader. That causes plenty of problems because when you say "mov eax,_label", the assembler produces a value that is relative to the start of the bootloader rather than a value relative to start of memory.

If you instead start with CS=0, EIP=0x7C00 (by the mean of ORG 0x7C00), those problems disappear (in both JMP offsets, loading offsets, GDT address, etc.)

Re:can not execute code loaded in memory

Posted: Sat Mar 11, 2006 7:19 pm
by asmboozer
Pype.Clicker wrote: what i mean is that for now you assume and enforce that CS=0x7c0 and IP=0 at the start of your bootloader. That causes plenty of problems because when you say "mov eax,_label", the assembler produces a value that is relative to the start of the bootloader rather than a value relative to start of memory.

If you instead start with CS=0, EIP=0x7C00 (by the mean of ORG 0x7C00), those problems disappear (in both JMP offsets, loading offsets, GDT address, etc.)

I know it's good as you pointed out, but I try the org 0 with 0x7c00 plused to have a deep understanding.

but I don't think the problem will disappear. because I want to jump to pmmode relative to 0x7f00 where I read the floppy disk into.

if I just want to jump pmmode relative to 0x7c00,

[org 7c00h] would help me as you consider.

Re:can not execute code loaded in memory

Posted: Sat Mar 11, 2006 7:46 pm
by asmboozer
Ok, i found the error, it's the wrong read sector cause the problem.

Re:can not execute code loaded in memory

Posted: Sun Mar 12, 2006 12:42 am
by asmboozer
Pype.Clicker wrote: what i mean is that for now you assume and enforce that CS=0x7c0 and IP=0 at the start of your bootloader. That causes plenty of problems because when you say "mov eax,_label", the assembler produces a value that is relative to the start of the bootloader rather than a value relative to start of memory.

If you instead start with CS=0, EIP=0x7C00 (by the mean of ORG 0x7C00), those problems disappear (in both JMP offsets, loading offsets, GDT address, etc.)

it's true, if i wrote some PM code in the first 512 bytes, I would have to add the 0x7f00 to label. it's very annoying.

Re:can not execute code loaded in memory

Posted: Sun Mar 12, 2006 7:53 am
by Pype.Clicker
yep. since your pmode code is loaded at 0x7F00 (i assume that's something like a 2nd stage loader or something similar), you might want to have "ORG 0x7F00" at the start of that code, and keep your code & data segment based at zero.