Translate C Language Program into x86 Assembly Language Chung-Yuan Christian University Information & Computer Eng. Dept. Teddy Hsiung
From “Hello, world” Example #include <stdio.h> int main(void) { printf("Hello, world\n"); return 0; } Why include file “stdio.h” ? Every include file might includes… Macro & constant definitions. User defined data type definitions. External data reference declarations. External function call prototype declarations.
Equivalent “Hello, world” Program #include <stdio.h> int main(void) { printf("Hello, world\n"); return 0; } int printf (const char *__format, ...); int main(void) { printf("Hello, world\n"); return 0; }
Translate C to Assembly Program #include <stdio.h> int x = 168; int y; int main(void) { int z = 166; y = 188; printf("x=%d,y=%d,z=%d\n",x,y,z); return 0; } DATA Segment BSS Segment STACK Segment CODE Segment
C to Assembly: Code Segment #include <stdio.h> int x = 168; int y; int main(void) { int z = 166; y = 188; printf("x=%d,y=%d,z=%d\n",x,y,z); return 0; } Code Segment contains CPU executable machine code. Basically, assembly inst. is 1-to-1 directly mapping to machine code. _TEXT segment byte public 'CODE' ; ; int main(void) assume cs:_TEXT _main proc near push bp mov bp,sp push si ; { ; int z = 166; mov si,166 ; y = 188; mov word ptr DGROUP:_y,188 ; printf("x=%d,y=%d,z=%d\n",x,y,z); push si push word ptr DGROUP:_y push word ptr DGROUP:_x mov ax,offset DGROUP:s@ push ax call near ptr _printf add sp,8 ; ; return 0; xor ax,ax ; } pop si pop bp ret _main endp _TEXT ends
C to Assembly: Code Segment 0000 _TEXT segment byte public 'CODE' assume cs:_TEXT 0000 _main proc near 0000 55 push bp 0001 8B EC mov bp,sp 0003 56 push si 0004 BE 00A6 mov si,166 0007 C7 06 0000 R 00BC mov word ptr DGROUP:_y,188 000D 56 push si 000E FF 36 0000 R push word ptr DGROUP:_y 0012 FF 36 0000 R push word ptr DGROUP:_x 0016 B8 0002 R mov ax,offset DGROUP:s@ 0019 50 push ax 001A E8 0000 E call near ptr _printf 001D 83 C4 08 add sp,8 0020 33 C0 xor ax,ax 0022 5E pop si 0023 5D pop bp 0024 C3 ret 0025 _main endp 0025 _TEXT ends
C to Assembly: Data Segment _DATA segment word public 'DATA' d@ label byte d@w label word _DATA ends _x label word db 168 db 0 s@ label byte db 'x=%d,y=%d,z=%d' db 10 #include <stdio.h> int x = 168; int y; int main(void) { int z = 166; y = 188; printf("x=%d,y=%d,z=%d\n",x,y,z); return 0; } Data segment contains the initialized data in the program. The initialized data include the initialized variable and constant string.
C to Assembly: BSS Segment #include <stdio.h> int x = 168; int y; int main(void) { int z = 166; y = 188; printf("x=%d,y=%d,z=%d\n",x,y,z); return 0; } _BSS segment word public 'BSS' b@ label byte b@w label word _BSS ends _y label word db 2 dup (?) ?debug C E9 BSS: BLOCK STARTED BY SYMBOL BSS Segment contains the uninitialized data in the program. The whole BSS Segment usually filled with zero at the program startup.
C to Assembly: Stack Segment #include <stdio.h> int x = 168; int y; int main(void) { int z = 166; y = 188; printf("x=%d,y=%d,z=%d\n",x,y,z); return 0; } ;In file ~\EXAMPLES\STARTUP\C0.ASM _STACK SEGMENT db 128 dup(?) ENDS Stack Segment contains: The return address in the function call. The parameter passed in the function call. Non-static local variable within the function. Defined in the C startup code “C0.ASM”.