Tubo C strange problem

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2008
Posts: 22
Reputation: paolomontero is an unknown quantity at this point 
Solved Threads: 0
paolomontero paolomontero is offline Offline
Newbie Poster

Tubo C strange problem

 
0
  #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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,902
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 302
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Cenosillicaphobiac

Re: Tubo C strange problem

 
0
  #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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 334
Reputation: Prabakar is on a distinguished road 
Solved Threads: 29
Prabakar's Avatar
Prabakar Prabakar is offline Offline
Posting Whiz

Re: Tubo C strange problem

 
0
  #3
Jun 5th, 2008
dont use trubo c. If you search this forum you would know why.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,629
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 121
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Tubo C strange problem

 
0
  #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).
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 22
Reputation: paolomontero is an unknown quantity at this point 
Solved Threads: 0
paolomontero paolomontero is offline Offline
Newbie Poster

Re: Tubo C strange problem

 
0
  #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 Quick reply to this message  
Join Date: Feb 2008
Posts: 1,629
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 121
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Tubo C strange problem

 
0
  #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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 22
Reputation: paolomontero is an unknown quantity at this point 
Solved Threads: 0
paolomontero paolomontero is offline Offline
Newbie Poster

Re: Tubo C strange problem

 
0
  #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 Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Tubo C strange problem

 
0
  #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 2:02 am.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 22
Reputation: paolomontero is an unknown quantity at this point 
Solved Threads: 0
paolomontero paolomontero is offline Offline
Newbie Poster

Re: Tubo C strange problem

 
0
  #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 Quick reply to this message  
Join Date: Jun 2008
Posts: 79
Reputation: Adak is an unknown quantity at this point 
Solved Threads: 7
Adak Adak is offline Offline
Junior Poster in Training

Re: Tubo C strange problem

 
0
  #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.

  1. #include <stdio.h>
  2.  
  3. void f(char *);
  4.  
  5. char h[] = "hello";
  6.  
  7. int main() {
  8. int gar;
  9. f(h);
  10. gar = getchar(); gar++;
  11. return 0;
  12. }
  13. void f(char *s) {
  14. int n = 0;
  15.  
  16. while (s[n] != '\0') {
  17. /*do something*/
  18. putchar(s[n++]);
  19. }
  20. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC