public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: microblaze getchar() not functioning properly
@ 2011-07-29  6:58 Jeremy Hall
  2011-08-24  5:01 ` naga raj
  0 siblings, 1 reply; 15+ messages in thread
From: Jeremy Hall @ 2011-07-29  6:58 UTC (permalink / raw)
  To: gcc-help

You might want to add

   buf[j] = '\0';

after the loop.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: microblaze getchar() not functioning properly
  2011-07-29  6:58 microblaze getchar() not functioning properly Jeremy Hall
@ 2011-08-24  5:01 ` naga raj
  2011-08-24 10:12   ` naga raj
  0 siblings, 1 reply; 15+ messages in thread
From: naga raj @ 2011-08-24  5:01 UTC (permalink / raw)
  To: Jeremy Hall, Michael Eager, jkenton, Andrew.Bennett; +Cc: gcc-help

Hi,

Sorry for the delay in my reply.
getchar() function is now working properly.
 There was some issue with microblaze read function in libgloss
directory. read function was returning one extra byte due to which it
was failing. I have adjusted the read function return value, then
getchar was working properly.

Thank you for your replies.

Thanks,
Nagaraju

On Fri, Jul 29, 2011 at 12:28 PM, Jeremy Hall <gcc.hall@gmail.com> wrote:
> You might want to add
>
>   buf[j] = '\0';
>
> after the loop.
>

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: microblaze getchar() not functioning properly
  2011-08-24  5:01 ` naga raj
@ 2011-08-24 10:12   ` naga raj
  2011-08-24 10:51     ` Andrew Bennett
  2011-08-24 15:23     ` Michael Eager
  0 siblings, 2 replies; 15+ messages in thread
From: naga raj @ 2011-08-24 10:12 UTC (permalink / raw)
  To: Michael Eager; +Cc: gcc-help

Hi Mike,

 I found that in libgloss/microblaze/read.c file we are returning (i+
1) value as below.

int read_micro (int fd, char* buf, int nbytes)
{
  int i = 0;

  for (i = 0; i < nbytes; i++) {
    *(buf + i) = inbyte();
    if ((*(buf + i) == '\n' || *(buf + i) == '\r'))
        break;
  }

  return i + 1;
}
getchar is failing due to this extra 1 byte(i + 1) that we are
returning from this function.


I have made changes to the same function as below..

int read_micro (int fd, char* buf, int nbytes)
{
  int i = 0;

  for (i = 0; i < nbytes; i++) {
    *(buf + i) = inbyte();
    if ((*(buf + i) == '\n' || *(buf + i) == '\r'))
         i++;
        break;
  }

  return i;
}

Can you please let me know whether the above change were correct..

Thanks in advance,
Nagaraju


On Wed, Aug 24, 2011 at 10:31 AM, naga raj <gnuuser.raj@gmail.com> wrote:
> Hi,
>
> Sorry for the delay in my reply.
> getchar() function is now working properly.
>  There was some issue with microblaze read function in libgloss
> directory. read function was returning one extra byte due to which it
> was failing. I have adjusted the read function return value, then
> getchar was working properly.
>
> Thank you for your replies.
>
> Thanks,
> Nagaraju
>
> On Fri, Jul 29, 2011 at 12:28 PM, Jeremy Hall <gcc.hall@gmail.com> wrote:
>> You might want to add
>>
>>   buf[j] = '\0';
>>
>> after the loop.
>>
>

^ permalink raw reply	[flat|nested] 15+ messages in thread

* RE: microblaze getchar() not functioning properly
  2011-08-24 10:12   ` naga raj
@ 2011-08-24 10:51     ` Andrew Bennett
  2011-08-24 11:05       ` naga raj
  2011-08-24 15:37       ` Michael Eager
  2011-08-24 15:23     ` Michael Eager
  1 sibling, 2 replies; 15+ messages in thread
From: Andrew Bennett @ 2011-08-24 10:51 UTC (permalink / raw)
  To: naga raj; +Cc: gcc-help

> Can you please let me know whether the above change were correct..

I am assuming here that the code you pasted is correct.

If so, the read_macro function will only read and return one byte due to
the if statement being incorrectly bracketed.  If the function is called
with a buffer of more than 1 byte (ie. nbytes > 1) the buffer will only
contain 1 character rather than the number of characters given in
nbytes.  

I think the code should read:

