I know that every x86 instruction has an opcode for that, but I don't know how data types and variables are converted to machine code. So I tried to figure out. I wrote a simple C program and converted it to assembly:
#include<stdio.h>
int variable1=1;
int variable2=1;
char variable3='A';
char variable4='A';
char name[20]="naveen prakash";
int main(int argc, char const *argv[])
{
char g[4]="hell";
int local_variable1=1;
int local_variable2=2;
int local_variable3;
variable1=0;
variable2=1;
variable3='B';
variable4='A';
printf("HeLLO wORLD\n");
getch();
}
Assembly for the above program (Compiled using gcc -S hello.c
):
.file "hello.c"
.globl _variable1
.data
.align 4
_variable1:
.long 1
.globl _variable2
.align 4
_variable2:
.long 1
.globl _variable3
_variable3:
.byte 65
.globl _variable4
_variable4:
.byte 65
.globl _name
_name:
.ascii "naveen prakash\0"
.space 5
.def ___main; .scl 2; .type 32; .endef
.section .rdata,"dr"
LC1:
.ascii "HeLLO wORLD\0"
LC0:
.ascii "hell\0"
.text
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $32, %esp
call ___main
movl LC0, %eax
movl %eax, 16(%esp)
movl $1, 20(%esp)
movl $2, 24(%esp)
movl $0, _variable1
movl $1, _variable2
movb $66, _variable3
movb $65, _variable4
movl $LC1, (%esp)
call _puts
call _getch
leave
ret
.def _puts; .scl 2; .type 32; .endef
.def _getch; .scl 2; .type 32; .endef
I could figure out only global variables and cannot find local variables. Then I created executable file of the above program and used IDA, ollyDBG, PE Explorer and tried to find where the variables are, but I could not find.
What happens to the variables and data type declarations when it converted into machine language?
I read that .data
segment contains the initialized data, then I tried to find my initialized data, but I couldn't find anything. I have a text "naveen prakash" but I can't find it anywhere in the executable file. I used Hex editor to view the binary file.
So my question is, How variables and data types are represented in machine code and how do we find it using debuggers?