assembly - .NET JIT compilation naivety -


i have 3 years experience working full time .net (c# , vb). have working knowledge of msil , can use debugging tool.

i don't have knowledge of next step of compilation process i.e. when jitter produces assembly code (displayed in dissassebly window). hans passant posted answer question here: what difference between native code, machine code , assembly code?. more experienced colleague said brilliant answer, still don't understand following code:

static void main(string[] args) {             console.writeline("hello world"); 00000000 55                push        ebp                           ; save stack frame pointer 00000001 8b ec             mov         ebp,esp                       ; setup current frame 00000003 e8 30 03 6f    call        6f03be38                      ; console.out property getter 00000008 8b c8             mov         ecx,eax                       ; setup "this" 0000000a 8b 15 88 20 bd 02 mov         edx,dword ptr ds:[02bd2088h]  ; arg = "hello world" 00000010 8b 01             mov         eax,dword ptr [ecx]           ; textwriter reference 00000012 ff 90 d8 00 00 00 call        dword ptr [eax+000000d8h]     ; textwriter.writeline() 00000018 5d                pop         ebp                           ; restore stack frame pointer         } 00000019 c3                ret                                       ; done, return 

can provide more information on happens on each line , more particularly why each register chosen e.g. why eax chosen instead of edx? alternatively can recommend book?

i'm bit rusty this, i'm interested in low level assembly side of things. here goes:

push ebp; save stack frame pointer 

push value stored in ebp onto stack, when return method, know came from.

mov ebp,esp; setup current frame 

move current stack position value esp ebp, ebp in context of current method.

the preceding 2 lines of code convention ensures there's fixed position (stored in ebp register) on stack determining relative location of local variables.

call 6f03be38; console.out property getter 

no prizes guessing call console.out

mov ecx,eax; setup "this" 

returned values methods stored in eax, matter of calling convention. returned value console.out stored in eax. here, value copied ecx later use, making eax usable other purposes.

mov edx,dword ptr ds:[02bd2088h]; arg = "hello world" 

the register edx given memory location of string "hello world". dword ptr ds:[02bd2088h] means dereferences memory location ds:[02bd2088h], ds data segment (where things initialised strings stored). [02bd2088h] offset in memory region of ds.

mov eax,dword ptr [ecx]; textwriter reference 

remember console.out call? put returned value ecx. here, memory address of ecx dereferenced, memory address of textwriter copied eax. eax contain actual memory address of textwriter object. if did mov eax,dword ptr ecx; eax contain pointer memory address of textwriter, not actual memory address of textwriter. (i still confused myself).

call dword ptr [eax+000000d8h]; textwriter.writeline() 

here call made textwriter.writeline(). i'm assuming textwriter.writeline() using _fastcall calling convention (a explanation of calling conventions can found here) means uses edx register find arguments passed method.

pop ebp; restore stack frame pointer 

we remove top-most (or bottom-most really, stacks grow downwards) value ebp, frame pointer in ebp corresponds calling method.

ret 

return location found @ top of stack, calling method. in case, it's main() being called, control returned system code , application exit.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -