Help

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

ADD.

int myadd(int a, int b)


{
return a+b;
}

SUB.c

int mysub(int a, int b)


{
return a-b;
}

MUL.c

int mymul(int a, int b)


{
return a*b;
}

AOP.c

#include<stdio.h>
int myadd(int, int);
int mysub(int, int);
int mymul(int, int);
int main()
{
int A=100;
int B=20;
int R1=myadd(A,B);
int R2=mysub(A,B);
int R3=mymul(A,B);
printf("R1=%d R2=%d R3=%d",R1,R2,R3);
return 0;
}

> gcc -c ADD.c


> gcc -c SUB.c
> gcc -c MUL.c
> gcc -c AOP.c

After executing the above 4 commands, you will get the object files: ADD.o, SUB.o,
MUL.o, AOP.o
Once we have object files, then generate an executable file with the help of the
following command
> gcc AOP.o ADD.o SUB.o MUL.o -o myexe
> ./myexe

output: R1=120 R2=80 R3=2000


Procedure to generate an assembly code for a given C program

Suppose we have the following C-Program (Name: ​EG1.c​)

#include<stdio.h>
#define VAL1 100
#define VAL2 20
int main()
{
int A=VAL1;
int B=VAL2;
int R1=A+B;
int R2=A-B;
int R3=A*B;
printf("R1=%d R2=%d R3=%d",R1,R2,R3);
return 0;
}

Type the following command at the command prompt.


Step1:
>​ ​gcc -E EG1.c > EG1.i
To see contents of the EG1.i file, just type the command
>​ cat EG1.i
----- Expanded code of the header file​ stdio.h -​ ------

At the end you will see the following statements. Here you could observe that VAL1
and VAL2 are replaced with constant values.

# 4 "EG1.c"
int main()
{
int A=100;
int B=20;
int R1=A+B;
int R2=A-B;
int R3=A*B;
printf("R1=%d R2=%d R3=%d",R1,R2,R3);
return 0;
}

Step2:
> ​gcc -S EG1.i > EG1.s

We will get an assembly file for the given C code (EG1.c) and it is stored in the file
EG1.s
>​ cat EG1.s
.file "EG1.c"
.text
.section .rodata
.LC0:
.string "R1=%d R2=%d R3=%d"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
endbr64
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
​ subq $32, %rsp
movl $100, -20(%rbp)
movl $20, -16(%rbp)
movl -20(%rbp), %edx
movl -16(%rbp), %eax
addl %edx, %eax
movl %eax, -12(%rbp)
movl -20(%rbp), %eax
subl -16(%rbp), %eax
movl %eax, -8(%rbp)
movl -20(%rbp), %eax
imull -16(%rbp), %eax
movl %eax, -4(%rbp)
movl -4(%rbp), %ecx
movl -8(%rbp), %edx
movl -12(%rbp), %eax
movl %eax, %esi
leaq .LC0(%rip), %rdi
movl $0, %eax
call printf@PLT
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 9.3.0-10ubuntu2) 9.3.0"
.section .note.GNU-stack,"",@progbits
.section .note.gnu.property,"a"
.align 8
.long 1f - 0f
.long 4f - 1f
.long 5
0:
.string "GNU"
1:
.align 8
.long 0xc0000002
.long 3f - 2f
2:
.long 0x3
3:
.align 8
4:

Our area of interest is to know the purpose of the instructions with blue color font.

Step3:
Compile the assembly code with the help of assembler
Just type the following command.
> as EG1.s -o EG1.o

It generates object file EG1.o

Step4:
To generate an executable file use the following command
> gcc EG1.o -o myEG1exe

Step5:
> ./myEG1exe

You will see the output


R1=120 R2=80 R3=2000

All the best

You might also like