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

5.2. ARRAY/STRING INSTRUCTIONS

111

1

2

3

4

5

6

7

8

9

10

11

12

segment .text

 

 

cld

 

 

mov

esi, block1

; address of first block

mov

edi, block2

; address of second block

mov

ecx, size

; size of blocks in bytes

repe

cmpsb

; repeat while Z flag is set

je

equal

; if Z set, blocks equal

; code to perform if blocks are not equal

jmp

onward

 

equal:

 

 

; code to perform if equal onward:

Figure 5.14: Comparing memory blocks

5.2.5Example

This section contains an assembly source file with several functions that implement array operations using string instructions. Many of the functions duplicate familiar C library functions.

memory.asm

1global _asm_copy, _asm_find, _asm_strlen, _asm_strcpy

2

3segment .text

4; function _asm_copy

5; copies blocks of memory

6; C prototype

7; void asm_copy( void * dest, const void * src, unsigned sz);

8; parameters:

9; dest - pointer to buffer to copy to

10

;

src

-

pointer to buffer to copy from

11

;

sz

-

number of bytes to copy

12

13 ; next, some helpful symbols are defined

14

15%define dest [ebp+8]

16%define src [ebp+12]

17

%define sz

[ebp+16]

18

_asm_copy:

 

19

enter

0, 0

20

push

esi

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

112

 

CHAPTER 5. ARRAYS

push

edi

 

mov

esi, src

; esi = address of buffer to copy from

mov

edi, dest

; edi = address of buffer to copy to

mov

ecx, sz

; ecx = number of bytes to copy

cld

 

; clear direction flag

rep

movsb

; execute movsb ECX times

pop

edi

 

pop

esi

 

leave

 

 

ret

 

 

36; function _asm_find

37; searches memory for a given byte

38; void * asm_find( const void * src, char target, unsigned sz);

39; parameters:

40

;

src

- pointer to

buffer to

search

41

;

target

-

byte value

to search

for

42

;

sz

-

number of bytes in buffer

43; return value:

44; if target is found, pointer to first occurrence of target in buffer

45; is returned

46 ; else

47 ; NULL is returned

48; NOTE: target is a byte value, but is pushed on stack as a dword value.

49; The byte value is stored in the lower 8-bits.

50;

51

%define src

[ebp+8]

 

52

%define target [ebp+12]

 

53

%define sz

[ebp+16]

 

54

 

 

 

55

_asm_find:

 

 

56

enter

0,0

 

57

push

edi

 

58

 

 

 

59

mov

eax, target

; al has value to search for

60

mov

edi, src

 

61

mov

ecx, sz

 

62

cld

 

 

5.2. ARRAY/STRING INSTRUCTIONS

113

63

64

65

repne scasb

; scan until ECX == 0 or [ES:EDI] == AL

66

je

found_it

; if zero flag set, then found value

67

mov

eax, 0

; if not found, return NULL pointer

68

jmp

short quit

 

69

found_it:

 

 

70

mov

eax, edi

 

71

dec

eax

; if found return (DI - 1)

72

quit:

 

 

73

74

75

76

77

pop edi leave

ret

78; function _asm_strlen

79; returns the size of a string

80; unsigned asm_strlen( const char * );

81; parameter:

82; src - pointer to string

83; return value:

84; number of chars in string (not counting, ending 0) (in EAX)

85

86%define src [ebp + 8]

87_asm_strlen:

88

enter

0,0

 

89

push

edi

 

90

 

 

 

91

mov

edi, src

; edi = pointer to string

92

mov

ecx, 0FFFFFFFFh ; use largest possible ECX

93

xor

al,al

; al = 0

94

cld

 

 

95

 

 

 

96

repnz

scasb

; scan for terminating 0

97

98;

99; repnz will go one step too far, so length is FFFFFFFE - ECX,

100; not FFFFFFFF - ECX

101;

102

103

mov eax,0FFFFFFFEh

sub

eax, ecx

; length = 0FFFFFFFEh - ecx

104

105

106

107

108

114

CHAPTER 5. ARRAYS

pop edi leave

ret

109; function _asm_strcpy

110; copies a string

111; void asm_strcpy( char * dest, const char * src);

112; parameters:

113; dest - pointer to string to copy to

114; src - pointer to string to copy from

115;

116%define dest [ebp + 8]

117%define src [ebp + 12]

118_asm_strcpy:

119

enter

0,0

120

push

esi

121

push

edi

122

 

 

123

mov

edi, dest

124

mov

esi, src

125cld

126cpy_loop:

127

 

 

lodsb

 

 

 

 

 

; load AL & inc si

128

 

 

stosb

 

 

 

 

 

; store AL & inc di

129

 

 

or

al, al

; set condition flags

130

 

 

jnz

cpy_loop

; if not past terminating 0, continue

131

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

132

 

 

pop

edi

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

133

 

 

pop

esi

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

134

 

 

leave

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

135

 

 

ret

 

 

 

memory.asm

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

memex.c

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1

#include <stdio.h>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3

#define STR

 

SIZE 30

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4

/ prototypes

 

 

/

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

6

void asm

 

copy( void , const void , unsigned )

 

 

 

attribute

 

 

 

((cdecl ));

 

 

 

 

 

7

void asm

 

find( const void ,

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

8

 

 

 

 

 

 

 

 

char target ,

unsigned )

 

 

 

attribute

 

 

 

((cdecl ));

 

5.2. ARRAY/STRING INSTRUCTIONS

115

9

unsigned asm

 

strlen( const char )

 

 

 

attribute

 

 

 

((cdecl ));

 

 

 

 

 

10

void asm

 

strcpy( char , const char )

 

 

 

attribute

 

 

 

((cdecl ));

11

12int main()

13{

14char st1 [STR SIZE] = ”test string”;

15char st2 [STR SIZE];

16char st ;

17 char ch;

18

 

 

 

 

 

19

asm

 

copy(st2, st1 , STR

 

SIZE); / copy all 30 chars of string /

 

 

20

printf (”%s\n”, st2);

21

22

23

24

25

26

27

28

printf (”Enter a char : ” ); / look for byte in string / scanf(”%c% [ˆ\n]”, &ch);

st = asm find(st2 , ch, STR SIZE); if ( st )

printf (”Found it: %s\n”, st ); else

printf (”Not found\n”);

29

 

 

 

 

 

30

st1 [0] = 0;

 

 

31

printf (”Enter string :”);

 

 

32

scanf(”%s”, st1);

 

 

33

printf (”len = %u\n”, asm

 

strlen(st1 ));

34

 

 

 

 

 

35

asm

 

strcpy( st2 , st1 );

 

/ copy meaningful data in string /

36

printf (”%s\n”, st2 );

 

 

37

38return 0;

39}

memex.c

116

CHAPTER 5. ARRAYS

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