Page 1 of 1

cannot make kernel calls + some forum wierdness

Posted: Wed Oct 21, 2009 12:44 pm
by smeezekitty
well first thing is:
i went to register at the forum and it said the username already used
so i went to log in with my normal forum username and password and it worked
and i never registered here
ok now getting on to the point:
i wrote a single tasking real mode kernel in tiny memory model
it far calls the child but i cannot call kernel calls like open() or write() or fopen()
it just locks up
child example:

Code: Select all

void far main(){
((void (far*)(char *))0x40000280L)("Hello world");
asm {retf; db 0EAH} //compiler likes to return near
}
map file:

Code: Select all


 Start  Stop   Length Name               Class

 00000H 01414H 01415H _TEXT              CODE
 01416H 02129H 00D14H _DATA              DATA
 0212AH 0212AH 00000H _BSS               BSS

  Address         Publics by Name

 0000:06EB       absread(int,int,int,void near*)
 0000:0746       abswrite(int,int,int,void near*)
 0000:0457       clock_hook()
 0000:0A33       createfile(char near*,int)
 0000:117C       debug(char near*,int,int)
 0000:033E       farstrcpy(char far*,char near*)
 0000:0C4E       fclose(jfile near*)
 0000:0DE5       feof(jfile near*)
 0000:09B5       findfile(char near*,int)
 0000:0BA7       fopen(char near*,char near*)
 0000:0CE2       fread(void near*,long,long,jfile near*)
 0000:039B       free(void near*)
 0000:0E06       fseek(jfile near*,int,char)
 0000:0E49       ftell(jfile near*)
 0000:0E8B       fwrite(void near*,int,int,jfile near*)
 0000:0F23       gets(char near*)
 0000:07F0       getstring(char near*,char near*,char,int)
 0000:0D60       getvector(unsigned char)
 0000:0F6E       hexdump(char near*,int)
 0000:0507       itoa(int,char near*,int)
 0000:11B5       kernel_execute(char near*)
 0000:0280       kernel_print(char near*)
 0000:0234       keyready()
 0000:0956       li(char near*,char)
 0000:096E       locate(char near*,char near*,int near*)
 0000:0FA3       login()
 0000:06AC       malloc(int)
 0000:0B08       mgen(char near*)
 0000:0141       rand()
 0000:0CAA       readbyte(jfile near*)
 0000:0245       read_keyboard()
 0000:0AC9       refresh(jfile near*)
 0000:07A1       seperate(char near*,__string near*)
 0000:0C69       setbyte(jfile near*,unsigned char)
 0000:0D38       setvector(unsigned char,(far*)(),...)
 0000:0569       sprintf(char near*,const char near*,...)
 0000:0828       str2int(char near*)
 0000:0366       strcat(char near*,char near*)
 0000:0314       strcpy(char near*,char near*)
 0000:018E       strlen(char near*)
 0000:03DA       strlwr(char near*)
 0000:01E6       strncat(char near*,char near*,int)
 0000:02A6       strncmp(char near*,char near*,int)
 0000:0414       strncmpi(char near*,char near*,int)
 0000:01AA       strncpy(char near*,char near*,int)
 0000:04AC       strrev(char near*)
 0000:03A0       strupr(char near*)
 0000:0D82       timer(unsigned int)
 0000:0E59       _k_panic(char near*,int)
 0000:025E       _video_clearscreen()
 0000:0271       _video_movecursor(char,char)
 0000:0250       _video_putchar(char)
 0000:0DBA       ____len(jfile near*)
 0000:04A0       ____ticker()
 0000:0110       DGROUP@
 0000:0112       EXEC
 0000:12EB       _MAIN
 0000:141E       _OLD_CLOCK
 0000:1E45       __P_
kernel print function:

Code: Select all


void _far kernel_print(char *data){
for(int h = 0;data[h] && h < 80;h++){
_video_putchar(data[h]);
}
return ;
}
boot.asm thanks to the TheNbomr

Code: Select all

