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….