int read_micro (int fd, char* buf, int nbytes)
{
  int i = 0;

  for (i = 0; i < nbytes; i++) {
    *(buf + i) = inbyte();
    if ((*(buf + i) == '\n' || *(buf + i) == '\r'))
    {
      i++;
      break;
    }
  }

  return i;
}

Regards,


Andrew

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: microblaze getchar() not functioning properly
  2011-08-24 10:51     ` Andrew Bennett
@ 2011-08-24 11:05       ` naga raj
  2011-08-24 15:37       ` Michael Eager
  1 sibling, 0 replies; 15+ messages in thread
From: naga raj @ 2011-08-24 11:05 UTC (permalink / raw)
  To: Andrew Bennett; +Cc: gcc-help

Hi Andrew,

   I have missed placing the braces, thank you for reviewing and
correcting the code.

Thanks,
Nagaraju

On Wed, Aug 24, 2011 at 4:21 PM, Andrew Bennett
<Andrew.Bennett@imgtec.com> wrote:
>> Can you please let me know whether the above change were correct..
>
> I am assuming here that the code you pasted is correct.
>
> If so, the read_macro function will only read and return one byte due to
> the if statement being incorrectly bracketed.  If the function is called
> with a buffer of more than 1 byte (ie. nbytes > 1) the buffer will only
> contain 1 character rather than the number of characters given in
> nbytes.
>
> I think the code should read:
>
> int read_micro (int fd, char* buf, int nbytes)
> {
>  int i = 0;
>
>  for (i = 0; i < nbytes; i++) {
>    *(buf + i) = inbyte();
>    if ((*(buf + i) == '\n' || *(buf + i) == '\r'))
>    {
>      i++;
>      break;
>    }
>  }
>
>  return i;
> }
>
> Regards,
>
>
> Andrew
>
>

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: microblaze getchar() not functioning properly
  2011-08-24 10:12   ` naga raj
  2011-08-24 10:51     ` Andrew Bennett
@ 2011-08-24 15:23     ` Michael Eager
  1 sibling, 0 replies; 15+ messages in thread
From: Michael Eager @ 2011-08-24 15:23 UTC (permalink / raw)
  To: naga raj; +Cc: gcc-help

On 08/24/2011 03:11 AM, naga raj wrote:
> Hi Mike,
>
>   I found that in libgloss/microblaze/read.c file we are returning (i+
> 1) value as below.

Libgloss is not part of GCC, it is part of the Newlib project.
The mailing list for Newlib is newlib@sourceware.org.  You will need to
subscribe to the mailing list at http://sourceware.org/newlib/.
Discussions about problems in Newlib should be directed to that mailing list.

I notice that there is no libgloss/microblaze/read.c in Newlib.  If you
have added files to your copy of Newlib, I encourage you to submit them
to the public repository.

-- 
Michael Eager	 eager@eagercon.com
1960 Park Blvd., Palo Alto, CA 94306  650-325-8077

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: microblaze getchar() not functioning properly
  2011-08-24 10:51     ` Andrew Bennett
  2011-08-24 11:05       ` naga raj
@ 2011-08-24 15:37       ` Michael Eager
  1 sibling, 0 replies; 15+ messages in thread
From: Michael Eager @ 2011-08-24 15:37 UTC (permalink / raw)
  To: Andrew Bennett; +Cc: naga raj, gcc-help

On 08/24/2011 03:51 AM, Andrew Bennett wrote:
>> Can you please let me know whether the above change were correct..
>
> I am assuming here that the code you pasted is correct.
>
> If so, the read_macro function will only read and return one byte due to
> the if statement being incorrectly bracketed.  If the function is called
> with a buffer of more than 1 byte (ie. nbytes>  1) the buffer will only
> contain 1 character rather than the number of characters given in
> nbytes.
>
> I think the code should read:
>
> int read_micro (int fd, char* buf, int nbytes)
> {
>    int i = 0;
>
>    for (i = 0; i<  nbytes; i++) {
>      *(buf + i) = inbyte();
>      if ((*(buf + i) == '\n' || *(buf + i) == '\r'))
>      {
>        i++;
>        break;
>      }
>    }
>
>    return i;
> }

This corrected version is identical to read() in libgloss/read.c in Newlib.

-- 
Michael Eager	 eager@eagercon.com
1960 Park Blvd., Palo Alto, CA 94306  650-325-8077

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: microblaze getchar() not functioning properly
  2011-07-28 11:31       ` naga raj
  2011-07-28 12:15         ` Jeff Kenton
  2011-07-28 12:31         ` Andrew Bennett
