Linux Platform x86 compilation (VI): Transfer of data

Source: Internet
Author: User

"Copyright Notice: respect for the original, reproduced please retain the source: blog.csdn.net/shallnet, the article only for learning Exchange, do not use for commercial purposes"
Before you define the data elements, you need to know what to do with the data elements. The data element is in memory, and the processor has many instructions to use a register, so the first step in processing the data element is to transfer it between the memory and the register. The data transfer instruction is MOV, which is one of the most commonly used instructions in assembly language. the basic format of the MOV instruction is as follows:Movx Source, destwhere source and dest values can be memory addresses, numeric values stored in memory, data values defined by directive statements, or registers. The GNU assembler's MOV instruction must declare the length of the transmitted data element by adding an additional character to the MOV to indicate the length, so the mov instruction becomes MOVX, where x can be these characters:L (32-bit), W (16-bit), B (8-bit). but when using the MOV instruction, not all locations can be transmitted to all locations, is by some restrictions, see below the MOV instruction can do the value of the location transfer.
    • Transfer immediate numbers to registers and memory
movl,%eax #把0值传送给寄存器eaxMOVL $, var #把值100传送到内存var的位置Precede each value with a $ symbol, which indicates that the value is an immediate number, and the immediate number can also be 16, as in 0x40,0xff.
    • Transferring data between registers
transferring data between registers is the fastest way to transfer, so the data is kept as much as possible in the processor register, which reduces the time it takes to access the memory location. The contents of the general register can be transmitted to any other type of register, while the contents of the dedicated register (control, debug, segment Register) can only be transferred to the contents of the Universal register. movl%eax,%ecx #把eax寄存器中数据传送到ecx寄存器.
    • Transferring data between memory and registers
movl value,%eax # VALUE specifies the memory locationmovl%eax, Value # Transfers 4 bytes of data in the EAX register to the memory location specified by value.
Use the memory location of the variable address. as follows, multi-memory specifies multiple values in one command:values:. int 10,20,30,40,50,60,70this creates a series of data values that are contiguous in memory (similar to an array of high-level languages). When referencing data in an array, you must use the system to determine which memory location you want to access. The memory location is determined by the following expression:base_addr (offset_addr, index, size)A value of 0 can be ignored, but a comma is still required as a placeholder. The OFFSET_ADDR and index must be registers, and the value of size can be a number. If you need to refer to the value 40 in the values array given earlier, it is possible to transfer the 4th value starting with values memory to the EAX register using the following instruction:MOVL $ $,%edi # similar to the C language, subscript starting from 0. MOVL values (,%edi, 4),%eax
Use register indirection. Registers not only hold data, but also memory addresses, and data that uses memory addresses in registers to access memory locations is referred to as indirect addressing. When using a variable to reference the memory location of the data, the address of its memory location (similar to the C-address symbol &) can be obtained by adding the $ symbol in front of the variable in the instruction. The following command transmits the memory address referenced by the variable to the EDI register:movl $value,%ediCommandmovl%ebx, (%edi)The value in the EBX register is passed to the memory location contained in the EDI register, and if there is no parentheses, the instruction loads the value in the EBX register into the EDI register. CommandMOVL%edx, 4 (%edi)The value in the edx register is stored in a memory address of 4 bytes after the EDI register points to the location. The number can be negative, and if 4 is stored in the memory location of the previous 4 bytes.
Here is an example of an indirect address to a register:. Section. Datavalues:. int , A, a, a, a, ten, A.
. Section. Text. Globl _start_start:NOPMOVL values,%eaxmovl $values,%ediMOVL $4 (%edi)MOVL $,%ediMOVL values (,%edi, 4),%EBXMOVL $,%eaxint $0x80to save compile time, we write a makefile file:# Makefile for Linux as
Src_bin=mov
src=$ (wildcard *.s)src_obj=$ (SRC:.S=.O)
All : $ (src_bin)
$ (Src_bin): $ (src_obj)ld-o [email protected] $<
$ (src_obj): $ (SRC)$ (AS)-o [email protected] $<--gstabs
Clean :$ (RM) $ (src_obj) $ (src_bin) *~. Phony: All Clean
The compilation chain is delivered as an executable file. $ Makeas-o mov.o mov.s--gstabsLd-o mov mov.o
now debug after running the program: $ GDB mov
GNU gdb (gdb) Red Hat Enterprise Linux (7.2-60.EL6)
...Reading symbols From/home/allen/as/2_mov/mov...done.
(GDB) b _start
Breakpoint 1 at 0x8048074:file Mov.s, line 8.
(GDB) R
Starting program:/home/allen/as/2_mov/mov

Breakpoint 1, _start () at Mov.s:8
8 NOP
(GDB) first look at the memory location values in memory:(gdb) x/9d &values0x804909c <values>: Ten0x80490ac <values+16>:0X80490BC <values+32>:(GDB)The value of the check register:(GDB) Info Registereax 0x0 0ecx 0x0 0edx 0x0 0ebx 0x0 0ESP 0XBFFFF3E0 0XBFFFF3E0EBP 0x0-0x0ESI 0x0 0EDI 0x0 0eip 0x8048074 0x8048074 <_start>eflags 0x212 [AF IF]CS 0x73SS 0x7b 123DS 0x7b 123ES 0x7b 123FS 0x0 0GS 0x0 0(GDB)
then step through the program and load the first data element from the values array into the register EAX:(GDB) s9 movl values,%eax(gdb) print $eax$ = 0(GDB) sTen movl $values,%edi(gdb) print $eax$ = Ten(GDB)you can see the value of the EAX register as the first element of the array, continue stepping, and monitor the values memory address that is being loaded in the EDI register:(GDB) nMOVL $4 (%edi)(GDB) nMOVL $,%edi(gdb) print/x $edi$ $ = 0x804909c(GDB)you can see the same address as the values. Next assembly CodeMOVL $4 (%edi)
transfers 100 to the memory address after four bytes of the address that the EDI register points to, which is the second data element of the values array. (gdb) x/9d &values0x804909c <values>: Ten0x80490ac <values+16>:0X80490BC <values+32>:(GDB)The x command sees that the second element of the array has changed. The following assembly code puts the second element 100 of the array into the EBX register, puts the system call number 1 (sys_exit) into the EAX, and finally executes the interrupt. Such a continuous exit code should be 100. Check the value in the shell after executing the MOV program: $./mov
$ echo $?
100
$

Linux Platform x86 compilation (VI): Transfer of data

Related Article

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.