I am constructing the control flow of the program. I am reading the bb.ls file and in that file I will search for ‘b’(branch instruction), if ‘b’ is found I create a newnode and store the node count and store the address of the next node( linked list).
My doubt is how will I know the next node address with out execute the next node.
PLZ check the output i got (in the attachments) and and the bb.ls file is below the program.
PLZ help me out with my problem.
thanks..

#include <stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include"opcodes.h"

typedef struct nodeTemplate{
		char	branchLabel[10];
		struct nodeTemplate *pFollow;
		int currentBlk;		
}node;
node *head;

node* createNode()
{
	node *newnode;
	newnode = (node*)malloc( sizeof(node));
	newnode->pFollow = NULL;
	newnode->pLabel = NULL;
	return newnode;
}

node* prevPointer;
node* temp;
node* head= NULL;

int main()
{
	formNode();	   // form only all the follow node
	getch();
}

int formNode()
{
	FILE * pFile;
	char readLine [1024];
	char inst,labelFound[50];
	int labelCnt=0;
	int add=1;
	node *newnode;
	
	pFile = fopen("bb.ls","r+");	
	while(fgets (readLine , 1024 , pFile) != NULL)
	{
		sscanf(readLine+26,"%c",&inst);
		sscanf(readLine+30,"%s",labelFound);
	
		if(inst=='b')
		{
			/* create new node */
/*we create a new node if we find a branch(‘b’) and store the label after branch instruction(L32,L4,..)*/
			newnode = createNode();
			newnode->currentBlk = add;
			strcpy(newnode->branchLabel,labelFound);
			newnode->pFollow=head; // store address of the pointer head
			head = newnode;  // transfer address of 'newnode' to 'head'
			printf("newnode->currentBlk %d\n",newnode->currentBlk);
			printf("newnode->branchLabel %s\n",newnode->branchLabel);
			printf("newnode->pFollow %p\n",newnode->pFollow);		
			printf("head  %p\n\n\n",head);			
			add++;
		}
		else if(inst=='r')
		{
			/* create new node */
			newnode = createNode();
			newnode->currentBlk = add;			
			newnode->pFollow=NULL; // store NULL as it is the last node
			head = newnode;  // transfer address of 'newnode' to 'head'
			printf("newnode->currentBlk %d\n",newnode->currentBlk);				printf("newnode->pFollow %p\n",newnode->pFollow);		
			printf("head  %p\n\n\n",head);			
			add++;
		}
	}
	fclose(pFile);
	return 1;
}
1                     ; C Compiler for M68HC08 (COSMIC Software)
   2                     ; Generator V4.5a.1 - 19 Jul 2004
   3                     ; Optimizer V4.4b - 28 Jun 2004
  39                     ; 1 void main(void)
  39                     ; 2 {
  40                     	switch	.text
  41  0000               _main:
  43  0000 a7fe          	ais	#-2
  44       00000002      OFST:	set	2
  47                     ; 4     if(X<4)
  49  0002 95            	tsx	
  50  0003 e601          	lda	OFST-1,x
  51  0005 a004          	sub	#4
  52  0007 f6            	lda	OFST-2,x
  53  0008 a200          	sbc	#0
  54  000a 9007          	bge	L32
  55                     ; 5       X=1;
  57  000c a601          	lda	#1
  58  000e e701          	sta	OFST-1,x
  59  0010 7f            	clr	OFST-2,x
  61  0011 2005          	bra	L4
  62  0013               L32:
  63                     ; 7       X++;
  65  0013 6c01          	inc	OFST-1,x
  66  0015 2601          	bne	L4
  67  0017 7c            	inc	OFST-2,x
  68  0018               L4:
  69                     ; 8 }
  72  0018 a702          	ais	#2
  73  001a 81            	rts	
  85                     	xdef	_main
  86                     	end

Thanks….

1. You may get 26-th character of readLine as readLine[26] - why sscanf(readLine+26...) ?
2. Check input line length: may be no 26-th character in the line...
3. Why 'b' in +26 position corresponds to branch instruction? May be it's 'b' in the comment line...
4. Why +30 position corresponds to "labelFound" condition?
5. Global variable head defined twice. You can't compile this code w/o errors ;)
6. You don't understand linked list building technique.
That's addNode pseudocode:

Node* head = NULL;
Node* tail;

