CET 3510 Lab 4

Objective: The objective of this experiment was to become familiarized with c programing language in order to understand how it is broken down into assembly language. In assembly language, any high level programing language such as c language is broken down into simpler terms for the processor to understand. Assembly is extremely close to machine language. With assembly you are able to observe how the information is transported from each of the registers in order to preform an action. In c language, it was possible to create functions in order to preform certain actions. These functions also included loops such as a while statement. However with assembly language, loops and functions do not exist. Instead, labels and jumps replace these. When the c program was converted to assembly, it was possible to see how all of the functions created were converted into labels. Each loop was also broken down into assembly processes which involved moving all variables in specific registers in order to complete the process. Jumps were used in these processes to provide controll to other parts of the prgrams once the loops satisified toe conditions needed to exit or execute the loop. Any text in the program was also converted into strings. Furthermore, by completing the experiment, it was possible to see how HLA was used to provide further insight on assembly language.

Code: Below is the c language code used to generate the assembly language code.

  #include

int roman(){
//Integer for do while loop
	
  int myX;
  
  printf( "Roman. Enter value for conversion: " );
 scanf("%d", &myX);
  //printf("x is %d", myX);
//Conversion for decimal to roman 

  while(myX > 0)
  {
    if(myX>=100)
    {
      myX-=100;
      printf("C");
    } 
    else if(myX>=99){
		myX-=99;
		printf("IC");
    }
    else if(myX>=90){
         myX-=90;
         printf("XC");
    }
    else if(myX>=50){
	     myX-=50;
	     printf("L");
    }
      else if(myX>=49){
	       myX-=49;
	       printf("IL");
    }
      else if(myX>=40){
	       myX-=40;
	       printf("XL");
    }
      else if(myX>=10){
	       myX-=10;
	       printf("X");
    }
      else if(myX>=9){
	       myX-=9;
	       printf("IX");
    }
      else if(myX>=5){
	       myX-=5;
	       printf("V");
    }
      else if(myX>=1){
	       myX-=1;
	       printf("I");
    }
  }
}

  int exponent (){
	 
	int i, j; 
	int sum = 0;
	int exp;
	int base;
	int q;
	 printf("Enter exponent");
 scanf("%d", &exp);
 printf("Enter base");
  scanf("%d", &base);
		q = 1;
		for (i=0; i< exp; i++)
		{
			sum=0;
			for (j=0; j<base; j++)
			{
				sum=sum+q;
				printf("sum = %d", sum);
			
			}
			q = sum;
			
			
	    }
}
int subtract(){
	int s;
	int m;
	int v;
	
	printf("Enter a value for m");
		scanf("%d", &m);
		
	printf("Enter a value for v");
		scanf("%d", &v);
		
	s=m-v;
	 printf("%d", &s);
 }
 int addition(){
	int r;
	int d;
	int f;
	
	printf("Enter a value for d");
		scanf("%d", &d);
		
	printf("Enter a value for f");
		scanf("%d", &f);
		
	r=d+f;
		printf("%d", &r);
	}
int multiply(){
	int j;
	int z;
	int y;
	int mul=0;

	printf("Enter a value for z");
		scanf("%d", &z);
		
	printf("Enter a value for y");
		scanf("%d", &y);
	do {
		mul+=z; 
		j++;
		
		} while (j<y);
		
    printf("%d", &mul);
    
}




main ()
{
	int option1;
	printf("%s", "Enter 1 for roman 2 for multiply, 3 for exponent, 4 for addition,and 5 for subtraction:");
	scanf("%d", &option1);
	if (option1==1)
	{
		roman();
    }   
    
	else if (option1==2)
	{
		multiply();
	}
	else if (option1==3)
	{
		exponent();
	}
	else if (option1==4)

	{
	addition();
}
else if (option1==5)
{subtract ();
}
else if (option1==6)
{
		printf("Invalid option. Select a choice from 1 to 5");
	}
	while(option1!=7);
	roman ();
	exponent ();
	multiply ();
	addition();
	subtract();
}

}

Here is the code once it is compiled into assembly language.

 

.file “assemblyredo.c”

 

.section .rodata

 

.align 4

 

.LC0:

 

.string “Roman. Enter value for conversion: ”

 

.LC1:

 

.string “%d”

 

.LC2:

 

.string “IC”

 

.LC3:

 

.string “XC”

 

.LC4:

 

.string “IL”

 

.LC5:

 

.string “XL”

 

.LC6:

 

.string “IX”

 

.text

 

.globl roman

 

