You are on page 1of 168

PC

: Paul A. Carter
:

2006 7 23

Copyright 2001, 2002, 2003, 2004 by Paul Carter


is book is translated into Korean by Lee Jae Beom
(, )
.
(fair use) .
,
.
,
.
http://kevin0960.tistory.com
kev0960@gmail.com .
.

1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1.1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1.1.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1.1.3 16

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1.2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1.2.2 CPU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1.2.3 CPU 80x86 . . . . . . . . . . . . . . . . . . . . . . . . . . .

1.2.4 8086 16 . . . . . . . . . . . . . . . . . . . . . . . . .

1.2.5 80386 32 . . . . . . . . . . . . . . . . . . . . . . . .

1.2.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1.2.7 16 . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1.2.8 32 . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1.2.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

10

1.3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

10

1.3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

10

1.3.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

11

1.3.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

11

1.3.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

12

1.3.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

14

1.3.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

15

1.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

16

1.4.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

16

1.4.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

19

1.4.3 . . . . . . . . . . . . . . . . . . . . . . . . . . .

20

1.4.4 C . . . . . . . . . . . . . . . . . . . . . . . . . .

20

1.4.5 . . . . . . . . . . . . . . . . . . . . . . . . . . .

20

1.4.6 . . . . . . . . . . . . . . . . . . . . . . .

21

1.5

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

22

ii

24

2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

24

2.1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

24

2.1.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

26

2.1.3 2 . . . . . . . . . . . . . . . . . . . . . . . . . .

29

2.1.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

30

2.1.5 . . . . . . . . . . . . . . . . . . . . . . . . .

32

2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

33

2.2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

33

2.2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

34

2.2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

36

2.3 . . . . . . . . . . . . . . . . . . . . . . . . .

37

2.3.1 If . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

37

2.3.2 While

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

38

2.3.3 Do while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

38

2.4 : . . . . . . . . . . . . . . . . . . . . . . . . . . .

38

41

3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

41

3.1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

41

3.1.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

42

3.1.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

42

3.1.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

42

3.1.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

43

3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

43

3.2.1 AND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

43

3.2.2 OR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

44

3.2.3 XOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

44

3.2.4 NOT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

45

3.2.5 TEST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

45

3.2.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . .

45

3.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . .

47

3.4 C . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

49

3.4.1 C . . . . . . . . . . . . . . . . . . . . . . . . . .

49

3.4.2 C . . . . . . . . . . . . . . . . . . . . . .

49

3.5 , . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

50

3.5.1 ? . . . . . . . . . . .

51

3.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

52

3.6.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

53

3.6.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

54

3.6.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

54

4.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

57
57

iii
4.2 . . . . . . . . . . . . . . . . . . . . . . . . . . .

58

4.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

60

4.4 CALL RET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

60

4.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

61

4.5.1 . . . . . . . . . . . . . . . . . . . . . . .

62

4.5.2 . . . . . . . . . . . . . . . . . . . . . . . . . .

66

4.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

68

4.7 C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

70

4.7.1 . . . . . . . . . . . . . . . . . . . . . . . . . . .

71

4.7.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

72

4.7.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

72

4.7.4 . . . . . . . . . . . . . . . . . . . . . . .

72

4.7.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

73

4.7.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

73

4.7.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

74

4.7.8 C . . . . . . . . . . . . . . . . . . .

77

4.8 . . . . . . . . . . . . . . . . . . . . . . . . .

78

4.8.1 . . . . . . . . . . . . . . . . . . . . . . . . . . .

78

4.8.2 C . . . . . . . . . . . . . . . . . . . . . . .

80

82

5.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

82

5.1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

82

5.1.2 . . . . . . . . . . . . . . . . . . . . . . . .

83

5.1.3 . . . . . . . . . . . . . . . . . . . . . . .

85

5.1.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

85

5.1.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

89

5.2 / . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

92

5.2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . .

93

5.2.2 REP . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

94

5.2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . .

94

5.2.4 REPx . . . . . . . . . . . . . . . . . . . . . . . . . . . .

94

5.2.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

95

101

6.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
6.1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
6.1.2 IEEE . . . . . . . . . . . . . . . . . . . . . . . . . 103
6.2 . . . . . . . . . . . . . . . . . . . . . . . . . 106
6.2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
6.2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
6.2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
6.2.4 . . . . . . . . . . . . . . . . . . . . . . . . 107
6.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108

iv

6.3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
6.3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
6.3.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
6.3.4 2 . . . . . . . . . . . . . . . . . . . . . . . . . 114
6.3.5 . . . . . . . . . . . . . . . . . . . . . . . . 116
6.3.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
7

C++

123

7.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
7.1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
7.1.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
7.1.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
7.1.4 . . . . . . . . . . . . . . . . . . . . . 128
7.2 C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
7.2.1 (Name Mangling) . . . . . . . . . . . . . . . 129
7.2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
7.2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
7.2.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
7.2.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
7.2.6 C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
7

80x86

147

A - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Index

154

154



.
C C++
.
. PC
1981 PC 8086
. 8086 .

. .
( ) 80386
.
.
. .
1. 8086
2. PC .
3. .
PC
.
/ . ,
NASM DJGPP C/C++ .
. NASM
Windows C/C++
.
http://www.drpaulcarter.com/pcasm .

.

.
.

vi


/
. , .
John S. Fine, Simon Tatham, Julian Hall, NASM
. DJGPP C/C++ DJ
Delorie DJGPP GNU gcc
, TEX LATEX 2 Donald Knuth,
Richard Stallman Linus Torvalds,
.
.

John S. Fine
Marcelo Henrique Pinto de Almeida
Sam Hopkins
Nick DImperio
Jeremiah Lawrence
Ed Beroset
Jerry Gembarowski
Ziqiang Peng
Eno Compton
Josh I Cates
Mik Miin
Luke Wallis
Gaku Ueda
Brian Heward
Chad Gorshing
F. Gotti
Bob Wilkinson
Markus Koegel
Louis Taber
Dave Kiddell
Eduardo Horowitz
Sbastien Le Ray
Nehal Mistry
Jianyue Wang
Jeremias Kleer
Marc Janicki
Trevor Hansen
Giacomo Bruschi
Leonardo Rodrguez Mjica
Ulrich Bicheler
Wu Xing

vii

http://www.drpaulcarter.com/

NASM SourceForge

http://sourceforge.net/projects/nasm/

DJGPP

http://www.delorie.com/djgpp

http://www.linuxassembly.org/

http://webster.cs.ucr.edu/

comp.lang.asm.x86

http://developer.intel.com/design/
Pentium4/documentation.htm

.
E-mail:

pacman128@gmail.com

WWW:

http://www.drpaulcarter.com/pcasm


1.1

. 10
(decimal) .
(binary) . ,
.

1.1.1

10 (0-9) .
10 .

234 = 2 102 + 3 101 + 4 100

1.1.2

2 (0,1) 2
. 2
.(, (bit) )

110012 = 1 24 + 1 23 + 0 22 + 0 21 + 1 20
= 16 + 8 + 1
= 25
. 1.1
.
1.1 (i.e., (bits)) . ,
.

110112
+100012
1011002

0
1
2
3
4
5
6
7

0000
0001
0010
0011
0100
0101
0110
0111

8
9
10
11
12
13
14
15

1000
1001
1010
1011
1100
1101
1110
1111

1.1: 0 15
(carry)
0
0
1
1
+0
+1
+0
+1
0
1
1
0
c

0
+0
1


0
1
1
+1
+0
+1
0
0
1
c
c
c

1.1: (c (carry) )

1234 10 = 123 r 4

. 2
. .
1)

11012 102 = 1102 r 1


.
1.2 .
(least signi cant bit, lsb) .
(most signi cant bit, msb) .
(byte) 8 .

1.1.3 16
16 16 . 16
. 16 16 6
. 9 A,
B, C, D, E, F . 16 A 10 10
B 11, F 15 . 16 16

1) 2

25 2 = 12 r
12 2 = 6 r
62=3r
32=1r
12=0r

1 11001 10 = 1100 r 1
0 1100 10 = 110 r 0
0
110 10 = 11 r 0
1
11 10 = 1 r 1
1
1 10 = 0 r 1

2510 = 110012
1.2:

589 16 = 36 r 13
36 16 = 2 r 4
2 16 = 0 r 2

589 = 24D16
1.3:

2BD16 = 2 162 + 11 161 + 13 160


= 512 + 176 + 13
= 701

16
. , 2
16 16 . 1.3
.
16 2 16 .
16
.
16 2 16 4
. 24D16 0010 0100 11012 .
16 4 ( 16
) 4
416 = 1002 100 .
16 4
16 .

(word)
(double word)
(quad word)
(paragraph)

2
4
8
16

1.2:

.2)

110
6

0000
0

0101
5

1010
A

0111
7

11102
E16

4 (nibble) , 16 1
. 2 1 , 1 2
16 . 1 0
11111111 , 16 0 FF, 0 255
.

1.2

1.2.1

. 32

( 210 =

3200 .

1, 024 ),

1.4 (adress)

( 220 = 1, 048, 576 )

( 230 =
1, 073, 741, 824 ).

0
2A

1
45

2
B8

3
20

4
8F

5
CD

6
12

7
2E

1.4:

.
PC 1.2 .
. (character code)
.
(ASCII, American Standard Code for Information Interchange) .
(Unicode) .

1 2 ( ) .
A 4116 (6510 )
004116 . 1
256 3)

2) 16

3) 7 128 .

.
.

1.2.2

CPU

(CPU) . CPU
. CPU
. CPU
. CPU

. CPU CPU (Machine language)
.
. . CPU
.
.
CPU
.
(compiler) . CPU
. Mac IBM PC .
(clock) .

GHz

. 1.5GHz 10
1.5Ghz . 4)

. 1.5 GHz

CPU

CPU 15

( ) CPU
.

1.2.3

CPU 80x86

IBM PC 80x86 CPU . CPU


.
.
8088,8086: CPU PC . AX, BX, CX,
DX, SI, DI, BP, SP, CS, DS, SS, ES, IP, FLAGS 16
. 1 MB
. .
.
.
(segments)
64KB .
80286: CPU AT PC . CPU
8088/86 . CPU
4) CPU
.

AX
AH AL
1.5: AX

16 (16-bits protected mode) . 16


MB
. 64 KB
.
80386: CPU 80286 CPU . , 16
32 (EAX, EBX, ECX, EDX, ESI, EDI, EBP, ESP, EIP)
16 FS GS . CPU
32 (32-bit protected mode) .
4 GB .
4 GB .
80486// : CPU CPU .
.
MMX: MMX( , MultiMedia eXtensions)
.
.
II: MMX . (
III II .)

1.2.4 8086 16
8086 CPU 4 16 AX, BX,
CX, DX . 2 8
. AX AH, AL 1.5
. AH AX 8 , AL AX 8
. AH AL 1 .
AX . AX
AH AL AH AL AX .
.
CPU 2 SI DI 16 .
.
2 8 .
16 BP SP
. 2 .
.
16 CS, DS, SS, ES (segment register)
. . CS
(Code Segment), DS (Data Segment), SS

(Stack Segment), ES (Extra Segment) . ES


.
.
(IP) CS CPU
. CPU IP
.
(FLAG)
.
0 Z 1 0 0 .

.

1.2.5

80386 32

80386 .
16 AX 32 . AX
16 EAX 32
. AL AX 8 AX EAX 16
. , EAX 16 .
EBX, ECX, EDX, ESI, EDI .
. BP EBP, SP ESP, FLAGS
EFLAGS, IP EIP . 32
.
80386 16 . 2
FS GS .
ES .
(word) CPU . 80x86
. 1.2 2 ( 16
) . 8086 .
80386
.

1.2.6

1 MB (220 ) . 00000

DOS 640KB

FFFFF . 20

8086 16

BIOS

1MB

2 16 . 16

(selector) . .
16 (oset) . 32
:

16 selector + oset

. 16 16 0
. 047C:0048

047C0
+0048
04808
. 16 (
) . ( 1.2 )
:
64 KB .
64KB
. 64KB
64KB (
) CS
. DS
. !

. 04808 047C:0048, 047D:0038, 047E:0028, 047B:0058
. .

1.2.7 16
80286 16
. .
(descriptor table) (index) .
.

.
. !
(virtual memory) .

.
. 16

.
.


. , ,
(e.g., ).
.

16 16 .
64KB .
.

PC
286 CPU

1.2.8

32

80386 32 . 386 32 286 16


2
1. 32 . 40
. , 4GB .
2. 4KB (page)
. .
.
286 16 ,
2 .
32 .
3.x (standard mode) 286 16
(enhanced mode) 32 . 9X,
NT/2000/XP, OS/2, 32
.

1.2.9

(event)
. (interrupt)
. ,
( )
. (interrupt handler)
. .
.
(interrupt vector table) .
.
CPU (External inturrpt) .
. (I/O) ,
, , , CD-ROM,
. CPU
. (trap) .
(soware interrupt)
. DOS API(Application Programming Interface)
. (, ) C
(interface) . 5)

5) .

10


.
.
. (, CPU )
.

1.3

1.3.1
CPU .
.
(operation code) opcode . 80x86
.
(e.g., )
.
. , EAX EBX
EAX
.
03 C3
. (assembler)
.

1.3.2
.
.
.
add eax, ebx
.
add
(mnemonic) .
mnemonic operand(s)
(assembler)
. (Compiler)

.
CPU
.

11

(porting)
.
Netwide Assembler NASM
. . ( )
MASM TASM . NASM
MASM/TASM .

1.3.3

.
( 0 - 3) .
.
: CPU .
: . ,
, .
.
: (immediate) .
(, )
: (implied) .
1 . 1
.

1.3.4

MOV .
. ( )

mov dest, src
src dest . , .
.
. .
AX (16 ) BL (8) .
. ( ):
mov

eax, 3

; eax 3 ( 3 )

mov

bx, ax

; ax bx .

ADD .
add

eax, 4

; eax = eax + 4

add

al, ah

; al = al + ah

SUB .

12

sub

bx, 10

; bx = bx - 10

sub

ebx, edi ; ebx = ebx - edi

INC DEC 1 . 1
INC DEC ADD SUB
.
inc

ecx

; ecx++

dec

dl

; dl--

1.3.5
(directive) CPU .

. . :





.
NASM C . NASM
C # % .
equ
equ (symbol) .
.
symbol equ value
.
e %
C #define . C

%define SIZE 100
mov

eax, SIZE

SIZE MOV
. .
.

13

Unit

10

Letter
B
W
D
Q
T

1.3: RESX DX


.
2 .
. .
RESX . X
. 1.3 .
( ) DX . X RESX
.
(label) .
. :
L1

db

; L1 0

L2

dw

1000

; L2 1000

L3

db

110101b

; L3 110101( 53)

L4

db

12h

; L4 16 12

L5

db

17o

; L5 8 17

L6

dd

1A92h

; L6 16 1A92

L7

resb

; L7 1

L8

db

"A"

; L8 A(65)

.
. , L2
L1 . .
L9

db

0, 1, 2, 3

; 4

L10

db

"w", "o", "r", 'd', 0

; C "word"

L11

db

'word', 0

; L10

DD 6) .
DQ 2 .
NASM TIMES .
.
L12

times 100 db 0

L13

resw

100

6) C float

; 100 (db 0)
; 100 .

14

.
2 . (,
) . ([])
. (pointer)
C . (MASM/TASM
) 32 32 . :
1

mov

al, [L1]

; AL L1 .

mov

eax, L1

; EAX = L1

mov

[L1], ah

; L1 ah

mov

eax, [L6]

; L6 EAX

add

eax, [L6]

; EAX = EAX + L6

add

[L6], eax

; L6 += EAX

mov

al, [L6]

; L6 AL

7 NASM .
.
.
C .
.
C .
.
mov

[L6], 1

; L6 1

. ?
1 , ,
. .
mov

dword [L6], 1

; L6 1

1 L6
. BYTE, WORD, QWORD, TWORD7) .

1.3.6
(I/O) .
. C I/O
. C . ,
(
) .
C .
C . , C
.
( ).
7) TWORD 10 .

15

print_int
print_char
print_string

print_nl
read_int
read_char

EAX .
AL

EAX
. C (i.e. null
).
.
EAX
.

EAX .
1.4: I/O

C
. 1.4 .
.
.
NASM %include .
I/O 8) NASM .
%include "asm_io.inc"
I/O EAX
CALL . CALL
.
() .
I/O .

1.3.7

.

. CPU .
asm_io.inc .
. (,) .
4 dump_regs, dump_mem,
dump_stack, dump_math . , , ,

dump_regs (stdout) 16
. 9) .
1 ZF . 0 .
dump_regs
.
8) asm_io.inc ( asm_io.inc asm_io )
http://www.drpaulcarter.com/pcasm
9) Chapter 2

16

dump_mem 16
. 3 .
dump_regs ,
, 16 .
.
dump_stack CPU . (
Chapter 4 )
. 3
dump_regs ,
EBP , EBP
.
dump_math .
dump_regs .

1.4


. . ,
.
. ,
.
?
1.
.
2.
.
3. .
4. C
.

.
.

1.4.1
1.6 C
. asm_main .
. C . C
. C
.

17

1
2
3
4
5
6

int main()
{
int ret_status ;
ret_status = asm_main();
return ret_status ;
}
1.6: driver.c

. C
. I/O . C I/O (
printf ) .
first.asm

; file: first.asm

; . 2

; .

; DJGPP :

; nasm -f coff first.asm

; gcc -o first first.o driver.c asm_io.o

8
9

%include "asm_io.inc"

10

11

; .data .

12
13

segment .data

14

15

; .

16

17

prompt1 db

"Enter a number: ", 0

18

prompt2 db

"Enter another number: ", 0

19

outmsg1 db

"You entered ", 0

20

outmsg2 db

" and ", 0

21

outmsg3 db

", the sum of these is ", 0

; !

22
23

24

; .bss .

25

26

segment .bss

27

28

; .

29

input1

resd 1

30

input2

resd 1

31

18

32

33

; .text .

34

35

segment .text
global

36
37

_asm_main

_asm_main:

38

enter

39

pusha

0,0

; (set up)

; prompt

40
41

mov

eax, prompt1

42

call

print_string

44

call

read_int

; .

45

mov

[input1], eax

; input1 .

47

mov

eax, prompt2

; prompt

48

call

print_string

50

call

read_int

; .

51

mov

[input2], eax

; input2 .

53

mov

eax, [input1]

; eax = input1 dword

54

add

eax, [input2]

; eax += input2 dword

55

mov

ebx, eax

; ebx = eax

43

46

49

52

56
57

dump_regs 1

58

dump_mem

2, outmsg1, 1

59

60

; .

61

;
;

62

mov

eax, outmsg1

63

call

print_string

64

mov

eax, [input1]

65

call

print_int

66

mov

eax, outmsg2

67

call

print_string

68

mov

eax, [input2]

69

call

print_int

70

mov

eax, outmsg3

71

call

print_string

72

mov

eax, ebx

73

call

