| | |
Tubo C strange problem
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2008
Posts: 22
Reputation:
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
•
•
•
•
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?
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.
•
•
Join Date: Apr 2008
Posts: 22
Reputation:
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: 22
Reputation:
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 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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Apr 2008
Posts: 22
Reputation:
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:
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.
C Syntax (Toggle Plain Text)
#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++]); } }
![]() |
Other Threads in the C Forum
- Previous Thread: How to parse a string?
- Next Thread: Program to convert PDU string to ASCII
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks binarysearch calculate changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fflush fgets file fork forloop framework function getlasterror givemetehcodez grade gtkwinlinux hacking hardware histogram inches include incrementoperators input intmain() iso kernel keyboard km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue number oddnumber odf opendocumentformat opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket socketprograming standard string systemcall testing threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






