Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * [noob_asm.c]
- * A simple assembly language lesson/demonstration.
- *
- * (c) Damion 'Phr0z3n.dev' Tapper, 2015.
- * Email: Phr0z3n.Dev@Gmail.com
- * Website: http://L337Stuff.Blogspot.com
- **/
- #include <stdio.h>
- int main(void)
- {
- int i; /* Variable declaration. */
- /**
- * The following loop will do 5 things (essentially):
- * 1. Initialize the 'i' variable to 0 (once).
- * 2. Check if the value of 'i' is less than 10.
- * 3. Print 'Hello world!' to the screen if step 2 is true.
- * 4. Increment the value of 'i' by 1.
- * 5. Repeat steps 2-4 until step 2 is false.
- */
- for(i = 0; i < 10; i++) /* Loop 10 times. */
- puts("Hello World!\n"); /* Put the string to the output. */
- return 0; /* Tell OS the program exited without errors. */
- }
- /**
- * Below is the assembly language syntax corresponding with the previous C-style operations.
- INTEL SYNTAX:
- Dump of assembler code for function main:
- 0x080483d4 <+0>: push ebp ; beginning of function prologue
- 0x080483d5 <+1>: mov ebp,esp
- 0x080483d7 <+3>: and esp,0xfffffff0
- 0x080483da <+6>: sub esp,0x20 ; end of function prologue
- 0x080483dd <+9>: mov DWORD PTR [esp+0x1c],0x0 ; initializes the 'i' variable with the value 0
- 0x080483e5 <+17>: jmp 0x80483f8 <main+36> ; unconditional jump to the for loop section that does the comparison
- 0x080483e7 <+19>: mov DWORD PTR [esp],0x80484e0 ; copies/prepares the 'Hello world!' string for output
- 0x080483ee <+26>: call 0x80482f0 <puts@plt> ; calls the puts() function, which prints 'Hello world!' to the screen
- 0x080483f3 <+31>: add DWORD PTR [esp+0x1c],0x1 ; adds 1 to the value of the 'i' variable
- 0x080483f8 <+36>: cmp DWORD PTR [esp+0x1c],0x9 ; compares the value of the 'i' variable with 9 (the result of the comparison will set a flag bit accordingly)
- 0x080483fd <+41>: jle 0x80483e7 <main+19> ; if the previously set flag bit indicates that the value of 'i' is less than or equal to 9, a conditional jump is made to the section in main() that prepares the 'Hello world!' string for output
- 0x080483ff <+43>: mov eax,0x0 ; copies/prepares the main() function's return value
- 0x08048404 <+48>: leave ; prepares the stack for procedure exit
- 0x08048405 <+49>: ret ; the main() function exits
- End of assembler dump.
- */
- /**
- * Below is the same set of instructions as above, only in a different syntax.
- AT&T SYNTAX:
- Dump of assembler code for function main:
- 0x080483d4 <+0>: push %ebp
- 0x080483d5 <+1>: mov %esp,%ebp
- 0x080483d7 <+3>: and $0xfffffff0,%esp
- 0x080483da <+6>: sub $0x20,%esp
- 0x080483dd <+9>: movl $0x0,0x1c(%esp)
- 0x080483e5 <+17>: jmp 0x80483f8 <main+36>
- 0x080483e7 <+19>: movl $0x80484e0,(%esp)
- 0x080483ee <+26>: call 0x80482f0 <puts@plt>
- 0x080483f3 <+31>: addl $0x1,0x1c(%esp)
- 0x080483f8 <+36>: cmpl $0x9,0x1c(%esp)
- 0x080483fd <+41>: jle 0x80483e7 <main+19>
- 0x080483ff <+43>: mov $0x0,%eax
- 0x08048404 <+48>: leave
- 0x08048405 <+49>: ret
- End of assembler dump.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement