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

4.6. MULTI-MODULE PROGRAMS

77

1cal_sum:

2

push

ebp

 

3

mov

ebp, esp

 

4

sub

esp, 4

; make room for local sum

5

 

 

 

6

mov

dword [ebp - 4], 0

; sum = 0

7

mov

ebx, 1

; ebx (i) = 1

8for_loop:

9

cmp

ebx, [ebp+12]

; is i <= n?

10

jnle

end_for

 

11

 

 

 

12

add

[ebp-4], ebx

; sum += i

13

inc

ebx

 

14

jmp

short for_loop

 

15

 

 

 

16

end_for:

 

 

17

mov

ebx, [ebp+8]

; ebx = sump

18

mov

eax, [ebp-4]

; eax = sum

19

mov

[ebx], eax

; *sump = sum;

20

 

 

 

21

mov

esp, ebp

 

22

pop

ebp

 

23

ret

 

 

Figure 4.8: Assembly version of sum

4.6Multi-Module Programs

A multi-module program is one composed of more than one object file. All the programs presented here have been multi-module programs. They consisted of the C driver object file and the assembly object file (plus the C library object files). Recall that the linker combines the object files into a single executable program. The linker must match up references made to each label in one module (i.e. object file) to its definition in another module. In order for module A to use a label defined in module B, the extern directive must be used. After the extern directive comes a comma delimited list of labels. The directive tells the assembler to treat these labels as external to the module. That is, these are labels that can be used in this module, but are defined in another. The asm io.inc file defines the read int, etc. routines as external.

In assembly, labels can not be accessed externally by default. If a label

78

 

CHAPTER 4. SUBPROGRAMS

ESP + 16

EBP + 12

 

 

n

 

ESP + 12

EBP + 8

 

 

sump

 

ESP + 8

EBP + 4

Return address

 

ESP + 4

EBP

saved EBP

 

ESP

EBP - 4

sum

 

 

 

 

 

 

 

Figure 4.9:

 

 

 

1

subprogram_label:

 

2

enter LOCAL_BYTES, 0

; = # bytes needed by locals

3; subprogram code

4leave

5ret

Figure 4.10: General subprogram form with local variables using ENTER and

LEAVE

can be accessed from other modules than the one it is defined in, it must be declared global in its module. The global directive does this. Line 13 of the skeleton program listing in Figure 1.7 shows the asm main label being defined as global. Without this declaration, there would be a linker error. Why? Because the C code would not be able to refer to the internal asm main label.

Next is the code for the previous example, rewritten to use two modules.

The two subprograms (get int and print sum) are in a separate source file than the asm main routine.

main4.asm

1%include "asm_io.inc"

2

3segment .data

4 sum

dd 0

5

6segment .bss

7 input resd 1

8

9segment .text

10global _asm_main

11extern get_int, print_sum

12_asm_main:

13

enter

0,0

; setup routine

14

pusha

 

 

 

4.6. MULTI-MODULE PROGRAMS

79

15

 

 

 

 

 

16

mov

edx, 1

; edx is ’i’ in pseudo-code

 

17

while_loop:

 

 

 

 

18

push

edx

; save i on stack

 

19

push

dword input

; push address on input on stack

20

call

get_int

 

 

 

21

add

esp, 8

; remove i and &input from stack

22

 

 

 

 

 

23

mov

eax, [input]

 

 

 

24

cmp

eax, 0

 

 

 

25

je

end_while

 

 

 

26

 

 

 

 

 

27

add

[sum], eax

; sum += input

 

28

 

 

 

 

 

29

inc

edx

 

 

 

30

jmp

short while_loop

 

31

 

 

 

 

 

32

end_while:

 

 

 

 

33

push

dword [sum]

; push value of sum onto stack

 

34

call

print_sum

 

 

 

35

pop

ecx

; remove [sum] from stack

 

36

 

 

 

 

 

37

popa

 

 

 

 

38

leave

 

 

 

 

39

ret

 

main4.asm

 

 

 

 

 

 

 

 

 

 

 

 

sub4.asm

1%include "asm_io.inc"

2

3segment .data

4 prompt db

") Enter an integer number (0 to quit): ", 0

5

6segment .text

7 global get_int, print_sum

8get_int:

9

10

11

12

13

14

15

enter

0,0

mov

eax, [ebp + 12]

call

print_int

mov

eax, prompt

call

print_string

16

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