@ 2011-07-28 15:18         ` Michael Eager
  2 siblings, 0 replies; 15+ messages in thread
From: Michael Eager @ 2011-07-28 15:18 UTC (permalink / raw)
  To: naga raj; +Cc: gcc-help

On 07/28/2011 04:30 AM, naga raj wrote:

> I have observed following output:
>
>   main
>   input value of i is:a           ---------->my input ( I have not
> pressed any key after entering 'a')
>   value of j is:0
>   input value of i is:             ------------->  It is automatically
> skipping to take my input
>   value of j is:1

You get better info if you print values in hex, rather than character.
How would you know that the extra character is a null and not a CR or LF?

> I have given the input to the program using HyperTerminal. I have
> entered only abc as my input.

>> Why do you think that this is a problem in getchar()?
>
> With the above observation I am thinking that getchar() function is
> appending a null character after each character.

I think that it is extremely unlikely that buffered input, which
getchar() uses, is inserting extra characters into the input stream.

You need to be careful using a terminal emulator (whether Hyperterminal,
minicom, or any other).  There are lots of problems when buffered input
and output are interspersed.  Check your configuration.

You can use GDB, as Jeff suggests, to step through getc() (which getchar()
calls) and see what actually is received from the terminal emulator.


-- 
Michael Eager	 eager@eagercon.com
1960 Park Blvd., Palo Alto, CA 94306  650-325-8077

^ permalink raw reply	[flat|nested] 15+ messages in thread

* RE: microblaze getchar() not functioning properly
  2011-07-28 11:31       ` naga raj
  2011-07-28 12:15         ` Jeff Kenton
@ 2011-07-28 12:31         ` Andrew Bennett
  2011-07-28 15:18         ` Michael Eager
  2 siblings, 0 replies; 15+ messages in thread
From: Andrew Bennett @ 2011-07-28 12:31 UTC (permalink / raw)
  To: naga raj; +Cc: gcc-help

> -----Original Message-----
> From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org]
On Behalf
> Of naga raj
>
> I have given the input to the program using HyperTerminal. I have
> entered only abc as my input.

Your program works fine for me under Linux running in an xterm.  I doubt
that getchar is at fault here, I suspect that it is to do with
HyperTerminal (and therefore probably not relevant to this mailing list
anymore).  I would suggest running it in a different terminal ie.
Windows command prompt and see what happens.

Regards,


Andrew

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: microblaze getchar() not functioning properly
  2011-07-28 11:31       ` naga raj
@ 2011-07-28 12:15         ` Jeff Kenton
  2011-07-28 12:31         ` Andrew Bennett
  2011-07-28 15:18         ` Michael Eager
  2 siblings, 0 replies; 15+ messages in thread
From: Jeff Kenton @ 2011-07-28 12:15 UTC (permalink / raw)
  To: naga raj; +Cc: Michael Eager, gcc-help

On 07/28/2011 07:30 AM, naga raj wrote:
> Hi Mike,
>
>> Post the code which actually failed, after you have verified the failure.
> Following is the code which has some problem with getchar() function.
>
> #include<stdio.h>
>   volatile int i,j=0;
>   int main()
> {
>     char buf[10];
>     xil_printf("main\n");
>       while(j<6)
>     {
>         i=getchar();
>         xil_printf("input value of i is:%c\n",i);
>         buf[j]=i;
>         xil_printf("value of j is:%d\n",j);
>        j++;
>     }
>         xil_printf("buffer contains:%s\n",buf);
>         return 0;
> }
>
> I have given input as abc
> .
> I have observed following output:
>
>   main
>   input value of i is:a           ---------->my input ( I have not
> pressed any key after entering 'a')
>   value of j is:0
>   input value of i is:             ------------->  It is automatically
> skipping to take my input
>   value of j is:1
>
>
>   input value of i is:b           ---------->my input ( I have not
> pressed any key after entering 'b')
>   value of j is:2
>   input value of i is:              ------------->  It is automatically
> skipping to take my input
>   value of j is:3
>
>
>   input value of i is:c           ---------->my input ( I have not
> pressed any key after entering 'c')
>   value of j is:4
>   input value of i is:             ------------->  It is automatically
> skipping to take my input
>   value of j is:5
>
>    buffer contains: a
>
>
> If we observe it is waiting for user input when the value of variable
> j is 0, 2 and 4.
>
> I have given the input to the program using HyperTerminal. I have
> entered only abc as my input.
>
>> Look at the assembly generated by gcc.  Where is j incremented?
> I have observed the code generated for j++ it is correct.
> Please look into the below objdump code.
>   j++;
> 880002c8:	b0008800 	imm	-30720
> 880002cc:	e8603d88 	lwi	r3, r0, 15752
> 880002d0:	30630001 	addik	r3, r3, 1
> 880002d4:	b0008800 	imm	-30720
> 880002d8:	f8603d88 	swi	r3, r0, 15752
>
>> Why do you think that this is a problem in getchar()?
> With the above observation I am thinking that getchar() function is
> appending a null character after each character.
>
> Can you please give me some inputs so that I can proceed further.
>
> Thanks in advance,
> Nagaraju

Attach gdb to your process and put a breakpoint in the loop.  printf() 
is not the best way to debug problems.

--jeff

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: microblaze getchar() not functioning properly
  2011-07-27 15:03     ` Michael Eager
