
                       TUTORIAL 1.    Date: 16th Jan, Wednesday
                       ----------

1. Comments in C consist of any sequence of characters enclosed between /*
and */. The enclosed characters should not contain the sequesnce
*/. Examples of valid comments are /* this is a comments */, /**/ and
/*/*/. Example of non-comments are /*/  and /*/*/*/. 

Write a regular expression for C comments. You can assume that apart from /
and *, the input alphabet consists of the characters
a-z. 

2. Similarly strings are characters enclosed within " and ". A " enclosed
in a string is represented as "". Write a regular expression for strings.

For both the problems above, you must think of an argument to show why your
regular expressions are correct.

3. Study the following programs and their corresponding code
generated. For each do the following:


   a. Annotate fragments of target code with the source statemets that they
      correspond to.

   b. Annotate each local variable and parameter with its (relative address)

   c. Whereever possible, optimize the generated code.

 
PROGRAM A

/* C Code for Factorial using Recursion*/

int fact(int);

int main()
{
 int X=5;
 return(fact(X));
 }

int fact(int x)
{
 if(x==1) return 1;
 else return (x*fact(x-1));
 }



/**********Assebly code of fact**********/

	.file	"fact.c"
	.version	"01.01"
gcc2_compiled.:
.text
	.align 4
.globl main
	.type	 main,@function
main:
	pushl %ebp
	movl %esp,%ebp
	subl $4,%esp
	movl $5,-4(%ebp)
	movl -4(%ebp),%eax
	pushl %eax
	call fact
	addl $4,%esp
	movl %eax,%edx
	movl %edx,%eax
	jmp .L1
	.p2align 4,,7
.L1:
	leave
	ret
.Lfe1:
	.size	 main,.Lfe1-main
	.align 4
.globl fact
	.type	 fact,@function
fact:
	pushl %ebp
	movl %esp,%ebp
	cmpl $1,8(%ebp)
	jne .L3
	movl $1,%eax
	jmp .L2
	jmp .L4
	.p2align 4,,7
.L3:
	movl 8(%ebp),%eax
	decl %eax
	pushl %eax
	call fact
	addl $4,%esp
	movl %eax,%eax
	movl %eax,%edx
	imull 8(%ebp),%edx
	movl %edx,%eax
	jmp .L2
	.p2align 4,,7
.L4:
.L2:
	leave
	ret
.Lfe2:
	.size	 fact,.Lfe2-fact
	.ident	"GCC: (GNU) egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)"






PROGRAM B

/* C Code for a program with a while*/

int main()
{
  int a=1,b=1;
  while(a<=10){
     b=b*a;
     a++;
  }
  return b;
 }


/***********corresponding Asembly Code **********/ 
	
	.file	"try.c"
	.version	"01.01"
gcc2_compiled.:
.text
	.align 4
.globl main
	.type	 main,@function
main:
	pushl %ebp
	movl %esp,%ebp
	subl $8,%esp
	movl $1,-4(%ebp)
	movl $1,-8(%ebp)
	.p2align 4,,7
.L2:
	cmpl $10,-4(%ebp)
	jle .L4
	jmp .L3
	.p2align 4,,7
.L4:
	movl -8(%ebp),%eax
	imull -4(%ebp),%eax
	movl %eax,-8(%ebp)
	incl -4(%ebp)
	jmp .L2
	.p2align 4,,7
.L3:
	movl -8(%ebp),%edx
	movl %edx,%eax
	jmp .L1
	.p2align 4,,7
.L1:
	leave
	ret
.Lfe1:
	.size	 main,.Lfe1-main
	.ident	"GCC: (GNU) egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)"





PROGRAM C


/* C Program using Structure*/

struct data{
	int sum;
	int b[5];
	};
	
int main()
{
  struct data rec1;
  rec1.sum=0;
   rec1.b[0]=2;
   rec1.sum=rec1.sum+rec1.b[0];
   return rec1.sum;
}



/* Corresponding Assebly Code*/

	.file	"rec.c"
	.version	"01.01"
gcc2_compiled.:
.text
	.align 4
.globl main
	.type	 main,@function
main:
	pushl %ebp
	movl %esp,%ebp
	subl $24,%esp
	movl $0,-24(%ebp)
	movl $2,-20(%ebp)
	movl -20(%ebp),%eax
	addl %eax,-24(%ebp)
	movl -24(%ebp),%edx
	movl %edx,%eax
	jmp .L1
	.p2align 4,,7
.L1:
	leave
	ret
.Lfe1:
	.size	 main,.Lfe1-main
	.ident	"GCC: (GNU) egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)"
