User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 423,500 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,675 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1665 | Replies: 26
Reply
Join Date: Apr 2008
Posts: 15
Reputation: paolomontero is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
paolomontero paolomontero is offline Offline
Newbie Poster

Tubo C strange problem

  #1  
Jun 4th, 2008
Hi,

I'm using Turbo C 2.01 and im stuck with this problem.

I've reduced it to the following. My code in Turbo C is this:

  1. void f(char *);
  2.  
  3. char h[] = "hello";
  4.  
  5. void main() {
  6. f(h);
  7. }
  8.  
  9. void f(char *s) {
  10. int n = 0;
  11. while (s[n] != 0) {
  12. /*do something*/
  13. n++;
  14. }
  15. }

It's just a function that takes a string as an argument and merely loops till it finds the terminating NULL character.

I compile it like this (the file is named c.c):

tcc -mt c.c
tlink C.OBJ

and then i do exe2bin C.EXE to get the raw binary image.

The disassembly for either the .bin or .exe (they are the same, but it's easier to work with the .bin because it doesn't have the "turbo c junk code") is the following:

  1. 0000 mov ax, 000E
  2. 0003 push ax
  3. 0004 call 0009
  4. 0007 pop cx
  5. 0008 ret
  6. 0009 push bp
  7. 000A mov bp, sp
  8. 000C push si
  9. 000D xor si, si
  10. 000F jmp 0012
  11. 0011 inc si
  12. 0012 mov bx, [bp+04]
  13. 0015 cmp byte ptr [bx+si], 00
  14. 0018 jne 0011
  15. 001A pop si
  16. 001B pop bp
  17. 001C ret
  18. 001E 'hello\0' ;you get me :)

So, the problem is the first line!

mov ax, 000E

It doesn't point to the string, which is located in 001E, so if i change that byte with a hexadecimal editor the program works just fine.

My question is: why does Turbo C commit this error? Am i doing something wrong?

Thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,776
Reputation: niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all 
Rep Power: 11
Solved Threads: 185
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Tubo C strange problem

  #2  
Jun 5th, 2008
Originally Posted by paolomontero View Post
It's just a function that takes a string as an argument and merely loops till it finds the terminating NULL character.
[...]

Am i doing something wrong?

Yes. To find the terminating char, you did this:
while (s[n] != 0)

But 0 and the string-terminating-char aren't the same. So change it to:
while (s[n] != '\0')

Now your array will stay within it's limits.
Just a hint: You can also use strlen() which does the exact same thing, but with a lot of error checking.

Also: this thread is in the wrong forum. I'll ask if it can be moved.
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
Reply With Quote  
Join Date: May 2008
Location: Infinite Castle, below Beltline
Posts: 287
Reputation: Prabakar is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 24
Prabakar's Avatar
Prabakar Prabakar is offline Offline
Posting Whiz in Training

Re: Tubo C strange problem

  #3  
Jun 5th, 2008
dont use trubo c. If you search this forum you would know why.
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 713
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 46
jephthah's Avatar
jephthah jephthah is offline Offline
Master Poster

Re: Tubo C strange problem

  #4  
Jun 5th, 2008
turbo C is a piece of crap.

use the GCC compiler (free) or MSVC compiler (free) with the Code::Blocks IDE (free).
Why so serious?
Reply With Quote  
Join Date: Apr 2008
Posts: 15
Reputation: paolomontero is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
paolomontero paolomontero is offline Offline
Newbie Poster

Re: Tubo C strange problem

  #5  
Jun 5th, 2008
Thanks for replying.

niek_e, you are right about the comparing but thats not the problem. It seems to me that you misunderstood my post, if you look at the disassembly, the array position is not ok when passed to the function, independent of what the function does.

I need to use Turbo C, so i think this thread should be in Legacy, i dont know why it was moved, because the discussion isnt about C but The Turbo C compiler itself.
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 713
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 46
jephthah's Avatar
jephthah jephthah is offline Offline
Master Poster

Re: Tubo C strange problem

  #6  
Jun 5th, 2008
hi. Paolo. i wish i had an answer for you. i would try, but i don't have the compiler to replicate it with.

well, i can see now you're not the typical Turbo C user who just doesn't know any better. ... so if you don't mind, i'd like to know why do you need to use it? curious.
Why so serious?
Reply With Quote  
Join Date: Apr 2008
Posts: 15
Reputation: paolomontero is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
paolomontero paolomontero is offline Offline
Newbie Poster

Re: Tubo C strange problem

  #7  
Jun 5th, 2008
jephthah, thank you very much for your interest.

Im developing an operating system (if you can call it that) as a project of my own and just for personal use. Im really excited about this.

I already have my bootloader coded in nasm and it works.

Now im working on the kernel mixing Turbo C and nasm. I want to stay in 16 bit and thats why i use Turbo C, i have tried other compilers (there arent many 16 bit C compilers for windows) but they didnt work or maybe i couldnt get them to work.

The problem is not mixing Turbo C with nasm or the fact that the program will not sit on dos whatsoever. If i just wanted to write a program with a function that takes an array or a pointer as a parameter and compile-link it with the tiny memory model, i couldnt. I really have no clue of why the address is not correct.

Thanks,

Gabriel
Reply With Quote  
Join Date: May 2006
Posts: 2,723
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 222
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Tubo C strange problem

  #8  
Jun 6th, 2008
The program you're writing will work on ANY C compiler, even piece of crap compilers, which Turbo C is not one of. It pisses me off every time someone denegrates good compilers just because they are older. If they do exactly what the user needs, it's fine. It just can't do advanced 32bit stuff that the user obviously doesn't need. It still is C. It still works fine. And TC can still do things none of the current compilers can do.

I agree upgrading should be considered, but is not necessary. And definitely TC should not be put down.
Last edited by WaltP : Jun 6th, 2008 at 1:02 am.
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Apr 2008
Posts: 15
Reputation: paolomontero is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
paolomontero paolomontero is offline Offline
Newbie Poster

Re: Tubo C strange problem

  #9  
Jun 6th, 2008
OK, i knew about that already and thats why im using Turbo C.

But im still having the problem i posted at the begging! Anyone can help me with that? If somebody wants my messenger to instruct me easier ill be happy to pm it.

Or maybe someone can recommend me another 16 bit compiler, and how to compile and link the project (i have watcom but i havent been able to get it to work).

Thanks,

Gabriel
Reply With Quote  
Join Date: Jun 2008
Posts: 79
Reputation: Adak is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 7
Adak Adak is offline Offline
Junior Poster in Training

Re: Tubo C strange problem

  #10  
Jun 9th, 2008
I don't have Turbo C 2.0, but this runs fine in Turbo C/C++ ver. 1.01, as a C program. Note int main and the include header.

IIRC Turbo C 2.0 came before Turbo C/C++, but it's been a long time.

#include <stdio.h>  

void f(char *);

char h[] = "hello";
  
int main() {
   int gar;
   f(h);
   gar = getchar(); gar++;
   return 0; 
}
void f(char *s) {
   int n = 0;
  
   while (s[n] != '\0') {
      /*do something*/
      putchar(s[n++]);
   }
}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C Forum

All times are GMT -4. The time now is 4:14 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC