Translate C Language Program into x86 Assembly Language

Slides:



Advertisements
Similar presentations
C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Advertisements

The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
IP high IP low IP high IP low BP high BP low IP high IP low BP high BP low FL high FL low CS high CS low IP high IP low _TEXTsegment byte public ‘CODE’
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
Embedded Systems Programming Introduction to cross development techniques.
Chapter3: Language Translation issues
Introduction to C Programming CE
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
C and Data Structures Baojian Hua
C Primer CAS CS210 Ying Ye Boston University. Outline Hello, world Basics in C Comparison of C and Java.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
Memory Layout C and Data Structures Baojian Hua
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Computer Science 210 Computer Organization Introduction to C.
“C” Programming Language CIS 218. Description C is a procedural languages designed to provide lowlevel access to computer system resources, provide language.
CSC 3210 Computer Organization and Programming Chapter 9 EXTERNAL DATA AND TEXT D.M. Rasanjalee Himali.
Enabling the ARM Learning in INDIA ARM DEVELOPMENT TOOL SETUP.
1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
5-1 Chapter 5 - Languages and the Machine Department of Information Technology, Radford University ITEC 352 Computer Organization Principles of Computer.
#include int main(void) { printf("Hello, world!\n"); return 0; } entry point called on program start only one main( ) in any program # for preprocessor.
1 Program Layout in memory –Code –Data Global variables –Stack Local variables & function Invocation Frames –Heap Dynamically allocated memory Today’s.
C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.
5-1 Chapter 5 - Languages and the Machine Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 3 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
LOW vs. HIGH Level Languages
First Compilation Rudra Dutta CSC Spring 2007, Section 001.
Computer Programming A simple example /* HelloWorld: A simple C program */ #include int main (void) { printf (“Hello world!\n”); return.
C is a high level language (HLL)
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
ECE 447: Lecture 13 Assembler Directives. ECE 447: Defining a Constant #define PORTB 0x1004PORTB EQU $1004 #define DELAY 100 ………. #undef DELAY #define.
Precept 7: Introduction to IA-32 Assembly Language Programming
Lecture2.
Assembly language programming
Programming what is C++
Computer Science 210 Computer Organization
Format of Assembly language
Functions and Structured Programming
C Language By Sra Sontisirikit
ICS103 Programming in C Lecture 3: Introduction to C (2)
Additional Assembly Programming Concepts
Homework Reading Machine Projects Labs
Program Execution in Linux
Computer Organization & Assembly Language
14th September IIT Kanpur
Computer Science 210 Computer Organization
Scope, Parameter Passing, Storage Specifiers
Ken D. Nguyen Department of Computer Science Georgia State University
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
Assembly Language Programming I: Introduction
Introduction to CS Your First C Programs
פרטים נוספים בסילבוס של הקורס
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Memory Allocation CS 217.
פרטים נוספים בסילבוס של הקורס
Lecture 18 Arrays and Pointer Arithmetic
Procedure Activations
#include <stdio.h> int main(void) { printf("Hello, world!\n");
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Lecture 2 SCOPE – Local and Global variables
Your first C and C++ programs
C (and C++) Pointers April 4, 2019.
Program Execution in Linux
Arrays.
The C Language: Intro.
Ken D. Nguyen Department of Computer Science Georgia State University
Scope Rules.
Procedures & Macros Introduction Syntax Difference.
Presentation transcript:

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”.