.type roman, @function

 

roman:

 

.LFB0:

 

.cfi_startproc

 

pushl %ebp

 

.cfi_def_cfa_offset 8

 

.cfi_offset 5, -8

 

movl %esp, %ebp

 

.cfi_def_cfa_register 5

 

subl $40, %esp

 

movl $.LC0, %eax

 

movl %eax, (%esp)

 

call printf

 

movl $.LC1, %eax

 

leal -12(%ebp), %edx

 

movl %edx, 4(%esp)

 

movl %eax, (%esp)

 

call __isoc99_scanf

 

jmp .L2

 

.L12:

 

movl -12(%ebp), %eax

 

cmpl $99, %eax

 

jle .L3

 

movl -12(%ebp), %eax

 

subl $100, %eax

 

movl %eax, -12(%ebp)

 

movl $67, (%esp)

 

call putchar

 

jmp .L2

 

.L3:

 

movl -12(%ebp), %eax

 

cmpl $98, %eax

 

jle .L4

 

movl -12(%ebp), %eax

 

subl $99, %eax

 

movl %eax, -12(%ebp)

 

movl $.LC2, %eax

 

movl %eax, (%esp)

 

call printf

 

jmp .L2

 

.L4:

 

movl -12(%ebp), %eax

 

cmpl $89, %eax

 

jle .L5

 

movl -12(%ebp), %eax

 

subl $90, %eax

 

movl %eax, -12(%ebp)

 

movl $.LC3, %eax

 

movl %eax, (%esp)

 

call printf

 

jmp .L2

 

.L5:

 

movl -12(%ebp), %eax

 

cmpl $49, %eax

 

jle .L6

 

movl -12(%ebp), %eax

 

subl $50, %eax

 

movl %eax, -12(%ebp)

 

movl $76, (%esp)

 

call putchar

 

jmp .L2

 

.L6:

 

movl -12(%ebp), %eax

 

cmpl $48, %eax

 

jle .L7

 

movl -12(%ebp), %eax

 

subl $49, %eax

 

movl %eax, -12(%ebp)

 

movl $.LC4, %eax

 

movl %eax, (%esp)

 

call printf

 

jmp .L2

 

.L7:

 

movl -12(%ebp), %eax

 

cmpl $39, %eax

 

jle .L8

 

movl -12(%ebp), %eax

 

subl $40, %eax

 

movl %eax, -12(%ebp)

 

movl $.LC5, %eax

 

movl %eax, (%esp)

 

call printf

 

jmp .L2

 

.L8:

 

movl -12(%ebp), %eax

 

cmpl $9, %eax

 

jle .L9

 

movl -12(%ebp), %eax

 

subl $10, %eax

 

movl %eax, -12(%ebp)

 

movl $88, (%esp)

 

call putchar

 

jmp .L2

 

.L9:

 

movl -12(%ebp), %eax

 

cmpl $8, %eax

 

jle .L10

 

movl -12(%ebp), %eax

 

subl $9, %eax

 

movl %eax, -12(%ebp)

 

movl $.LC6, %eax

 

movl %eax, (%esp)

 

call printf

 

jmp .L2

 

.L10:

 

movl -12(%ebp), %eax

 

cmpl $4, %eax

 

jle .L11

 

movl -12(%ebp), %eax

 

subl $5, %eax

 

movl %eax, -12(%ebp)

 

movl $86, (%esp)

 

call putchar

 

jmp .L2

 

.L11:

 

movl -12(%ebp), %eax

 

testl %eax, %eax

 

jle .L2

 

movl -12(%ebp), %eax

 

subl $1, %eax

 

movl %eax, -12(%ebp)

 

movl $73, (%esp)

 

call putchar

 

.L2:

 

movl -12(%ebp), %eax

 

testl %eax, %eax

 

jg .L12

 

leave

 

.cfi_restore 5

 

.cfi_def_cfa 4, 4

 

ret

 

.cfi_endproc

 

.LFE0:

 

.size roman, .-roman

 

.section .rodata

 

.LC7:

 

.string “Enter exponent”

 

.LC8:

 

.string “Enter base”

 

.LC9:

 

.string “sum = %d”

 

.text

 

.globl exponent

 

.type exponent, @function

 

exponent:

 

.LFB1:

 

.cfi_startproc

 

pushl %ebp

 

.cfi_def_cfa_offset 8

 

.cfi_offset 5, -8

 

movl %esp, %ebp

 

.cfi_def_cfa_register 5

 

subl $56, %esp

 

movl $0, -16(%ebp)

 

