1                                  ; printf1.asm   print an integer from storage and from a register
     2                                  ; Assemble:	nasm -f elf -l printf.lst  printf1.asm
     3                                  ; Link:		gcc -o printf1  printf1.o
     4                                  ; Run:		printf1
     5                                  ; Output:	a=5, eax=7
     6                                  
     7                                  ; Equivalent C code
     8                                  ; /* printf1.c  print an int and an expression */
     9                                  ; #include <stdio.h>
    10                                  ; int main()
    11                                  ; {
    12                                  ;   int a=5;
    13                                  ;   printf("a=%d, eax=%d\n", a, a+2);
    14                                  ;   return 0;
    15                                  ; }
    16                                  
    17                                  ; Declare some external functions
    18                                  ;
    19                                          extern	printf		; the C function, to be called
    20                                  
    21                                          SECTION .data		; Data section, initialized variables
    22                                  
    23 00000000 05000000                	a:	dd	5		; int a=5;
    24 00000004 613D25642C20656178-     fmt:    db "a=%d, eax=%d", 10, 0 ; The printf format, "\n",'0'
    25 0000000D 3D25640A00         
    26                                  
    27                                  
    28                                          SECTION .text                   ; Code section.
    29                                  
    30                                          global main		; the standard gcc entry point
    31                                  main:				; the program label for the entry point
    32 00000000 55                              push    ebp		; set up stack frame
    33 00000001 89E5                            mov     ebp,esp
    34                                  
    35 00000003 A1[00000000]            	mov	eax, [a]	; put a from store into register
    36 00000008 83C002                  	add	eax, 2		; a+2
    37 0000000B 50                      	push	eax		; value of a+2
    38 0000000C FF35[00000000]                  push    dword [a]	; value of variable a
    39 00000012 68[04000000]                    push    dword fmt	; address of ctrl string
    40 00000017 E8(00000000)                    call    printf		; Call C function
    41 0000001C 83C40C                          add     esp, 12		; pop stack 3 push times 4 bytes
    42                                  
    43 0000001F 89EC                            mov     esp, ebp	; takedown stack frame
    44 00000021 5D                              pop     ebp		; same as "leave" op
    45                                  
    46 00000022 B800000000              	mov	eax,0		;  normal, no error, return value
    47 00000027 C3                      	ret			; return
    48