;
;   Boot-n-Load
;
;   Bootable floppy.  BIOS loads one sector, executes it.  The
;   executable loads the next level of code.
;
;   To build & use (DOS Batch file):
;
;;  ;;            NASM16 -f bin -o bnl1.bin -l bnl1.lst bnl1.asm
;;  ;;            NASM16 -f bin -o bnl2.bin -l bnl2.lst bnl2.asm
;;  ;;            copy  /b bnl1.bin+bnl2.bin bnl.bin
;;  ;;            rawrite2 -f bnl.bin -d A -n
;
LEVEL2_SEG      equ     0x4000;0x9000
LEVEL2_OFF      equ     0x0100;0x0000

[BITS 16]
[ORG 0]

BnL_start:
;
;   We know the absolute address, but some BIOSes use different
;   SEG:OFF combos.  This makes sure we are using offsets that agree
;   with our ORG statement
;
    jmp     0x07c0:BnL_Go


BnL_HelloMsg:
    db      'BnL ver 0.1',0x0d,0x0a,0

BnL_Go:
    mov     ax, cs
    mov     es, ax
    mov     ds, ax
;
;   Set up a stack
;
    cli
    mov     sp, 0xffff
    mov     ax, LEVEL2_SEG
    mov     ss, ax
    sti

    mov     si, BnL_HelloMsg    ; Print msg
BnL_SayHello:
    lodsb                       ; AL=memory contents at DS:SI

    cmp     al, 0               ; If AL=0 then done talking
    je      BnL_ResetDisk       ; go on to next step

    mov     ah, 0x0E            ; Print AL
    mov     bx, 7               ; text attribute/colour
    int     10h                 ; BIOS Print Char

    jmp     BnL_SayHello        ; Print next character

;
;   Load the next piece of code from sector 2, 3...
;

BnL_ResetDisk:

    mov     ax, 0
    int     0x13            ;
    jc      BnL_ResetDisk

BnL_Reload:
    mov     ax, LEVEL2_SEG
    mov     es, ax          ; SEgment address for destination
    mov     ah, 02          ; BIOS Int 13h func: Read disk
    mov     al, 17           ; sector count
    mov     bx, LEVEL2_OFF  ; Address OFFSET for destination (ES:BX --> dest)
    mov     ch, 0           ; Disk cylinder
    mov     cl, 2           ; Sector (numbered from 1)
    mov     dh, 0           ; Head
    mov     dl, 0           ; Drive

    int     0x13
    jc      BnL_Reload


    jmp     LEVEL2_SEG:LEVEL2_OFF

TIMES 510-($-$$) db 0x90
    dw      0AA55h

;
;=========================================================================
;   The following code is loaded from disk sector(s) following
;   the sector 1 code loaded and invoked by the BIOS.
;   We jump to this code from the bootstrap loader above.
;
;
;    mov     ax, cs
;    mov     es, ax
;    mov     ds, ax
;
;    mov     si, BnL2_HelloMsg
;BnL2_SayHello:
;    lodsb                       ; AL=memory contents at DS:SI
;
;    cmp     al, 0               ; If AL=0 then done talking
;    je      BnL2_Go             ; go on to next step
;
;    mov     ah, 0x0E            ; Print AL
;    mov     bx, 7               ; text attribute/colour
;    int     10h                 ; BIOS Print Char
;
;    jmp     BnL2_SayHello       ; Print next character
;
;
;BnL2_Go:
;    jmp     $
;
;BnL2_HelloMsg:
;    db      'Now using level2 code', 0
;
;times 1022-($-$$) db 0xff
;dw 0AA55h
[/code]
kernel execute function:

Code: Select all

