Bruce's C Compiler

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

Bruce's C Compiler

Post 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.
Schol-R-LEA

Re:Bruce's C Compiler

Post 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?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Bruce's C Compiler

Post 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... ???
Every good solution is obvious once you've found it.
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:Bruce's C Compiler

Post 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;
}
-- Stu --
edy42

Re:Bruce's C Compiler

Post 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.
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:Bruce's C Compiler

Post by df »

hmm i think there is something wrong with your bcc then if it didnt work........
-- Stu --
edy42

Re:Bruce's C Compiler

Post 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.
Tim

Re:Bruce's C Compiler

Post 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.
Post Reply