print_int

; (ebx)

74

call

print_nl

75

;
; input1
;
; input2
;

19

76

popa

77

mov

78

leave

79

ret

eax, 0

; C .

first.asm

13 (.data)
. . 17 21
. C
(NULL) ( 0). 0 '0'
.
bss (.bss) .
(block
started by symbol). . .
.text . .
( 38 ) _ (under score) .
C (C calling convention) . C
. C
. . ,
C (i.e., ) C _
. ( DOS/Windows C
C )
37 global _asm_main
. C (internal scope) .
. (global)
(external scope) .
. asm_io
print_int . first.asm
.

1.4.2

GNU10) DJGPP C/C++


11) .
386 PC , DOS Windows 95/98, NT .
COFF(Common Object File Forma) (Object File)
. nasm -f coff
. ( ) o .
C GNU .
37 38 _ .
ELF( , Excutable and Linkable Format) .
-f elf . o .

10) (Free Soware Foundation)


11) http://www.delorie.com/djgpp

20

C/C++ .
OMF . -f obj .
OMF . 13
. obj .
segment _DATA public align=4 class=DATA use32
26 bss :
segment _BSS public align=4 class=BSS use32
36 .
segment _TEXT public align=1 class=CODE use32
36 .
group DGROUP _BSS _DATA
C/C++ OMF Win32
. (OMF Win32
) Win32 DJGPP .
-f win32 . obj .

1.4.3
.
nasm -f object-format first.asm
, object-format co , elf , obj, win32 C
. (
)

1.4.4

DJGPP :
gcc -c driver.c
-c . . ,
.

1.4.5

. .
C C (startup code) .
C
. DJGPP ,
gcc -o first driver.o first.o asm_io.o

21

first.exe ( first) .

bcc32 first.obj driver.obj asm_io.obj
.
. , first.exe .
.
gcc -o first driver.c first.o asm_io.o
gcc driver.c .

1.4.6

-l listing-file nasm
. . 17 18
( ) . (
)
48 00000000 456E7465722061206E-

prompt1 db

"Enter a number: ", 0

prompt2 db

"Enter another number: ", 0

49 00000009 756D6265723A2000
50 00000011 456E74657220616E6F51 0000001A 74686572206E756D6252 00000023 65723A2000
16
. 16 .
. .
.
( )
. ( 1.4.5)
.
.
54 56
.
94 0000002C A1[00000000]

mov

eax, [input1]

95 00000031 0305[04000000]

add

eax, [input2]

96 00000037 89C3

mov

ebx, eax

3 .
. 94 input1 (
) . mov
(Opcode) ( A1 )
. bss
0 . bss
.

22

. , 96
.
.
,
95
. input2 4 ( ) .
, 00000004 04000000 !
.
2 . (Big endian) (Little endian)
. .
(i.e. ) .
dword 00000004 4 00 00 00 04 . IBM ,
RISC , .
.
. 00000004 04 00 00 00 .
CPU .
.

1. ( ).
2. (multibyte)
( )
.
. ( )
. .

1.5

1.7 (Skeleton le) .


.

1
2
3
4
5

skel.asm
%include "asm_io.inc"
segment .data
;
; .
;

6
7
8
9
10

segment .bss
;
; bss .
;

11
12
13
14
15
16

segment .text
global
_asm_main:
enter
pusha

_asm_main
0,0

17
18
19
20
21

;
; .
;
;

22
23
24
25
26

popa
mov
leave
ret

eax, 0

; C .
skel.asm
1.7:

23

24


2.1

2.1.1
: (unsigned)
(signed) . (, ) .
200 1 , 11001000 ( 16 C8) .
( )
. , 56 +56 00111000 .
56 111000
? ?
3 .
(sign bit) .
0, 1 .

, (signed magnitude)
. ,
() . 56 00111000
( ) 56 110111000 .
01111111, +127 11111111 127
. .
. 0 ,
0 . +0(00000000) 0(10000000) .
CPU .
10 56 10 56
. CPU
.
1
1 (ones complement) . 1
(1 0, 0 1) . (

25

(1 ) .) 00111000 (+56) 1
11000111 . 1 , 1
- . 11000111 56 . 1
. 0
00000000 (+0) 11111111 (0) . 16
F (
15) . +56 16 38 1
F C7 . .
2
.
2 . 2
:
1. 1 .
2. 1 .
00111000(56) . , 56 1 : 11000111.
1 :

11000111
+
1
11001000
2 2 - .
11001000 56 2 .
2 .
11001000 2 .

00110111
+
1
00111000
2 1 , (carry)
. . ( )
. . (
, ) 2
. 1 0 . (00000000). 2

11111111
+
1
c 00000000
, c (carry) . ( ,
.) 0
2 . 2
.

26

0
1
127
-128
-127
-2
-1

16 n
00
01
7F
80
81
FE
FF

2.1: 2
2 128 +127
. 2.1 2 . 16
32, 768 +32, 767 . +32, 767
7FFF , 32, 768 8000 , -128 FF80, -1 FFFF .
32 20 +20 .
CPU ( )
. (type) .
. 16
FF 1 ( ) +255 (
) . C (signed)
(unsigned) . C
.

2.1.2
.
. .

.
.
mov

ax, 0034h

; ax = 52 (16 )

mov

cl, al

; cl = ax 8

,
. AX 0134h ( 10 308,
h 16 (hex) .)
CL 34h .
. AX FFFFh ( 1) CL
FFh ( 1) . , AX
.

0 . 0
1 . ,

27

. .
.

. 1
FF . ,
? FF . FF
( 255), , 00FF . ,
FF ( 1) FFFF .
0 . FF
00FF . ,
. . , FF
1 1
FFFF . 5A ( 90) 005A
.
80386 .
, .
.
MOV 0 .
AL AX :
mov

ah, 0

; 8 0

MOV AX EAX
. , EAX 16 MOV
. 80386 MOVZX . 2
. (, destination)
, 16 32 . (,
source) 8 16 ,
.
. (
) :
movzx

eax, ax

; ax eax

movzx

eax, al

; al eax

movzx

ax, al

; al ax

movzx

ebx, ax

; ax ebx

MOV . 8086
. CBW ( )
AL AX . , . CWD (
) AX DX:AX . , DX:AX DX AX
32 . 16
DX , 16 AX . (8086 32
!) 80386 . CWDE

28
1
2
3
4

unsigned char uchar = 0xFF;


signed char schar = 0xFF;
int a = ( int ) uchar;
/* a = 255 (0x000000FF) */
int b = ( int ) schar ;
/* b = 1 (0xFFFFFFFF) */
2.1:
char ch;
while( (ch = fgetc (fp )) != EOF ) {
/* ch */
}
2.2:

( ) AX EAX . CDQ EAX


EDX:EAX ( 64 !) . MOVSX
MOVZX .
C
ANSI C char

C / . C /

. (int ). 2.1 . 3

a (MOVZX )

. 2.1

4 b .

(MOVSX )
C . 2.2 .
fgetc() :
int fgetc( FILE * );
. int
. char . (, int 0
) char EOF .
1 . , fgetc() char int
( 000000xx 16 ) EOF ( 16
FFFFFFFF ) .
2.2 fgetc() int char
. C char int
. (16 ) 000000FF FFFFFFFF FF .
while FF ,
.
char , .
2 ch EOF . EOF int
1) ch int 2)

1) EOF .
2)

29

2.1 , .
char FF 000000FF . EOF (FFFFFFFF)
. , !
char FF FFFFFFFF . EOF
. , FF
. , .
ch char int .
2 .
ch .

2.1.3

add sub .
2
(over ow) (carry ag) .
, (set) .
,
/ (MSB, Most Signi cant Bit) .
. 2
. add sub
/ .

002C
+ FFFF
002B

44
+ (1)
43

.
2 . , MUL
IMUL . MUL
IMUL . ,
2 .
, FF . ,

255 255 65025 ( 16 FE01) .


1 1 1 ( 16 0001) .
. .
mul

source

source . (immediate value)


. (source)
. AL
16 AX . 16 AX 32
DX:AX . 32 EAX
64 EDX:EAX .
IMUL MUL . 2
3 :

30

destination

reg16
reg32
reg16
reg32
reg16
reg32
reg16
reg32
reg16
reg32

source1
reg/mem8
reg/mem16
reg/mem32
reg/mem16
reg/mem32
immed8
immed8
immed16
immed32
reg/mem16
reg/mem32
reg/mem16
reg/mem32

source2

immed8
immed8
immed16
immed32

AX = AL*source1
DX:AX = AX*source1
EDX:EAX = EAX*source1
dest *= source1
dest *= source1
dest *= immed8
dest *= immed8
dest *= immed16
dest *= immed32
dest = source1*source2
dest = source1*source2
dest = source1*source2
dest = source1*source2

2.2: imul

imul

dest, source1

imul

dest, source1, source2

2.2 .
DIV IDIV 2 . /
. :
div

source

8 AX . AL
AH . 16 DX:AX
AX , DX . 32 EDX:EAX
EAX , EDX . IDIV .
IDIV IMUL .
, 0 .
DX EDX .
NEG 2
. 8, 16, 32
.

2.1.4
math.asm

%include "asm_io.inc"

segment .data

prompt

db

"Enter a number: ", 0

square_msg

db

"Square of input is ", 0

cube_msg

db

"Cube of input is ", 0

cube25_msg

db

"Cube of input times 25 is ", 0

quot_msg

db

"Quotient of cube/100 is ", 0

rem_msg

db

"Remainder of cube/100 is ", 0

neg_msg

db

"The negation of the remainder is ", 0

31

10
11

segment .bss

12

input

resd 1

13
14
15
16

segment .text
global

_asm_main

_asm_main:

17

enter

18

pusha

0,0

19
20

mov

eax, prompt

21

call

print_string

23

call

read_int

24

mov

[input], eax

26

imul

eax

; edx:eax = eax * eax

27

mov

ebx, eax

; ebx

28

mov

eax, square_msg

29

call

print_string

30

mov

eax, ebx

31

call

print_int

32

call

print_nl

34

mov

ebx, eax

35

imul

ebx, [input]

36

mov

eax, cube_msg

37

call

print_string

38

mov

eax, ebx

39

call

print_int

40

call

print_nl

42

imul

ecx, ebx, 25

43

mov

eax, cube25_msg

44

call

print_string

45

mov

eax, ecx

46

call

print_int

47

call

print_nl

49

mov

eax, ebx

50

cdq

51

mov

ecx, 100

; .

52

idiv

ecx

; edx:eax / ecx

53

mov

ecx, eax

; ecx

22

25

33

; ebx *= [input]

41

; ecx = ebx*25

48

; edx

32

54

mov

eax, quot_msg

55

call

print_string

56

mov

eax, ecx

57

call

print_int

58

call

print_nl

59

mov

eax, rem_msg

60

call

print_string

61

mov

eax, edx

62

call

print_int

63

call

print_nl

65

neg

edx

66

mov

eax, neg_msg

67

call

print_string

68

mov

eax, edx

69

call

print_int

70

call

print_nl

64

71
72

popa

73

mov

74

leave

75

ret

eax, 0

; C

math.asm

2.1.5
/ .
. ADD SUB
/ .
( )
/ .
ADC SBB . ADC
:
operand1 = operand1 + carry flag + operand2
SBB :
operand1 = operand1 - carry flag - operand2
? EDX:EAX EBX:ECX 64
. EDX:EAX .
1

add

eax, ecx

; 32

adc

edx, ebx

; 32 .

. EBX:ECX EDX:EAX .

33

sub

eax, ecx

; 32 .

sbb

edx, ebx

; 32 .

, . ( 2.2 ) ,
ADC . ADD
CLC ( , Clear Carry)
0 . 0
ADD ADC .
.

2.2

(e.g. if
while ) . .
goto .
.

. ( )

2.2.1

.
. 80x86
CMP . CMP
.
.
CMP SUB .
2 ( )
: (ZF) (CF) . 0
3) . .
.
cmp

vleft, vright

vleft - vright . CMP


0 vleft = vright, ZF (i.e. 1) CF (
) . vleft > vright , ZF CF
( ) . vleft < vright ZF , CF
( ) .
, 3 : (ZF) ,
(OF) , (SF) .

vleft

( (under ow) ) .

SF = OF ?

. vleft = vright ZF . (

>

vright

.
3) ) 1 . 0

SF = OF = 0 .

.
( .)
, SF = OF = 1 .

34

) vleft > vright ZF SF = OF


. vleft < viright ZF SF 6= OF .
CMP
.

2.2.2
(Branch instruction)
. , goto . 2 :
. goto
.
.
.
JMP (jump ) .
(code label) . ,
.
. JMP
.
.
SHORT . 128
.
. (displacement) 1
. . (EIP
) short SHORT JMP
.
NEAR / ,
. , 80386 near .
2 . 32,000 ,
. 4
. 386
4 . 2
JMP WORD .
FAR . 386
.
.
(label) .
(:) .
. .

. 2.3 . (PF (parity ag)
8 .)

35

JZ
JNZ
JO
JNO
JS
JNS
JC
JNC
JP
JNP

ZF
ZF
OF
OF
SF
SF
CF
CF
PF
PF

2.3:

(pseudo code,
) :
if ( EAX == 0 )
EBX = 1;
else
EBX = 2;
.
1

cmp

eax, 0

; (eax - 0 = 0 ZF )

jz

thenblock

; ZF thenblock .

mov

ebx, 2

; if else

jmp

next

; if then .

ebx, 1

; if then .

thenblock:
mov

6
7

next:
2.3 .

if ( EAX >= 5 )
EBX = 1;
else
EBX = 2;
EAX 5 , ZF SF OF .
. ( , EAX
)

cmp

eax, 5

js

signon

; SF 1 signon

jo

elseblock

; OF = 1, SF = 0 elseblock

jmp

thenblock

; SF = 0, OF = 0 thenblock

signon:

36

JE
JNE
JL, JNGE
JLE, JNG
JG, JNLE
JGE, JNL


vleft = vright
vleft 6= vright
vleft < vright
vleft vright
vleft > vright
vleft vright

JE
JNE
JB, JNAE
JBE, JNA
JA, JNBE
JAE, JNB


vleft = vright
vleft 6= vright
vleft < vright
vleft vright
vleft > vright
vleft vright

2.4: /
jo

6
7

elseblock:

mov

ebx, 2

jmp

next

10

thenblock:
mov

11
12

; SF = 1, OF = 1 thenblock

thenblock

ebx, 1

next:
. , 80x86
. /
. 2.4 . /
(JE, JNE) / . (, JE JNE JZ JNZ
) 2 . JL
( , jump less than) JNGE( , jump not greater
than or equal to) .

x < y = not(x y)
A(above), B(below)
G L .

.
1

cmp

eax, 5

jge

thenblock

mov

ebx, 2

jmp

next

thenblock:
mov

6
7

ebx, 1

next:

2.2.3
80x86 for .
.
LOOP ECX , ECX 6= 0 .

37

LOOPE, LOOPZ ECX ( )


ECX 6= 0 ZF = 1 .
LOOPNE, LOOPNZ ECX (
) ECX 6= 0 ZF = 0, .
.
.
sum = 0;
for( i =10; i >0; i )
sum += i ;
:
1

mov

eax, 0

; eax

mov

ecx, 10

; ecx i

loop_start:

add

eax, ecx

loop

loop_start

2.3

2.3.1

If

:
if ( condition )
then_block;
else
else_block ;
:
1

; code to set FLAGS

jxx

; then block

jmp

5
6
7

else_block

; xx .

endif

else_block:
; else block
endif:
else else_block endif .

; code to set FLAGS

jxx

; then block

endif:

endif

; xx .

38

2.3.2

While

while :
while( condition ) {
body of loop;
}
:
1

while:

; code to set FLAGS based on condition

jxx

; body of loop

jmp

endwhile:

2.3.3

; xx

endwhile
while

Do while

do while :
do {
body of loop;
} while( condition );
:
1

do:

; body of loop

; .

jxx

2.4

; xx

do

. 1
. .

4) . 2.3
C .
:

%include "asm_io.inc"

segment .data

Message

db

4
5

segment .bss
4) 2

prime.asm

"Find primes up to: ", 0

: 39

1
2
3

unsigned guess;
unsigned factor;
unsigned limit ;

/* */
/* guess */
/* */

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

printf (Find primes up to : );


scanf(%u, & limit );
printf (2\n );
/* */
printf (3\n );
/*
*/
guess = 5;
/*guess */
while ( guess <= limit ) {
/* guess */
factor = 3;
while ( factor * factor < guess &&
guess % factor != 0 )
factor += 2;
if ( guess % factor != 0 )
printf (%d\n, guess);
guess += 2;
/* */
}
2.3:

Limit

resd

; .

Guess

resd

8
9
10
11

segment .text
global

_asm_main

_asm_main:

12

enter

13

pusha

0,0

14
15

mov

eax, Message

16

call

print_string

17

call

read_int

18

mov

[Limit], eax

20

mov

eax, 2

21

call

print_int

22

call

print_nl

23

mov

eax, 3

24

call

print_int

25

call

print_nl

mov

dword [Guess], 5

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

19

; printf("2\n");

; printf("3\n");

26
27
28

; Guess = 5;
; while ( Guess <= Limit )

while_limit:

29

mov

eax,[Guess]

30

cmp

eax, [Limit]

40

31

jnbe

end_while_limit

; jnbe

mov

ebx, 3

; ebx factor = 3;

32
33
34

while_factor:

35

mov

eax,ebx

36

mul

eax

; edx:eax = eax*eax

37

jo

end_while_factor

; .

38

cmp

eax, [Guess]

39

jnb

end_while_factor

40

mov

eax,[Guess]

41

mov

edx,0

42

div

ebx

43

cmp

edx, 0

44

je

end_while_factor

; if !(guess % factor != 0)

46

add

ebx,2

; factor += 2;

47

jmp

while_factor

; if !(factor*factor < guess)

; edx = edx:eax % ebx

45

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

54

add

dword [Guess], 2

55

jmp

while_limit

53

56

end_if:
; guess += 2

end_while_limit:

57
58

popa

59

mov

60

leave

61

ret

eax, 0

; return back to C

prime.asm

41


3.1

.
(shi) .
. (i.e ) (
) .

3.1.1

.
. 3.1 .


1
1
0

1
1
1

1
0
1

0
1
1

1
0
0

0
1
1

1
0
0

0
0
1

3.1:
0 . SHL SHR
, .
. , CL
. .
.
1

mov

ax, 0C123H

shl

ax, 1

; 1 ,

shr

ax, 1

; 1 ,

ax = 4123H, CF = 0

shr

ax, 1

; 1 ,

ax = 2091H, CF = 1

mov

ax, 0C123H

shl

ax, 2

mov

cl, 3

shr

ax, cl

; 2 ,

ax = 8246H, CF = 1

ax = 048CH, CF = 1

; 3 , ax = 0091H, CF = 1

42

3.1.2
.
10 .
. 2 .

10112 ( 11) 2 101102


