473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Turbo C Strange Problem

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:

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:

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?

NOTE: if the function f has no code then the address is correctly
compiled, but if it as ANY code, like a variable declaration, then the
problem appears.

Thanks
Jun 27 '08 #1
16 2344

"Gabriel" <ga***********@gmail.comwrote in message
news:01**********************************@z66g2000 hsc.googlegroups.com...
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:

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:

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.
The 000E isn't necessarily wrong, it depends on the segment registers. But
if you use a memory model where CS=DS=SS=ES then no it doesn't look right
(assuming exe2bin is doing it's job properly).

What happens when you add in this 0x10 offset to the source code (ie.
s[n+0x10])? How about varying the call, eg from f(h) to f(&h[0]) to
f("hello") and so on?

Of interest might be the contents of c.obj also, can you dump that? What
does -mt compilation switch do?

--
bartc

Jun 27 '08 #2
Gabriel <ga***********@gmail.comwrites:
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:

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.
<NITPICK>
That's null character, not NULL character. NULL (all-caps) refers
specifically to the macro that expands to a null *pointer* constant.
</NITPICK>

I'd say you've narrowed it down a bit *too* far. The program produces
no output, so a sufficiently clever compiler could reduce the whole
thing to the equivalent of ``int main(void) { }''. I doubt that Turbo
C 2.01 is that clever, but who knows what it could decided to do. (I
don't.)

Note that main returns int, not void. It's possible that TC2.01
accepts "void main()", but it's still a good habit to declare it
correctly. And since main returns int, it's a good idea to actually
return an int.

Try something like this:

#include <stdio.h>

void f(char *);

char h[] = "hello";

int main(void) {
f(h);
return 0;
}

void f(char *s) {
int n = 0;
while (s[n] != 0) {
putchar(s[n]);
putchar('\n');
n++;
}
}

If that doesn't do it, try reproducing the problem (assuming there is
one) with a program that produces output rather than asking us to look
through assembly listings.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #3
Im sorry i took so long to answer.

exe2bin works OK, because the output executable also has the address
corrupted. the bin and the exe are the same.

the -mt is to tell the compiler to use tiny model. i just noted that
in my post i missed the -c (i use it, i just forgot to write it), its
purpose is to compile and not link, so i can link in the second step.

if i try f(&h[0]) or f("hello") the problem persists, however calling
f this way: f(h + 0x10) fixes it.

Gabriel
Jun 27 '08 #4
Keith, im sorry for the assembly i just thought it would be helpful.

I tried your code but i cant get it compiled because i get undefined
external symbol when linking (putchar) even thou i have fixed my
turboc.cfg. I recall that once i got that same code to get compiled
(but i dont remember how) and it was OK, however if i try my original
code bit with int main the problem is still there. so i conclude that
stdio fixes it, but this is really strange.

Ill keep experimenting,

Gabriel
Jun 27 '08 #5
Gabriel wrote:
>
Im sorry i took so long to answer.

exe2bin works OK, because the output executable also has the address
corrupted. the bin and the exe are the same.

the -mt is to tell the compiler to use tiny model. i just noted that
in my post i missed the -c (i use it, i just forgot to write it), its
purpose is to compile and not link, so i can link in the second step.

if i try f(&h[0]) or f("hello") the problem persists, however calling
f this way: f(h + 0x10) fixes it.
This is completely meaningless.

If you want to post a followup via groups.google.com, ensure you
quote enough for the article to make sense. Google is only an
interface to Usenet; it's not Usenet itself. Don't assume your
readers can, or ever will, see any previous articles.

More details at: <http://cfaj.freeshell.org/google/and below.

--
Some useful links on quoting:
<http://www.xs4all.nl/%7ewijnands/nnq/nquote.html>
<http://www.complang.tuwien.ac.at/anton/mail-news-errors.html>
<http://www.netmeister.org/news/learn2quote2.html>
<http://www.star-one.org.uk/computer/format.htm>
** Posted from http://www.teranews.com **
Jun 27 '08 #6
Gabriel <ga***********@gmail.comwrote:
Keith, im sorry for the assembly i just thought it would be helpful.
You're not being helpful by snipping all context.
I tried your code
What code? Which code? I see no CODE here.
but i cant get it compiled because i get undefined
external symbol when linking (putchar)
That's an ISO C function. If your implementation doesn't recognise it,
either get a real C compiler, or reinstall the one you have, and this
time get it right.

Richard
Jun 27 '08 #7
Richard Bos wrote:
>
>but i cant get it compiled because i get undefined
external symbol when linking (putchar)
You are not linking against the library that contains putchar. Read your
toolset's documentation to find out how to correctly link.
That's an ISO C function. If your implementation doesn't recognise it,
either get a real C compiler, or reinstall the one you have, and this
time get it right.
With responses like this, its no wonder CLC has a reputation for rudeness.

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Jun 27 '08 #8
On Jun 7, 11:08 am, Gabriel <gabriel.ma...@gmail.comwrote:
I've reduced it to the following. My code in Turbo C is this:
and then i do exe2bin C.EXE to get the raw binary image.
So, the problem is the first line!
mov ax, 000E
Can you write a program that actually behaves
in some erratic way ? If the program's output
matches the expected output, then it doesn't
really matter what the assembly is (and it
shouldn't be taken as a sign of a bug).

BTW (to others in this thread) Turbo C 2.01
shipped in May 1989, so ANSI compliance should
not be expected.
Jun 27 '08 #9
Old Wolf said:

<snip>
BTW (to others in this thread) Turbo C 2.01
shipped in May 1989, so ANSI compliance should
not be expected.
Given that fact, it's remarkable how conforming it actually is. Yes, there are
problem bits, but really they are few and far between, and I believe that for
the most part they comprise mere corner cases. The most in-your-face
violation seems to be the use of CLOCK_TCK rather than CLOCKS_PER_SEC, which
is hardly earth-shattering.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #10
Mark McIntyre wrote:
Richard Bos wrote:
>>
>>but i cant get it compiled because i get undefined
external symbol when linking (putchar)

You are not linking against the library that contains putchar. Read
your toolset's documentation to find out how to correctly link.
>That's an ISO C function. If your implementation doesn't recognise
it, either get a real C compiler, or reinstall the one you have,
and this time get it right.

With responses like this, its no wonder CLC has a reputation for
rudeness.
What's rude about it? Terse, but factual, IMO.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #11
Richard Heathfield wrote:
Old Wolf said:

<snip>
BTW (to others in this thread) Turbo C 2.01
shipped in May 1989, so ANSI compliance should
not be expected.

Given that fact, it's remarkable how conforming it actually is. Yes,
there are problem bits, but really they are few and far between, and
I believe that for the most part they comprise mere corner cases. The
most in-your-face violation seems to be the use of CLOCK_TCK rather
than CLOCKS_PER_SEC, which is hardly earth-shattering.
I used Turbo C for quite some time back in the day. Even after I
stopped using it as a compiler, I used the manual. It had good
descriptions of the library functions, along with whether they were
ANSI, "UNIX", or their own.


Brian
Jun 27 '08 #12
CBFalconer wrote:
>
What's rude about it?
How about the implication that the OP is too incompetent to install his
toolset?
Terse, but factual, IMO.
Terse, factual and rude. Richard is in peril of turning into Dan Pop.

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Jun 27 '08 #13
Op Thu, 12 Jun 2008 19:36:22 +0100 schreef Mark McIntyre:
Richard Bos wrote:
>Mark McIntyre <ma**********@TROUSERSspamcop.netwrote:
>>Richard Bos wrote:
but i cant get it compiled because i get undefined
external symbol when linking (putchar)
You are not linking against the library that contains putchar. Read your
toolset's documentation to find out how to correctly link.

The library that contains putchar is the Standard C library. If it isn't
linked against by default, something is wrong in his installation.

This doesn't follow, as you know well.
It is, I've just checked my installated TC 2.01 and putchar works.
Either the OP has a bad installed compiler or forgot to #include <stdio.h>
--
Coos

Jun 27 '08 #14
Coos Haak wrote:
Op Thu, 12 Jun 2008 19:36:22 +0100 schreef Mark McIntyre:
>Richard Bos wrote:
>>Mark McIntyre <ma**********@TROUSERSspamcop.netwrote:

Richard Bos wrote:
>but i cant get it compiled because i get undefined
>external symbol when linking (putchar)
You are not linking against the library that contains putchar. Read your
toolset's documentation to find out how to correctly link.
The library that contains putchar is the Standard C library. If it isn't
linked against by default, something is wrong in his installation.
This doesn't follow, as you know well.

It is, I've just checked my installated TC 2.01 and putchar works.
I know. That's not the point.
Either the OP has a bad installed compiler or forgot to #include <stdio.h>
Or indeed isn't using his linker right - so it is not necessarily
something wrong with his installation.

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Jun 27 '08 #15
Mark McIntyre wrote:
CBFalconer wrote:
>>
What's rude about it?

How about the implication that the OP is too incompetent to install his
toolset?
>Terse, but factual, IMO.

Terse, factual and rude. Richard is in peril of turning into Dan Pop.
But that's a compliment, surely.
Jun 27 '08 #16
In article <g2**********@renpen.nelsonbe.com>,
Bob Nelson <bn*****@nelsonbe.comwrote:
>Mark McIntyre wrote:
>CBFalconer wrote:
>>>
What's rude about it?

How about the implication that the OP is too incompetent to install his
toolset?
>>Terse, but factual, IMO.

Terse, factual and rude. Richard is in peril of turning into Dan Pop.

But that's a compliment, surely.

Somehow, I don't think it was intended as such.

BTW, I just love it when chicks^Wregulars fight.

Jun 27 '08 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Developwebsites | last post by:
I am taking advanced C++ at college and we use Borland Turbo C++ 4.5 compiler. How different is Turbo C++ from the standard C++? I know Borland used to call their versions of C++ and Pascal Turbo,...
7
by: git_cs | last post by:
Hey, guys and gals Somedays ago, I had asked for the DES algorithm in C language. Although I have written the algorthim in C myself, I am facing a peculiar problem, which I hope some of u guys and...
7
by: marek | last post by:
Dear colleagues, for one project, I need to link Turbo (or Borland) Pascal with (Turbo) C code. I would strongly like to avoid rewriting (my) C routines to Pascal (other part of project is under...
3
by: Eli Hughes | last post by:
Hello All: Does anyone know where I could find a copy of Borland Turbo C version 3.0? I have piece of embedded hardware (Lantronix Xport) that can be reprogrammed but I need an old x86 compiler...
16
by: scott | last post by:
I am looking for a copy of Turbo C 1.5 from 1987 for some historical research I'm doing into computing from that time period.
3
by: postrishi | last post by:
Hello Everybody , I am a new user. I am currently using Turbo C++ 3.0 editor in my engg.Can you tell me or post me a ebook on turbo c++ and NOT on c or C++.MInd it I want a book on TURBO C++ editor...
2
by: phillip | last post by:
Im havin' a problem in turbo c that i just cant solve.. Here's the prob.. i am going to create a timer... sample run... Enter hour:1 hr Enter minutes:1 minute Enter seconds: 50 seconds then...
0
by: shatol | last post by:
Hi All, I have a C# assembly registered with regasm.exe in the system. I need to use this assembly via COM interop in a console application written on Turbo C++ for Win32. I added the TLB file...
7
by: ashjas | last post by:
Hello, I have run this logic on turboc++ 3.0 and it is working fine on it but its not running on msvs2008 c++. i am not able to assign the value like this *temp=*main,where main and temp are char...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.