Node* pnew = malloc(...);
pnew->next = NULL;
if (head == NULL) {
    head = pnew;
} else {
    tail->next = pnew;
}
tail = pnew;

I'm afraid there are more defects in your code. Well, correct ones mentioned above then come back...

1. You may get 26-th character of readLine as readLine[26] - why sscanf(readLine+26...) ?
2. Check input line length: may be no 26-th character in the line...
3. Why 'b' in +26 position corresponds to branch instruction? May be it's 'b' in the comment line...
4. Why +30 position corresponds to "labelFound" condition?
5. Global variable head defined twice. You can't compile this code w/o errors ;)

hi ArkM,
thanks for readLine[26] its working,
i am checking for 26th character because branch statemtent ends the block(node) and next instruction after the branch instruction is the starting of the new block.
every 30th position we got label, i have to store that label(L32, L4..) and search for its target branch label[L32:, L4:...)

PLZ check the code i have modified, still i am not able to understand how we can store the next node address in the current node. i have searched for many linked list examples, but failed to understand them..

#include <stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include"opcodes.h"
static nodeNumber = 1;
typedef struct nodeTemplate{
		char	branchLabel[10];
		struct nodeTemplate *pFollow;
		int currentBlkNum;		
}node;
node *head = NULL;
node* tail;

int main()
{
	formNode();	   // form only all the follow node
	getch();
}

int formNode()
{
	FILE * pFile;
	char readLine [1024];
	char inst,labelFound[50];
	int labelCnt=0;
	
	node *newnode;
	
	pFile = fopen("bb.ls","r+");	
	while(fgets (readLine , 1024 , pFile) != NULL)
	{	
		if(readLine[26]=='b')
		{
			/* create new node */									
			newnode = (node*)malloc( sizeof(node));			
			newnode->pFollow = NULL;									
			if(head == NULL) {
				head = newnode;				
				tail = newnode;	
			}
			else {
				tail ->pFollow = newnode;
				tail = newnode;
			}				
		}
	}
	fclose(pFile);
	return 1;
}

>still i am not able to understand how we can store the next node address in the current node.
Please, read my previous post again. And again. And again...
Nobody can store the next node address in the current node until this the next node will been created! That's why we set the next node asddress to NULL in the current (just created) node.

Linked list structure (3 nodes, for example):
head -> node1
        next  -> node2
                 next  -> node3 (the last)    <- tail
                          NULL  (end of list)

Now take a sheet of paper and a pencil and try to add the next (4th) node to the list (see C-like pseudocode in my post above).

You will never write this program until understand linked list structure.

>i have searched for many linked list examples, but failed to understand them.
No comments... Is the patient's case hopeless?
This linked list is the simplest part of your assignment!

hi ArkM,
thanks for ur help...
i think now i am able to construct the control flow of the program using linked list..Can you PLZ check my program?

#include <stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include"opcodes.h"

typedef struct nodeTemplate{
		char	branchLabel[10];
		struct nodeTemplate *pFollow;
		int currentBlkNum;		
}node;
	node *newnode;
	node *head = NULL;
	node* prevPointer;

node* createNode()
{
	static nodeNumber = 1;
	node *newnode;
	newnode = (node*)malloc( sizeof(node));		
	newnode->pFollow = NULL;
	newnode->currentBlkNum = nodeNumber++;
	printf("currentBlkNum %d\n",newnode->currentBlkNum);
	printf("newnode address(malloc) %p\n",newnode);
	return newnode;
}

int main()
{
	FILE * pFile;
	char readLine [1024];
	char inst,labelFound[50];
	int labelCnt=0;
	

	pFile = fopen("bb.ls","r+");	
	while(fgets (readLine , 1024 , pFile) != NULL)
	{	
		if(readLine[26]=='b')
		{
			/* create new node */									
			newnode = createNode();									
			if(head == NULL)
			{
				head = newnode;				
				prevPointer = newnode;	
				newnode->pFollow = prevPointer;		
			}
			else 
			{
				newnode->pFollow = prevPointer;				
				prevPointer ->pFollow = newnode;
				prevPointer = newnode;
			}
		}
		else if(readLine[26]=='r') //end of nodes
		{
			newnode = createNode();
			newnode->pFollow = prevPointer;
			prevPointer ->pFollow = NULL;
		}
	}
	fclose(pFile);
	getch();
}

Better try to check it with your compiler.
Create test scenario(s): what to do - desired results. Write test driver, compile then run it. Compare results - and so on...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.