Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Carter P.A.PC assembly language.2005.pdf
Скачиваний:
15
Добавлен:
23.08.2013
Размер:
1.07 Mб
Скачать

4.5. CALLING CONVENTIONS

75

72

73

74

75

pop

ebp

ret

; jump back to caller

76; subprogram print_sum

77; prints out the sum

78; Parameter:

79; sum to print out (at [ebp+8])

80; Note: destroys value of eax

81;

82segment .data

83 result db

"The sum is ", 0

84

85segment .text

86print_sum:

87

push

ebp

88

mov

ebp, esp

89

 

 

90

91

92

mov

eax, result

call

print_string

93

94

95

96

mov

eax, [ebp+8]

call

print_int

call

print_nl

97

98

pop

ebp

ret

 

sub3.asm

 

 

 

 

4.5.2Local variables on the stack

The stack can be used as a convenient location for local variables. This is exactly where C stores normal (or automatic in C lingo) variables. Using the stack for variables is important if one wishes subprograms to be reentrant. A reentrant subprogram will work if it is invoked at any place, including the subprogram itself. In other words, reentrant subprograms can be invoked recursively. Using the stack for variables also saves memory. Data not stored on the stack is using memory from the beginning of the program until the end of the program (C calls these types of variables global or static). Data stored on the stack only use memory when the subprogram they are defined for is active.

Local variables are stored right after the saved EBP value in the stack. They are allocated by subtracting the number of bytes required from ESP

 

 

76

 

 

 

CHAPTER 4. SUBPROGRAMS

 

 

 

 

 

 

1

subprogram_label:

 

 

2

 

push

 

 

ebp

; save original EBP value on stack

 

3

 

mov

 

 

ebp, esp

; new EBP = ESP

 

4

 

sub

 

 

esp, LOCAL_BYTES

; = # bytes needed by locals

 

5

; subprogram code

 

 

6

 

mov

 

 

esp, ebp

; deallocate locals

 

7

 

pop

 

 

ebp

; restore original EBP value

 

8

 

ret

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 4.6: General subprogram form with local variables

 

 

 

 

 

 

1

void calc

 

sum( int n, int sump )

 

2

{

 

 

 

 

 

 

3

int i , sum = 0;

 

 

 

4

 

 

 

 

 

 

 

5

for (

i=1; i <= n; i++ )

 

 

6sum += i;

7sump = sum;

8 }

Figure 4.7: C version of sum

Despite the fact that ENTER and LEAVE simplify the prologue and epilogue they are not used very often. Why? Because they are slower than the equivalent simplier instructions! This is an example of when one can not assume that a one instruction sequence is faster than a multiple instruction one.

in the prologue of the subprogram. Figure 4.6 shows the new subprogram skeleton. The EBP register is used to access local variables. Consider the

C function in Figure 4.7. Figure 4.8 shows how the equivalent subprogram could be written in assembly.

Figure 4.9 shows what the stack looks like after the prologue of the program in Figure 4.8. This section of the stack that contains the parameters, return information and local variable storage is called a stack frame. Every invocation of a C function creates a new stack frame on the stack.

The prologue and epilogue of a subprogram can be simplified by using two special instructions that are designed specifically for this purpose. The ENTER instruction performs the prologue code and the LEAVE performs the epilogue. The ENTER instruction takes two immediate operands. For the C calling convention, the second operand is always 0. The first operand is the number bytes needed by local variables. The LEAVE instruction has no operands. Figure 4.10 shows how these instructions are used. Note that the program skeleton (Figure 1.7) also uses ENTER and LEAVE.

Соседние файлы в предмете Электротехника