char kernel_execute(char *file){
kernel_print("\r\nExecuting program : ");
kernel_print(file);
 FILE *f; char *buff,*prog,test[10];
    int file_length;

       f = fopen(file, "r");
       kernel_print("\r\n");
       kernel_print(itoa(f->sector, test, 10));
       if(f==NULL)return FALSE;
	     file_length = ____len(f)+8;
	       hexdump(f->buffer); //  read_keyboard();
		   buff = (char *) malloc(file_length + 16);
		      if (buff == NULL) {
				  return FALSE;
				     }
					prog = buff + 16 - (((unsigned int) buff) % 16);
					kernel_print(itoa(FP_SEG(prog), test, 16));
					kernel_print(":");
					kernel_print(itoa(FP_OFF(prog), test, 16));
					   kernel_print("\r\n");
					   for(int j = 0;j < file_length;j++){
					   fread(&prog[j], 1, 1, f);
					   kernel_print(itoa(prog[j], test, 10));
					   kernel_print(" ");
					   }
					      fclose(f);
					     exec((void *)prog);
					      free(buff);
					      return TRUE;

}
floppy image at http://forums.osdever.net/viewtopic.php?f=6&t=53&p=745

Re: cannot make kernel calls + some forum wierdness

Posted: Wed Oct 21, 2009 10:33 pm
by tantrikwizard
good luck with that

Re: cannot make kernel calls + some forum wierdness

Posted: Thu Oct 22, 2009 12:50 am
by Combuster
void far main(){
((void (far*)(char *))0x40000280L)("Hello world");
asm {retf; db 0EAH} //compiler likes to return near
}
To be sure, is this Turbo C?
- I haven't ever cast far pointers in this way, does this work like you think? Is the kernel really loaded at 0x40000?
- The asm retf should not be necessary, and the reason of the stray jump-far is beyond me.
- You pass near pointers to the kernel, i.e. to a different memory model.

Re: cannot make kernel calls + some forum wierdness

Posted: Thu Oct 22, 2009 1:01 am
by smeezekitty
To be sure, is this Turbo C?
yes but it is not very good
- I haven't ever cast far pointers in this way, does this work like you think? Is the kernel really loaded at 0x40000?
not sure
- The asm retf should not be necessary, and the reason of the stray jump-far is beyond me.
what stray jump far? you mean the retf? i kind of need to return before the db 0EAh
- You pass near pointers to the kernel, i.e. to a different memory model.
what do you mean?

Re: cannot make kernel calls + some forum wierdness

Posted: Thu Oct 22, 2009 1:17 am
by Combuster
From the debugging-101.
Nur Hussein wrote:First rule of building software: The compiler is always right.
Morpheus wrote:Don't think you can do it, know you can do it
Gravaera wrote:People don't learn from copy-pasting code
Michael Abrash wrote:Always check your assumptions

Re: cannot make kernel calls + some forum wierdness

Posted: Thu Oct 22, 2009 1:49 am
by smeezekitty
I am impressed with your MOS. anyway:


here is a nasm code that should print hello world but the problem is is that the "hello world" string is in a diferent segment as the kernel:

Code: Select all

[BITS 16]
GO:
jmp code
HW: db 'Hello world',0
code:
mov ax,HW
push ax
call 0x04000:0x0280
retf
db 0EAH
end
so i get garbage ascii chars

Re: cannot make kernel calls + some forum wierdness

Posted: Thu Oct 22, 2009 3:06 pm
by smeezekitty
forgot exec.asm :

Code: Select all

.MODEL TINY
.CODE
PUBLIC EXEC
EXEC PROC NEAR
PUSH  bp
MOV   bp, sp
SUB   sp, 4
PUSH  ds
MOV   bx, [bp+4]
SUB   bx, 256
SHR   bx, 4
MOV   ax, cs
ADD   ax, bx
MOV   ds, ax
MOV   WORD PTR [bp - 4], 256
MOV   WORD PTR [bp - 2], ax
CALL  DWORD PTR [bp - 4]
POP   ds
ADD   sp, 4
POP   bp
RET 2
ENDP 
END
and a different test program:

Code: Select all

#define _video_putchar(c) (((void (far *)(char))MK_FP(0x4000, 0x0250))((c)))
#define MK_FP( seg,ofs )( (void _seg * )( seg ) +( void near * )( ofs ))
void far main(){
_video_putchar('H');
_video_putchar('i');
_video_putchar('!');
_video_putchar('\n');
return;
}
asm {db 0EAh}

prints H and then freezes