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

1

2

3

4

5

6

7

8

5.1. INTRODUCTION

99

 

 

 

mov

ebx, array1

; ebx = address of array1

mov

dx, 0

; dx will hold sum

mov

ecx, 5

 

lp:

 

 

add

dl, [ebx]

; dl += *ebx

adc

dh, 0

; dh += carry flag + 0

inc

ebx

; bx++

loop

lp

 

 

 

 

Figure 5.5: Summing elements of an array (Version 3)

constant is a 32-bit constant. The constant can be a label (or a label expression).

5.1.4Example

Here is an example that uses an array and passes it to a function. It uses the array1c.c program (listed below) as a driver, not the driver.c program.

array1.asm

1%define ARRAY_SIZE 100

2%define NEW_LINE 10

3

4segment .data

5

FirstMsg

db

"First 10

elements of array", 0

6

Prompt

db

"Enter index of element to display: ", 0

7

SecondMsg

db

"Element %d is %d", NEW_LINE, 0

8

ThirdMsg

db

"Elements

20 through 29 of array", 0

9

InputFormat

db

"%d", 0

 

10

 

 

 

 

11

segment .bss

 

 

 

12

array

resd

ARRAY_SIZE

 

13

14segment .text

15extern _puts, _printf, _scanf, _dump_line

16global _asm_main

17_asm_main:

18

enter

4,0

; local dword variable at EBP - 4

19

push

ebx

 

20

push

esi

 

21

100

CHAPTER 5. ARRAYS

22 ; initialize array to 100, 99, 98, 97, ...

23

 

 

 

24

mov

ecx, ARRAY_SIZE

 

25

mov

ebx, array

 

26

init_loop:

 

 

27

mov

[ebx], ecx

 

28

add

ebx, 4

 

29

loop

init_loop

 

30

 

 

 

31

push

dword FirstMsg

; print out FirstMsg

32

call

_puts

 

33

pop

ecx

 

34

 

 

 

35

push

dword 10

 

36

push

dword array

 

37

call

_print_array

; print first 10 elements of array

38

add

esp, 8

 

39

40; prompt user for element index

41Prompt_loop:

42

push

dword Prompt

 

43

call

_printf

 

44

pop

ecx

 

45

 

 

 

46

lea

eax, [ebp-4]

; eax = address of local dword

47

push

eax

 

48

push

dword InputFormat

 

49

call

_scanf

 

50

add

esp, 8

 

51

cmp

eax, 1

; eax = return value of scanf

52

je

InputOK

 

53

 

 

 

54

55

56

call

_dump_line

; dump rest of line

and start over

jmp

Prompt_loop

; if input

invalid

57 InputOK:

58

59

60

61

62

63

mov

esi, [ebp-4]

 

push

dword [array + 4*esi]

 

push

esi

 

push

dword SecondMsg

; print out value of element

call

_printf

 

add

esp, 12

 

5.1. INTRODUCTION

101

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

push

dword ThirdMsg

; print out elements 20-29

call

_puts

 

pop

ecx

 

push

dword 10

 

push

dword array + 20*4

; address of array[20]

call

_print_array

 

add

esp, 8

 

pop

esi

 

pop

ebx

 

mov

eax, 0

; return back to C

leave

 

 

ret

 

 

80;

81; routine _print_array

82; C-callable routine that prints out elements of a double word array as

83; signed integers.

84; C prototype:

85; void print_array( const int * a, int n);

86; Parameters:

87; a - pointer to array to print out (at ebp+8 on stack)

88; n - number of integers to print out (at ebp+12 on stack)

89

 

 

90

segment .data

 

91

OutputFormat

db "%-5d %5d", NEW_LINE, 0

92

93segment .text

94global _print_array

95_print_array:

96

enter

0,0

 

97

push

esi

 

98

push

ebx

 

99

 

 

 

100

xor

esi, esi

; esi = 0

101

mov

ecx, [ebp+12]

; ecx = n

102

mov

ebx, [ebp+8]

; ebx = address of array

103

print_loop:

 

 

104

push

ecx

; printf might change ecx!

105

106

107

108

109

110

111

112

113

114

115

102

 

CHAPTER 5. ARRAYS

push

dword [ebx + 4*esi]

; push array[esi]

push

esi

 

push

dword OutputFormat

 

call

_printf

 

add

esp, 12

; remove parameters (leave ecx!)

inc

esi

 

pop

ecx

 

loop

print_loop

 

116

117

118

119

 

pop

ebx

 

pop

esi

 

leave

 

 

 

 

 

 

ret

 

array1.asm

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

array1c.c

 

1 #include <stdio.h>

2

3 int asm main( void );

4 void dump line( void );

5

6 int main()

7 {

8int ret status ;

9 ret status = asm main();

10return ret status ;

11}

12

13/

14function dump line

15 dumps all chars left in current line from input bu er

16/

17void dump line()

18{

19int ch;

20

21while( (ch = getchar()) != EOF && ch != ’\n’)

22/ null body / ;

23}

array1c.c

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