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

2.4. EXAMPLE: FINDING PRIME NUMBERS

43

2.3.2While loops

The while loop is a top tested loop:

while( condition ) { body of loop;

}

This could be translated into:

1while:

2; code to set FLAGS based on condition

3

jxx

endwhile

; select xx so that branches if false

4; body of loop

5

jmp

while

6endwhile:

2.3.3Do while loops

The do while loop is a bottom tested loop:

do {

body of loop;

} while( condition );

This could be translated into:

1do:

2

3

4

;body of loop

;code to set FLAGS based on condition

jxx

do

; select xx so that branches if true

2.4Example: Finding Prime Numbers

This section looks at a program that finds prime numbers. Recall that prime numbers are evenly divisible by only 1 and themselves. There is no formula for doing this. The basic method this program uses is to find the factors of all odd numbers3 below a given limit. If no factor can be found for an odd number, it is prime. Figure 2.3 shows the basic algorithm written in C.

Here’s the assembly version:

32 is the only even prime number.

 

44

 

CHAPTER 2. BASIC ASSEMBLY LANGUAGE

 

 

 

 

1

unsigned guess;

/ current guess for prime

/

2

unsigned factor ;

/

possible factor of guess

/

3

unsigned limit ;

/

find primes up to this value /

4

5 printf (”Find primes up to : ”);

6scanf(”%u”, &limit);

7

printf (”2\n”);

/ treat

first two primes as

/

8

printf (”3\n”);

/ special

case

/

9

guess = 5;

/

initial

guess /

 

10

while ( guess <= limit ) {

 

 

11

/ look for a

factor

of guess /

 

12

factor = 3;

 

 

 

 

13

while ( factor factor < guess &&

 

14

guess % factor != 0 )

 

15factor += 2;

16if ( guess % factor != 0 )

17printf (”%d\n”, guess);

18guess += 2; / only look at odd numbers /

19}

Figure 2.3:

prime.asm

1%include "asm_io.inc"

2segment .data

3 Message

db

"Find primes up to: ", 0

4

5segment .bss

6

Limit

resd

1

;

find primes

up to

this limit

7

Guess

resd

1

;

the current

guess

for prime

8

9segment .text

10global _asm_main

11_asm_main:

12

enter

0,0

; setup routine

13

pusha

 

 

14

 

 

 

15

mov

eax, Message

 

16

call

print_string

 

17

call

read_int

; scanf("%u", & limit );

18

mov

[Limit], eax

 

19

20

21

22

23

24

25

26

2.4. EXAMPLE: FINDING PRIME NUMBERS

45

mov

eax, 2

; printf("2\n");

 

call

print_int

 

 

call

print_nl

 

 

mov

eax, 3

; printf("3\n");

 

call

print_int

 

 

call

print_nl

 

 

27

mov

dword [Guess], 5

; Guess = 5;

28

while_limit:

 

; while ( Guess <= Limit )

29

mov

eax,[Guess]

 

30

cmp

eax, [Limit]

 

31

jnbe

end_while_limit

; use jnbe since numbers are unsigned

32

 

 

 

33

mov

ebx, 3

; ebx is factor = 3;

34

while_factor:

 

 

35

mov

eax,ebx

 

36

mul

eax

; edx:eax = eax*eax

37

jo

end_while_factor

; if answer won’t fit in eax alone

38

cmp

eax, [Guess]

 

39

jnb

end_while_factor

; if !(factor*factor < guess)

40

mov

eax,[Guess]

 

41

mov

edx,0

 

42

div

ebx

; edx = edx:eax % ebx

43

cmp

edx, 0

 

44

je

end_while_factor

; if !(guess % factor != 0)

45

 

 

 

46

add

ebx,2

; factor += 2;

47

jmp

while_factor

 

48

end_while_factor:

 

49

je

end_if

; if !(guess % factor != 0)

50

mov

eax,[Guess]

; printf("%u\n")

51

call

print_int

 

52

call

print_nl

 

53

end_if:

 

 

54

add

dword [Guess], 2

; guess += 2

55

jmp

while_limit

 

56

end_while_limit:

 

 

57

58

59

60

61

popa

 

 

 

mov

eax, 0

; return back to C

leave

 

 

 

ret

 

prime.asm

 

 

 

 

 

 

 

46

CHAPTER 2. BASIC ASSEMBLY LANGUAGE

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