movl $.LC7, %eax

 

movl %eax, (%esp)

 

call printf

 

movl $.LC1, %eax

 

leal -32(%ebp), %edx

 

movl %edx, 4(%esp)

 

movl %eax, (%esp)

 

call __isoc99_scanf

 

movl $.LC8, %eax

 

movl %eax, (%esp)

 

call printf

 

movl $.LC1, %eax

 

leal -28(%ebp), %edx

 

movl %edx, 4(%esp)

 

movl %eax, (%esp)

 

call __isoc99_scanf

 

movl $1, -12(%ebp)

 

movl $0, -24(%ebp)

 

jmp .L14

 

.L17:

 

movl $0, -16(%ebp)

 

movl $0, -20(%ebp)

 

jmp .L15

 

.L16:

 

movl -12(%ebp), %eax

 

addl %eax, -16(%ebp)

 

movl $.LC9, %eax

 

movl -16(%ebp), %edx

 

movl %edx, 4(%esp)

 

movl %eax, (%esp)

 

call printf

 

addl $1, -20(%ebp)

 

.L15:

 

movl -28(%ebp), %eax

 

cmpl %eax, -20(%ebp)

 

jl .L16

 

movl -16(%ebp), %eax

 

movl %eax, -12(%ebp)

 

addl $1, -24(%ebp)

 

.L14:

 

movl -32(%ebp), %eax

 

cmpl %eax, -24(%ebp)

 

jl .L17

 

leave

 

.cfi_restore 5

 

.cfi_def_cfa 4, 4

 

ret

 

.cfi_endproc

 

.LFE1:

 

.size exponent, .-exponent

 

.section .rodata

 

.LC10:

 

.string “Enter a value for m”

 

.LC11:

 

.string “Enter a value for v”

 

.text

 

.globl subtract

 

.type subtract, @function

 

subtract:

 

.LFB2:

 

.cfi_startproc

 

pushl %ebp

 

.cfi_def_cfa_offset 8

 

.cfi_offset 5, -8

 

movl %esp, %ebp

 

.cfi_def_cfa_register 5

 

subl $40, %esp

 

movl $.LC10, %eax

 

movl %eax, (%esp)

 

call printf

 

movl $.LC1, %eax

 

leal -16(%ebp), %edx

 

movl %edx, 4(%esp)

 

movl %eax, (%esp)

 

call __isoc99_scanf

 

movl $.LC11, %eax

 

movl %eax, (%esp)

 

call printf

 

movl $.LC1, %eax

 

leal -12(%ebp), %edx

 

movl %edx, 4(%esp)

 

movl %eax, (%esp)

 

call __isoc99_scanf

 

movl -16(%ebp), %edx

 

movl -12(%ebp), %eax

 

movl %edx, %ecx

 

subl %eax, %ecx

 

movl %ecx, %eax

 

movl %eax, -20(%ebp)

 

movl $.LC1, %eax

 

leal -20(%ebp), %edx

 

movl %edx, 4(%esp)

 

movl %eax, (%esp)

 

call printf

 

leave

 

.cfi_restore 5

 

.cfi_def_cfa 4, 4

 

ret

 

.cfi_endproc

 

.LFE2:

 

.size subtract, .-subtract

 

.section .rodata

 

.LC12:

 

.string “Enter a value for d”

 

.LC13:

 

.string “Enter a value for f”

 

.text

 

.globl addition

 

.type addition, @function

 

addition:

 

.LFB3:

 

.cfi_startproc

 

pushl %ebp

 

.cfi_def_cfa_offset 8

 

.cfi_offset 5, -8

 

movl %esp, %ebp

 

.cfi_def_cfa_register 5

 

subl $40, %esp

 

movl $.LC12, %eax

 

movl %eax, (%esp)

 

call printf

 

movl $.LC1, %eax

 

leal -16(%ebp), %edx

 

movl %edx, 4(%esp)

 

movl %eax, (%esp)

 

call __isoc99_scanf

 

movl $.LC13, %eax

 

movl %eax, (%esp)

 

call printf

 

movl $.LC1, %eax

 

leal -12(%ebp), %edx

 

movl %edx, 4(%esp)

 

movl %eax, (%esp)

 

call __isoc99_scanf

 

movl -16(%ebp), %edx

 

movl -12(%ebp), %eax

 

addl %edx, %eax

 

movl %eax, -20(%ebp)

 

movl $.LC1, %eax

 

leal -20(%ebp), %edx

 

movl %edx, 4(%esp)

 

