Hey Everyone, I am Vikram Salunke and Welcome to yet another video of x86 Assembly.
In this video we will convert C program to it's assembly equivalent and we will see changes
in registers, modification of stack in both in Visual Studio as well as in animation format.
So, let's get started.
This is my C Program.
You can download this program using my github account.
I will give the link in the description box below.
I have created a new project C_to_Assembly2 and I have set the project properties as I
have described in setting project properties video.
I will give the link of that video in the "i" button above.
This is a C program.
First we are including stdio.h file After that we will go to the main()
main() is calling function1() then we are in function1()
function1() is calling function2() we are in function2()
and function1() returns with 0x22222222 it will return to function1()
after that function1() will return with 0x11111111 and it will return to the main()
and lastly main() will return with the 0xAAAAAAAA for clearer understanding let's look at our
C program in an animation format first, we are including stdio.h file
after that we will directly go to the main() main() is calling function1()
then we are into the function1() function1() calling function2()
after that we are into function2() function2() is returning with 0x22222222
function2() is ending here function1() is returning with 0x11111111
function1() is ending here after that we are returned to the main()
and main() is returning with the 0xAAAAAAAA and main() ends here
now we will build the C program for this click on Build -> Build Solution
We have successfully built the program after that we will debug the program
but before that we will set a breakpoint here then click on Debug
Right click here -> Go to Disassembly This is our generated assembly code
we will go to our breakpoint so the breakpoint is here
and this arrow indicates the next instruction to execute
this is our stack here and these our registers here
now for better understanding I have divided the program into different
different parts this is our program
this is our main() this is our function1()
and this our function2() now we will look at our program's flowchart
this is a main() main() is calling function1()
this a function1() then function1() is calling function2()
this is our function2() then function2()
is returning to function1() and function1() returning to the main()
this is our program's flowchart in this video we will only look at the half
of the program and other half we will see in the next video
this is main() main() is calling function1()
function1() calling function2() so we will only see this part in this video
this is our C program and it's assembly code we are executing from main()
now main() will save it's stack frame onto the stack
for this it will first push value from the ebp to the stack
for that we will click on Step Into as we can see the value is pushed onto the
stack after that esp will get moved into ebp
ebp's value has been changed and esp is now into the ebp
and from this address it is main()'s stack frame
next instruction is main() is calling function1() now to call another function
the next instruction's address will get pushed onto the stack
so, 00401028 will get pushed onto the stack click Step Into
so, it got pushed onto the stack and now execution has been passed to the function1()
and we will look at the same thing into the animation format for better understanding
So, we are starting execution from main() EIP is pointing at the push ebp
that means the value from the EBP i.e. 0x0012FFB8 will get pushed onto the stack
let's execute that instruction the next instruction is mov esp into the ebp
the value pointed by the ESP will move into the EBP
now in the C program main() is calling function1() now EIP is pointing at the call function1()
so what happens when one function calls another function?
the next instruction's address will get pushed onto the stack
so, 00401028 will get pushed onto the stack and after that the control will be passed
to he function1() now we are in function1()
first function1() sets it's stack frame onto the stack
for that it will push the value from ebp onto the stack
ebp contains the stack frame pointer for the main()
so, it will get pushed onto the stack the next instruction is we are moving esp
into the ebp currently ESP is 0x0012FF60 that will get
moved into the EBP let's execute that
after that function1() is calling function2() for that next instruction's address i.e. 00401018
will get pushed onto the stack because when function2() finishes it's execution it can
return to function1() let's execute that instruction
00401018 got pushed onto the stack and we have passed control to the function2()
let's look at the same thing in an animation format for better understanding
main() called function1() we are starting the execution from function1()
now first it will push ebp onto the stack
ebp is 0012FF68 i.e. main()'s stack frame pointer
it will get pushed onto the stack and to setup function1()'s stack frame pointer
the value pointed by the ESP will be moved into the EBP
now EBP contains 0x0012FF60 and from this point function1()'s stack frame
has began next function1() is calling function2()
it's assembly code is call function2() now to call another function what we will
do? we will push next instruction's address onto
the stack i.e. 00401018 will get pushed onto the stack
and after that the control will be passed to function2()
function2() will push function1()'s stack frame pointer onto the stack i.e. 0012FF60
let's step into that it got pushed onto the stack
after that value pointed by the ESP will be moved into the EBP
let's execute that so, ESP moved into EBP
now function2() is returning with the 0x22222222 for that 0x22222222 will be copies into EAX
let's step into that now EAX contains 22222222
as we can see here let's see the same thing in an animation format
function1() called function2() and passed control to the function2()
function2() will now begins the execution first, it will push the value from EBP onto
the stack to save the function1()'s stack frame pointer
EBP got pushed onto the stack now EIP is pointing at mov esp into the ebp
to set the function2()'s stack frame onto the stack
now ESP is pointing to the 0x0012FF58 that will get moved into EBP
let's execute that so, now EBP contains 0x0012FF58 and from this
point function2()'s stack frame begins now function2() is returning with 0x22222222
for that what will we do? we will copy 0x22222222 into the EAX
we have moved 0x22222222 into the EAX
so, that's it for this video
in this video we saw, main() calls function1() function1() calls function2()
and function2() returns with 0x22222222 in the next video we will see the remaining part
so stay tuned
Thank you :)
Không có nhận xét nào:
Đăng nhận xét