GUN ASM Description

Source: Internet
Author: User
Tags integer reserved

A period of time to look at arm's assembly, found that many have a small point, but borrowed the grammar of the book did not, ask the students do not know, so on the Internet to find that I saw the book is arm's standard assembly, and there are small points of the GNU assembly, so the collected information will be placed here.

GnucompilationLanguage structure
It consists of three commonly used segments:
Data data segment declares an element with an initial value
BSS data Segment declaration element with 0 or null initialization
The text body segment contains instructions for eachcompilationThe program must contain this segment

Use the. section directive to define segments, such as:
. Section. Data
. Section. BSS
. section. Text

Starting point:
GnucompilationUse the _start tag to indicate the default starting point, and if you want tocompilationInternal tags can be accessed by external programs,
Need to use.Globldirectives, such as:.Globl_start


When using common library functions, you can use:
Ld-dynamic-linker/lib/ld-linux.so.2


################################################################################################
# Four, data transfer
################################################################################################
1, data segment
Use. DataDeclares a data segment in which any data element declared in this segment is kept in memory and can becompilationThe instruction of the program is read,
You can also use. Rodata to declare read-only data segments, which you need to use tags and commands when declaring a data element:

Tags: tags used as reference data elements, which are similar to the C language variables, are meaningless to the processor and
Just forcompilationA location used as a reference pointer when the device tries to access the memory location.

Directive: this name indicatescompilationTo retain a specific amount of memory for the data element referenced by the tag, the declaration command must be given
One or more default values.

Declaration directives:
. ASCII text string
. Asciz string ending with a null character
. Byte byte value
The. Double double-precision floating-point value
. Float single-precision floating-point value
. int 32-bit integer
. Long 32-bit integer, same as int
. octa 16-byte integer
. Quad 8-byte integer
. short 16-bit integers
. single precision floating point number (same as float)


Example:
Output
. ASCII "Hello World."

Pi:
. Float 2.14

A declaration can define multiple values in a row, such as:
Ages
. int 20, 10, 30, 40


To define a static symbol:
Use the. equ command to define a constant value as a symbol that can be used in a text segment, such as:
. Section. Data
. equ Linux_sys_call, 0x80
. section. Text
MOVL $LINUX _sys_call,%eax



2, BSS segment
Unlike data segments, you do not need to declare a specific data type, just declare the original memory portion reserved for the desired purpose.
GnucompilationUse the following two commands to declare a memory region:
. Comm declared as an uninitialized universal memory region
. Lcomm declared as uninitialized local memory area

The two declarations are similar, but. Lcomm is not from the localcompilationData that is accessed outside of the code is reserved,formatFor:
. Comm/.lcomm symbol, length

Example:
. Section. BSS
. Lcomm Buffer, 1000
This statement assigns a 1000-byte memory address to the label buffer, and the function outside the program that declares the local common memory area is
Unable to access their. (cannot be in.GloblUse them in the command)


The benefit of declaring in a BSS segment is that the data is not included in the executable file. When you define data in a data segment, it must be included in the
Executable program because it must be initialized with a specific value. Because data regions declared in BSS segments are not initialized with
So the memory area is reserved for use at run time and does not have to be included in the final program




3, transfer data
Move directive:
formatMovex the number of source operands, the purpose operand. where x is the length of the data to be transmitted, the values are:
L long byte for 32 bits
W for 16-bit words
b byte value for 8 bits


Immediately before the number is added a $ sign, the register is preceded by a% sign.

8 General-purpose registers are the most common registers used to hold data, and the contents of these registers can be passed
Give any other registers that are available. Unlike general-purpose registers, special registers (control, debug, segment)
Can only be transmitted to the general register, or to the content transmitted from the General Register.


When referencing a label:
Cases:
. Section. Data
Value
. int 100
_start:
MOVL value,%eax
MOVL $value,%eax
Movl%ebx, (%edi)
MOVL%EBX, 4 (%edi)

Where: Movl value,%eax simply passes the memory value currently referenced by the tag value to EAX
MOVL $value,%eax passes the memory address pointer currently referenced by the label value to EAX
Movl%ebx, (%edi) If there is no parenthesis outside of EDI, then this instruction just puts the EBX
Value is loaded into the EDI, and if you have parentheses, it means that the contents of the EBX
To the memory location contained in the EDI.
MOVL%EBX, 4 (%edi) indicates that the value in EDI is placed in a 4-byte memory location after the location of the EDI point
Movl%ebx, -4 (%edi) indicates that the value in EDI is placed in a 4-byte memory location before the location of the EDI point



