Advertisement
Phr0zen_Penguin

noob_asm.c - A Simple Assembly Language Lesson/Demonstration

May 16th, 2015
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.14 KB | None | 0 0
  1. /**
  2.  * [noob_asm.c]
  3.  * A simple assembly language lesson/demonstration.
  4.  *
  5.  *      (c) Damion 'Phr0z3n.dev' Tapper, 2015.
  6.  *      Email: Phr0z3n.Dev@Gmail.com
  7.  *      Website: http://L337Stuff.Blogspot.com
  8.  **/
  9.  
  10. #include <stdio.h>
  11.  
  12. int main(void)
  13. {
  14.     int i;                                  /* Variable declaration. */
  15.  
  16.         /**
  17.          * The following loop will do 5 things (essentially):
  18.          * 1. Initialize the 'i' variable to 0 (once).
  19.          * 2. Check if the value of 'i' is less than 10.
  20.          * 3. Print 'Hello world!' to the screen if step 2 is true.
  21.          * 4. Increment the value of 'i' by 1.
  22.          * 5. Repeat steps 2-4 until step 2 is false.
  23.          */
  24.         for(i = 0; i < 10; i++)         /* Loop 10 times. */
  25.             puts("Hello World!\n");         /* Put the string to the output. */
  26.  
  27.     return 0;                               /* Tell OS the program exited without errors. */
  28. }
  29.  
  30. /**
  31.  * Below is the assembly language syntax corresponding with the previous C-style operations.
  32.  
  33.     INTEL SYNTAX:
  34.     Dump of assembler code for function main:
  35.        0x080483d4 <+0>:     push   ebp                          ; beginning of function prologue
  36.        0x080483d5 <+1>:     mov    ebp,esp
  37.        0x080483d7 <+3>:     and    esp,0xfffffff0
  38.        0x080483da <+6>:     sub    esp,0x20                     ; end of function prologue
  39.        0x080483dd <+9>:     mov    DWORD PTR [esp+0x1c],0x0     ; initializes the 'i' variable with the value 0
  40.        0x080483e5 <+17>:    jmp    0x80483f8 <main+36>          ; unconditional jump to the for loop section that does the comparison
  41.        0x080483e7 <+19>:    mov    DWORD PTR [esp],0x80484e0    ; copies/prepares the 'Hello world!' string for output
  42.        0x080483ee <+26>:    call   0x80482f0 <puts@plt>         ; calls the puts() function, which prints 'Hello world!' to the screen
  43.        0x080483f3 <+31>:    add    DWORD PTR [esp+0x1c],0x1     ; adds 1 to the value of the 'i' variable
  44.        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)
  45.        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
  46.        0x080483ff <+43>:    mov    eax,0x0                      ; copies/prepares the main() function's return value
  47.        0x08048404 <+48>:    leave                               ; prepares the stack for procedure exit
  48.        0x08048405 <+49>:    ret                                 ; the main() function exits
  49.     End of assembler dump.
  50. */
  51.  
  52. /**
  53.  * Below is the same set of instructions as above, only in a different syntax.
  54.  
  55.     AT&T SYNTAX:
  56.     Dump of assembler code for function main:
  57.        0x080483d4 <+0>:     push   %ebp
  58.        0x080483d5 <+1>:     mov    %esp,%ebp
  59.        0x080483d7 <+3>:     and    $0xfffffff0,%esp
  60.        0x080483da <+6>:     sub    $0x20,%esp
  61.        0x080483dd <+9>:     movl   $0x0,0x1c(%esp)
  62.        0x080483e5 <+17>:    jmp    0x80483f8 <main+36>
  63.        0x080483e7 <+19>:    movl   $0x80484e0,(%esp)
  64.        0x080483ee <+26>:    call   0x80482f0 <puts@plt>
  65.        0x080483f3 <+31>:    addl   $0x1,0x1c(%esp)
  66.        0x080483f8 <+36>:    cmpl   $0x9,0x1c(%esp)
  67.        0x080483fd <+41>:    jle    0x80483e7 <main+19>
  68.        0x080483ff <+43>:    mov    $0x0,%eax
  69.        0x08048404 <+48>:    leave
  70.        0x08048405 <+49>:    ret
  71.     End of assembler dump.
  72. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement