Thread: How to get ponter output without using printf in this C program

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    52

    How to get ponter output without using printf in this C program

    Code:
    int main()
    {
    
    
         unsigned lfsr  = 0xCD;
           int bit, i,j, *b;
           int arr[50];
    
    
            for ( i = 0; i < 50; i++)
                  {
                  bit = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 4) ) & 1;
                  arr[i] = bit;
                  b=arr;
                  lfsr = (lfsr >> 1) | (bit << 7);
                  }
            for (j = 0; j < 50; j++)
            {
            printf("%d", *b); /*How to write this statement without using printf statement {Without modification in final output of program*/
    
        b++;
            }
    return 0;
            }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you want to do this? What's wrong with using printf? (Though to print a pointer, you should use %p, and ideally cast the pointer argument to void* if it is not already void*. You appear to be printing an int argument though.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    52
    I need to execute above program in VIVADO HLS. HLS won`t accept the printf and scanf statements. But i need to return the result without using printf statement. How ?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So read the manual to find out what it will accept.

    Will this work?
    char buff[] = "hello world\n";
    write(1,buff,strlen(buff));


    Basically, use sprintf() to build whatever strings you need, then find out what low-level write function can emit those to the output stream.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2017
    Posts
    52
    Basically i need Buffer concpet only to execute this program. your help is need to execute the PRBS program.

    I don`t have knowledge on buff concept. I dont have basic c programming. I want to Store bits in some memory, then in hardware i need to read one by one. I need to return values. Please help me

    Code:
    int main() {
    
    
         unsigned lfsr  = 0xCD;
           int bit, i,j, *b;
           int arr[50];
    char buffer[50];
    
            for ( i = 0; i < 50; i++)
                  {
                  bit = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 4) ) & 1;
                  arr[i] = bit;
                  b=arr;
                  lfsr = (lfsr >> 1) | (bit << 7);
                  }
            for (j = 0; j < 50; j++)
            {
            buff[j] = buff[j++]; /*How to write this statement without using printf statement {Without modification in final output of program*/
    
       
            }
    return 0;
            }

    Quote Originally Posted by Salem View Post
    So read the manual to find out what it will accept.

    Will this work?
    char buff[] = "hello world\n";
    write(1,buff,strlen(buff));


    Basically, use sprintf() to build whatever strings you need, then find out what low-level write function can emit those to the output stream.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I want to Store bits in some memory, then in hardware i need to read one by one. I need to return values.
    So what OS are you running this code on?
    What hardware platform?

    Yes you can make your C code write to any random bit of memory (whether real or virtual), but it requires help from some external agent.

    Imagine that we know about as much about HLS as you know about C.

    You're going to have to help us with domain specific information about your environment as to the kinds of things you can do. It's not enough to issue blanket edicts like "can't use printf" and then expect us to do all your google for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2017
    Posts
    52
    Thanks for your response. My assignment includes : i need to execute C code (PRBS) in VIVADO HLS, and i will bundle into Single IP. That IP i will call and dup the PROGRAM file into FPGA hardware platform. The hardware should return random bits through Terminal. Long process it will go. But i am struggling with C code itself. Please sort out my problem. Finally, trying with buff concept.


    In this link : AR# 59228: 2013.4 Vivado HLS - Example showing how to use logic debug to test an AXI Lite Slave and AXI Master interface, and then verify it in SDK., (our workflow has been expained) what they did that i am going to follow with some modification. But i need just C code [buff concept] for PRBS for implementing hardware tool.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's hard to understand what you want when you say you can't use printf, and the very example you cite as being something you want actually uses printf.

    Quote Originally Posted by xilinx.com
    Code:
    #include <ap_cint.h>
    #include "head.h"
    int main(){
    	uint32 m[N];
    	int i;
    	for(i=0;i<N;i++){
    		m[i]=0;
    	}
    	foo_top (m,1,0);
    	for(i=0;i<N;i++){
    		if(m[i]!=i+1){
    			printf("FAIL\n");
    			return 1;
    		}
    	}
    	printf("SUCCESS");
    	return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Mar 2017
    Posts
    52
    Quote Originally Posted by Salem View Post
    It's hard to understand what you want when you say you can't use printf, and the very example you cite as being something you want actually uses printf.
    In hls, we will handle two processes such as simulation and synthesis.

    That we will the written C code whether is right or wrong through Test bench (optional) [simulation process]. In Test bench, printf() accepted to verify the result.

    C code is executed in synthesis process [compulsory] which important for Hardware implementation.

    Synthesis does not support :
    Systems calls: abort(), exit(), printf(), etc•
    Dynamic objects: malloc(), alloc (), free(), new, delete.

    Synthesis support :Memory functions: memcpy () and memset () supported.

    Least bother about Test bench. I need C code only for synthesis process.

    In synthesis, I need C code to return "result value" instead of result 0.

    In synthesis: The code should be for e.g.

    Sum = A+B;
    return sum;

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > (our workflow has been expained) what they did that i am going to follow with some modification.
    Have you managed to make it work with NO modification?

    Code:
    #include <ap_cint.h>
    #include "head.h"
    
    void foo_top (uint32 *m,int con,uint32 BASE_ADDR) {
    	uint32 buff[N];
    	int i;
    
    	if (con == 0x00000001)
    	{
    		memcpy(buff, m+(BASE_ADDR/4), N * sizeof(uint32));
    		buff[0] +=1;
    		for (i = 0; i < N-1; i++) {
    			buff[i+1]= buff[i]+ 1;
    		}
    		memcpy(m+(BASE_ADDR/4), buff, N * sizeof(uint32));
    	}
    }
    Have you tried replacing
    buff[i+1]= buff[i]+ 1;
    with
    buff[i+1]= rand();

    Is this your paid work?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Mar 2017
    Posts
    52
    It is a part of my project work . The above code is one of hardware verification code. Then how it works for Random bits. Surely, it wont work even if i replace buff[i+1]= buff[i]+ 1 with buff[i+1]= rand(). In the below code, Instead of printf statement, i given just return statement. Is it function properly? Please suggest me. ASAP

    Code:
    void PRBS(int n, int *values) {
    
            int i;
    
            srand((unsigned) time(NULL));
    
            for (i = 0; i < n; i++) {
    
                values[i] = rand() % 2;
    
                return values[i]       //printf("%d", values[i]);
    
            }
    
        }

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well it won't work if you put a return statement inside the for loop!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-03-2016, 06:23 AM
  2. Formatting output with printf?
    By Makara in forum C Programming
    Replies: 6
    Last Post: 01-17-2012, 12:41 PM
  3. ponter doubt abc = *(int*)test;
    By abhijit_mohanta in forum C++ Programming
    Replies: 7
    Last Post: 05-18-2010, 09:40 AM
  4. printf output
    By shyguy24x7 in forum C Programming
    Replies: 3
    Last Post: 01-31-2009, 02:01 PM
  5. Printf Output Confusion
    By simpleid in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2006, 11:36 AM

Tags for this Thread