( 22) . 2
. 2 ; 4 (22 )
, ; 8 (23 ) 3
. MUL DIV .
, .
. 2 FFFF (1)
7FFF +32, 767 .
.

3.1.3
2
. .
SAL - SHL . SHL
. , .
SAR - (i.e. )
.
. (,
1 1 ) ,
7 .
.
1

mov

ax, 0C123H

sal

ax, 1

; ax = 8246H, CF = 1

sal

ax, 1

; ax = 048CH, CF = 1

sar

ax, 2

; ax = 0123H, CF = 0

3.1.4

. ,
. ROL ROR
.
.
1

mov

ax, 0C123H

rol

ax, 1

; ax = 8247H, CF = 1

rol

ax, 1

; ax = 048FH, CF = 1

43

rol

ax, 1

; ax = 091EH, CF = 0

ror

ax, 2

; ax = 8247H, CF = 1

ror

ax, 1

; ax = C123H, CF = 1

,
RCL RCR . AX AX
17 .
ax, 0C123H

mov

clc

rcl

ax, 1

; ax = 8246H, CF = 1

rcl

ax, 1

; ax = 048DH, CF = 1

rcl

ax, 1

; ax = 091BH, CF = 0

rcr

ax, 2

; ax = 8246H, CF = 1

rcr

ax, 1

; ax = C123H, CF = 0

3.1.5

; clear the carry flag (CF = 0)

EAX (i.e. 1 )
.

mov

bl, 0

; bl

mov

ecx, 32

; ecx

count_loop:

shl

eax, 1

; .

jnc

skip_inc

; CF == 0 skip_inc

inc

bl

skip_inc:
loop

count_loop

EAX . (EAX 0
) EAX 4 rol eax, 1
.

3.2

4 (Boolean) : AND, OR, XOR, NOT .



.

3.2.1

AND

1 AND 1 .
3.1 0 .
.
AL BL AND , AND

44

X AND Y

0
0
1
1

0
1
0
1

0
0
0
1

3.1: AND

AND

1
1
1

0
1
0

1
0
0

0
0
0

1
1
1

0
0
0

1
0
0

0
1
0

3.2: AND
X

X OR Y

0
0
1
1

0
1
0
1

0
1
1
1

3.2: OR
8 . 3.2
. .
1

mov

ax, 0C123H

and

ax, 82F6H

3.2.2

; ax = 8022H

OR

0 (inclusive) OR 1) 0 .
1 3.2 .
:
1

mov

ax, 0C123H

or

ax, 0E831H

3.2.3

; ax = E933H

XOR

OR 0 .
1 3.3 . :
1) ( ) OR

X XOR Y

0
0
1
1

0
1
0
1

0
1
1
0

3.3: XOR

45

NOT X

0
1

1
0

3.4: NOT

2i OR . ( i (1
) .)
i (0 ) AND
. (mask)
.
2i XOR .

i
i

3.5:

mov

ax, 0C123H

xor

ax, 0E831H

3.2.4

; ax = 2912H

NOT

NOT (unary) . (i.e


. AND ) NOT
3.4 .
:
1

mov

ax, 0C123H

not

ax

; ax = 3EDCH

NOT 1 . (Bitwise operation) NOT FLAGS .

3.2.5

TEST

TEST AND .
FLAGS . ( CMP
FLAGS ) ,
0 ZF .

3.2.6


. 3.5 3 .
.
1

mov

ax, 0C123H

or

ax, 8

; 3 ,

and

ax, 0FFDFH

; 5 ,

xor

ax, 8000H

; 31 ,

or

ax, 0F00H

; ,

ax = C12BH
ax = C10BH

ax = 410BH

ax = 4F0BH

46
1
2
3
4
5
6

mov
mov
count_loop:
shl
adc
loop

bl, 0
ecx, 32

; bl 1 .
; ecx .

eax, 1
bl, 0
count_loop

;
; bl .

3.3: ADC

and

ax, 0FFF0H

; , ax = 4F00H

xor

ax, 0F00FH

; ,

xor

ax, 0FFFFH

; 1 ,

ax = BF0FH

ax = 40F0H

AND 2 . 2i
2i 1 AND .

i 1 1 . . AND
0 . 100 16
.
1

mov

eax, 100

; 100 = 64H

mov

ebx, 0000000FH

; mask = 16 - 1 = 15 or F

and

ebx, eax

; ebx = remainder = 4

CL .
EAX (1 ) . BH
.
; OR

mov

cl, bh

mov

ebx, 1

shl

ebx, cl

; CL

or

eax, ebx

(o, 0 ) .
;AND

mov

cl, bh

mov

ebx, 1

shl

ebx, cl

; cl

not

ebx

and

eax, ebx

.
80x86 :
xor

eax, eax

; eax = 0

XOR 0 . MOV
.

47

3.3


. (speculative execution) . CPU
.
.
.
. .
.
.
3.1.5 .
EAX . INC
. 3.3 ADC
.
SETxx .
0 1
. SET .
SETxx 1, 0 .
setz

; Z 1, 0 al

al


.
.
CMP
.
.

; file: max.asm

%include "asm_io.inc"

segment .data

4
5

message1 db "Enter a number: ",0

message2 db "Enter another number: ", 0

message3 db "The larger number is: ", 0

8
9

segment .bss

10
11

input1

12

segment .text

13
14

resd
global

_asm_main

_asm_main:

15

enter

16

pusha

17

0,0

48

18

mov

eax, message1

19

call

print_string

20

call

read_int

21

mov

[input1], eax

23

mov

eax, message2

24

call

print_string

25

call

read_int

; eax

27

xor

ebx, ebx

; ebx = 0

28

cmp

eax, [input1]

29

setg

bl

; ebx = (input2 > input1) ?

30

neg

ebx

; ebx = (input2 > input1) ? 0xFFFFFFFF : 0

31

mov

ecx, ebx

; ecx = (input2 > input1) ? 0xFFFFFFFF : 0

32

and

ecx, eax

; ecx = (input2 > input1) ?

33

not

ebx

; ebx = (input2 > input1) ?

0 : 0xFFFFFFF

34

and

ebx, [input1]

; ebx = (input2 > input1) ?

0 : input1

35

or

ecx, ebx

; ecx = (input2 > input1) ?

input2 : input1

37

mov

eax, message3

38

call

print_string

39

mov

eax, ecx

40

call

print_int

41

call

print_nl

22

26

1 : 0

input2 : 0

36

42
43

popa

44

mov

45

leave

46

ret

eax, 0

; C


. 30 SETG 1,
0 BL . .
31 EBX NEG
. ( EBX 0 ) EBX 0
. EBX 1 -1 2 ,
0xFFFFFFFF . .
.
DEC . NEG DEC
0 0xFFFFFFFF . NEG
.

C 49

S_IRUSR
S_IWUSR
S_IXUSR
S_IRGRP
S_IWGRP
S_IXGRP
S_IROTH
S_IWOTH
S_IXOTH







(other)

3.6: POSIX

3.4

3.4.1

C . AND
& 2) . OR | . XOR
^ . NOT ~ .
C << >> . << ,
>> . 2 .
.
.
( int) .
.
/* short int 16 */

short int s ;

short unsigned u;

s = 1;

/* s = 0xFFFF (2 ) */

u = 100;

/* u = 0x0064 */

u = u | 0x0100;

/* u = 0x0164 */

s = s & 0xFFF0;

/* s = 0xFFF0 */

s = s ^ u;

/* s = 0xFE94 */

u = u << 3;

/* u = 0x0B20 ( ) */

s = s >> 2;

/* s = 0xFFA5 ( ) */

3.4.2

C
. . ,
C x*=2 .
API3) ( POSIX 4) Win32 .)

2) & & & !


3) (Application Programming Interface)
4) (Portable Operating System Interface for Computer Environment)
, UNIX IEEE

50

(encode) .
POSIX 3 :
(user) ( (owner)), (group), (others) .
, , .
C . POSIX
( 3.6 ) chmod
. ,
, (Integer) 5)
. ,
, .
chmod(foo, S_IRUSR | S_IWUSR | S_IRGRP );
POSIX stat .
chmod
. , .
.
/* stat() */

struct stat

le_stats ;

stat (foo , & le_stats );

le_stats.st_mode . */

3
4

/*

chmod(foo, ( le_stats . st_mode & S_IWOTH) | S_IRUSR);

3.5

,
1 ,

. ,
.
( )
.
. . ,
. (
) . x86 .
1234567816 .
12 34 56 78 . 78 56 34 12
.
.
?
? CPU
. (
) , .
CPU . CPU
.
( ) CPU .
5) , int typedef mode_t

, 51

unsigned short word = 0x1234; /* sizeof(short) == 2 */


unsigned char * p = (unsigned char *) &word;
if ( p[0] == 0x12 )
printf (Big Endian Machine\n);
else
printf ( Little Endian Machine\n);
3.4: ?

2 AX . AH AL
. CPU AH AL . CPU
. , AH AL
. AX
mov AL AH CPU
.
. CPU
. , CPU
CPU
( ) .
3.4 CPU . p word
. p[0] word
. CPU
.

3.5.1

CPU
.
.
( ) . .

UNICODE

TCP/IP ( (network
byte order) ) . TCP/IP C

. htonl ()

. UNICODE

( long integer) (host) (network)


. ntohl () . 6)
, .

.
W. Richard Steven UNIX Network Programming
.
6) , : ,
.
.

52
1
2
3
4
5

unsigned invert_endian( unsigned x )


{
unsigned invert;
const unsigned char * xp = (const unsigned char *) &x;
unsigned char * ip = (unsigned char *) & invert ;

ip[0]
ip[1]
ip[2]
ip[3]

7
8
9
10

=
=
=
=

/* */

xp[3];
xp[2];
xp[1];
xp[0];

11

13

/* */

return invert ;

12

}
3.5: _

1
2
3

int count_bits( unsigned int data )


{
int cnt = 0;

while( data != 0 ) {
data = data & (data 1);
cnt++;
}
return cnt ;

5
6
7
8
9
10

}
3.6: :
3.5 C . 486
32 BSWAP
.
bswap

edx

; edx

16 . XCHG 8
16
.
xchg

3.6

ah,al

; ax


.
.

53

static unsigned char byte_bit_count[256]; /* */

2
3
4
5

void initialize_count_bits ()
{
int cnt , i , data;

for( i = 0; i < 256; i ++ ) {


cnt = 0;
data = i ;
while( data != 0 ) {
/* */
data = data & (data 1);
cnt++;
}
byte_bit_count[i ] = cnt ;
}

7
8
9
10
11
12
13
14
15
16

17
18
19
20

int count_bits( unsigned int data )


{
const unsigned char * byte = ( unsigned char *) & data;

21

return byte_bit_count[byte[0]] + byte_bit_count[byte[1]] +


byte_bit_count[byte[2]] + byte_bit_count[byte [3]];

22
23
24

}
3.7:

3.6.1

. 3.6 .
, data .
(i.e. data 0 ) . data 0
data .
6 data . ? data
1 . , 1
0 . data - 1 ?
1
.
data
= xxxxx10000
data - 1

xxxxx01111

, x . data data - 1 AND


1 0
.

54

3.6.2
(lookup table)
7)

. , 2 . ,
40 . ,
. (, 40

)

. 4
. 3.7
.
initialize_count_bits count_bits
. byte_bit_count
. count_bits data 4
. dword 4 .
dword[0] data ( ,
) .

(data >> 24) & 0x000000FF
, .
, .
for 22 23
. for ,
, . 4
. , for 4
.
(loop unrolling) .

3.6.3
.
1 0 . 1
. data 1 1 .
.
data = (data & 0x55) + ((data >> 1) & 0x55);
? 16 0x55 01010101 .
, data 0x55 AND .
((data >> 1) & 0x55)
. ,
7) ( )
. sine .

55

1
2
3
4
5
6
7
8
9

int count_bits(unsigned int x )


{
static unsigned int mask[] = { 0x55555555,
0x33333333,
0x0F0F0F0F,
0x00FF00FF,
0x0000FFFF };
int i ;
int shift ;
/* */

10

for( i =0, shift =1; i < 5; i ++, shift *= 2 )


x = (x & mask[i]) + ( (x >> shift ) & mask[i] );
return x ;

11
12
13
14

}
3.8:

data , .
data
. data 101100112
data & 010101012
+ (data >> 1) & 010101012

or

00

01

00

01

01

01

00

01

01 10 00 10
.
4 2 4
. 2 2
.
, . ,
.
:
data = (data & 0x33) + ((data >> 2) & 0x33);
, (data 011000102 )
data & 001100112
+ (data >> 2) & 001100112

or

0010

0010

0001

0000

0011

0010

, 2 4 .
2 .
data = (data & 0x0F) + ((data >> 4) & 0x0F);
data (001100102 )
data & 000011112
00000010
+ (data >> 4) & 000011112

or

00000011
00000101

56

data 5 . 3.8
. for .
. ,
.

57

(subprogram) (modular
program) C .
.

. ,
(calling convention) .
C (interface) , C
. (
) (i.e. )
.

4.1

.
([]) .
1

mov

ax, [Data]

mov

ebx, Data

; ebx = & Data

mov

ax, [ebx]

; ax = *ebx

AX 3 EBX
. AX AL
. C (type)
. EBX
. EBX . EBX
. ,
.
.
32 (EAX, EBX, EDX, EDX) (ESI, EDI)
. 16 8
,

58

4.2

.
, C .
.

.
JMP .
. (,
C (function pointer) ) 1
.
sub1.asm

; : sub1.asm

%include "asm_io.inc"

4
5

segment .data

prompt1 db

"Enter a number: ", 0

prompt2 db

"Enter another number: ", 0

outmsg1 db

"You entered ", 0

outmsg2 db

" and ", 0

10

outmsg3 db

", the sum of these is ", 0

11
12

segment .bss

13

input1

resd 1

14

input2

resd 1

15
16

segment .text
global

17
18

_asm_main

_asm_main:

19

enter

20

pusha

0,0

; prompt

21
22

mov

eax, prompt1

23

call

print_string

25

mov

ebx, input1

; input1 ebx

26

mov

ecx, ret1

; ecx

27

jmp

short get_int

29

mov

eax, prompt2

; prompt

30

call

print_string

mov

ebx, input2

24

28

ret1:

31
32

59

33

mov

ecx, $ + 7

; ecx = + 7

34

jmp

short get_int

36

mov

eax, [input1]

; eax = input1 dword

37

add

eax, [input2]

; eax += input2 dword

38

mov

ebx, eax

; ebx = eax

40

mov

eax, outmsg1

41

call

print_string

42

mov

eax, [input1]

43

call

print_int

44

mov

eax, outmsg2

45

call

print_string

46

mov

eax, [input2]

47

call

print_int

48

mov

eax, outmsg3

49

call

print_string

50

mov

eax, ebx

51

call

print_int

; (ebx)

52

call

print_nl

eax, 0

; C

35

39

;
; input1
;
; input2
;

53
54

popa

55

mov

56

leave

57

ret

58

; get_int

59

; :

60

ebx - dword

61

ecx -

62

; :

63

64

get_int:

eax .

65

call

read_int

66

mov

[ebx], eax

67

jmp

ecx

; input
;
sub1.asm

get_int . EBX
DWORD , ECX
. 25 28 ret1
. 32 34 $
. $ . $ + 7 36 MOV
.
.
.

60

. near short
$ 7 . ,
. (stack) .

4.3

CPU . (Last-In First-Out,


LIFO , ) .
. PUSH
POP .
. ( )
SS . (
) ESP
. (top) .
. , .
PUSH ESP 4 , 1) [ESP]
. POP [ESP] , ESP
4 . ESP 1000H
.
1

push

dword 1

; 1 0FFCh , ESP = 0FFCh

push

dword 2

; 2 0FF8h , ESP = 0FF8h

push

dword 3

; 3 0FF4h , ESP = 0FF4h

pop

eax

; EAX = 3, ESP = 0FF8h

pop

ebx

; EBX = 2, ESP = 0FFCh

pop

ecx

; ECX = 1, ESP = 1000h

.
(parameter) (local variable)
.
80x86 PUSHA EAX, EBX, ECX, EDX, ESI, EDI, EBP
() . (
) POPA () .

4.4

CALL RET

80x86 2
. CALL ,
(push). RET (pop)
. , RET
.
25 34
.
1) , 32
.

61

mov

ebx, input1

call

get_int

mov

ebx, input2

call

get_int

get_int :
get_int:
call

read_int

mov

[ebx], eax

ret
CALL RET .
!
. get_int read_int
. . read_int
RET , get_int .
get_int RET asm_main
. LIFO .
. ,
:
1

get_int:

call

read_int

mov

[ebx], eax

push

eax

ret

; EAX !

4.5

, (
(callee) . (calling
conventions) .

. ,
(e.g ) .
CALL RET .
PC C .
(reentrant) .
. (
)

62

ESP + 4
ESP

4.1:

ESP + 8
ESP + 4
ESP



4.2:

1
2
3
4
5
6

subprogram_label:
push
ebp
mov
ebp, esp
; subprogram code
pop
ebp
ret

; EBP
; EBP = ESP
; EBP .

4.3:

4.5.1
. CALL
. C
, .
, .
(Pop) . ,
. ,
CALL

. ,
, .
,
.

,
80x86

, 4.1 . ([ESP+4]
2)

) .

, , ESP

. 4.2

. ESP ( EBP)
,

. , ESP + 4 ESP + 8

EAX, EBX, ECX, EDX


. ,

.

2) .
.

63

ESP + 8
ESP + 4
ESP

EBP + 8
EBP + 4
EBP


EBP

4.4:
push
call
add

1
2
3

dword 1
fun
esp, 4

; 1
;

4.5:

. , ESP .
80386 EBP .
. C
EBP EBP ESP .
EBP ESP
. EBP
( EBP ) 4.3
.
4.3 2 3 . 5
6 . 4.4
. , [EBP + 8]

.
, .
C .
. .
( RET ) C
. pascal
. ,
API C stdcall .
? C . C
? C
(e.g., printf scanf ).
. ,
. C
. stdcall
.
. API
.
4.5 C .
3 . POP
, . ,

64

POP ECX .
ADD . POP ECX
. C .
54 ( )
.
.
.

%include "asm_io.inc"

sub3.asm

2
3

segment .data

sum

dd

5
6

segment .bss

input

resd 1

8
9

10

11

; i = 1;

12

; sum = 0;

13

; while( get_int(i, &input), input != 0 ) {

14

sum += input;

15

i++;

16

; }

17

; print_sum(num);

18

segment .text

19
20

global

_asm_main

_asm_main:

21

enter

22

pusha

0,0

edx, 1

; i edx .

23
24

mov

25

while_loop:

26

push

edx

; i

27

push

dword input

; % ?

28

call

get_int

29

add

esp, 8

31

mov

eax, [input]

32

cmp

eax, 0

33

je

end_while

add

[sum], eax

; i &input

30

34
35

; sum += input

65

36
37

inc

edx

38

jmp

short while_loop

39
40

end_while:

41

push

dword [sum]

42

call

print_sum

43

