[ Pobierz całość w formacie PDF ]

0 and ZF = 0, branches
The last two loop instructions are useful for sequential search loops. The
following pseudo-code:
42 CHAPTER 2. BASIC ASSEMBLY LANGUAGE
sum = 0;
for ( i=10; i >0; i-- )
sum += i;
could be translated into assembly as:
1 mov eax, 0 ; eax is sum
2 mov ecx, 10 ; ecx is i
3 loop_start:
4 add eax, ecx
5 loop loop_start
2.3 Translating Standard Control Structures
This section looks at how the standard control structures of high level
languages can be implemented in assembly language.
2.3.1 If statements
The following pseudo-code:
if ( condition )
then block ;
else
else block ;
could be implemented as:
1 ; code to set FLAGS
2 jxx else_block ; select xx so that branches if condition false
3 ; code for then block
4 jmp endif
5 else_block:
6 ; code for else block
7 endif:
If there is no else, then theelseblockbranch can be replaced by a
branch toendif.
1 ; code to set FLAGS
2 jxx endif ; select xx so that branches if condition false
3 ; code for then block
4 endif:
2.4. EXAMPLE: FINDING PRIME NUMBERS 43
2.3.2 While loops
The while loop is a top tested loop:
while( condition ) {
body of loop;
}
This could be translated into:
1 while:
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
6 endwhile:
2.3.3 Do while loops
The do while loop is a bottom tested loop:
do {
body of loop;
} while( condition );
This could be translated into:
1 do:
2 ; body of loop
3 ; code to set FLAGS based on condition
4 jxx do ; select xx so that branches if true
2.4 Example: 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:
3
2 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 :  );
6 scanf( %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
11 /" look for a factor of guess "/
12 factor = 3;
13 while ( factor " factor
14 guess % factor != 0 )
15 factor += 2;
16 if ( guess % factor != 0 )
17 printf ( %d\n , guess);
18 guess += 2; /" only look at odd numbers "/
19 }
Figure 2.3:
prime.asm
1 %include "asm_io.inc"
2 segment .data
3 Message db "Find primes up to: ", 0
4
5 segment .bss
6 Limit resd 1 ; find primes up to this limit
7 Guess resd 1 ; the current guess for prime
8
9 segment .text
10 global _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
2.4. EXAMPLE: FINDING PRIME NUMBERS 45
20 mov eax, 2 ; printf("2\n");
21 call print_int
22 call print_nl
23 mov eax, 3 ; printf("3\n");
24 call print_int
25 call print_nl
26
27 mov dword [Guess], 5 ; Guess = 5;
28 while_limit: ; while ( Guess
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
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 popa
59 mov eax, 0 ; return back to C
60 leave
61 ret
prime.asm
46 CHAPTER 2. BASIC ASSEMBLY LANGUAGE
Chapter 3
Bit Operations
3.1 Shift Operations
Assembly language allows the programmer to manipulate the individual
bits of data. One common bit operation is called a shift. A shift operation
moves the position of the bits of some data. Shifts can be either toward
the left (i.e. toward the most significant bits) or toward the right (the least
significant bits).
3.1.1 Logical shifts [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • skydive.htw.pl
  • Copyright © 2016 Moje życie zaczęło siÄ™ w dniu, gdy ciÄ™ spotkaÅ‚em.
    Design: Solitaire