In-depth understanding of the C language (function of parameter transfer and function using parameters)

zhaozj2021-02-08  224

In-depth understanding of C language

The code generated by the C language is high than other advanced languages ​​than other advanced languages. Now let's take a look at what the code generated by the C language is what is like. When you read this article, you will be more step more. This article Explain the C language through a actual case program.

Research case one

Tools: TurboC C V2.0, DEBUG, MASM V5.0, NASM Instances C Program: / * EXAMPLE1.C * / CHAR CH; int E_MAIN () {

E_PUTCHAR (CH);

}

Target content: method and details of C language call functions

The C compiler we use is 16-bit TurboC C v2.0. It generates 16-bit code, relatively simple, easy to study. At the same time, we also need to use DEBUG under DOS to make an anti-assembly. Due to us The program in many cases is not a complete C program, so TLINK under Turboc does not generate the target program for us, so I will use Link.exe in MASM, and exe2bin.com can also convert the exe file for us. Cheng Bin file.

This program doesn't have a main function. We use e_main instead of the main function. This way we can avoid the C language to make a series of processing for the main function. Similarly, we also use e_putchar () to replace our usual use Putchar (). Here The meaning of "e" is "eXample".

Without the main function, our C program has no entrance, so before starting to compile this C code, I have to write a few simple assembly code, which is used as the entrance of our program.

; Entourt Start.asm [BITS 16] [Global Start] [EXTERN _E_MAIN] Start: Call_e_main

According to C language habits, the total nature of C must be automatically added to a "_" underline. So, the E_MAIN function we are in c, if you want to call in the assembly, turn it into a _e_main function. This paragraph The assembly code has only one sentence: call _e_main, is called our E_MAIN function in C

This code I will compile with NASM. Generate start.objnasmw -f obj -o start.obj start.asm

Let's compile this C code with TurboC C: Tcc -mt -oexample1.obj -c example1.clink start.obj eXample1.obj, example1.exe ,,, exe2bin example.exe, we got this C The machine code file (Example1.bin) compiled. Below we use the DEBUG for this old DOS tool to disassemble the example1.bin.

Debug-n example1.bin-l 0-u 0xxxx: 0000 call 0003xxxxx: 0003 MOV AX, 000BXXXX: 0006 PUSH AXXXX: 0007 CALL 0020xxxx: 000A POP CX

The code here is the code that the entire C program is generated. The first first sentence call 0003 is the code generated by the Start.asm compiled with NASM. Our main goal is to study the blue C The code of the language, the code generated by the first Start.ASM is too simple, that is, call the E_MAIN function. And our E_MAIN function is the blue code section.

From the C source program, we do it in e_main is a thing: call E_PUTCHAR (CH); where CH is passing the parameters of E_PUTCHAR.

The MOV AX, 000B 000B is the address of our overall variable CH. C language will pass all global variables in another memory area. C code first to AX, then pass the value of AX by PUSH AX, That is, the address of the CH is pressed into the stack. Then call 0020 and 0020 is the address of the E_PUTCHAR code. With this hop statement, the computer jumps to the E_PUTCHAR code section to execute. I don't give E_PUTCHAR code, because Our case is just how to transfer parameters in the C language to other functions, regardless of how e_putchar takes parameters. In one case, we will study how to take the function.

Here I have to explain the CALL instruction, because everyone can confuse this in the part of the next study function. Thecall xxxx instruction is simple or Push IPJMP XXXX it first presses the current execution address IP into the stack, then jump Go to the address to be called. Call and RET instructions are supported. RET instructions are equivalent to POP IP is also replying to the execution address IP before CALL. Because of this, once you use the CALL instruction, your stack pointer SP is It will automatically reduce 2.POP CX is an essential operation after each function call is completed. It is not a function here. Maybe the only role is to correspond to the PUSH AX before the call 0020. Such stack pointer SP can be back original.

Ok, simple first case studies are over. Although this 4 jump instructions, we can see how the C language passes the parameters method. Summary is through "MOV AX, Parameter Address" to pass the address of the parameters to AX, then "Push AX" press the address of the parameter into the stack. The last "Call function address" turns the function to be called. Finally, "POP CX", restore the stack pointer SP.

Research case two

Tools: TurboC C V2.0, DEBUG, MASM V5.0, NASM, TASM Instances C Program: / * Example1.c * / CHAR CH; Extern Void E_PUTCHAR (CHAR C); int E_MAIN () {

CH = 0x44; E_PUTCHAR (CH);

} Example assembler:; eio.asm_TEXT segment byte public 'CODE'DGROUP group _TEXT assume cs: _TEXT, ds: DGROUP, ss: DGROUP public _k_putchar_k_putchar proc near push bp mov bp, sp mov ah, 0eh mov bx, 7h mov al , byte PTR [BP 4] INT 10H POP BP RET_K_PUTCHAR ENDP

Target content: Method for functioning function using parameters in C language

In this section we will use TASM to write a standard C function with compilation. The content of this section may have seen in many compilation books. Talking about the connection method of the C language and assembly language. Maybe you will Weird, we already have MASM, NASM two compilers, why also use Tasm another assembly compiler. I don't know if MASM can cooperate with our TurboC C, but TASM is definitely available with TurboC C. Coordinated. After all, they are the products of Borland, and the assembly code generated in TurboC C is fully fixed according to the syntax in TASM. This is enough to see "intimate" between TurboC C and TASM.

In this case, we mainly do not study C code. Instead, study the C function written with compiled. Push BP MOV BP, SP MOV AH, 0EH MOV BX, 7H MOV Al, Byte PTR [BP 4] INT 10h Pop BP RET where Byte PTR [BP 4] is the parameter value we pass to E_PUTCHAR (). In the previous case, we have always known that the C language is pushing the address of the parameter into the stack to pass the function. So in the standard C In the function, it is to read the parameters by taking the value in the stack. The front two lines before the standard C function are PUSH BP MOV BP. The SP first saves the value of the BP, and then the current stack pointer is passed to the BP, and our access is delivered to The parameter of this function is through BP. The first parameter value is placed in the address of the BP 4, and the second parameter value is placed in BP 6, ..., this can be used to correspond to the address of each parameter. BP is the value of IP before call call. Because Call is executed, the system automatically presses the current IP into the stack. About this previous case has been introduced.

转载请注明原文地址:https://www.9cbs.com/read-3175.html

New Post(0)