pop

ecx

;
; [sum]

44
45

popa

46

leave

47

ret

48
49

; get_int

50

; ( )

51

([ebp + 12] )

52

([ebp + 8] )

53

; :

54

55

segment .data

56

prompt

eax ebx .
db

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

57
58

segment .text

59

get_int:

60

push

ebp

61

mov

ebp, esp

63

mov

eax, [ebp + 12]

64

call

print_int

66

mov

eax, prompt

67

call

print_string

69

call

read_int

70

mov

ebx, [ebp + 8]

71

mov

[ebx], eax

73

pop

ebp

74

ret

62

65

68

72

75
76

; print_sum

77

78

; :

79

([ebp+8] )

66

1
2
3
4
5
6
7
8

subprogram_label:
push
ebp
mov
ebp, esp
sub
esp, LOCAL_BYTES
;
mov
esp, ebp
pop
ebp
ret

; EBP .
; EBP = ESP
; = #
; (deallocate)
; EBP .

4.6:

80

; : eax .

81

82

segment .data

83

result

db

"The sum is ", 0

84
85

segment .text

86

print_sum:

87

push

ebp

88

mov

ebp, esp

90

mov

eax, result

91

call

print_string

93

mov

eax, [ebp+8]

94

call

print_int

95

call

print_nl

97

pop

ebp

98

ret

89

92

96

sub3.asm

4.5.2
. C
(C automatic) .
.
.
, (recursively) .
.
. (C
(global) (static) )
.
EBP .
, ESP . 4.6

67

1
2
3

void calc_sum( int n, int * sump )


{
int i , sum = 0;

for( i =1; i <= n; i ++ )


sum += i ;
*sump = sum;

5
6
7
8

}
4.7: C

1
2
3
4

cal_sum:
push
mov
sub

ebp
ebp, esp
esp, 4

; sum

dword [ebp - 4], 0


ebx, 1

; sum = 0
; ebx (i) = 1

ebx, [ebp+8]
end_for

; i <= n ?

[ebp-4], ebx
ebx
short for_loop

; sum += i

ebx, [ebp+12]
eax, [ebp-4]
[ebx], eax

; ebx = sump
; eax = sum
; *sump = sum;

5
6
7
8
9
10

mov
mov
for_loop:
cmp
jnle

11
12
13
14

add
inc
jmp

15
16
17
18
19

end_for:
mov
mov
mov

20
21
22
23

mov
pop
ret

esp, ebp
ebp

4.8:

. EBP
. 4.7 C . 4.8
.
4.9 4.8 .
, , (stack frame)
. C .

ENTER LEAVE

ENTER
LEAVE . ENTER LEAVE
. ENTER . .
C 0 .
. LEAVE . 4.10

,

.


.

68

ESP + 16
ESP + 12
ESP + 8
ESP + 4
ESP

EBP + 12
EBP + 8
EBP + 4
EBP
EBP - 4

sump
n

EBP
sum

4.9:
1
2
3
4
5

subprogram_label:
enter LOCAL_BYTES, 0
; subprogram code
leave
ret

; = #

4.10: ENTER LEAVE ,

. , ( 1.7)
ENTER LEAVE .

4.6

(multi-module program) (object


le) .
. C (C
) .
(executable) . (i.e.
) . B
A , extern . extern
.
(external) . ,
. asm_io.inc
read_int, .
.

. global . 1.7
13 , _asm_main .
. C (internal) _asm_main
.
.
(get_int print_sum) _asm_main 2
.

1
2

%include "asm_io.inc"

main4.asm

69

segment .data

sum

dd

5
6

segment .bss

input

resd 1

8
9

segment .text

10

global

_asm_main

11

extern

get_int, print_sum

12

_asm_main:

13

enter

14

pusha

0,0

edx, 1

; edx 'i' .

15
16

mov

17

while_loop:

18

push

edx

; i .

19

push

dword input

20

call

get_int

21

add

esp, 8

23

mov

eax, [input]

24

cmp

eax, 0

25

je

end_while

add

[sum], eax

29

inc

edx

30

jmp

short while_loop

; i &input

22

26
27

; sum += input

28

31
32

end_while:

33

push

dword [sum]

34

call

print_sum

35

pop

ecx

;
; [sum]

36
37

popa

38

leave

39

ret

main4.asm

%include "asm_io.inc"

sub4.asm

2
3

segment .data

prompt

db

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

70
6

segment .text
global

get_int, print_sum

enter

0,0

11

mov

eax, [ebp + 12]

12

call

print_int

14

mov

eax, prompt

15

call

print_string

17

call

read_int

18

mov

ebx, [ebp + 8]

19

mov

[ebx], eax

7
8

get_int:

9
10

13

16

20
21

leave

22

ret

23
24

segment .data

25

result

db

"The sum is ", 0

26
27

segment .text

28

print_sum:
enter

0,0

31

mov

eax, result

32

call

print_string

34

mov

eax, [ebp+8]

35

call

print_int

36

call

print_nl

29
30

33

37
38

leave

39

ret

sub4.asm

. ,
.

4.7

, .
.
, . ,
!

C 71

1
2
3

segment .data
x
dd
format
db

0
"x = %d\n", 0

4
5
6
7
8
9
10

segment .text
...
push
dword [x]
push
dword format
call
_printf
add
esp, 8

;
;
;
;

x

_ !

4.11: printf


. : C ,
(inline) . C
. .
, .
. , NASM
. .
MASM . DJGPP gcc GAS 3) . PC
.
C :
C
,

.
, (,
) . : ,
.
C . ,
.

4.7.1

, C : EBX, ESI,
EDI, EBP, CS, DS, SS, ES. ,

register

. ,

. EBX, ESI, EDI

C (register variable)

. .

3) GAS GNU . AT&T , AT&T


MASM, TASM, NASM .

72

EBP + 12
EBP + 8
EBP + 4
EBP

x


EBP

4.12: printf

4.7.2
C , / _ .
f _f . ,
f _f .
gcc . ELF ,
C f f . DJGPP gcc _
. ( 1.7) _asm_main
.

4.7.3
C ,
.
C :printf("x = %d\n",x); 4.11
. ( NASM ) 4.12
printf . printf C
, . C
C

.
,

. EBP+8 . printf
stdarg.h ,

. C
.

.
, printf("x = %d\n") printf [EBP+12]
. , x !

4.7.4
data bss .
. , ( )
. ,
. (x ) ( foo )
. x EBP 8 ,
.
mov

eax, ebp - 8

MOV EAX . (
.) ,

C 73

. LEA (Load Eective Address ) . EAX x


.
lea

eax, [ebp - 8]

EAX x , foo .
[EBP8] ,
. LEA .
, .
(e.g dword) .

4.7.5

void C . C
. . (char, int, enum,
etc.) EAX . 32
32 EAX . 64 EDX:EAX
. EAX . ,
ST0 . ( , .)

4.7.6

80x86 C C
. .
,
. . ,
. 4) ,

. , C
. , ,
.
GCC . __attribute__
. . , f
1 int void ,
.
void f ( int ) __attribute__((cdecl ));
GCC (standard call) . stdcall
cdecl stdcall . stdcall cdecl stdcall
. ( Pascal
) , stdcall
. (i.e printf scanf )

4) Watcom C . Watcom
.

74

GCC regparm attribute


3 .
.
.
C __cdecl __stdcall .
. f
.
void __cdecl f ( int );
. cdecl
. C C
. . ,
,
. ( ,
)
stdcall cdecl . CALL
. ,
.

. , . ,
.

4.7.7
C .
( 1.7 , driver.c
.)
main5.c
1

#include <stdio . h>

/* */

void calc_sum( int , int * ) __attribute__((cdecl ));

4
5

int main( void )

{
int n, sum;

7
8

printf (Sum integers up to : );

9
10

scanf(%d, &n);

11

calc_sum(n, &sum);

12

printf (Sum is %d\n, sum);

13

return 0;

14

C 75

main5.c
sub5.asm

; _calc_sum

; 1 n .

; :

sump - sum int ([ebp + 12] )

; C :

; void calc_sum( int n, int * sump )

; {

int i, sum = 0;

10

for( i=1; i <= n; i++ )

11

sum += i;

12

*sump = sum;

13

; }

- ([ebp + 8] )

14
15

segment .text
global

16
17

18

; :

19

20

_calc_sum:

_calc_sum

[ebp-4]

21

enter

4,0

; sum

22

push

ebx

; !

24

mov

dword [ebp-4],0

; sum = 0

25

dump_stack 1, 2, 4

26

mov

23

27

; ebp-8 ebp+16

ecx, 1

; ecx i

for_loop:

28

cmp

ecx, [ebp+8]

; cmp i n

29

jnle

end_for

; if i <= n ,

31

add

[ebp-4], ecx

; sum += i

32

inc

ecx

33

jmp

short for_loop

36

mov

ebx, [ebp+12]

; ebx = sump

37

mov

eax, [ebp-4]

; eax = sum

38

mov

[ebx], eax

40

pop

ebx

41

leave

30

34
35

end_for:

39

; restore ebx

76

Sum integers up to: 10


Stack Dump # 1
EBP = BFFFFB70 ESP = BFFFFB68
+16 BFFFFB80 080499EC
+12 BFFFFB7C BFFFFB80
+8 BFFFFB78 0000000A
+4 BFFFFB74 08048501
+0 BFFFFB70 BFFFFB88
-4 BFFFFB6C 00000000
-8 BFFFFB68 4010648C
Sum is 55
4.13: sub5
ret

42

sub5.asm

sub5.asm 22 ? C EBX
. ,
.
25 dump_stack .
, EBP
. 4.13 . ,
BFFFFB80 (EBP + 12 ) , 0000000A
(EBP + 8 ), 08048501(EBP + 4 ), EBP
BFFFFB88(EBP ), 0 (EBP - 4 ),
EBX 4010648C(EBP - 8 ) .
calc_sum
. EAX
. main5.c 11 :
sum = calc_sum(n);
, calc_sum . :
sub6.asm

; _calc_sum

; 1 n .

; :

; :

; C :

; int calc_sum( int n )

; {

- ([ebp + 8] )

10

int i, sum = 0;

11

for( i=1; i <= n; i++ )

12

sum += i;

C 77

1
2

segment .data
format
db "%d", 0

3
4
5
6
7
8
9
10
11

segment .text
...
lea
eax, [ebp-16]
push
eax
push
dword format
call
_scanf
add
esp, 8
...
4.14: scanf

13

return sum;

14

; }

15

segment .text
global

16

_calc_sum

17

18

; local variable:

19

20

_calc_sum:

sum at [ebp-4]
enter

4,0

23

mov

dword [ebp-4],0

; sum = 0

24

mov

ecx, 1

; ecx i

21
22

25

for_loop:

26

cmp

ecx, [ebp+8]

; cmp i n

27

jnle

end_for

; if i <= n ,

29

add

[ebp-4], ecx

; sum += i

30

inc

ecx

31

jmp

short for_loop

mov

eax, [ebp-4]

28

32
33

end_for:

34

; eax = sum

35
36

leave

37

ret

4.7.8

sub6.asm

C
C . ,
scanf . 4.14

78

. scanf C
. , EBX, ESI, EDI , EAX, ECX,
EDX . , EAX scanf
. C
asm_io.obj asm_io.asm
.

4.8

(reetrant) .
. ,
. :
mov

word [cs:$+7], 5

; 5 7

add

ax, 2

; 2 5 .

.
(Read-Only) . ,
.
. , , , (code sharing)
. ( )
. ( data bss
) .
.
.
.
, (instance)
, .
DLL ( (Dynamic Link Libraries))
.
(multi-threaded) . 5) programs.
Windows 9x/NT UNIX (Solaris, Linux, etc.)
.

4.8.1
. (direct)
(indirect) . foo foo
.
5) . ,
.

79

1
2
3
4
5

; finds n!
segment .text
global _fact
_fact:
enter 0,0

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

mov
cmp
jbe
dec
push
call
pop
mul
jmp
term_cond:
mov
end_fact:
leave
ret

eax, [ebp+8]
; eax = n
eax, 1
term_cond
; n <= 1
eax
eax
_fact
; eax = fact(n-1)
ecx
; eax
dword [ebp+8]
; edx:eax = eax * [ebp+8]
short end_fact
eax, 1

4.15:
.

n=3

n(3)

EBP
n(2)
n=2

EBP
n(1)
n=1

EBP
4.16:

. foo bar , bar foo
.
(termination condition) .
.
,
. ( )
4.15 . C
:
x = fact (3);

/*

nd 3! */

80
1
2
3
4
5
6
7
8

void f ( int x )
{
int i ;
for( i =0; i < x ; i ++ ) {
printf (%d\n, i );
f ( i );
}
}
4.17: (C )

4.16 .
4.17 4.18 C .
f(3) ? ENTER i
. , f i
. i .

4.8.2

C .
(global) ,
(data bss ) ,
.
. , static ,
(i.e. ,
(external) (internal) ).
(static) (local) , static . (, C
static !)
, (data bss)
.
(automatic) C .
,
. .
(register)
. . ,
.
( ). ,
. ;
! C
.
(volatile)
.
. ,

81

1
2
3
4
5
6
7
8
9

%define i ebp-4
%define x ebp+8
;
segment .data
format
db "%d", 10, 0
; 10 = '\n'
segment .text
global _f
extern _printf
_f:
enter 4,0
; i

10
11
12

mov

dword [i], 0

; i = 0

mov
cmp
jnl

eax, [i]
eax, [x]
quit

; i < x ?

push
push
call
add

eax
format
_printf
esp, 8

; printf

push
call
pop

dword [i]
_f
eax

; f

inc
jmp

dword [i]
short lp

; i++

lp:

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

quit:
leave
ret

29
30

4.18: ( )

. , volatile
. volatile
.

x = 10;

y = 20;

z = x;
x , 1 3 x
z 0 . , x
x z 10 .
volatile .

82

5.1

(array) .
, .
(index) .
.



0 (C ).
.

5.1.1
(data) bss
(data) db, dw,
. NASM TIMES
.
5.1 .
bss , resb, resw,
.
. 5.1 .

.
( ) ESP
. ( , ENTER ) , 1 char
, 2 , 1 50
, 1 + 2 4 + 50 2 = 109 . , ESP

83

1
2
3
4
5
6
7
8
9
10

segment .data
; 1,2,... 10 10
a1
dd
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
; 10 0
a2
dw
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
; TIMES .
a3
times 10 dw 0
; 200 0 100 1
a4
times 200 db 0
times 100 db 1

11
12
13
14
15
16

segment .bss
; 10
a5
resd 10
; 100
a6
resw 100
5.1:
EBP - 1
EBP - 8
EBP - 12

char

1
2

EBP - 100
EBP - 104
EBP - 108
EBP - 109

EBP - 112

1
2
char

5.2:

4 ( 112) . ESP
(Double word boundary) . 109
. 5.2 .

.

5.1.2

C [] .
. .
array1

db

5, 4, 3, 2, 1

array2

dw

5, 4, 3, 2, 1

84

1
2
3
4
5

mov
mov
mov
mov

ebx, array1
dx, 0
ah, 0
ecx, 5

; ebx = array1
; dx .
; ?

mov
add
inc
loop

al, [ebx]
dx, ax
ebx
lp

; al = *ebx
; dx += ax (al !)
; bx++

lp:

6
7
8
9

5.3: ( 1)
1
2
3
4

6
7

9
10

ebx, array1
dx, 0
ecx, 5

; ebx = array1
; dx

add
jnc
inc

dl, [ebx]
next
dh

; dl += *ebx
; next
; dh 1

inc
loop

ebx
lp

; bx++

lp:

mov
mov
mov

next:

5.4: ( 2)

mov

al, [array1]

; al = array1[0]

mov

al, [array1 + 1]

; al = array1[1]

mov

[array1 + 3], al

; array1[3] = al

mov

ax, [array2]

; ax = array2[0]

mov

ax, [array2 + 2]

; ax = array2[1] (array2[2] !)

mov

[array2 + 6], ax

; array2[3] = ax

mov

ax, [array2 + 1]

; ax = ??

5 ( 1) . ( 2)
. , 2
1 2 . 7 1
1 . C ,

. .
5.3 array1 . 7 AX DX
. AL , ADD
. 1 . DX
65,535 . , AH
AH 3 0 . 1)
1) AH 0 AL .
6 7 CBW .

85

1
2
3
4

mov
mov
mov

ebx, array1
dx, 0
ecx, 5

; ebx = array1
; dx

add
adc
inc
loop

dl, [ebx]
dh, 0
ebx
lp

; dl += *ebx
; dh += + 0
; bx++

lp:

5
6
7
8

5.5: ( 3)

5.4 5.5 .
5.3 6 7 .

5.1.3

, .
:
[ base reg + factor*index reg + constant]

(base reg) EAX, EBX, ECX, EDX, EBP, ESP, ESI EDI .
(factor) 1, 2, 4 or 8 . (1 )
(index reg) EAX, EBX, ECX, EDX, EBP, ESI, EDI . (ESP
)
(constant) 32 . (label expression)
.

5.1.4

. array1c.c
( ) . driver.c .

%define ARRAY_SIZE 100

%define NEW_LINE 10

array1.asm

3
4

segment .data

FirstMsg

db

"First 10 elements of array", 0

Prompt

db

"Enter index of element to display: ", 0

SecondMsg

db

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

ThirdMsg

db

"Elements 20 through 29 of array", 0

InputFormat

db

"%d", 0

10

86

11

segment .bss

12

array

resd ARRAY_SIZE

13
14

segment .text

15

extern

_puts, _printf, _scanf, _dump_line

16

global

_asm_main

17

_asm_main:

18

enter

4,0

19

push

ebx

20

push

esi

; EBP - 4

21
22

; 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

31

push

dword FirstMsg

32

call

_puts

33

pop

ecx

35

push

dword 10

36

push

dword array

37

call

_print_array

38

add

esp, 8

30

; FirstMsg

34

; 10

39
40

; prompt user for element index

41

Prompt_loop:

42

push

dword Prompt

43

call

_printf

44

pop

ecx

46

lea

eax, [ebp-4]

47

push

eax

48

push

dword InputFormat

49

call

_scanf

50

add

esp, 8

51

cmp

eax, 1

52

je

InputOK

call

_dump_line

45

; eax =

; eax = scanf

53
54

; .

87

jmp

Prompt_loop

58

mov

esi, [ebp-4]

59

push

dword [array + 4*esi]

60

push

esi

61

push

dword SecondMsg

62

call

_printf

63

add

esp, 12

65

push

dword ThirdMsg

66

call

_puts

67

pop

ecx

69

push

dword 10

70

push

dword array + 20*4

71

call

_print_array

72

add

esp, 8

74

pop

esi

75

pop

ebx

76

mov

eax, 0

77

leave

78

ret

55
56
57

InputOK:

64

; 20-29

68

; array[20]

73

; C

79
80

81

; _print_array

82

; C ,

83

; .

84

; C :

85

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

86

; :

87

a - ( ebp+8 )

88

n - ( ebp+12 )

