Page 1 of 1
Bruce's C Compiler
Posted: Fri Nov 21, 2003 7:49 pm
by edy42
I am using bcc (a linux 8086 c compiler). When I try to compile the following code, an run it, it returns incorrect values:
Code: Select all
int strlen(c)char *c; {
int x = 0;
for(;c[x]!=0;x++);
return x;
}
For example, I try to call it like this:
strlen("Hello World");
and it returns the value 2. When compiled with gcc, it returns correct values. Is this a compiler bug, or am I doing something wrong? Does anyone have experience with Bruce's C Compiler? I am using it for writing a simple 8086 kernel (I didn't post this in the OS forum because I didn't think that this related directly to OS programming). Thanks.
Re:Bruce's C Compiler
Posted: Sat Nov 22, 2003 12:17 am
by Schol-R-LEA
Hmmn, it's been a while since I've seen K&R-style arg declarations, but according the the compiler's
man page, that's what it supports. The code looks fine to me.
How are you running the code, though, and what format are you compiling and linking it into? It looks like a.out is the default object format, am I right? If you are running it in your OS, how are you loading it?
Re:Bruce's C Compiler
Posted: Sat Nov 22, 2003 1:56 am
by Solar
Off-chance: Your "Hello World" doesn't happen to be in 16 bit Unicode? (You could check this with a hex editor.)
Otherwise, the code looks good... ???
Re:Bruce's C Compiler
Posted: Sat Nov 22, 2003 3:27 am
by df
hmm
my strlen is. this is an old veneralbe bsd strlen.
Code: Select all
size_t strlen(char *string)
{
char *ret = string;
while (*string++);
return string - 1 - ret;
}
Re:Bruce's C Compiler
Posted: Sat Nov 22, 2003 7:42 am
by edy42
I examined the output file and it does not produce unicode. This code did not work either:
Code: Select all
? ? char *ret = string;
? ? while (*string++);
? ? return string - 1 - ret;
I am compiling it using the -c comand line option with bcc:
bcc -c <sourcefile>
Then I am linking it like this:
ld86 -d <objectfiles> -o kernel
I am loading it by writing it to the second sector of a floppy drive, and writing a bootsector which loads the second sector to the first sector. However, I finally got it to work, using this assembly language code which I found in the standard library for this compiler:
Code: Select all
int strlen(string) char * string;
{#asm
#if !__FIRST_ARG_IN_AX__
mov bx,sp
#endif
push di
#ifdef PARANOID
push es
push ds ! Im not sure if this is needed, so just in case.
pop es
cld
#endif
! This is almost the same as memchr, but it can
! stay as a special.
#if __FIRST_ARG_IN_AX__
mov di,ax
#else
mov di,[bx+2]
#endif
mov cx,#-1
xor ax,ax
repne
scasb
not cx
dec cx
mov ax,cx
#ifdef PARANOID
pop es
#endif
pop di
#endasm
}
Thanks.
Re:Bruce's C Compiler
Posted: Sat Nov 22, 2003 10:09 am
by df
hmm i think there is something wrong with your bcc then if it didnt work........
Re:Bruce's C Compiler
Posted: Sat Nov 22, 2003 10:55 am
by edy42
hmm i think there is something wrong with your bcc then if it didnt work.....
That may be so. I tried recompiling bcc, and it still fails to function properly.
Re:Bruce's C Compiler
Posted: Sat Nov 22, 2003 11:44 am
by Tim
You mentioned that you're using your own loader. bcc probably supports something as simple as strlen implemented as a for loop. Did you try this code in, say, a DOS .COM file? Realistically the bug is more likely to be in your loader than it is to be in the compiler.