Http://firstdot.spaces.live.com/
1. GCC Embedded Assembly
(1) is the input and output using the same register in the GCC embedded assembly?
Static void * _ memcpy (void * To, const void * From, size_t N)
{
Int D0, D1, D2;
_ ASM _ volatile __(
"Rep; movsl/n/t"
"Testb $2, % B4/n/t"
"Je 1f/n/t"
"Movsw/N"
"1:/ttestb $1, % B4/n/t"
"Je 2f/n/t"
"Movsb/N"
"2 :"
: "= & C" (D0), "= & D" (D1), "= & S" (D2)
: "0" (N/4), "Q" (N), "1" (long) to), "2" (long) from)
: "Memory ");
Return ();
}
The Operations 0, 1, 2, 3, 5, 6 use the same register:
A., 6 read the addresses of N/4, to, and from into ECx, EDI, and ESI registers during input;
The values in the ECX, EDI, and ESI registers are stored in the D0, D1, and D2 memory variables during the output process.
C. note that the "&" qualifier is also available in the preceding statement, but the input and output still use the same register. For example, if the operands 0 and 3 both use the register ECx, this is because the result is limited by "0, if
If you change "0" (N/4) to "C" (N/4), an error is reported due to the limitation of "= & C.
(2) What is the role of the "&" qualifier in GCC embedded assembly?
"&" The qualifier is used to output the operands so that it can use a register only.
Int bar, foo;
_ ASM _ volatile __(
"Call func/n/t"
"Mov EBX, % 1"
: "= A" (FOO)
: "R" (bar ));
During GCC compilation, bar will also use the eax register by default. If you change "= a" to "= & A", Foo will use eax only, let bar use other registers.
(3) What is the relationship between. _ start and main?
Main is the entry point of the program seen by GCC, while the entry points of LD and as are actually _ start. The _ start function in the libc library calls the main function.
A. to compile an assembly program with _ start: As-o. o. s, LD-o. o; gcc-g-c. s, LD-o. o. (You can add the debugging option-G when compiling with GCC.
The reason why gcc-o a. s cannot be compiled directly is that GCC searches for the main function in the program by default and adds _ start in libc. If gcc-o a. s is used for compilation
, Two errors will be reported: "Duplicate _ start" and "Main not found ")
B. Steps for compiling an assembly program or C program with main: gcc-o a. s; gcc-o a. c.
(4). Section and. Previous
Set the two. section and. the code in the middle of previous is compiled into the defined segments, and then jumped back to compile the code after this into the previous section (generally. text ),
That is, the segment before the custom segment .. Section and. Previous must be used together.
2. at&t assembly
(1). Data,. Section, and so on are all pseudo operations and cannot be directly translated into machine codes. Only the corresponding operator can recognize them.
(2) Section divides the program into several parts, such as. Data,. Text,. BSS
(3) globl function name indicates that the function is export and can be called by functions in other files.
(4) BSS can be used to apply for a space, but it does not need to be initialized.
(5) using kernel-defined functions such as open and read can be interrupted by INT $80.
(6) movl $ Foo, % eax puts the address of Foo in the memory into eax, while movl Foo and % eax put the content of Foo in eax
(7). Include "file name" to include other files
(8) how to represent the structure in the assembly? For example, the structure in C is as follows:
Struct para
{
Char firstname [40];
Char lastname [40];
Char Address [240];
Long age; // 4 bytes
}
In assembly, it can be expressed:
. Section Data
Record1:
. ASCII "Fredrick/0"
. Rept 31 # padding to 40 bytes
. Byte 0
. Endr
. ASCII "Bartlett/0"
. Rept 31 # padding to 40 bytes
. Byte 0
. Endr
. ASCII "4242 s prairie/ntulsa, OK 55555/0"
. Rept 209 # padding to 240 bytes
. Byte 0
. Endr
. Long 45
The. Rept N and. endr indicate that the sequence between the two is repeated n times, which can be used to fill data.
(9) When an assembly program consists of multiple files, you can compile and connect the program in the following ways:
As write-records.s-O write-records.o (GCC-g-C write-records.s)
As write-record.s-O write-record.o (GCC-g-C write-record.s)
LD write-record.o write-records.o-O write-records
(10) How do I use functions in a dynamic library in an assembly language?
# Helloworld-lib.s
. Section. Data
Helloworld:
. ASCII "Hello World/n/0"
. Section. Text
. Globl _ start
_ Start:
Pushl $ helloworld
Call printf
Pushl $0
Call exit
As helloworld-lib.s-O helloworld-lib.o
LD-dynamic-linker/lib/ld-linux.so.2-O helloworld-lib helloworld-lib.o-lC
Produce dynamic library: LD-shared write-record.o read-record.o-O librecord. So
(11) use an assembly file to generate a dynamic library
As write-record.s-O write-record.o
As read-record.s-O read-record.o
LD-shared write-record.o read-record.o-O librecord. So
As write-records.s-O write-records.o
LD-L.-dynamic-linker/lib/ld-linux.so.2-O write-records-lrecord write-records.o
Remember to add the dynamic library path to/etc/lD. So. conf when running write-records, and run ldconfig
(12) how to generate debugging symbols when compiling assembly files
As -- maid A. S-0 A. O or GCC-g-c a. S
Appendix:
(1) function call stack
# Parameter # n <--- N * 4 + 4 (% EBP)
#...
# Parameter 2 <--- 12 (% EBP)
# Parameter 1 <--- 8 (% EBP)
# Return address <--- 4 (% EBP)
# Old % EBP <--- (% EBP)
# Local variable 1 <----4 (% EBP)
# Local variable 2 <----8 (% EBP) and (% ESP)
(2) Example
. Include "external_func.s"
. Section. Data
Data_array: # define a long array
. Long 3,67, 34,0
Data_strings: # define a string
. ASCII "Hello there/0"
Data_long: # define long Variables
. Long 5
. Section. BSS
. Lcomm my_buffer, 500 # apply for a 500-byte memory
. Section. Text
. Equ linux_syscall, 0x80 # defines the value of linux_syscall as 0x80
. Globl _ start
_ Start:
Pushl % edX
Movl data_long, % edX # Put the value of the data_long variable into the edX register
Movl $ data_long, % edX # put the address of data_long into the edx register
Popl % edX
Pushl $3 # Push second argument
Pushl $2 # push first argument
Call power # Call the Function
Addl $8, % ESP # Move the stack pointer back
Pushl % eax # Save the first answer before, calling the next Function
Movl $1, % eax # exit (% EBX is returned)
Int $ linux_syscall
. Type Power, @ function # define function power
Power:
Pushl % EBP # Save old base pointer
Movl % ESP, % EBP # Make Stack pointer the base pointer
Subl $4, % ESP # Get room for our local storage
Movl 8 (% EBP), % eax # put first argument in % eax
Movl 12 (% EBP), % EBX # Put second argument in % EBX
Imull % EBX, % eax
Movl % EBP, % ESP # restore the stack pointer
Popl % EBP # restore the base pointer
RET