@ 2011-07-28 11:31       ` naga raj
  2011-07-28 12:15         ` Jeff Kenton
                           ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: naga raj @ 2011-07-28 11:31 UTC (permalink / raw)
  To: Michael Eager; +Cc: gcc-help

Hi Mike,

> Post the code which actually failed, after you have verified the failure.

Following is the code which has some problem with getchar() function.

#include<stdio.h>
 volatile int i,j=0;
 int main()
{
   char buf[10];
   xil_printf("main\n");
     while(j<6)
   {
       i=getchar();
       xil_printf("input value of i is:%c\n",i);
       buf[j]=i;
       xil_printf("value of j is:%d\n",j);
      j++;
   }
       xil_printf("buffer contains:%s\n",buf);
       return 0;
}

I have given input as abc
.
I have observed following output:

 main
 input value of i is:a           ---------->my input ( I have not
pressed any key after entering 'a')
 value of j is:0
 input value of i is:             -------------> It is automatically
skipping to take my input
 value of j is:1


 input value of i is:b           ---------->my input ( I have not
pressed any key after entering 'b')
 value of j is:2
 input value of i is:              -------------> It is automatically
skipping to take my input
 value of j is:3


 input value of i is:c           ---------->my input ( I have not
pressed any key after entering 'c')
 value of j is:4
 input value of i is:             -------------> It is automatically
skipping to take my input
 value of j is:5

  buffer contains: a


If we observe it is waiting for user input when the value of variable
j is 0, 2 and 4.

I have given the input to the program using HyperTerminal. I have
entered only abc as my input.

> Look at the assembly generated by gcc.  Where is j incremented?

I have observed the code generated for j++ it is correct.
Please look into the below objdump code.
 j++;
880002c8:	b0008800 	imm	-30720
880002cc:	e8603d88 	lwi	r3, r0, 15752
880002d0:	30630001 	addik	r3, r3, 1
880002d4:	b0008800 	imm	-30720
880002d8:	f8603d88 	swi	r3, r0, 15752

> Why do you think that this is a problem in getchar()?

With the above observation I am thinking that getchar() function is
appending a null character after each character.

Can you please give me some inputs so that I can proceed further.

Thanks in advance,
Nagaraju

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: microblaze getchar() not functioning properly
  2011-07-27 11:57   ` naga raj
@ 2011-07-27 15:03     ` Michael Eager
  2011-07-28 11:31       ` naga raj
  0 siblings, 1 reply; 15+ messages in thread
From: Michael Eager @ 2011-07-27 15:03 UTC (permalink / raw)
  To: naga raj; +Cc: gcc-help

On 07/27/2011 04:57 AM, naga raj wrote:
> Hi Andrew,
>
>   Thanks for you reply. I am sorry I forgot to add j++ while posting.

Before posting code to gcc-help, please make sure that it is the
code that shows the problem.  It wastes everyone's time to look
at code which you didn't test and which doesn't show the problem
you want help with.

 > I tried to print variable 'j' after getchar() function in code. Then
 > for each character I enter 'j' value is incremented by two.

Post the code which actually failed, after you have verified the failure.

Look at the assembly generated by gcc.  Where is j incremented?

Why do you think that this is a problem in getchar()?

Does the test case fail (i.e., increment j twice) without getchar()?


-- 
Michael Eager	 eager@eagercon.com
1960 Park Blvd., Palo Alto, CA 94306  650-325-8077

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: microblaze getchar() not functioning properly
  2011-07-27 11:53 ` Andrew Bennett
