Download presentation
Presentation is loading. Please wait.
Published byHillary Anthony Modified over 9 years ago
1
Translate C Language Program into x86 Assembly Language
Chung-Yuan Christian University Information & Computer Eng. Dept. Teddy Hsiung
2
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.
3
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; }
4
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
5
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 push ax call near ptr _printf add sp,8 ; ; return 0; xor ax,ax ; } pop si pop bp ret _main endp _TEXT ends
6
C to Assembly: Code Segment
_TEXT segment byte public 'CODE' assume cs:_TEXT _main proc near push bp B EC mov bp,sp push si 0004 BE 00A6 mov si,166 0007 C R 00BC mov word ptr DGROUP:_y,188 000D push si 000E FF R push word ptr DGROUP:_y 0012 FF R push word ptr DGROUP:_x 0016 B R mov ax,offset push ax 001A E E call near ptr _printf 001D 83 C add sp,8 C0 xor ax,ax E pop si D pop bp 0024 C3 ret _main endp _TEXT ends
7
C to Assembly: Data Segment
_DATA segment word public 'DATA' label byte label word _DATA ends _x label word db 168 db 0 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.
8
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' label byte 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.
9
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 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”.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.