In: Computer Science
analyze the assembly code and explain what each line is doing
000000000000063a <main>: 63a: 55 push ebp 63b: 48 89 e5 mov ebp,esp 63e: 48 83 ec 10 sub esp,0x10 642: c7 45 fc 00 00 00 00 mov DWORD PTR [ebp-0x4],0x0 649: eb 16 jmp 661 <main+0x27> 64b: 83 7d fc 09 cmp DWORD PTR [ebp-0x4],0x9 64f: 75 0c jne 65d <main+0x23> 651: 48 8d 3d 9c 00 00 00 lea edi,[eip+0x9c] # 6f4 <_IO_stdin_used+0x4> 658: e8 b3 fe ff ff call 510 <printf@plt> 65d: 83 45 fc 01 add DWORD PTR [ebp-0x4],0x1 661: 83 7d fc 09 cmp DWORD PTR [ebp-0x4],0x9 665: 7e e4 jle 64b <main+0x11> 667: b8 00 00 00 00 mov eax,0x0 66c: c9 leave 66d: c3 ret
Assembly language provides two instructions for stack operations: PUSH and POP.
63a:Push the content in the register ebp.EBP stands for extended base pointer,It keeps track of the current stackframe.
63b:Move the content of the register ebp to register esp.
63e:Subtact 10 from the esp and store it in the ebp.The function will take 10 byte.
642:Now assign value 0 to the address pointed by 4 bytes less than ebp.
649:Jmp switches the CPU to execute a different piece of code! Here, it jumps to 661.
64b:Compare the content of the register and test if function return is 0.
64f:jne is a conditional jump that follows a test.It jumps to the specified location if the zero flag is cleared(0).jne is commonly used to explicitly test for something not being equal to zero whereas jne is commonly found after a cmp instruction.Jmp to 65d.
651:The lea is the load effective address instruction which is a way of obtaining the address which arises from any of the Intel processor's memory addressing modes.It is not a specific arithmetic instruction:it is a way of intercepting the effective address arising from any of the processsor's memory addressing modes.
658:print@plt is actually a small stub which eventually calls the real printf function,modifying things on the way to make subsequent calls faster.So the plt is a smaller process-specific area at a reliably -calculated-at-runtime address that is not shared between processes,so any given process is free to change it however it wants to,without adverse effects.Here the call operand transfers the program sequence to the memory address given in the operand(550).
65d:The dword ptr part is called a size directive.Basically,it means " the size of the target operand is 32 bits" so it will ADD the 32-bit value at the address computed by taking the contents of the ebp register and subtracting four with 0.
661:It will compare the 32 bit value at the address computed by taking the contents of the ebp register and subtracting four with 0.It will test that the function return is 0 or not.
665:The jle instruction is a conditional jump that follows a test.It performs a signed comparison jump after a cmp if the destination operand is less than or equal to the source operand.Depends on the statisfaction of the condition here it will jump to 64b.
667:Move the content of the register eax .
66c:The leave instruction reverse the action of an enter instruction.Leave copies the frame pouinter to the stack point and releases the stack space formerly used by a procedure for its local variables.leave pops the old frame pointer into ebp,thus restoring the caller's frame.
66d:The ret instruction transfers control to return address located on the stack.