89
90

segment .data

91

OutputFormat

db

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

92
93
94

segment .text
global

_print_array

95

_print_array:

96

enter

0,0

97

push

esi

98

push

ebx

88

99
100

xor

esi, esi

; esi = 0

101

mov

ecx, [ebp+12]

; ecx = n

102

mov

ebx, [ebp+8]

; ebx =

103

print_loop:
push

ecx

; printf ecx !

106

push

dword [ebx + 4*esi]

; array[esi]

107

push

esi

108

push

dword OutputFormat

109

call

_printf

110

add

esp, 12

112

inc

esi

113

pop

ecx

114

loop

print_loop

116

pop

ebx

117

pop

esi

118

leave

119

ret

104
105

; (ecx !)

111

115

array1.asm
array1c.c

#include <stdio . h>

2
3

int asm_main( void );

void dump_line( void );

5
6

int main()

int ret_status ;

ret_status = asm_main();
return ret_status ;

10
11

12
13

/*

14

* dump_line

15

* (char) .

16

*/

17

void dump_line()

18

19
20

int ch;

89

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

21

/* null body*/ ;

22
23

}
array1c.c

LEA
LEA .
. .
ebx, [4*eax + eax]

lea

5 EAX EBX . LEA MUL


. ,
. , 6
.

5.1.5

1 .
, 1 .
2
2 . 2
. .
, .
3 2
int a [3][2];
C 6 (= 2 3) ,
.

a[0][0]

a[0][1]

a[1][0]

a[1][1]

a[2][0]

a[2][1]

a[0][0] 6 1
. a[0][1] . 2
.
.
(rowwise) , C/C++ .
a[i][j] ?
i j . 2i + j
. . 2
. i 2i . j
j 2i .

90
1
2
3
4
5

mov
sal
add
mov
mov

eax,
eax,
eax,
eax,
[ebp

[ebp 1
[ebp [ebp +
- 52],

44]
48]
4*eax - 40]
eax

;
;
;
;
;

ebp - 44 i
i 2 .
j .
ebp - 40 a[0][0] .
x (ebp - 52 )

5.6: x = a[ i ][ j ]

N
. N i + j . .
gcc . ( a
):
x = a[ i ][ j ];
5.6 .
.
x = *(&a[0][0] + 2*i + j );
, .
. (columnwise)
.

a[0][0]

a[1][0]

a[2][0]

a[0][1]

a[1][1]

a[2][1]

. [i][j] i + 3j
. (FORTRAN ) .
.
3
2 .
3 :
int b[4][3][2];
[3][2]
. .

b[0][0][0]

b[0][0][1]

b[0][1][0]

b[0][1][1]

b[0][2][0]

b[0][2][1]

10

11

b[1][0][0]

b[1][0][1]

b[1][1][0]

b[1][1][1]

b[1][2][0]

b[1][2][1]

b[i][j][k] 6i + 2j + k . 6 [3][2]
. a[L][M][N] a[i][j][k]

M N i + N j + k . , (L)
.

91

.
n D1 Dn , i1 in
:

D2 D3 Dn i1 + D3 D4 Dn i2 + + Dn in1 + in
,
n

j=1

Dk

ij

k=j+1

D1 .

i1 + D1 i2 + + D1 D2 Dn2 in1 + D1 D2 Dn1 in



n

(j1

j=1

k=1

Dk

ij

Dn .
C
C .

. .

. .
.
void f ( int a[ ][ ] );

/* */

.
void f ( int a[ ][2] );
2 .
. 2)
.
void f ( int * a[ ] );
(
.).
2) , .

92

LODSB
LODSW
LODSD

AL = [DS:ESI]
ESI = ESI 1
AX = [DS:ESI]
ESI = ESI 2
EAX = [DS:ESI]
ESI = ESI 4

STOSB
STOSW
STOSD

[ES:EDI] = AL
EDI = EDI 1
[ES:EDI] = AX
EDI = EDI 2
[ES:EDI] = EAX
EDI = EDI 4

5.7:
1
2

segment .data
array1 dd 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

3
4
5

segment .bss
array2 resd 10

6
7
8
9
10
11
12
13
14
15

segment .text
cld
mov
esi, array1
mov
edi, array2
mov
ecx, 10
lp:
lodsd
stosd
loop lp

; !

5.8: .


. , 4 .
void f ( int a[ ][4][3][2] );

5.2

80x86 .
(string instruction) .
(ESI, EDI) ,
1 . (Direction Flag,
DF) .
.
CLD 0 . .
STD 1 . .
80x86
. (
)
.

/ 93

MOVSB

MOVSW

MOVSD

byte [ES:EDI] = byte [DS:ESI]


ESI = ESI 1
EDI = EDI 1
word [ES:EDI] = word [DS:ESI]
ESI = ESI 2
EDI = EDI 2
dword [ES:EDI] = dword [DS:ESI]
ESI = ESI 4
EDI = EDI 4
5.9:

1
2

segment .bss
array resd 10

3
4
5
6
7
8
9

segment .text
cld
mov
edi, array
mov
ecx, 10
xor
eax, eax
rep stosd

; !

5.10: 0

5.2.1

, .
, . 5.7
. . ESI
, EDI . SI (Source Index), DI
(Destination Index)
. , . ( AL, AX, EAX )
DS ES
. .
, ES
. (DS ) ,
3) ES . 5.8
.
LODSx STOSx ( 5.8 13 14 )
. , MOVSx .
5.9 . 5.8 13 14
MOVSD . EAX
.

3) MOV DS ES
. DS (AX ) ,
ES . MOV .

94

CMPSB

CMPSW

CMPSD

SCASB
SCASW
SCASD

[DS:ESI] [ES:EDI]
ESI = ESI 1
EDI = EDI 1
[DS:ESI] [ES:EDI]
ESI = ESI 2
EDI = EDI 2
[DS:ESI] [ES:EDI]
ESI = ESI 4
EDI = EDI 4
AL [ES:EDI]
EDI 1
AX [ES:EDI]
EDI 2
AX [ES:EDI]
EDI 4
5.11:

5.2.2

REP

80x86 4) REP ,
. CPU
. ECX (LOOP ). REP
5.8 ( 12 15 )
.
rep movsd
5.10 0 .

5.2.3
5.11
.
. CMP . CMPSx
SCASx .
5.12 12 . 10
SCASD EDI 4 . ,
12 16 EDI 4
.

5.2.4

REPx

REP-
. 5.13 . REPE REPZ
. (REPNE REPNZ )
4) , ,
.
.

/ 95

1
2

segment .bss
array
resd 100

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

segment .text
cld
mov
edi, array
;
mov
ecx, 100
;
mov
eax, 12
;
lp:
scasd
je
found
loop
lp
;
jmp
onward
found:
sub
edi, 4
; edi 12 .
; code to perform if found
onward:
5.12:
REPE, REPZ
REPNE, REPNZ

Z , ECX
.
Z , ECX
.
5.13: REPx


, ECX .
. , Z ,
ECX 0 .
5.14 . 7
JE .
Z
. ECX 0
Z , equal .

5.2.5

.
C
.

memory.asm
global _asm_copy, _asm_find, _asm_strlen, _asm_strcpy

2
3

segment .text

ECX 0
?

96

1
2
3
4
5
6
7
8
9
10
11
12

segment .text
cld
mov
esi, block1
;
mov
edi, block2
;
mov
ecx, size
;
repe
cmpsb
; Z
je
equal
; Z .
;
jmp
onward
equal:
;
onward:
5.14:

; _asm_copy

; .

; C

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

; :

dest -

10

src

11

sz

12
13

; .

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

push

edi

23

mov

esi, src

; esi =

24

mov

edi, dest

; edi =

25

mov

ecx, sz

; ecx =

22

26

27

cld

28

rep

movsb

30

pop

edi

31

pop

esi

32

leave

33

ret

29

; movsb ECX

/ 97

34
35
36

; _asm_find

37

; .

38

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

39

; :

40

src

41

target -

42

sz

43

; :

44

target target

45

46

NULL .

47

; : target .

48

49

50

%define src

51

%define target [ebp+12]

52

%define sz

-
-

8 .
[ebp+8]
[ebp+16]

53
54

_asm_find:

55

enter

0,0

56

push

edi

58

mov

eax, target

59

mov

edi, src

60

mov

ecx, sz

61

cld

57

; al

62

repne

scasb

; ECX == 0 [ES:EDI] == AL

65

je

found_it

66

mov

eax, 0

; NULL

67

jmp

short quit

63
64

68

found_it:

69

mov

eax, edi

70

dec

eax

72

pop

edi

73

leave

74

ret

71

quit:

75
76
77

; _asm_strlen

(DI - 1)

98

78

79

; unsigned asm_strlen( const char * );

80

; :

81

82

; :

83

src -
char ( 0 ) (EAX )

84
85

%define src [ebp + 8]

86

_asm_strlen:

87

enter

0,0

88

push

edi

90

mov

edi, src

91

mov

ecx, 0FFFFFFFFh ; ECX

92

xor

al,al

; al = 0

93

cld
scasb

; 0 .

89

; edi =

94

repnz

95
96
97

98

; repnz , FFFFFFFE - ECX,

99

; FFFFFFFF - ECX .

100

101

mov

eax,0FFFFFFFEh

102

sub

eax, ecx

104

pop

edi

105

leave

106

ret

; = 0FFFFFFFEh - ecx

103

107
108

; _asm_strcpy

109

; .

110

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

111

; :

112

dest -

113

src

114

115

%define dest [ebp + 8]

116

%define src

117

_asm_strcpy:

[ebp + 12]

118

enter

0,0

119

push

esi

120

push

edi

121

/ 99

122

mov

edi, dest

123

mov

esi, src

124

cld

125

cpy_loop:

126

lodsb

; AL & si

127

stosb

; AL & di

128

or

al, al

; .

129

jnz

cpy_loop

; 0 ,

131

pop

edi

132

pop

esi

133

leave

134

ret

130

memory.asm
memex.c

#include <stdio . h>

2
3

#de ne STR_SIZE 30

/* prototypes */

5
6

void asm_copy( void *, const void *, unsigned ) __attribute__((cdecl ));

void * asm_ nd( const void *,


char target , unsigned ) __attribute__((cdecl ));

8
9
10

unsigned asm_strlen( const char * ) __attribute__((cdecl ));


void asm_strcpy( char *, const char * ) __attribute__((cdecl ));

11
12

int main()

13

14

char st1[STR_SIZE] = test string ;

15

char st2[STR_SIZE];

16

char * st ;

17

char

ch;

18
19

asm_copy(st2, st1 , STR_SIZE);

20

printf (%s\n, st2 );

/* 30 */

21
22

printf (Enter a char : );

23

scanf(%c%*[^\n], &ch);

24

st = asm_ nd(st2, ch, STR_SIZE);

25

if ( st )

26
27
28

/* . */

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


else
printf (Not found\n);

100 5
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 );

36

printf (%s\n, st2 );

/* */

37

return 0;

38
39

}
memex.c

101


6.1

6.1.1

,
. ,
. 10
.

0.123 = 1 101 + 2 102 + 3 103


, .

0.1012 = 1 21 + 0 22 + 1 23 = 0.625
1
.

110.0112 = 4 + 2 + 0.25 + 0.125 = 6.375


. ,
: . 1
. .
a, b, c, . . . .
:

0.abcdef . . .
2 .

a.bcdef . . .
1 . a 0

0.bcdef . . .

102 6

0.5625 2
0.125 2
0.25 2
0.5 2

=
=
=
=

rst bit = 1

1.125
0.25
0.5
1.0

second bit = 0
third bit = 0
fourth bit = 1

6.1: 0.5625

0.85 2
0.7 2
0.4 2
0.8 2
0.6 2
0.2 2
0.4 2
0.8 2

=
=
=
=
=
=
=
=

1.7
1.4
0.8
1.6
1.2
0.4
0.8
1.6

6.2: 0.85

b.cdef . . .
, (b) 1 .
. 6.1 0.5625
. 0 .
23.85 .
(23 = 101112 ). (0.85) ? 6.2
. ,
. 0.85
. 1) .

0.85 = 0.1101102 . , 23.85 = 10111.1101102 .


, 23.85
. ( 13
) C float
double . 23.85
. 23.85
1) ,
. 13 3
3 0.13 .

103

31 30
s
s
e
f

23

22

0
f

- 0 = , 1 =
(8 ) = + 7F ( 127). 00 FF
.
- 1. 23
6.3: IEEE

.
.
( 10 2
) . 23.85, 10111.11011001100110 . . .2
.

1.011111011001100110 . . . 2100
( (100) .).
.

1.ssssssssssssssss 2eeeeeee
1.sssssssssssss (signi cand) eeeeeeee (exponent)
.

6.1.2

IEEE

IEEE ( , Institute of Electrical and Electronic Engineers)


.
( !) .
.
(numeric coprocessor) . ( Pentium CPU )
IEEE . (single precision)
2 (double precision) . C float
2 double .
(extended
precision) . ,
. 2
.

2)

IEEE oat double

104 6

e=0 , f =0
e = 0 , f 6= 0
e = FF , f = 0
e = FF , f 6= 0

0 . ( ) +0
-0 .
.
.
() .
.
NaN(Not A Number)
.
6.1: f e

IEEE
32 . 7
.
. 6.3 IEEE .
. 2
. (signed magnitude) . 31
.
. 7F 23 30
. (biased exponent) .
(1.sssssssss ).
1 , 1 .
. 1
(hidden one representation) .

23.85 ? 0 .

41 BE CC CD

4 , 7F + 4 = 8316 .

01111101100110011001100 . ( 1 )

, 4 )


23.850000381
.

1,103,023,309 .
CPU
.

0 100 0001 1 011 1110 1100 1100 1100 11002 = 41BECCCC16


23.85 (
). 23.849998474 .
23.85 . , C 23.85
. C , ,
1 1 . 23.85 41 BE
CC CD . 23.850000381 23.85
.
-23.85 ? . C1 BE CC CD
. 2 !
e f IEEE oat . 6.1

2) ( Borland) long double .


double long double 2 . ( ANSI C )

105

63 62
s

52

51

0
f

6.4: IEEE 2
. (over ow) 0
. (NaN) ,
, .
1.0 2126 ( 1.1755 1035 ) 1.11111 . . .

2127 ( 3.4028 1035 ) .



(Denormalized number)
. (i.e. 1.0 2126 ) 1.0012 2129
( 1.6530 1039 ) . .
, : 0.010012 2127 .
0 . ( 6.1 ) 2127
. , 1.001 2129
:

0 000 0000 0 001 0010 0000 0000 0000 0000

IEEE 2
IEEE 2 64 , 15
. 6.4
. 11 52 .
.
3FF(1023) ( 7F ).
. 2
10308 to 10308 .
.
23.85 . 16 4 + 3FF = 403
. 2 :

0 100 0000 0011 0111 1101 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1010
40 37 D9 99 99 99 99 9A .
23.8500000000000014 ( 12 0 !) 23.85
.
2 . 3)
. 2127 21023 .

3) NaN , 7FF FF .

106 6

6.2

.
.
.
. 8
.

6.2.1
. ,
.

10.375 + 6.34375 = 16.71875 .


1.0100110 23
+ 1.1001011 22

.

1.0100110 23
+ 0.1100110 23
10.0001100 23
1.1001011 22
0.1100110 23 . 10.0001100 23 (

1.00001100 24 ) 10000.1102 16.75 .


16.71875 !
.
( ) .
.
.
(a + b) b = a !

6.2.2
, . 16.75

15.9375 = 0.8125 .
1.0000110 24
1.1111111 23
1.1111111 23 1.0000000 24 ( )
1.0000110 24
1.0000000 24
0.0000110 24

107

0.0000110 24 = 0.112 = 0.75 .

6.2.3

, . 10.375

2.5 = 25.9375
1.0100110 23
1.0100000 21
10100110
+ 10100110
1.10011111000000 24
8

1.1010000 24 = 11010.0002 = 26
.

6.2.4

.
.
. f (x)
. .
. 4) x
.
if ( f (x) == 0.0 )
f (x) 1 1030 ? .
, . f (x)
IEEE x 0 .
.
if ( fabs( f (x)) < EPS )
EPS . ( 1 1010 ) f (x)
0 . (x) (y)
:
if ( fabs(x y)/fabs(y) < EPS )

4) f (x) = 0 x .

108 6

6.3

6.3.1
.
.
- .
(math coprocessor) .

( 10 !).
8086/8088 8087 . 80286 80287, 80386
80387 . 80486DX 80486
. 5) 80x86
. , .

.
.
( )
8 . 80
. 80
. ST0, ST1, ST2, . . . ST7 .
CPU .
(stack) . (LIFO)
. ST0 .
.
.
(status register) .
. 4 :C0 , C1 , C2 , C3 .
.

6.3.2
CPU
F .


.

5) 80486SX . 80487SX .

109

FLD source

.
source 2 ,
.

FILD source

,
. source , ,
.

FLD1

1 .

FLDZ
0 .
.
(pop) (i.e. ) .
FST dest
(ST0) . dest 2
.
FST .

FSTP dest

. dest 2
.
. dest

FIST dest

.
.
(control word) .
(- )
.
. , FSTCW (
Store Control Word) FLDCW ( , Load
Control Word) .
FIST . ,

FISTP dest

, dest .
, .
FXCH STn

ST0 STn . ( , n 1 7
.)

FFREE STn


ST0 .
.
FADD src
ST0 += src. src
2 .
FADD dest, ST0

dest += ST0. dest .

FADDP dest

dest += ST0 . dest

FADDP dest, STO

FIADD src

ST0 += (float) src. ST0 . src


.

110 6

1
2
3

segment .bss
array
resq SIZE
sum
resq 1

4
5
6
7
8
9
10
11
12
13

segment .text
mov
ecx, SIZE
mov
esi, array
fldz
lp:
fadd
qword [esi]
add
esi, 8
loop
lp
fstp
qword sum

; ST0 = 0
; ST0 += *(esi)
; double
; sum

6.5:

, 2
.
. (i.e. a + b = b + a, a b 6= b a !)
. R RP .
6.5 double . 10
13 .
oat() double()
.
FSUB src

ST0 -= src. src


2 .

FSUBR src

ST0 = src - ST0. src


2 .

FSUB dest, ST0

dest -= ST0. dest .

FSUBR dest, ST0

dest = ST0 - dest.dest


.

FSUBP dest

dest -= ST0 . dest

FSUBP dest, STO

FSUBRP dest or

dest = ST0 - dest . dest

FSUBRP dest, STO

FISUB src

ST0 -= (float) src.ST0 . src


.

FISUBR src

ST0 = (float) src - ST0.ST0 . src


.

111

ST0 *= src. src

FMUL src

2 .
FMUL dest, ST0

dest *= ST0.dest .

FMULP dest

dest *= ST0 . dest

FMULP dest, STO

FIMUL src

ST0 *= (float) src. ST0 . src

.
. 0
.
ST0 /= src. src

FDIV src

2 .
ST0 = src / ST0. src

