•
•
•
•
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
![]() |
•
•
Join Date: Apr 2008
Posts: 15
Reputation:
Rep Power: 1
Solved Threads: 0
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:
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:
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
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:
C Syntax (Toggle Plain Text)
void f(char *); char h[] = "hello"; void main() { f(h); } void f(char *s) { int n = 0; while (s[n] != 0) { /*do something*/ n++; } }
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:
ASM Syntax (Toggle Plain Text)
0000 mov ax, 000E 0003 push ax 0004 call 0009 0007 pop cx 0008 ret 0009 push bp 000A mov bp, sp 000C push si 000D xor si, si 000F jmp 0012 0011 inc si 0012 mov bx, [bp+04] 0015 cmp byte ptr [bx+si], 00 0018 jne 0011 001A pop si 001B pop bp 001C ret 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
•
•
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,776
Reputation:
Rep Power: 11
Solved Threads: 185
•
•
•
•
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
do NOT pm me for help, in the best case, you'll get ignored
•
•
Join Date: May 2008
Location: Infinite Castle, below Beltline
Posts: 287
Reputation:
Rep Power: 1
Solved Threads: 24
•
•
Join Date: Apr 2008
Posts: 15
Reputation:
Rep Power: 1
Solved Threads: 0
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.
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.
•
•
Join Date: Apr 2008
Posts: 15
Reputation:
Rep Power: 1
Solved Threads: 0
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
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
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.
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
•
•
Join Date: Apr 2008
Posts: 15
Reputation:
Rep Power: 1
Solved Threads: 0
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
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
•
•
Join Date: Jun 2008
Posts: 79
Reputation:
Rep Power: 1
Solved Threads: 7
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.
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++]);
}
}![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the C Forum
- Previous Thread: How to parse a string?
- Next Thread: Program to convert PDU string to ASCII



Linear Mode