Cmove directive (conditional transfer):
Cmovex the number of source operands, the purpose operand. The value of X is:
Unsigned number:
A/nbe greater than/not less than or equal to
AE/NB greater than or equal to/not less than
NC Non-rounding
B/nae less than/not greater than or equal
C Rounding
Be/na less than or equal to/not greater than
E/z equals/0
Ne/nz not equal to/not zero
P/pe Parity/Even check
Np/po Non parity/parity check

Signed Number:
GE/NL greater than or equal to/not less than
L/nge less than/not greater than or equal to
Le/ng less than or equal to/not greater than
o Overflow
No no Overflow
S-band sign (negative)
NS unsigned (non-negative)





Exchanging data:
Xchg exchange of values between two registers or between registers and memory
Such as:
Xchg operand, operand, requires both operands to be of the same length and cannot be both memory locations
Where the register can be a 32,16,8 bit


Bswap reverses the byte order of a 32-bit register

such as: Bswap%EBX


Xadd Exchange two values and store two values only and stored in the destination operand

Example: Xadd source operand, target operand
Where the source operand must be a register, the target operand can be a memory location, or it can be a register
Where the register can be a 32,16,8 bit

Cmpxchg
Cmpxchg Source, Destination
Where source must be a register, destination can be a memory or register, which is used to compare
The value of both, if equal, loads the value of the source operand into the target operand, and if not unequal,
The target operand is loaded into the source operand, where the register can be a 32,16,8 bit, where the source operation
Number is the value in the Eax,ax or Al register


CMPXCHG8B is the same as CMPXCHG, but it handles a 8-byte value while it has only one operand
cmpxchg8b destination
Where destination refers to a memory location where the 8-byte value is associated with the edx and EAX registers
The included values (edx high register, EAX low register) are compared if the target value and Edx:eax
The value in the pair is equal, the 64-bit value in the Edx:eax pair is passed to the memory location, and if it does not match, the
The values in the memory address are loaded into the edx:eax pair



4, Stack
The ESP register holds the starting position of the current stack, which is automatically decremented when a data is pressed into the stack.
Instead, it automatically increments

To press the stack operation:
Pushx source, x value is:
L 32-bit long word
W 16-bit word

To eject a stack operation:
POPX Source
Where source must be a 16 or 32-bit register or memory location, when the last element of the pop is the ESP value should
And the same as before


5, press in and eject all registers
Pusha/popa Press in or eject all 16-bit general-purpose registers
Pushad/popad Press in or eject all 32-bit general-purpose registers
Pushf/popf Low 16-bit press-in or eject-eflags register
PUSHFD/POPFD Press in or eject all 32 bits of the EFlags register

6, Data address alignment
GascompilationThe. Align command, which is used to align defined data elements at a specific memory boundary, in the data segment
The. Align command clings to the front of the data definition

Comparison:
CMP Operend1, Operend2


Carry flag Modify Directive:
CLC empties the Carry flag (set to 0)
The CMC reverses the carry flag (changes it to the opposite value)
STC set carry Flag (set to 1)


Cycle:
Loop Loop until ECX register is 0
Loope/loopz Loop until the ECX register is 0 or the ZF flag is not set
LOOPNE/LOOPNZ Loop until ECX is 0 or the ZF flag is set

The instruction format is: Loopxx address note loop instruction only supports 8-bit offset addresses

The above is transferred from

Http://hi.baidu.com/walkingman520/blog/item/7296bbeec777012a2cf5344a.html

Another comparative article is as follows:

ARM compilation and the GNU compilation conversion

When porting the assembler code under ARM ads to the GCC for arm compiler, there are the following rules:
1, the comment line takes "@" or "/* ... * *" instead of ";"

2, get or include = =. INCLUDE
such as: Get OPTION.A =. include "Option.a"

3, EQU = EQU
TCLK2 EQU PB25 = EQU TCLK2, PB25
SETA ==>. equ
Setl ==>. equ
Buswidth SETA equ Buswidth, 16

4, EXPORT = +. Global
IMPORT =. extern
GBLL =. Global
Gbla =. Global

5, DCD =. Long

6, IF:D EF: = =. Ifdef
ELSE =. ELSE
ENDIF =. ENDIF
: OR: = |
: SHL: = <<