@ 2011-07-27 11:57   ` naga raj
  2011-07-27 15:03     ` Michael Eager
  0 siblings, 1 reply; 15+ messages in thread
From: naga raj @ 2011-07-27 11:57 UTC (permalink / raw)
  To: Andrew Bennett; +Cc: gcc-help

Hi Andrew,

 Thanks for you reply. I am sorry I forgot to add j++ while posting.

 It was included in my code.

Thanks,
Nagaraju

On Wed, Jul 27, 2011 at 5:22 PM, Andrew Bennett
<Andrew.Bennett@imgtec.com> wrote:
>
>> For the above program if i give input as:
>> stefen
>>
>> Then it is printing on 's'
>>
>> I tried to print variable 'j' after getchar() function in code. Then
>> for each character I enter 'j' value is incremented by two.
>
> The problem is due to the fact that the j variable is not incremented in
> the
> while loop.  Adding j++; in the while loop after the buf assignment will
> solve your problem, ie:
>
> #include<stdio.h>
> int main()
> {
>  int i,j=0;
>  char buf[10];
>  xil_printf("main\n");
>  while(j<6)
>  {
>    i=getchar();
>    buf[j]=i;
>    j++;
>  }
>  xil_printf("%s\n",buf);
>  return 0;
> }
>
> Regards,
>
>
> Andrew
>
>

^ permalink raw reply	[flat|nested] 15+ messages in thread

* RE: microblaze getchar() not functioning properly
  2011-07-27 10:36 naga raj
@ 2011-07-27 11:53 ` Andrew Bennett
  2011-07-27 11:57   ` naga raj
  0 siblings, 1 reply; 15+ messages in thread
From: Andrew Bennett @ 2011-07-27 11:53 UTC (permalink / raw)
  To: gcc-help


> For the above program if i give input as:
> stefen
> 
> Then it is printing on 's'
> 
> I tried to print variable 'j' after getchar() function in code. Then
> for each character I enter 'j' value is incremented by two.

The problem is due to the fact that the j variable is not incremented in
the 
while loop.  Adding j++; in the while loop after the buf assignment will
solve your problem, ie:

#include<stdio.h>
int main()
{
  int i,j=0;
  char buf[10];
  xil_printf("main\n");
  while(j<6)
  {
    i=getchar();
    buf[j]=i;
    j++;
  }
  xil_printf("%s\n",buf);
  return 0;
}

Regards,


Andrew

^ permalink raw reply	[flat|nested] 15+ messages in thread

* microblaze getchar() not functioning properly
@ 2011-07-27 10:36 naga raj
  2011-07-27 11:53 ` Andrew Bennett
  0 siblings, 1 reply; 15+ messages in thread
From: naga raj @ 2011-07-27 10:36 UTC (permalink / raw)
  To: Michael Eager, gcc-help

Hi,

  getchar function is not working properly. An extra null character is
appended for each and every input character.

  Ex:
 #include<stdio.h>
int main()
{
    int i,j=0;
    char buf[10];
    xil_printf("main\n");
      while(j<6)
    {
    	i=getchar();
    	buf[j]=i;
    }
        xil_printf("%s\n",buf);
        return 0;
}


For the above program if i give input as:
stefen

Then it is printing on 's'

I tried to print variable 'j' after getchar() function in code. Then
for each character I enter 'j' value is incremented by two.

Thanks,
Nagaraju

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2011-08-24 15:37 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-29  6:58 microblaze getchar() not functioning properly Jeremy Hall
2011-08-24  5:01 ` naga raj
2011-08-24 10:12   ` naga raj
2011-08-24 10:51     ` Andrew Bennett
2011-08-24 11:05       ` naga raj
2011-08-24 15:37       ` Michael Eager
2011-08-24 15:23     ` Michael Eager
  -- strict thread matches above, loose matches on Subject: below --
2011-07-27 10:36 naga raj
2011-07-27 11:53 ` Andrew Bennett
2011-07-27 11:57   ` naga raj
2011-07-27 15:03     ` Michael Eager
2011-07-28 11:31       ` naga raj
2011-07-28 12:15         ` Jeff Kenton
2011-07-28 12:31         ` Andrew Bennett
2011-07-28 15:18         ` Michael Eager

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).