"C Language" Reading Notes (5) [Let's learn the compilation together! (Segment)]

Source: Internet
Author: User
This article describes how to use registers in the past few days. The memory is mainly stuck in the assembly language, so the blog posting process is slow. Let's talk about how to define data elements today? Because Assembly Language Program One of the most important tasks is to process objects.

Segment

Three most commonly used segments: 1. Data segments: data segments declare data elements with initial values. These data elements are used as variables in assembly language programs. 2. BBS section: the BBS section declares the data elements initialized with zero (or null) values. These data elements are commonly used as buffers in assembly language programs. 3. Text Segment: the place where the instruction code is declared in the executable program. 1. Define the Section. The GNU assembler uses the. Section command to declare the section. Syntax:. Section type (. Data,. BSS,. Text) is the layout of assembly language programs.

2. Define the start point 1) when an assembly language program is converted to an executable file, the linker must know the start point in the command. The GNU assembler declares a default tag or identifier, it should be used as the entry point of the application. The _ start tab indicates that the program should start running from this command. 2) In addition to declaring the start tag in the application, an entry point must be provided for the external application. This is done using the. globl command. With the above information, you can create basic templates for all assembly language programs. As follows:

 

With such a template, you are ready to write assembly language programs.

3. Create a simple program to demonstrate how to combine the components above. First, write a simple "Hello, world". Isn't that true for many languages? I am not vulgar. -- For other parts, I will Article .
1 . Section. Data2Output:3. ASCII"Hello, world \ n"45 . Section. Text6 . Globl _ start7_ Start:8Movl $4, % Eax9Movl $1, % EBX10 Movl $ output, % ECx11Movl $13, % EdX12Int$0x8013 14 # Success exit15Movl $1, % Eax16Movl $0, % EBX17Int$0x80

Compile and link this process

 

 

Data Segment 1) all data elements defined by data segments Keep in memory .
Note UseAny data element defined by the. Data Segment can followRead and Write mode access. Any data element defined by the. rodata Data Segment can only be accessed in read-only mode. -- Therefore, the RO prefix is used.
2) when defining a data element in a data segment, two statements are required: a tag, like a variable name in C language, used as a tag for referencing a data element. -- The tag is meaningless to the processor. It is only a location used by the assembler to reference the pointer when trying to access the memory location. (The machine code uses bit to indicate this mark) a command: defines the number of bytes reserved by the data element.

3) The following table describes different commands for retaining memory for a specific data element type. 4) let's take a look at the distribution in the memory. To make it clearer, I will write another example.

 

 

 

 

 

 

 

Memory Distribution chart

Observation

Each data element is stored in the memory according to the sequence of data elements defined in the data segment. Elements with multiple values are stored in the order listed in the Command (such as MSG and length ).

 

Static symbols The. equ command is used to set constants (don't say this, constant? It cannot be changed in the program, which is also its biggest feature). It is set to a symbol that can be used in a text segment (we can understand it as a command segment. The setting method is as follows:

Note The. equ command can appear anywhere in the data segment, but for ease of reading, it is best to define it at the beginning or end.

 

These two types are defined in the data segment, which is also frequently used in assembly. Next we will introduce another paragraph Declaration-BBs paragraph.

BSS section BBS section only declares the original memory part. The GNU assembler uses two commands to declare the buffer zone.

 

To sum up, I have been wondering from which point of view I can enter the C language for analysis. Many materials show that I can really understand C language from the perspective of assembly and machine language. I also read a lot of information and thought it should be done in the most basic aspect. So let's talk about it today. -- You need to know what is going on in the future. The materials referenced in the past two days, assembly language, are mainly for Windows platforms. The syntax is Intel syntax. My platform is also my work platform. I used to use Linux, while Linux uses"At&t Syntax ". So I also read the previous chapters on hardware (but I didn't understand much about it, huh, huh ). Assembly Language Programming-this book is my focus and jumps directly to the second part of the basic language for research. Linux C Programming one-stop learning-This book focuses on the "x86 assembly program basics" several times ". Benefits. Deep understanding of computer systems-I have read this book a little, but I still don't understand it in many places. I have to read it several times.   Digress

Machine commands are commands that can be correctly executed by machines.

A computer's machine instruction is a binary number. A computer converts an instruction into a column of high and low levels to drive the computer's electronic devices and perform computation. CPU is the core component of a computer. It controls the operation of the entire computer and performs computation.
1. The address bus width determines the addressing capability of the CPU; 2. The width of the Data Bus determines the amount of data transferred at a time when the CPU and other devices transmit data. 3. The control bus width determines the control capability of the CPU on other devices in the system.   Unit conversion: 1B = 8B 1kb = 1024b 1 MB = 1024kb 1 GB = 1024 MB

Recommendation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.