FDIVR src

2 .
FDIV dest, ST0

dest /= ST0. dest .

FDIVR dest, ST0

dest = ST0 / dest. dest


.

FDIVP dest

dest /= ST0 . dest

FDIVP dest, STO

FDIVRP dest

dest = ST0 / dest . dest

FDIVRP dest, STO

FIDIV src

ST0 /= (float) src. ST0 . src


.

FIDIVR src

ST0 = (float) src / ST0. ST0 . src


.

. FCOM
.
FCOM src

ST0 src . src


2 .

FCOMP src

ST0 src .src


2 .

FCOMPP

ST0 ST1 .

FICOM src

ST0 (float) src . src


.

FICOMP src

ST0 (float)src . src


.

FTST
ST0 0 .
C0 , C1 , C2 , C3 .
CPU .
.

112 6

1
2
3
4
5
6
7
8
9
10
11
12
13

;
;

if ( x > y )

fld
fcomp
fstsw
sahf
jna
then_part:
; then
jmp
else_part:
; else
end_if:

qword [x]
qword [y]
ax

; ST0 = x
; STO y
; C

else_part

x>y else_part


end_if

6.6:


FSTSW dest

AX
.
AH .

SAHF

LAHF
AH .
6.6 . 5 6 C0 , C1 ,
C2 , C3 .
. 7 JNA .
( ( II, III))
CPU .
FCOMI src
ST0 src . src
.
FCOMIP src

ST0 src .src

.
6.7 double FCOMIP
. (FICOM FICOMP).

.
FCHS
ST0 = - ST0, ST0 .
FSQRT

ST0 = |ST0| ,ST0 .

ST0 = STO, ST0 .

FSCALE

ST0 = ST0 2bST1c ,ST0 2 . ST1

FABS

. 6.8
.

113

global _dmax

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

segment .text
; function _dmax
; double .
; C
; double dmax( double d1, double d2 )
; :
;
d1
- double
;
d2
- double
; :
; d1 d2 (ST0 )
%define d1
ebp+8
%define d2
ebp+16
_dmax:
enter
0, 0

17
18
19
20
21
22
23
24
25
26
27
28

fld
fld
fcomip
jna
fcomp
fld
jmp
d2_bigger:
exit:
leave
ret

qword
qword
st1
short
st0
qword
short

[d2]
[d1]

; ST0 = d1, ST1 = d2


; ST0 = d2

d2_bigger
[d1]
exit

; d2 .
; ST0 = d1
; d2

6.7: FCOMIP

1
2
3

segment .data
x
dq
five
dw

2.75
5

; double

4
5
6
7
8

segment .text
fild
dword [five]
fld
qword [x]
fscale

; ST0 = 5
; ST0 = 2.75, ST1 = 5
; ST0 = 2.75 * 32, ST1 = 5

6.8: FSCALE

114 6

6.3.3
6.3.4 2
2 .

ax2 + bx + c = 0
. x: x1 x2 .

x1 , x2 =

b2 4ac
2a

(b2 4ac) (discriminant) .


.
1. . b2 4ac = 0
2. . b2 4ac > 0
3. . b2 4ac < 0
C .
quadt.c
1

#include <stdio . h>

2
3

int quadratic( double, double, double, double *, double *);

4
5

int main()

{
double a,b,c , root1, root2;

7
8

printf (Enter a , b, c : );

9
10

scanf(%lf %lf %lf , &a, &b, &c);

11

if (quadratic( a , b, c , &root1, &root2) )


printf ( roots : %.10g %.10g\n, root1, root2 );

12

else

13

printf (No real roots \n );

14

return 0;

15
16

}
quadt.c
:

; quadratic

; 2 .

quad.asm

115

a*x^2 + b*x + c = 0

; C :

; :

a, b, c - 2 ( )

root1

- double

10

root2

- double

11

; :

12

int quadratic( double a, double b, double c,


double * root1, double *root2 )

1 , 0

13
14

%define a

qword [ebp+8]

15

%define b

qword [ebp+16]

16

%define c

qword [ebp+24]

17

%define root1

dword [ebp+32]

18

%define root2

dword [ebp+36]

19

%define disc

qword [ebp-8]

20

%define one_over_2a

qword [ebp-16]

21
22

segment .data

23

MinusFour

dw

-4

24
25
26
27

segment .text
global

_quadratic

_quadratic:

28

push

ebp

29

mov

ebp, esp

30

sub

esp, 16

; 2 (disc one_over_2a)

31

push

ebx

; ebx

33

fild

word [MinusFour]; -4

34

fld

; : a, -4

35

fld

; : c, a, -4

36

fmulp

st1

; : a*c, -4

37

fmulp

st1

; : -4*a*c

38

fld

39

fld

; : b, b, -4*a*c

40

fmulp

st1

; : b*b, -4*a*c

41

faddp

st1

; : b*b - 4*a*c

42

ftst

43

fstsw

44

sahf

45

jb

46

fsqrt

32

; 0
ax
no_real_solutions ; disc < 0, .
; : sqrt(b*b - 4*a*c)

116 6
disc

; .

47

fstp

48

fld1

49

fld

50

fscale

51

fdivp

st1

; : 1/(2*a)

52

fst

one_over_2a

; : 1/(2*a)

53

fld

; : b, 1/(2*a)

54

fld

disc

; : disc, b, 1/(2*a)

55

fsubrp

st1

; : disc - b, 1/(2*a)

56

fmulp

st1

; : (-b + disc)/(2*a)

57

mov

ebx, root1

58

fstp

qword [ebx]

59

fld

; : b

60

fld

disc

; : disc, b

61

fchs

62

fsubrp

st1

; : -disc - b

63

fmul

one_over_2a

; : (-b - disc)/(2*a)

64

mov

ebx, root2

65

fstp

qword [ebx]

; *root2

66

mov

eax, 1

; 1

67

jmp

short quit

; : 1.0
a

; : a, 1.0
; : a * 2^(1.0) = 2*a, 1

*root1

; : -disc, b

68
69

no_real_solutions:
mov

eax, 0

73

pop

ebx

74

mov

esp, ebp

75

pop

ebp

76

ret

70

; 0

71
72

quit:

quad.asm

6.3.5
double . C
.
readt.c
1

/*

* 32 read_doubles() .

* stdin double . (

* (redirection) .)

*/

#include <stdio . h>

117

extern int read_doubles( FILE *, double *, int );

#de ne MAX 100

9
10

int main()

11

12

int i , n;

13

double a[MAX];

14

n = read_doubles(stdin, a , MAX);

15
16

for( i =0; i < n; i ++ )

17

printf (%3d %g\n, i , a[ i ]);

18

return 0;

19
20

}
readt.c
.

segment .data

format

db

read.asm
"%lf", 0

; format for fscanf()

3
4

segment .text

global

_read_doubles

extern

_fscanf

7
8

%define SIZEOF_DOUBLE

%define FP

dword [ebp + 8]

10

%define ARRAYP

dword [ebp + 12]

11

%define ARRAY_SIZE

dword [ebp + 16]

12

%define TEMP_DOUBLE

[ebp - 8]

13
14

15

; _read_doubles

16

; C :

17

18

; double EOF

19

; .

20

; :

21

fp

- FILE ( )

22

arrayp

- double

23

array_size -

24

; :

25

int read_doubles( FILE * fp, double * arrayp, int array_size );

double (EAX )

118 6
26
27

_read_doubles:

28

push

ebp

29

mov

ebp,esp

30

sub

esp, SIZEOF_DOUBLE

; double

32

push

esi

; esi

33

mov

esi, ARRAYP

; esi = ARRAYP

34

xor

edx, edx

; edx = ( 0)

31

35
36

while_loop:

37

cmp

edx, ARRAY_SIZE

; edx < ARRAY_SIZE ?

38

jnl

short quit

39

40

; TEMP_DOUBLE double fscanf()

41

; fscanf() edx .

42

;
; edx

43

push

edx

44

lea

eax, TEMP_DOUBLE

45

push

eax

; &TEMP_DOUBLE

46

push

dword format

; &format

47

push

FP

48

call

_fscanf

49

add

esp, 12

50

pop

edx

; edx

51

cmp

eax, 1

; fscanf 1 ?

52

jne

short quit

53

54

; TEMP_DOUBLE ARRAYP[edx]

55

; (8 double 4 .)

56

57

mov

eax, [ebp - 8]

58

mov

[esi + 8*edx], eax

59

mov

eax, [ebp - 4]

60

mov

[esi + 8*edx + 4], eax

62

inc

edx

63

jmp

while_loop

pop

esi

; esi .

mov

eax, edx

; eax

; 4
; 4

61

64
65
66

quit:

67
68
69

119

70

mov

esp, ebp

71

pop

ebp

72

ret

6.3.6

read.asm

.
.

.

.
. 10
11 . RC ( , Rounding Control)
. 0 ()
. 1 .
.
C
fprime.c
1

#include <stdio . h>

#include < stdlib . h>

/*

* :

*/

10

nd_primes

extern void nd_primes( int * a , unsigned n );

11
12

int main()

13

14

int status ;

15

unsigned i;

16

unsigned max;

17

int * a ;

18
19

printf (How many primes do you wish to nd? );

20

scanf(%u, &max);

21
22
23

a = calloc ( sizeof( int ), max);

120 6
if ( a ) {

24
25

nd_primes(a , max);

26
27
28

/* 20 */

29

for( i = ( max > 20 ) ? max 20 : 0; i < max; i ++ )


printf (%3d %d\n, i+1, a[ i ]);

30
31
32

free (a );

33

status = 0;

34

35

else {

36

fprintf ( stderr , Can not create array of %u ints\n , max);

37

status = 1;
}

38
39

return status ;

40
41

}
fprime.c
.

prime2.asm

segment .text
global

_find_primes

; find_primes

; :

array

n_find -

; C :

10

;extern void find_primes( int * array, unsigned n_find )

11

12

%define array

ebp + 8

13

%define n_find

ebp + 12

14

%define n

ebp - 4

15

%define isqrt

ebp - 8

; guess

16

%define orig_cntl_wd

ebp - 10

17

%define new_cntl_wd

ebp - 12

18
19

_find_primes:

20

enter

21

12,0

121

22

push

ebx

23

push

esi

25

fstcw

word [orig_cntl_wd]

26

mov

ax, [orig_cntl_wd]

27

or

ax, 0C00h

28

mov

[new_cntl_wd], ax

29

fldcw

word [new_cntl_wd]

31

mov

esi, [array]

; esi .

32

mov

dword [esi], 2

; array[0] = 2

33

mov

dword [esi + 4], 3

; array[1] = 3

34

mov

ebx, 5

; ebx = guess = 5

35

mov

dword [n], 2

; n = 2

24

;
; 11 . ()

30

36

37

; .

38

; .

39

; .

40

; . (

41

; .)

42

43

while_limit:

44

mov

eax, [n]

45

cmp

eax, [n_find]

46

jnb

short quit_limit

48

mov

ecx, 1

; ecx

49

push

ebx

; guess

50

fild

dword [esp]

;guess

51

pop

ebx

; guess .

52

fsqrt

53

fistp

; while ( n < n_find )

47

; sqrt(guess) .
dword [isqrt]

; isqrt = floor(sqrt(quess))

54

55

; guess (ebx)

56

; guess

57

; floor(sqrt(guess)) .

58

;(floor )

59

while_factor:

60

mov

eax, dword [esi + 4*ecx]

; eax = array[ecx]

61

cmp

eax, [isqrt]

; while ( isqrt < array[ecx]

62

jnbe

short quit_factor_prime

63

mov

eax, ebx

64

xor

edx, edx

65

div

dword [esi + 4*ecx]

122 6
; && guess % array[ecx] != 0 )

66

or

edx, edx

67

jz

short quit_factor_not_prime

68

inc

ecx

69

jmp

short while_factor

70
71

72

; !

73

74

quit_factor_prime:

75

mov

eax, [n]

76

mov

dword [esi + 4*eax], ebx

77

inc

eax

78

mov

[n], eax

; guess
; inc n

79
80

quit_factor_not_prime:

81

add

ebx, 2

82

jmp

short while_limit

83
84

quit_limit:

85
86

fldcw

word [orig_cntl_wd]

87

pop

esi

88

pop

ebx

89
90

leave

91

ret

prime2.asm

123

C++
7.1

7.1.1

C
. .
1.
.
2. .
.
3. (locality)1) .
.
, .
,
.
. (
)
(tag) ( ) .

. ,
(relative oset) . ,
,
.
.
struct S {
short int x ;

/* 2 */

int

y;

/* 4 */

double

z;

/* 8 oat */

};
1) .

124 7 C++

0
2

x
y

6
z
7.1: S

0
2
4

x

y

8
z
7.2: S
7.1 S . ANSI
C struct
.
. (i.e 0 ) stddef.h offsetof()

.
. 7.1
offsetof(S,y) 2 .

7.1.2
gcc , offsetof y 2
4

4 . gcc ( )

. 32 CPU

. 7.2
S gcc .
2 y ( z )
. offsetof
.

. C C
.
C .
gcc S 7.2 ,
7.1 . C

125

struct S {
short int x ;
/* 2 */
int
y;
/* 4 */
double z ;
/* 8 oat */
} __attribute__((packed));
7.3: gcc
. ANSI C
.
gcc
.
.
typedef short int unaligned_int __attribute__((aligned (1)));
unaligned_int .
(__attribute__ ) aligned 1 2
. (2 , 4 )
y unaligned_int gcc y 2 .
, z 8 double
. z 6 .
gcc (pack) .
. 7.3 S
. S ,
14 .
#pragma
.
#pragma pack(1)
. (i.e
) 1 2, 4, 8, 16 ,
, , .
.
.
(include)
. .
.
.
. 7.4 .

7.1.3

.
8 . unsigned int
int (:) . 7.5 .
32 .

126 7 C++
#pragma pack(push) /* */
#pragma pack(1)
/* */
struct S {
short int x ;
int
y;
double z ;
};

/* 2 */
/* 4 */
/* 8 oat */

#pragma pack(pop)

/* */

7.4:
struct S {
unsigned f1 :
unsigned f2 :
unsigned f3 :
unsigned f4 :
};

3;
10;
11;
8;

/*3 */
/*10 */
/* 11 */
/* 8 */

7.5:

11

10

f4

f3

f2

f1

. 2)

. .
.
S .
5

f2l

f1

f3l

f2m

f3m

f4

f2l f2 5 (i.e LSB) . f2m f2


5 . .
f2 f3 .

. ( )
,
.
SCSI 3) . SCSI
7.6 6 .
2) ANSI/ISO C .
C (gcc, , )
.
3) (Small Computer Systems Interface)
.

127

\
0
1
2
3
4
5

4
3
2
1
0
(08h)
#



7.6: SCSI
1
2

#de ne MS_OR_BORLAND (de ned(__BORLANDC__) \


|| de ned(_MSC_VER))

3
4
5
6
7

#if MS_OR_BORLAND
# pragma pack(push)
# pragma pack(1)
#endif

8
9
10
11
12
13
14
15
16
17
18
19
20
21

struct SCSI_read_cmd {
unsigned opcode : 8;
unsigned lba_msb : 5;
unsigned logical_unit : 3;
unsigned lba_mid : 8;
/* */
unsigned lba_lsb : 8;
unsigned transfer_length : 8;
unsigned control : 8;
}
#if de ned(__GNUC__)
__attribute__((packed))
#endif
;

22
23
24
25

#if MS_OR_BORLAND
# pragma pack(pop)
#endif
7.7: SCSI
8

8 bits

8
lba_lsb

8 bits
lba_mid

5 bits
lba_msb

7.8: SCSI_read_cmd

(logical block address) ,


3 . 7.6
. 7.7
. 2
. 11 14
. lba_mid lba_lsb 16
.

128 7 C++
1
2
3
4
5
6
7
8
9
10
11
12
13

struct SCSI_read_cmd {
unsigned char opcode;
unsigned char lba_msb : 5;
unsigned char logical_unit : 3;
unsigned char lba_mid;
/* */
unsigned char lba_lsb;
unsigned char transfer_length ;
unsigned char control;
}
#if de ned(__GNUC__)
__attribute__((packed))
#endif
;
7.9: SCSI
. 16 .
lba_msb logical_unit .
. . 7.8 48
. ( .)

. ( 7.6).
, SCSI_read_cmd C
. sizeof(SCSI_read_cmd) ,
C 6 8 .
.
unsigned 2
. unsigned short
. 6 2
. 4)
. 7.9 3
. unsigned char
.
.
.
.

7.1.4

. S y 0
. .
void zero_y( S * s_p );

4) .

C++ 129
1

#include <stdio . h>

2
3
4
5
6

void f ( int x )
{
printf (%d\n, x );
}

7
8
9
10
11

void f ( double x )
{
printf (%g\n, x );
}
7.10: f()

%define

_zero_y:

y_offset

enter

0,0

mov

eax, [ebp + 8]

mov

dword [eax + y_offset], 0

leave

ret

; s_p( )

C .
. ,
.
.
C .
, EAX .
.
.
.
(NASM )
.
.

7.2

C++

C++ C . C
C++ . ,
. , C++ .
C++ .

7.2.1

(Name Mangling)

C++ ( )
. 2 ,

130 7 C++
(overloaded) . C
C .
. 7.10 .
_f .
C++ C ,
(name mangling) . C
. C _ .
C 7.10 .
C++
. 7.10 DJGPP _f__Fi
_f__Fd .
.
C++
. C++
7.10 @f$qi @f$qd .
. .
. int
i . ( DJGPP
) double d .
f ,
void f ( int x , int y , double z);
DJGPP _f__Fiid @f$qiid .
,
. C++ .
. C++
,
. C++ .

.
. ,
. C++
(typesafe linking) .
.
. C .
C .

. C++
.
C++ (parsing) ,
. 5)
CALL .
5) . .
. C++ .

C++ 131


C++ .
C++ . C++
C++
. (
)
7.10
. C++ printf
_printf CALL .
. printf
. :
int printf ( const char *,

...);

DJGPP _printf__FPCce . (F (function), P


(pointer), C (const), c char, e (ellipsis) )
C printf ! C++
C . C
. C C++
C .
C++ extern C
. C++ C (C linkage)
. , printf C .
extern C int printf ( const char *, ... );
C++ ,
C . , printf
. C++ .
C , C .
, C++
. .
extern C {
/* C */
}
, C/C++ ANSI C ,
.
#ifdef __cplusplus
extern C {
#endif
. C++ __cplusplus
(_ ) . C++
extern "C" , C
. ( C extern "C" ).

132 7 C++
1
2

void f ( int & x )


{ x++; }

// &

3
4
5
6
7
8
9
10

int main()
{
int y = 5;
f (y );
// y . & !
printf (%d\n, y ); // 6
return 0;
}
7.11: (Reference)

C C++
.

7.2.2
(Reference) C++ .
. 7.11
. , . .
. ( var
) 7
, y . f
6)
void f ( int * xp);
,
. C++
. (+) .
, a b a + b a b
. C++ . (,
operator +(a,b) ) ,
.
operator +(&a, &b) , &a + &b
. a + b ,
.

