80386ASM programming foundation (six)

zhaozj2021-02-16  47

Advanced Language Support, Condition Byte Settings Directive AA. Advanced Language Support Instruction, starting at 80186, mainly to simplify certain features of advanced languages, totaling 3 instructions: Enter, Leave, Bound a.enter, Leave, establish With the release stack frame command. In the C language, the stack is not only used to transfer the entry parameters to the function, but also stored in the stack in the local variable inside the function. In order to accurately access these local variables and accurately obtain the entry parameters, you need to establish a stack framework, first look at a small program: // c programming-language int sum (int x, int y) {int sum; sum = x Y; Return Sum;} // The Corresponding ASM CODES LISTS BELOW _SUM PROC NEAR; Note The Function of the Function Parameters in the C language is from the right direction left, that is, the parameter y is the stack, and then x, then the stack, then The return address of the function is incorporated into the stack PUSH BP MOV BP, SP; establishes a stack frame SUB SP, 2 MOV AX, WORD PTR [BP 4]; Take the parameter x add ax, Word PTR [BP 6]; plus parameters y MOV WORD PTR [BP-2], AX MOV AX, WORD PTR [BP-2] MOV SP, BP; Release Stack Framework POP BP RET _SUM ENDP This hopgram is: | ---------- ------------ | | BP | <==== SP | ---------------------- | | Function Return Address | <==== BP 2 | ---------------------- | | | Parameter x | <==== BP 4 | ---- ------------------ | | Parameters Y | <==== BP 6 | ------------------ ---- | | ...... | <==== BP 8 | ---------------------- | | .... .... | <==== BP N, N is a number of renewed numbers | ---------------------- | And release the stack frame instruction, then the corresponding assembler should be: _Sum Proc Near Enter 2,0; build a stack frame MOV AX, Word PTR [BP 4]; Take the parameter x add ax, Word PTR [BP 6]; Add parameters y MoV Word PTR [BP-2], AX Mo v AX, Word PTR [BP-2] Leave; Release Stack Frame RET _SUM ENDP B. Building a stack frame instruction ENTER, format as follows: Enter CNT1, CNT2. Where CNT1 represents the size of the frame, that is, the number of bytes in the stack is placed in the stack; CNT2 is an immediate number, indicating the subroutine nested level, that is, the number of pointers that are copied from the calling frame to the current frame.

When the number CNT2 is 0, the process of ENTER instructions is: push bp sp => bp sp <= SP-CNT1 C. Release Stack Frame Instruction Leave, the specific implementation process: 8086: bp => sp pop bp 80386: EBP => ESP POP EBP D.Enter and Leave instructions do not affect the flags in the flag register, and the Leave instruction is only responsible for the release of the stack framework, and is not responsible for the function returns. Therefore, arrange a return instruction after the Leave instruction.

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

New Post(0)