Linux Platform x86 compilation (14): Use of functions

Source: Internet
Author: User
Tags define function

"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"
like high-level languages, assembly language may write the same process and processing across multiple projects, and if you use a function, you can call it when you need it without repeating the code of the utility every time you need it. The function contains the code required to complete a particular function, the data is passed from the main program to the function, and the result is returned to the main program. When the function is called, the program execution path is changed to switch to the first instruction of the function code. The processor starts executing the instruction from this position until the function indicates that it can return control to its original location in the main program. In assembly language, it takes 3 steps to create a function: 1. Defining an input value function requires some form of input data, and the programmer must define how the program passes this information to the function, with a basic three techniques: using registers, using global variables, using stacks. The use of global variables and formal parameters is similar to the C language. 2. Defining function processing in order to define a function in the GNU assembler, the function name must be declared as a label in the program. Declare the function name for the assembler, using the. Type command:
. Type Func, @functionfunc:
The. Type command informs the GNU assembler that the Func tag defines the beginning of the function used in the assembly language program. The end of the function is defined by the RET directive, and the control returns to the main program when executed to the RET instruction. 3. Define function output values
When the function finishes processing the data, it is likely that you want to pass the result back to the area where the calling program was emitted. The ability to pass the results back to the main program so that the main program can use this data for further processing or display. There are two common ways to complete the transfer results: Store the results in a register, and present the results in a global variable memory location.
Access to functions use the call instruction, which uses a single operand: The function is to place all input values in the correct position before executing the ASK command. In assembly language, functions can be placed behind the main program.        Unlike the C language, functions do not have to be defined before calling a function in the main program. When a program calls a function, the programmer should know that the function uses those registers to do its internal processing. When execution returns to the main program, any registers or memory used in the function may not be the original value. If the calling function modifies the register used by the main program, the current state of the register is saved before the function is called, and the state of the register is resumed after the function returns. Before calling, you can use the push instruction to save a specific register separately, or you can use the Pusha instruction to save all registers. Similarly, you can use the pop instruction to recover the original I state of the register separately, or you can use the Popa instruction to restore the state of all registers at the same time.
#func. S.section database:    . int 100.section. BSS    . Lcomm result, 4.section. Text.globl _start_start:    NOP    MOVL $8,%eax call    cal_func    movl $,%eax    movl result,%ebx    int $0x80cal_func:    addl $,%eax< C9/>ADDL base,%eax    movl%eax, result    ret
The result is output after make:
$./func $ echo $?118
Actually using global variables to pass parameters and return results is not a common method of program design, even if it is not often done in C.        In C, it is common to pass a value to a function by passing it to the function, which is actually the stack used, and any function in the program can access the stack, so the assembly language passes the function parameter in the stack without worrying about the confusion caused by the use of registers and global variables. Before calling the function, the program stores the input parameters required by the function on top of the stack, and when the call instruction is executed, the return address of the calling program is placed on top of the stack, and the stack pointer (ESP) points to the top of the stack. The function can access the input parameters according to the ESP register using indirect addressing, and does not have to eject the stack to prevent the loss of the return address. As a general practice, when entering a function, copy the ESP register to the EBP register so that there is a register that always holds the correct pointer to the top of the stack when the function is called, and the value of the EBP register is placed on the stack before the value of the ESP register is copied.        Now that the EBP register contains the stack start position, the first input parameter of the main program is located at the indirection location 8 (%EBP), the second parameter is in a (%EBP), and so on. In front of the call command, I talked about the assembly language function template, as follows:
Func_lable:    pushl%ebp            #函数开头把ebp的原始值保存到堆栈的顶部    movl%esp,%ebp    #把当前esp堆栈指针复制到ebp寄存器    ... MOVL%ebp,%esp    #获取存储在ebp寄存器中原始的esp寄存器值.    popl%EBP    ret
You can use the Enter and leave directives to establish the beginning and end of functions, and you can use them instead of manually creating the start and end. When working in function code, it is possible to store data elements in a location, which is similar to the C language, and we can use the stack at this time. After the EBP register is set to execute the top of the stack pointer, this time the local variables of the function can be placed in the stack after this pointer, it can also be used instead of the EBP register reference them, for example, for a 4-byte variable, you can access the first local variable by -4 (%EBP), with 8 (% EBP) accesses a second local variable.
The following example exits by passing two parameters to the function and calculating them and then exiting them as a program's return code.
. Section. Database:    . int 100plus_no:    . int 8.section. Text.globl _start_start:    nop    PUSHL Base            # Press the function required parameters onto the top of the stack    PUSHL plus_no call    cal_func    movl $,%eax    int $0x80cal_func:    pushl%ebp    movl% ESP,%EBP    movl $,%eax    Addl 8 (%EBP),%eax     # The first input parameter is located in the indirect addressing position 8 (%EBP)    Addl (%EBP),%eax    # The second parameter is located in%ebp    movl%eax,%ebx        # puts and values in the%EBX register, and finally returns the MOVL%EBP as a program return value,%esp popl%ebp    ret
The compile output after make is as follows:
$./func $ echo $?108
Assembly language, like the C language, functions can be defined in a separate file and finally connected to the main program file. A separate function file is similar to a main program file that is typically created, except that the _start segment is not used, and the function name must be declared as a global label so that other programs can access it. As follows:
. section. Text.type addfunc, @function. Globle addfuncaddfunc:
The type command declares that the Addfunc tag points to the beginning of the function. We will unload the two number of functions in the previous example to add a separate file, modified as follows:
#main. S.section database:    . int 100plus_no:    . int 8.section. BSS    . Lcomm result, 4.section. Text.globl _ Start_start:    nop    PUSHL base    pushl plus_no call    addfunc    movl $,%eax    int $0x80

#func. s.section. Text.type addfunc, @function. Globl addfuncaddfunc:    pushl%ebp    movl%esp,%EBP movl $    ,% EAX    Addl 8 (%EBP),%eax    Addl (%EBP),%eax    movl%eax,%ebx    movl%ebp,%esp    popl%ebp    ret
The make and output results are as follows:
$ make as-   o add.o add.sas   

Linux Platform x86 compilation (14): Use of functions

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.