7.2.3
(Inline functions) C++ . 7)
, . C
.
#de ne SQR(x) ((x)*(x))
6) , 7.2.1 C .
7) C ANSI C

C++ 133
1
2

inline int inline_f ( int x )


{ return x*x ; }

3
4
5

int f ( int x )
{ return x*x ; }

6
7
8
9
10
11
12
13

int main()
{
int y , x = 5;
y = f (x );
y = inline_f (x );
return 0;
}
7.12:

C ,
. ,
SQR(x++) .

. ,
.
!
, CALL .
, . C++
inline
. 7.12 . 10 f
. ( x ebp-8 , y ebp-4
)

push

dword [ebp-8]

call

_f

pop

ecx

mov

[ebp-4], eax

11 inline_f .

mov

eax, [ebp-8]

imul

eax, eax

mov

[ebp-4], eax

. , .
, ,
. . ,
.

134 7 C++
1
2
3
4
5
6
7
8
9

class Simple {
public:
Simple ();
~Simple();
int get_data() const;
void set_data( int );
private :
int data;
};

//
//
//

//

10
11
12

Simple :: Simple()
{ data = 0; }

13
14
15

Simple::~Simple()
{ /* */ }

16
17
18

int Simple :: get_data() const


{ return data; }

19
20
21

void Simple :: set_data( int x )


{ data = x ; }
7.13: C++

.
. ,
, .
. , .

.
, .
. C
.

7.2.4
C++ (object) . C++
8) , (method) . ,
. 7.13
. Simple C int
C++ this

. .

. .

7.13 Simple set_data . C


7.14
8) (member function) .

C++ 135

void set_data( Simple * object , int x )


{
object>data = x;
}
7.14: C Simple::set_data()
1
2
3

_set_data__6Simplei:
push
ebp
mov
ebp, esp

mov
mov
mov

5
6
7

eax, [ebp + 8]
edx, [ebp + 12]
[eax], edx

; eax = (this)
; edx =
; 0

leave
ret

9
10

7.15: Simple::set_data( int )

. DJGPP ( gcc
) -S
. DJGPP gcc .s , NASM
MASM AT&T . 9) (Borland MS
.asm MASM ) 7.15 DJGPP
NASM .
. set_data
.
set_data
. C++
.
.
2 3 . 5
EAX . x . 10)
. 6 , x EDX 7 EDX
EAX . Simple
data . Simple 0
.

9) gcc gas . gas AT&T


gas . INTEL AT&T . a2i
(http://www.multimania.com/placr/a2i.html) AT&T NASM .
10) !

136 7 C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

class Big_int {
public:
/*
* :
*
size
, unsigned int
*

*
initial_value Big_int unsigned int
*/
explicit Big_int ( size_t
size ,
unsigned initial_value = 0);
/*
* :
*
size
, unsigned int
*

*
initial_value Big_int 16
*/
Big_int ( size_t
size ,
const char * initial_value );

19
20
21

Big_int ( const Big_int & big_int_to_copy );


~Big_int ();

22
23
24

// Big_int (unsigned int )


size_t size () const;

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

const Big_int & operator = ( const Big_int & big_int_to_copy );


friend Big_int operator + ( const Big_int & op1,
const Big_int & op2 );
friend Big_int operator ( const Big_int & op1,
const Big_int & op2);
friend bool operator == ( const Big_int & op1,
const Big_int & op2 );
friend bool operator < ( const Big_int & op1,
const Big_int & op2);
friend ostream & operator << ( ostream &
os,
const Big_int & op );
private :
size_t
size_ ;
//
unsigned * number_; //
};
7.16: Big_int

C++ 137
1
2
3
4
5
6
7
8
9

//
extern C {
int add_big_ints( Big_int &
const Big_int
const Big_int
int sub_big_ints( Big_int &
const Big_int
const Big_int
}

res ,
& op1,
& op2);
res ,
& op1,
& op2);

10
11
12
13
14
15
16
17
18
19
20

inline Big_int operator + ( const Big_int & op1, const Big_int & op2)
{
Big_int result (op1.size ());
int res = add_big_ints( result , op1, op2);
if ( res == 1)
throw Big_int :: Over ow();
if ( res == 2)
throw Big_int :: Size_mismatch();
return result ;
}

21
22
23
24
25
26
27
28
29
30
31

inline Big_int operator ( const Big_int & op1, const Big_int & op2)
{
Big_int result (op1.size ());
int res = sub_big_ints( result , op1, op2);
if ( res == 1)
throw Big_int :: Over ow();
if ( res == 2)
throw Big_int :: Size_mismatch();
return result ;
}
7.17: Big_int


C++ .
() .
. . 11) (i.e 0
) 7.16 Big_int . 12) Big_int
. size_
0 , number_ 4 .
(instance)
.
3 . ( 9 )
( 18 ) 16
11) .
12) . .

138 7 C++
. ( 21 )
(copy constructor) .
,
. 7.17
.
.
C
. ,
.
(throw) .
? ,
. C++ ( C)
CPU .
C++
.
, ADC .
add_big_ints
. . (big_math.asm )

1
2

big_math.asm

segment .text
global

add_big_ints, sub_big_ints

%define size_offset 0

%define number_offset 4

5
6

%define EXIT_OK 0

%define EXIT_OVERFLOW 1

%define EXIT_SIZE_MISMATCH 2

9
10

11

%define res ebp+8

12

%define op1 ebp+12

13

%define op2 ebp+16

14
15

add_big_ints:

16

push

ebp

17

mov

ebp, esp

18

push

ebx

19

push

esi

20

push

edi

21

22

; op1 esi

23

; op2 edi

24

; res ebx

C++ 139
25

mov

esi, [op1]

26

mov

edi, [op2]

27

mov

ebx, [res]

28

29

; Big_int

30

31

mov

eax, [esi + size_offset]

32

cmp

eax, [edi + size_offset]

33

jne

sizes_not_equal

34

cmp

eax, [ebx + size_offset]

35

jne

sizes_not_equal

; op1.size_ != res.size_

37

mov

ecx, eax

; ecx = Big_int

38

39

; .

40

esi = op1.number_

41

edi = op2.number_

42

ebx = res.number_

43

44

mov

ebx, [ebx + number_offset]

45

mov

esi, [esi + number_offset]

46

mov

edi, [edi + number_offset]

; op1.size_ != op2.size_

36

47

48

clc

49

xor

50

51

; addition loop

52

edx, edx

; edx = 0

add_loop:

53

mov

eax, [edi+4*edx]

54

adc

eax, [esi+4*edx]

55

mov

[ebx + 4*edx], eax

56

inc

edx

57

loop

add_loop

jc

overflow

61

xor

eax, eax

62

jmp

done

58
59
60

63

ok_done:

overflow:

64

mov

eax, EXIT_OVERFLOW

65

jmp

done

66

sizes_not_equal:
mov

67
68

done:

eax, EXIT_SIZE_MISMATCH

; = EXIT_OK

140 7 C++
1
2
3

#include big_int . hpp


#include <iostream>
using namespace std;

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

int main()
{
try {
Big_int b(5,8000000000000a00b);
Big_int a(5, 80000000000010230);
Big_int c = a + b;
cout << a << + << b << = << c << endl;
for( int i =0; i < 2; i ++ ) {
c = c + a;
cout << c = << c << endl;
}
cout << c1 = << c Big_int(5,1) << endl;
Big_int d(5, 12345678);
cout << d = << d << endl;
cout << c == d << (c == d) << endl;
cout << c > d << (c > d) << endl;
}
catch( const char * str ) {
cerr << Caught: << str << endl;
}
catch( Big_int :: Over ow ) {
cerr << Over ow << endl;
}
catch( Big_int :: Size_mismatch ) {
cerr << Size mismatch << endl;
}
return 0;
}
7.18: Big_int

69

pop

edi

70

pop

esi

71

pop

ebx

72

leave

73

ret

big_math.asm

. 25 27
Big_int .
. 31 35
. ( size_
.) 44 46 ,
. ( number_
)
52 57 .

C++ 141

( 2.1.5 ) . 59
. ,
.
.
7.18 Big_int . Big_int
16 . . ,
Big_int .
Big_int .
.
.
. (,
)

7.2.5

(Inheritance)
. 7.19 . A B ,
B A .
Size of a: 4 Offset of ad: 0
Size of b: 8 Offset of ad: 0 Offset of bd: 4
A::m()
A::m()
(A B) ad
. f A A (i.e. )
. (i.e ) 7.20
. (gcc , NASM )
A m a b
. A::m()
.
. (polymorphism) . C++
. virtual .
7.21 .
. . gcc
.

.
.
virtual :
Size of a: 8 Offset of ad: 4
Size of b: 12 Offset of ad: 4 Offset of bd: 8
A::m()
B::m()

142 7 C++
1
2
3

#include <cstddef>
#include <iostream>
using namespace std;

4
5
6
7
8
9

class A {
public:
void __cdecl m() { cout << A :: m() << endl; }
int ad;
};

10
11
12
13
14
15

class B : public A {
public:
void __cdecl m() { cout << B :: m() << endl; }
int bd;
};

16
17
18
19
20
21

void f ( A * p )
{
p>ad = 5;
p>m();
}

22
23
24
25
26
27
28
29
30
31
32
33
34
35

int main()
{
A a;
B b;
cout << Size of
<< Oset
cout << Size of
<< Oset
<< Oset
f(&a );
f(&b);
return 0;
}

a:
of
b:
of
of

<< sizeof(a)
ad: << osetof (A,ad) << endl;
<< sizeof(b)
ad: << osetof (B,ad)
bd: << osetof (B,bd) << endl;

7.19:
1
2
3
4
5
6
7
8
9
10
11

_f__FP1A:
push
mov
mov
mov
mov
push
call
add
leave
ret

;
ebp
ebp, esp
eax, [ebp+8]
dword [eax], 5
eax, [ebp+8]
eax
_m__1A
esp, 4

; eax .
; ad 0
; A::m()
; A::m()

7.20:

C++ 143
1
2
3
4
5

class A {
public:
virtual void __cdecl m() { cout << A :: m() << endl; }
int ad;
};

6
7
8
9
10
11

class B : public A {
public:
virtual void __cdecl m() { cout << B :: m() << endl; }
int bd;
};
7.21: (Polymorphic Inheritance)

1
2
3

?f@@YAXPAVA@@@Z:
push
ebp
mov
ebp, esp

4
5
6

mov
mov

eax, [ebp+8]
dword [eax+4], 5

; p->ad = 5;

mov
mov
mov
push
call
add

ecx, [ebp + 8]
edx, [ecx]
eax, [ebp + 8]
eax
dword [edx]
esp, 4

;
;
;
;
;
;

pop
ret

ebp

7
8
9
10
11
12
13

ecx = p
edx = vtable
eax = p
"this"
vtable

14
15
16

7.22: f()

, f B B::m()
. . A 8 . ( B
12) ad 0 4 . 0 ?
.
C++
. 13) (vtable)
. A B 0
.
. f ( 7.19) (
7.22) m . 9
. 11 . 12
14) .
13) C++
C
14) ECX . 8 . 10

144 7 C++

1
2
3
4
5
6

class A {
public:
virtual void __cdecl m1() { cout << A :: m1() << endl; }
virtual void __cdecl m2() { cout << A :: m2() << endl; }
int ad;
};

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

class B : public A {
// B inherits A s m2()
public:
virtual void __cdecl m1() { cout << B :: m1() << endl; }
int bd;
};
/* vtable */
void print_vtable ( A * pa )
{
// p pa
unsigned * p = reinterpret_cast<unsigned *>(pa);
// vt
void ** vt = reinterpret_cast<void **>(p [0]);
cout << hex << vtable address = << vt << endl;
for( int i =0; i < 2; i ++ )
cout << dword << i << : << vt[ i ] << endl;

23

// !
void (*m1func_pointer)(A *);
//
m1func_pointer = reinterpret_cast<void (*)(A*)>(vt [0]);
m1func_pointer(pa);
// m1

24
25
26
27
28

void (*m2func_pointer)(A *);


//
m2func_pointer = reinterpret_cast<void (*)(A*)>(vt [1]);
m2func_pointer(pa);
// m2

29
30
31
32

33
34
35
36
37
38
39
40
41

int main()
{
A a ; B b1; B b2;
cout << a : << endl;
cout << b1: << endl;
cout << b2: << endl;
return 0;
}

print_vtable (&a );
print_vtable (&b);
print_vtable (&b2);

7.23:

C++ 145

0 vtablep s

0-

ad

4 &A::m2()

bd

&B::m1()

vtable

b1
7.24: b1
a:
vtable address = 004120E8
dword 0: 00401320
dword 1: 00401350
A::m1()
A::m2()
b1:
vtable address = 004120F0
dword 0: 004013A0
dword 1: 00401350
B::m1()
A::m2()
b2:
vtable address = 004120F0
dword 0: 004013A0
dword 1: 00401350
B::m1()
A::m2()
7.25: 7.23

, EDX .
(late binding) . .
. .
( 7.20) (early binding) . (
(compile time) .)
7.21 __cdecl
C .
C C++ .

ECX . .
__cdecl C . C+++ C
.

ECX .

146 7 C++
( 7.23). , A B
2 : m1 m2. B m2
A . 7.24 b
. 7.25
. , . B
.
( ).
. m1 0 ( 0)
m2 4 ( 1) . m2 A B
, B m2 A
.
25 32
.

15)

. C this

. 7.25
. , .
.
.
.
, .
,
. C , C
. A B
.

. COM (Component Object Model)
COM .16)
COM
. , gcc
COM .
- .
.
, . (e.g
)

7.2.6 C++
C++ (e.g , )
. ,
Ellis Stroustrup e Annotated C++ Reference Manual Stroustrup e
Design and Evolution of C++ .

15) MS . gcc
16) COM __stdcall . C

147

80x86
A

-
80x86 CPU -

.
.
R

R8

R16

16

R32

32

SR

M8

M16

M32

.
, R, R .
. O2
: R,R R,M R,I M,R M,I. 8
R/M8 .

. ,
. 1 0 .
C .
? . , CLD
STD .

ADC

O2

ADD

O2

148 7 8086

AND

And

O2

BSWAP

(swap)

R32

CALL

RMI

CBW

CDQ

CLC

CLD

CMC

CMP

CMPSB

0
C
O2

CMPSW

CMPSD

CWD


DX:AX

CWDE


EAX

DEC

RM

DIV

RM

ENTER

I,0

IDIV

RM

IMUL

R16,R/M16
R32,R/M32
R16,I R32,I
R16,R/M16,I
R32,R/M32,I

INC

RM

INT

JA

JAE

JB

JBE

JC

JCXZ

CX = 0

JE

JG

JGE

JL

JLE

JMP

RMI

- 149

JNA

JNAE

JNB

JNBE

JNC

JNE

JNG

JNGE

JNL

JNLE

JNO

JNS

JNZ

JO

JPE

JPO

JS

JZ

LAHF

AH

LEA

LEAVE

LODSB

LODSW

LODSD

LOOP

LOOPE/LOOPZ

LOOPNE/LOOPNZ

MOV

O2

R32,M

SR,R/M16
R/M16,SR
MOVSB

MOVSW

MOVSD

MOVSX

R16,R/M8
R32,R/M8
R32,R/M16

MOVZX

R16,R/M8
R32,R/M8
R32,R/M16

MUL

RM

150 7 8086

NEG

NOP

NOT

RM

OR

OR

O2

POP

R/M16

RM

R/M32
POPA

POPF

PUSH

R/M16
R/M32 I

PUSHA

PUSHF

RCL

R/M,I

R/M,CL
RCR

R/M,I
R/M,CL

REP

REPE/REPZ

REPNE/REPNZ

RET

ROL

R/M,I
R/M,CL

ROR

R/M,I
R/M,CL

SAHF

AH


SAL

R/M,I R/M,

CL
SBB

SCASB

O2

SCASW

SCASD

SETA

R/M8

SETAE

R/M8

SETB

R/M8

SETBE

R/M8

SETC

R/M8

SETE

R/M8

SETG

R/M8

SETGE

R/M8

SETL

R/M8

- 151

SETLE

R/M8

SETNA

R/M8

SETNAE

R/M8

SETNB

R/M8

SETNBE

R/M8

SETNC

R/M8

SETNE

R/M8

SETNG

R/M8

SETNGE

R/M8

SETNL

R/M8

SETNLE

R/M8

SETNO

R/M8

SETNS

R/M8

SETNZ

R/M8

SETO

R/M8

SETPE

R/M8

SETPO

R/M8

SETS

R/M8

SETZ

R/M8

SAR

R/M,I R/M,

CL
SHR

R/M,I R/M,

CL
SHL

R/M,I R/M,

CL
STC

STD

STOSB

STOSW

STOSD

SUB

O2

TEST

R/M,R

R/M,I
XCHG

R/M,R
R,R/M

XOR

XOR

O2

152 7 8086

B
80x86 .
.
.

.
STn

I16

I32

I64

(*) ) .

FABS

ST0 = |ST0|

FADD src

ST0 += src

STn F D

FADD dest, ST0

dest += STO

STn

FADDP dest[,ST0]

dest += ST0

STn

FCHS

ST0 = ST0

FCOM src

ST0 src

STn F D

FCOMP src

ST0 src

STn F D

FCOMPP src

ST0 ST1

*)

STn

STn

FDIV src

ST0 /= src

STn F D

FDIV dest, ST0

dest /= STO

STn

FDIVP dest[,ST0]

dest /= ST0

STn

FDIVR src

ST0 = src/ST0

STn F D

FDIVR dest, ST0

dest = ST0/dest

STn

FDIVRP dest[,ST0]

dest = ST0/dest

STn

FFREE dest

STn

FIADD src

ST0 += src

I16 I32

FICOM src

ST0 src

I16 I32

FICOMP src

ST0 src

I16 I32

FIDIV src

STO /= src

I16 I32

FIDIVR src

STO = src/ST0

I16 I32

FILD src

src

I16 I32 I64

FIMUL src

ST0 *= src

I16 I32

FINIT

FIST dest

ST0

FCOMI

src

*)

FCOMIP

src

I16 I32

153

FISTP dest

ST0

I16 I32 I64

FISUB src

ST0 -= src

I16 I32

FISUBR src

ST0 = src - ST0

I16 I32

FLD src

src

STn F D E

FLD1

1.0

FLDCW src

FLDPI


0.0
ST0 *= src
dest *= STO
dest *= ST0
ST0
ST0 = ST0 2bST1c

ST0 = STO
ST0
ST0


ST0 -= src
dest -= STO
dest -= ST0
ST0 = src-ST0
dest = ST0-dest
dest = ST0-dest
ST0 0.0
ST0 dest

FLDZ
FMUL src
FMUL dest, ST0
FMULP dest[,ST0]
FRNDINT
FSCALE
FSQRT
FST dest
FSTP dest
FSTCW dest
FSTSW dest
FSUB src
FSUB dest, ST0
FSUBP dest[,ST0]
FSUBR src
FSUBR dest, ST0
FSUBP dest[,ST0]
FTST
FXCH dest