movl %eax, (%esp)

 

call printf

 

leave

 

.cfi_restore 5

 

.cfi_def_cfa 4, 4

 

ret

 

.cfi_endproc

 

.LFE3:

 

.size addition, .-addition

 

.section .rodata

 

.LC14:

 

.string “Enter a value for z”

 

.LC15:

 

.string “Enter a value for y”

 

.text

 

.globl multiply

 

.type multiply, @function

 

multiply:

 

.LFB4:

 

.cfi_startproc

 

pushl %ebp

 

.cfi_def_cfa_offset 8

 

.cfi_offset 5, -8

 

movl %esp, %ebp

 

.cfi_def_cfa_register 5

 

subl $40, %esp

 

movl $0, -16(%ebp)

 

movl $.LC14, %eax

 

movl %eax, (%esp)

 

call printf

 

movl $.LC1, %eax

 

leal -24(%ebp), %edx

 

movl %edx, 4(%esp)

 

movl %eax, (%esp)

 

call __isoc99_scanf

 

movl $.LC15, %eax

 

movl %eax, (%esp)

 

call printf

 

movl $.LC1, %eax

 

leal -20(%ebp), %edx

 

movl %edx, 4(%esp)

 

movl %eax, (%esp)

 

call __isoc99_scanf

 

.L21:

 

movl -16(%ebp), %edx

 

movl -24(%ebp), %eax

 

addl %edx, %eax

 

movl %eax, -16(%ebp)

 

addl $1, -12(%ebp)

 

movl -20(%ebp), %eax

 

cmpl %eax, -12(%ebp)

 

jl .L21

 

movl $.LC1, %eax

 

leal -16(%ebp), %edx

 

movl %edx, 4(%esp)

 

movl %eax, (%esp)

 

call printf

 

leave

 

.cfi_restore 5

 

.cfi_def_cfa 4, 4

 

ret

 

.cfi_endproc

 

.LFE4:

 

.size multiply, .-multiply

 

.section .rodata

 

.LC16:

 

.string “%s”

 

.align 4

 

.LC17:

 

.string “Enter 1 for roman 2 for multiply, 3 for exponent, 4 for addition,and 5 for subtraction:”

 

.align 4

 

.LC18:

 

.string “Invalid option. Select a choice from 1 to 5”

 

.text

 

.globl main

 

.type main, @function

 

main:

 

.LFB5:

 

.cfi_startproc

 

pushl %ebp

 

.cfi_def_cfa_offset 8

 

.cfi_offset 5, -8

 

movl %esp, %ebp

 

.cfi_def_cfa_register 5

 

andl $-16, %esp

 

subl $32, %esp

 

movl $.LC16, %eax

 

movl $.LC17, 4(%esp)

 

movl %eax, (%esp)

 

call printf

 

movl $.LC1, %eax

 

leal 28(%esp), %edx

 

movl %edx, 4(%esp)

 

movl %eax, (%esp)

 

call __isoc99_scanf

 

movl 28(%esp), %eax

 

cmpl $1, %eax

 

jne .L23

 

call roman

 

jmp .L30

 

.L23:

 

movl 28(%esp), %eax

 

cmpl $2, %eax

 

jne .L25

 

call multiply

 

jmp .L30

 

.L25:

 

movl 28(%esp), %eax

 

cmpl $3, %eax

 

jne .L26

 

call exponent

 

jmp .L30

 

.L26:

 

movl 28(%esp), %eax

 

cmpl $4, %eax

 

jne .L27

 

call addition

 

jmp .L30

 

.L27:

 

movl 28(%esp), %eax

 

cmpl $5, %eax

 

jne .L28

 

call subtract

 

jmp .L30

 

.L28:

 

movl 28(%esp), %eax

 

cmpl $6, %eax

 

jne .L30

 

movl $.LC18, %eax

 

movl %eax, (%esp)

 

call printf

 

.L30:

 

nop

 

.L29:

 

movl 28(%esp), %eax

 

cmpl $7, %eax

 

jne .L29

 

call roman

 

call exponent

 

call multiply

 

call addition

 

call subtract

 

leave

 

.cfi_restore 5

 

.cfi_def_cfa 4, 4

 

ret

 

.cfi_endproc

 

.LFE5:

 

.size main, .-main

 

.ident “GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3”

 

.section .note.GNU-stack,””,@progbits

 

 

Screenshots: The image below displays the program once it is executed. Unfortunately the system begins to hang when an output is needed for display.

 

assembly

Leave a Reply

Your email address will not be published. Required fields are marked *