Help
Help
Help
SUB.c
MUL.c
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;
}
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
#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;
}
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
Step4:
To generate an executable file use the following command
> gcc EG1.o -o myEG1exe
Step5:
> ./myEG1exe