I16

STn F D
STn
STn

STn F D
STn F D E
I16
I16 AX
STn F D
STn
STn
STn F D
STn
STn
STn

154

. . . . . . . . . . . . . . . . 134146

16 . . . . . . . . . . . . . . . . . . . . . . . . . 24

. . . . . . . . . . . . 130

2 . . . . . . . . . . . . . . . . . . . . . 2526

CALL . . . . . . . . . . . . . . . . . . . . . . . . 6061

. . . . . . . . . . . . . . . . 2933

CBW . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

80x86
CPU . . . . . . . . . . . . . . . . . . . . . . . . . . 5
A
ADC . . . . . . . . . . . . . . . . . . . . . . . . . 32, 47
ADD . . . . . . . . . . . . . . . . . . . . . . . . . 11, 32
AND . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
array1.asm . . . . . . . . . . . . . . . . . . . 8589
B

CLC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
CLD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
CMP . . . . . . . . . . . . . . . . . . . . . . . . . 3334
CMPSB . . . . . . . . . . . . . . . . . . . . . . . . . . 94
CMPSD . . . . . . . . . . . . . . . . . . . . . . . . . . 94
CMPSW . . . . . . . . . . . . . . . . . . . . . . . . . 94
COM . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
CPU . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

BSWAP . . . . . . . . . . . . . . . . . . . . . . . . . . 52

CWD . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

BYTE . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

CWDE . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

C driver . . . . . . . . . . . . . . . . . . . . . . . . . . 17

DEC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

C . . . . . . . . . . . . . . . . . . . . 70

DIV . . . . . . . . . . . . . . . . . . . . . . . . . . 30, 42

C . . . . . . . . . . . . . . . 78

do while . . . . . . . . . . . . . . . . . . . . . 38

C++ . . . . . . . . . . . . . . . . . . . . . . . 129146

DWORD . . . . . . . . . . . . . . . . . . . . . . . . . 14

Big_int . . . . . . . . . . . 137141
name mangling . . . . . . . . . . . . . 129
references . . . . . . . . . . . . . . . . . . 132
virtual . . . . . . . . . . . . . . . . . . . . . . 141
vtable . . . . . . . . . . . . . . . . . 143, 146
. . . . . . . . . . . . . . . . 145
. . . . . . . . . . . . . . . . 141146
. . . . . . . . . . . . . . . . . . . 132
. . . . . . , 2
. . . . . . . . . . . . . . . . 138
. . . . . . . . . . . . . . . . 145
. . . . . . . . . . . . . . . . . . 141146
. . . . . . . . . . . . . . . . 132
. . . . . . . . . . . 132134

F
FABS . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
FADD . . . . . . . . . . . . . . . . . . . . . . . . . . 109
FADDP . . . . . . . . . . . . . . . . . . . . . . . . . 109
FCHS . . . . . . . . . . . . . . . . . . . . . . . . . . 112
FCOM . . . . . . . . . . . . . . . . . . . . . . . . . . 111
FCOMI . . . . . . . . . . . . . . . . . . . . . . . . . 112
FCOMIP . . . . . . . . . . . . . . . . . . . 112, 113
FCOMP . . . . . . . . . . . . . . . . . . . . . . . . 111
FCOMPP . . . . . . . . . . . . . . . . . . . . . . . 111
FDIV . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
FDIVP . . . . . . . . . . . . . . . . . . . . . . . . . 111
FDIVR . . . . . . . . . . . . . . . . . . . . . . . . . 111
FDIVRP . . . . . . . . . . . . . . . . . . . . . . . . 111

155
FFREE . . . . . . . . . . . . . . . . . . . . . . . . . . 109

read_int . . . . . . . . . . . . . . . . . . . 15

FIADD . . . . . . . . . . . . . . . . . . . . . . . . . 109

I/O . . . . . 14

FICOM . . . . . . . . . . . . . . . . . . . . . . . . . 111

IDIV . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

FICOMP . . . . . . . . . . . . . . . . . . . . . . . . 111

if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

FIDIV . . . . . . . . . . . . . . . . . . . . . . . . . . 111

IMUL . . . . . . . . . . . . . . . . . . . . . . . . 2930

FIDIVR . . . . . . . . . . . . . . . . . . . . . . . . . 111

INC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

FILD . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
FIST . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
FISUB . . . . . . . . . . . . . . . . . . . . . . . . . . 110
FISUBR . . . . . . . . . . . . . . . . . . . . . . . . . 110
FLD . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
FLD1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
FLDCW . . . . . . . . . . . . . . . . . . . . . . . . 109
FLDZ . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
FMUL . . . . . . . . . . . . . . . . . . . . . . . . . . 111
FMULP . . . . . . . . . . . . . . . . . . . . . . . . . 111
FSCALE . . . . . . . . . . . . . . . . . . . 112, 113
FSQRT . . . . . . . . . . . . . . . . . . . . . . . . . 112
FST . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
FSTCW . . . . . . . . . . . . . . . . . . . . . . . . . 109
FSTP . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
FSTSW . . . . . . . . . . . . . . . . . . . . . . . . . 112
FSUB . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
FSUBP . . . . . . . . . . . . . . . . . . . . . . . . . 110
FSUBR . . . . . . . . . . . . . . . . . . . . . . . . . 110
FSUBRP . . . . . . . . . . . . . . . . . . . . . . . . 110
FTST . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
FXCH . . . . . . . . . . . . . . . . . . . . . . . . . . 109
G
gas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
I

J
JC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
JE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
JG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
JGE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
JL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
JLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
JMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
JNC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
JNE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
JNG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
JNGE . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
JNL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
JNLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
JNO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
JNP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
JNS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
JNZ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
JO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
JP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
JS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
JZ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
L
LAHF . . . . . . . . . . . . . . . . . . . . . . . . . . 112
LEA . . . . . . . . . . . . . . . . . . . . . . . . . . 73, 89

I/O . . . . . . . . . . . . . . . . . . . . . . . . . . 1416

listing le . . . . . . . . . . . . . . . . . . . . . . . . 21

asm_io library . . . . . . . . . . . . . . . . 16

LODSB . . . . . . . . . . . . . . . . . . . . . . . . . . 92

dump_math . . . . . . . . . . . . . . . 16

LODSD . . . . . . . . . . . . . . . . . . . . . . . . . . 92

dump_mem . . . . . . . . . . . . . . . . 16

LODSW . . . . . . . . . . . . . . . . . . . . . . . . . 92

dump_regs . . . . . . . . . . . . . . . . . 15

LOOP . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

dump_stack . . . . . . . . . . . . . . . . 16

LOOPE . . . . . . . . . . . . . . . . . . . . . . . . . . 37

print_char . . . . . . . . . . . . . . . . . 15

LOOPNE . . . . . . . . . . . . . . . . . . . . . . . . 37

print_int . . . . . . . . . . . . . . . . . . . 15

LOOPNZ . . . . . . . . . . . . . . . . . . . . . . . . 37

print_nl . . . . . . . . . . . . . . . . . . . 15

LOOPZ . . . . . . . . . . . . . . . . . . . . . . . . . . 37

print_string . . . . . . . . . . . . . . . . 15
read_char . . . . . . . . . . . . . . . . . . 15

M
MASM . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

156
math.asm . . . . . . . . . . . . . . . . . . . . . 3032

SCASW . . . . . . . . . . . . . . . . . . . . . . . . . . 94

memory.asm . . . . . . . . . . . . . . . . . 95100

SCSI . . . . . . . . . . . . . . . . . . . . . . . 126128

MOV . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

SETxx . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

MOVSB . . . . . . . . . . . . . . . . . . . . . . . . . . 93

SETG . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

MOVSD . . . . . . . . . . . . . . . . . . . . . . . . . 93

SHL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

MOVSW . . . . . . . . . . . . . . . . . . . . . . . . . 93

SHR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

MOVSX . . . . . . . . . . . . . . . . . . . . . . . . . 28

STD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

MOVZX . . . . . . . . . . . . . . . . . . . . . . . . . 27

STOSB . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

MUL . . . . . . . . . . . . . . . . . . . . . 29, 42, 89

STOSD . . . . . . . . . . . . . . . . . . . . . . . . . . 92

N
NASM . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

SUB . . . . . . . . . . . . . . . . . . . . . . . . . . 11, 32
T

NEG . . . . . . . . . . . . . . . . . . . . . . . . . 30, 48

TASM . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

NOT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

TCP/IP . . . . . . . . . . . . . . . . . . . . . . . . . . 51

O
OR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
P
prime.asm . . . . . . . . . . . . . . . . . . . . 3840
prime2.asm . . . . . . . . . . . . . . . . 119122
Q
quad.asm . . . . . . . . . . . . . . . . . . 114116
QWORD . . . . . . . . . . . . . . . . . . . . . . . . . 14
R
RCL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
RCR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

TEST . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
TWORD . . . . . . . . . . . . . . . . . . . . . . . . . 14
U
UNICODE . . . . . . . . . . . . . . . . . . . . . . . 51
W
while . . . . . . . . . . . . . . . . . . . . . . . 38
WORD . . . . . . . . . . . . . . . . . . . . . . . . . . 14
X
XCHG . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
XOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

read.asm . . . . . . . . . . . . . . . . . . . 116119

. . . . . . . . . . . . . . . . . . . 57

REP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

. . . . . . . . . . . . . . . . . . . . . 8589

REPE . . . . . . . . . . . . . . . . . . . . . . . . 94, 95

. . . . . . . . . . . . . . . . . . . . . . . . 20

REPNE . . . . . . . . . . . . . . . . . . . . . . . 94, 95

. . . . . . . . . . . . . . . . . . . . . 123129

REPNZ . . . . . . . . . . . . REPNE , 2

osetof() . . . . . . . . . . . . . . . . . . . 124

REPZ . . . . . . . . . . . . . . . . REPE , 2

. . . . . . . . . . . . . . 124125

RET . . . . . . . . . . . . . . . . . . . . . . 6061, 63

. . . . . . . . . . . . . . 125128

ROL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

. . . . . . . . . . . . . . . . . . . . . . . . 5, 10

ROR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
S
SAHF . . . . . . . . . . . . . . . . . . . . . . . . . . 112
SAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
SAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
SBB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
SCASB . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
SCASD . . . . . . . . . . . . . . . . . . . . . . . . . . 94

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

. . . . . . . . . . . 6870
. . . . . . . . . . . . . . . . . . 19
. . . . . . . . . . . . . . . . . . . . . . . 1516

157

. . . . . . . . . . . . . . . . . . . . . . . . . 1314

16 . . . . . . . . . . . . . . . . . . . . . . . 8

. . . . . . . . . . . . . . . . . . . . . . . 57

32 . . . . . . . . . . . . . . . . . . . . . . . 9

32 . . . . . . . . . . . . . . . . . . . . . . . 7

. . . . . . . . . . . . . . . . 101122

EDI . . . . . . . . . . . . . . . . . . . . . . . . . 93

. . . . . . . . . . . . . . 106107

EDX:EAX . . . . . . . . . 2830, 32, 73

. . . . . . . . . . . . . . . . . . 101105

EFLAGS . . . . . . . . . . . . . . . . . . . . . . 7

2 . . . . . . . . . . . . . . . 105

EIP . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

IEEE . . . . . . . . . . . . . . . . 103105

ESI . . . . . . . . . . . . . . . . . . . . . . . . . . 93

. . . . . . . . . 104105

IP . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

. . . . . . . . . . . . . . . . . 105

. . . . . . . . . . . . . . . . . 7

1 . . . . . . . . . . . . . . . . . 104

. . . . . . . . . . . . . . . . . . . . . . . . . . 6

. . . . . 108122

. . . . . . . . . . . . . . . . 6, 7, 93

. . . . . . . . . 110111

. . . . . . . . . . . . . . . . . 6, 7

. . . . . . . . . . . 109110

. . . . . . . . . . . . . . . . . 6

. . . . . 108109

. . . . . . . . . . . . . . . . . 7, 3334

. . . . . . . . . . . . . . . . . . 111112

CF . . . . . . . . . . . . . . . . . . . . . . . . 33

. . . . . . . . . . . . . . . . . . . 108

DF . . . . . . . . . . . . . . . . . . . . . . . . 92

. . . . . . . . . . . . . . . . . . . . . . . . 47

OF . . . . . . . . . . . . . . . . . . . . . . . . 33

. . . . . . . . . . . . . . . . . . 5256

PF . . . . . . . . . . . . . . . . . . . . . . . . 34

. . . . . . . . . . . . . . . . . 54

SF . . . . . . . . . . . . . . . . . . . . . . . . 33

. . . . . . . . . . . . . 5456

ZF . . . . . . . . . . . . . . . . . . . . . . . . 33

. . . . . . . . . . . . . . . . . 53

. . . . . . . . . . . . . . . . . . . . . . 22
. . . . . . . . . . . . . . . . . . . . . . . . . 2021

. . . . . . . . . . . . . . . . . . . . . . . . . . 45
. . . . . . . . . . . . . . . . . . . . . . . . 8, 9
. . . . . . . . . . . . . . . . . . . 8, 9
. . . . . . . . . . . . . . . . . . . . . . . . 9
: . . . . . . . . . . . . . . . . . . 8
. . . . . . . . . . . . . . . . . . . . . . . . . . 134
. . . . . . . . . . . . . . . . . 92100

. . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
. . . . . . . . . . . . . . . . . . . . . . . . 82100
. . . . . . . . . . . . . . . . . . . 8992
2 . . . . . . . . . . . . . . . . . 8990
. . . . . . . . . . . . . . . . . . . 9192
. . . . . . . . . . . . . . . . . . . . . 8389
. . . . . . . . . . . . . . . . . . . . . 8283
. . . . . . . . . . . . . . . . . . . . . . 82
. . . . . . . . . . . . . . . . . . 82


AND . . . . . . . . . . . . . . . . . . . . . . . . 43
C . . . . . . . . . . . . . . . . . . . . . . . . 4950
NOT . . . . . . . . . . . . . . . . . . . . . . . . 45
OR . . . . . . . . . . . . . . . . . . . . . . . . . . 44
XOR . . . . . . . . . . . . . . . . . . . . . . . . 44
. . . . . . . . . . . . . . . . . . . 4143
. . . . . . . . . . . . 4142
. . . . . . . . . . . . . . . 42
. . . . . . . . . . . . . . . . . . . . . . 43
. . . . . . . . . . . . . . . 42
. . . . . . . . . . . . . . . . . 4546
. . . . . . . . . . . . . . . . . . . . . . . . 22

. . . . , 2
. . . . . . . . . . . . . . . . . 5881
. . . . . . . . . . . . . . . . . . . . . . 78
. . . . . . . . . . . . . . . . . . . . . 6068
. . . . . . . . . . . . . . . . . . . . . . 60, 6268
. . . . . . . . . . . . . . . . . . . . . 6264
. . . . . . . . . . . . . 68, 7273

158
. . . . . . . . . . . . . . . . . . . . 66

equ . . . . . . . . . . . . . . . . . . . . . . . . . . 12

. . . . . . . . . . . . . . . . . . . . . . . . . 7

extern . . . . . . . . . . . . . . . . . . . . . . . 68

. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

RESX . . . . . . . . . . . . . . . . . . . . 13, 82

. . . . . . . . . . . . . . . . . . . . . . . . . 10
. . . . . . . . . . . . . . . . . . . 1011
. . . . . . . . . . . . . . . . . . . . 22, 5052
. . . . . . . . . . . . . . . . . . 52
. . . . . . . . . . . . . . . . . . . . . . . . 10
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
. . . . . . . . . . . . . . . . . . . . . . . . . . 12
. . . . . . . . . . . . . . . . . . . . . 2
. . . . . . . . . . . . . . . . . . . . . . . . . . 9

TIMES . . . . . . . . . . . . . . . . . . . 13, 82
. . . . . . . . . . . . . . . . . . . 1314
. . . . . . . . . . . . . . . . . . 19, 68, 70
. . . . . . . . . . . . . . . . . . . . . . . . . . 123

. . . . . . . . . . . . . . . . . . . . . . 47

. . . . . . . . . . . . . . . . . . . . . . 5, 10
DJGPP . . . . . . . . . . . . . . . . . . . 19, 20
gcc . . . . . . . . . . . . . . . . . . . . . . . . . . 19
__attribute__ 73, 125, 127, 128

. . . . . . . . . . . . . . . . . . . . . . . . . 7880

Watcom . . . . . . . . . . . . . . . . . . . . . 73

. . . . . . . . . . . . . . 20

. . . . . . . . . . . . . . . . . . . . 80

pragma pack . . . . . . . . . 125128

. . . . . . . . . . . . . . . . . . . . . . . . . 80

. . . . . . . . . . . . . . . . . . . 20, 21

. . . . . . . . . . . . . . . . . . . . . . . . . 80

. . . . . . . . . . . . . . . . . . . . 19

. . . . . . . . . . . . . . . . . . . . . . . . . 80
. . . . . . . . . . . . . . . . . . . . . . 80
. . . . . . . . . . . . . . . . . . . . . . . . . 2434

,
2

. . . . . . . . . . . . . . . . . . . . . 2930
. . . . . . . . . . . . . . . . . . . . . . 30

. . . . . . . . . . . . . . . . 24, 27

. . . . . . . . . . . . . 6168, 7374

. . . . . . . . . . . 24, 33

__cdecl . . . . . . . . . . . . . . . . . . . . . . 74

. . . . 2426, 3334

__stdcall . . . . . . . . . . . . . . . . . . . . . 74

. . . . . . . . . . . . . . . . 2629

C . . . . . . . . . . . . . . . . . 19, 63, 7074

. . . . . . . . . . . . . . . . . . . . . 3334

. . . . . . . . . . . . . . . . . . . . . . 72

. . . . . . . . . . . . . . . . . . . . . 2429

. . . . . . . . . . . . . . . . . . 71

1 . . . . . . . . . . . . . . . . . . 24

. . . . . . . . . . . . . . . . . . . . 73

2 . . . . . . . . . . . . . . . 2526

. . . . . . . . . . . . . . . . . . . . . . 72

. . . . . . . . . . . . . 24

stdcall . . . . . . . . . . . . . . . 63, 73, 146

. . . . . . . . . . . . . . 3233

. . . . . . . . . . . . . . . . . . . . 74

. . . . . . . . . . . . . . . . . . . . 3436

. . . . . . . . . . . . . . . . . . . . . . 63

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

. . . . . . . . . . . . . . . . . . . . 74

. . . . . . . . . . . . . . . . . . . . 11
. . . . . . . . . . . . . . . . . . . . . . . 1214
% . . . . . . . . . . . . . . . . . . . . . . . 12
DX . . . . . . . . . . . . . . . . . . . . . . 13, 82
DD . . . . . . . . . . . . . . . . . . . . . . . . . . 13
DQ . . . . . . . . . . . . . . . . . . . . . . . . . 13

You might also like