Hey Everyone, I am Vikram Salunke and welcome to the x86 Assembly Course
Till now we have seen the necessary theory and instructions.
From now on, real fun begins, so let's get started.
In this video we are going to convert C program to it's Assembly code
Let's look at our C program
This is our C Program first we are including stdio.h file
after that we will go to the main() and our program execution begins here
next thing we are calling function() function() starts from here
function() returning with value 0x22222222 as we have seen previously } means function()
ends here function() ended so we have returned to main()
now execution resume from main() and main() returning with value 0x11111111
} means main() ends here
Let's debug that program in Visual Studio This is our program
first we will put breakpoint here After that we will debug program
to debug will click on this play icon then right click here and click on Go to disassembly
- this is equivalent our assembly code language for it's equivalent C code
let's look at our C program and assembly code in simpler form
This is our C program and it's equivalent assembly code
in the right hand side there is stack and ESP is pointing at 0x0012FF6C and value is
0x004012E8 that value belongs to function who called
main() so, that is saved return address for that function
we will start execution from main() first instruction in our assembly code is
at address 00401010 and EIP is pointing at that address
instruction is push ebp, so we are pushing stack frame pointer (EBP) to the stack of
previous initialization function who called main()
EBP is 0x0012FFB8 so it will get pushed onto the stack
next instruction is used to move address pointed by ESP into EBP to set up a main()'s stack
frame address pointed by ESP is 0x0012FF68 will
be moved to EBP and now this is all main()'s stack frame
for every function, compiler will generate these 2 lines push EBP and mov EBP, ESP to
set up a stack frame for that function
as we saw earlier next C code is calling function let's see it's assembly equivalent
call instruction let's recall to pass control to another
function we use CALL instruction for that first we will push next instruction's address
onto the stack, so next instruction's address is 0x00401018
that will get pushed onto the stack why we are doing that?
because when function() finishes it's execution it will be used as return address to return
to the main(), in this case when function() finishes it's execution it will return on
00401018 and from that instruction main() will resume it's execution
now we are executing from function() and EIP us pointing to 00401000
now just think what will happen when new function() starts?
as we have seen earlier the compiler will set their stack frame on stack
for that it will first push main()'s stack frame pointer which is saved into EBP onto
the stack and it will move address pointed by ESP into EBP to start function()'s stack
frame so, the first instruction is push ebp
after that what will do?
We will move address pointed pointed by ESP into EBP
so now EBP's value is 0x0012FF60 and from this address function()'s stack
frame has started now look at our stack
as we can see there are total 3 stack frames for 3 different functions
this is for initialization function this stack frame is for main()
and this is function()'s stack frame
next C code is return with value 0x22222222 let's see it's assembly equivalent
first we will mov 0x22222222 to EAX function() has finishes it's execution
for that we will pop value and put that into EBP to give main()'s it's own stack frame
pointer next is a return instruction
let's recall what is return instruction?
return instruction is used for to return from one function to another
and for that we will pop the value from stack and put that into EIP
the value on stack is 0x00401018 we will put that value into EIP
and as we can see EIP has returned to the main()
so 00401018 was stored onto the stack and we popped that value into EIP and EIP has
return to the main() using that address next line of c code is return 0x11111111
for that we will mov 0x11111111 to EAX now EAX contain 0x11111111
this is form of mov instruction, we are moving immediate value into register
} means main() ends here for that what we will do?
first we will give back the function who called main() it's own stack frame pointer
yes you are right that is stored onto the stack
so, we will move value from stack into the EBP
pop instruction pop top of the stack into EBP
next is a return instruction we will return to the function who called
main() how we will do that?
for that we will take value from stack put that into the EIP and program execution will
begin from there
That's all for this video we are going to see lot of examples with different
different options in upcoming videos, so you will understand everything crystal and clear
But, if you had any trouble understanding what is a push, pop, call, ret, mov or how
stack works or what is general stack frame layout etc. then please watch my previous
videos.
here, you can find all the videos in x86 Assembly and if you have any doubts, any question then
you can contact me on these handles.
Thank you :)
Không có nhận xét nào:
Đăng nhận xét