7, END =>.end
Note: In the header file of the include, such as "Option.a", the. End is no longer required, or it causes the main assembler to end.

8, symbol definition Plus ":" Number
Entry = Entry:
Area Word, CODE, READONLY ==>. Text
Area Block, data, READWRITE ==>. Data
CODE32 ==>. Arm
CODE16 ==>. Thumb

9, macro ==>. macro
MEND ==>. Endm

Http://www.diybl.com/course/6_system/linux/Linuxjs/200871/129550.html

Gnu-arm Assembly Instructions

The first part of the Linux under the ARM assembly syntax although it is convenient to write programs in C or C + + under Linux, the assembler source program is used for the most basic initialization of the system, such as initializing the stack pointer, setting the page table, and manipulating the arm's coprocessor. After initialization is complete, you can jump to C code execution. It is important to note that the GNU assembler follows the assembler syntax for the-T and can download the specifications from the GNU site (www.gnu.org).

I. Linux assembler row structure

Any assembly line is structured as follows:

[:] [} @ Comment

[:] [} @ Comment

In the Linux ARM assembly, any identifier that ends with a colon is considered to be a label, not necessarily the beginning of a line.

"Example 1" defines a function of "add", which returns the and of two parameters.

. section. Text, "X"

. Global add @ give the symbol add external linkage

Add

Add R0, R0, r1 @ Add input arguments

MOV pc, LR @ return from subroutine

@ End of Program

Two. Labels in the Linux assembler

The label can only be made up of a~z,a~z,0~9, ".", _ and other characters. When the number labeled 0~9 is a local label, the local label can be repeated, using the following methods:

Label F: The marking of the forward in the reference place

Label B: Label backward in place of reference

Example 2, using a local notation, a cyclic procedure

1:

Subs R0,r0, #1 @ each cycle makes r0=r0-1

BNE 1f @ Jump to 1 marking to execute

The local label represents the address where it is located, so it can also be used as a variable or function.

Three. Fragmentation in the Linux assembler

(1). Section Pseudo-operation

Users can customize a segment by using the. Section pseudo operation, in the following format:

. section section_name [, "Flags" [,%type[,flag_specific_arguments]]

Each segment begins with a paragraph name, and the following paragraph name or end of file ends. Each of these segments has a default flag (flags) that the connector can recognize. (same as area in Armasm).

The following are the allowed segment flags in the ELF format

< logo > Meaning

A allow segment

W Writable Segment

X Execution Segment

"Example 3" definition segment

. section. mysection @ Custom Data segment, segment named ". MySection"

. Align 2

strtemp:

. ASCII "Temp string \n\0"

(2) Assembly system pre-defined segment name

. Text @ Code Snippet

. Data @ Initialize Segment

. BSS @ Uninitialized data segment

. sdata @

. SBSS @

It is important to note that the. BSS segment in the source program should precede. Text.

Four. Defining entry points

The default entry for the assembler is the start label, and the user can also indicate other entry points with the entry flag in the connection script file.

"Example 4" defines the entry point

. section.data

< initialized data here>

. Section. BSS

< uninitialized data here>

. section. Text

. globl _start

_start:

<instruction Code goes here>

Five. Macro definitions in the Linux assembler

The format is as follows:

The. Macro Name parameter list @ pseudo-directive. Macro defines a key

Macro Body

. ENDM @.endm Indicates the end of the macro

If the macro uses parameters, the prefix "\" is added when the parameter is used in the macro body. Parameters that are defined by the macro can also use the default values.

You can use the. EXITM pseudo-directive to exit a macro.

"Example 5" macro definition

. macro Shiftleft A, b

. If \b < 0

MOV \a, \a, ASR #-\b

. exitm

. endif

MOV \a, \a, LSL #\b

. endm

Six. Constants in the Linux assembler

(1) The decimal number begins with a number other than 0, such as: 123 and 9876;

(2) The binary number starts with 0b, where the letter can also be capitalized;

(3) Octal number starts with 0, for example: 0456,0123;

(4) The hexadecimal number starts with 0x, for example: 0xabcd,0x123f;

(5) String constants need to be enclosed in quotation marks, and escape characters can be used in the middle, such as: "You are welcome!\n";

(6) Current address with "." Indicates that this symbol can be used in assembler to represent the address of the current instruction;

(7) Expression: An expression in a assembler can use constants or values

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.