Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Windows assembly language & systems programming
Nội dung xem thử
Mô tả chi tiết
WINDOWS ASSEMBLY
LANGUAGE & SYSTEMS
PROGRAMMING
16- and 32-bit low-level programming
for the PC and Windows
2nd edition
bY
Barry Kauler
Lecturer, Edith Cowan University
M.Sc.(EE), C.Eng.
R&D Books
Lawrence, Kansas 66046
0 Copyright 1997, Barry Kauler
All rights reserved. No part of this publication may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, electronic, mechanical,
photocopying, recording, or otherwise, without prior written permission of the Publisher.
In this book, many of the designations used by manufacturers and sellers to distinguish
their products may be claimed as trademarks. Due acknowledgement is hereby made of
all legal protection. Windows TM is a trademark of Microsoft Corporation.
Disclaimer. Whilst due care has been taken in the preparation of this book, no
responsibility is accepted for any inaccuracy, loss or damage to data, or consequential
loss or damage. The content of the Companion Disk is not guaranteed to be exactly as
described.
This edition published by R&D Books / Miller Freeman
ISBN: 087930474X
DISTRIBUTION:
USA
Publishers Group West
P.O. Box 8843
Emeryville, CA 94662
Tel: (800) 788-3123
Fax: (510) 658-1834
UK and Europe
McGraw-Hill Publishing Co.
Shopper&angers Road
Maidenhead
Berkshire SL6 2QL
United Kingdom
Tel: 0800 810800 or 01628 502500
Fax: 01628 770224
e-mail: [email protected]
Latin America
ID International
126 Old Ridgefield Road
Wilton, CT 06897 USA
Tel: (203) 834-2272
Fax: (203) 762-9725
Canada
Publishers Group West Canada
543 Richmond Street West
Suite 223
Toronto, Ontario M5V lY6
Canada
Tel: (416) 504-3900
Fax: (4 16) 504-3902
Asia
Longman Singapore
25 First Lok Yang Road
Singapore 2262
Tel: 65 268 2666
Fax: 65 268 7023
Editorial & Marketing Oftice
R&D Books
1601 West 23rd Street, Suite 200
Lawrence, KS 66046
Tel: (913) 841-1631
Fax: (913) 841-2624
e-mail: [email protected]
Web: http://www.rdbooks.com
Contents
Ch.
Preface
Page
xi
1 CPU Architecture 1
Preamble ..................................................1
Power-up the PC ........................................... 2
The System Files ....................................... 3
Number Systems ........................................... 6
Registers and Memory ..................................... 9
MemoryMapofthePC .............................. 12
The CPU & Support Chips ........................... 12
Conventional and Extended Memory ..................... 14
Segments ............................................ 14
Real Mode ........................................... 17
DOS Real Mode Programming ....................... 18
DOS Protected Mode Programming .................. 18
Coding Restraints .................................... 20
Inside the 286/386l486/etc. ............................... 2 1
CPU Registers ....................................... 22
Instructions .......................................... 23
Real and Protected Modes ............................ 25
Memory Management .................................... 25
Segmentation Only ................................... 25
Shadow Registers .................................... 26
Descriptors .......................................... 28
386 Paging ........................................... 28
Virtual-86 ........................................... 29
Contention Issues ........................................ 3 1
Privileges.. .......................................... 31
I/O Privilege ......................................... 3 1
Task Switching ...................................... 32
Interrupts ................................................ 3 3
Real Mode Interrupts ................................. 33
Protected Mode Interrupts ............................ 34
Postamble ............................................... 36
..I
111
iv
Ch. Page
2 Basic Assembly Language 37
Preamble ................................................ 3 7
Stack Instructions ........................................ 38
Transfer of Control ....................................... 39
Conditional Jump .................................... 43
Addressing Modes ....................................... 44
Segment Registers ................................... 46
String Instructions ....................................... 47
Arithmetic Instructions ................................... 50
Logical Instructions ...................................... 54
Code and Data Labels .................................... 56
Code Labels ......................................... 56
Data Labels .......................................... 58
Accessing Data ...................................... 5 8
Pointers .............................................. 59
LES, LDS, and LEA Instructions ..................... 60
Local Data ........................................... 62
Type Override ........................................... 63
Structures ................................................ 65
Label Equates ........................................ 66
Postamble ...............................................67
3 Opening Windows 69
Preamble ................................................69
DOS versus Windows Programming ..................... 70
Internal Differences .................................. 7 1
Building a Windows Application ......................... 72
Library Functions .................................... 72
The Mechanics of Assembling and Linking ........... 73
The Link Step ........................................ 74
Two Steps for Resources ............................. 74
Windows Programming Mechanics ....................... 75
Objects .............................................. 75
Handles .............................................. 76
Instances ............................................. 76
Messages ............................................ 77
C Syntax ............................................. 78
Message Loop ....................................... 78
Callback Functions ................................... 79
Data Types .............................................. 82
V
Ch. Page
4 The Bare Bones 85
Preamble ................................................ 85
Getting Started ........................................... 86
Tools Required ...................................... 86
Source Files ......................................... 89
Resource and Definition Files ........................ 89
Message Format ..................................... 90
Make File ............................................ 91
Development Cycle .................................. 92
Application Structure .................................... 94
Preliminary Code .................................... 94
Startup Code ......................................... 96
WINMAIN{) ......................................... 98
Callback Function ................................... 102
5 High-Level Assembly 109
Preamble ............................................... 109
Include Files ............................................ 109
Microsoft versus Borland ........................... 110
Skeleton Analysis ....................................... 111
.MODEL Directive ...................................... 119
Private and Global Data ................................. 120
MASM versus TASM Scope ........................ 121
TASM’s @@ ....................................... 121
Life of Automatic Data .............................. 122
Assembling and Linking ................................ 123
MASM6 versus TASM .................................. 125
WINDOWS Qualifier ............................... 126
Prototypes .......................................... 127
Callback Design .................................... 129
Other Incompatibilities .............................. 130
MASM Assembling and Linking .................... 13 l
MASM6 Program Listing ............................... 132
6 Program Design 137
Preamble ...............................................137
Object Addressing ...................................... 138
Calling a Function .................................. 138
Early Binding ....................................... 14 1
Late Binding ........................................ 142
C++ Binding ........................................ 142
vi
Ch. Page
Assembly Language Binding ........................ 145
Use of THIS ............................................ 145
Interfacing with C++ .................................... 147
Compiling to ASM O/P ............................. 147
In-Line Assembly ................................... 148
In-Line DOS and Don’ts .............................. 149
The ASM Stub ...................................... 150
Compile and Assemble Steps ........................ 15 1
The Amazing 9-Line Program ........................... 153
A Skeleton Program ..................................... 154
Overrides ........................................... 156
Kickstart ............................................ 157
Message Handling .................................. 157
The WINDOW Object .................................. 158
WINMAIN ) ........................................... 162
Callback ................................................ 165
MA=( ) ............................................... 168
Inheritance .............................................. 171
Getting it Together ...................................... 175
Postamble .............................................. 178
7 PC Hardware 179
Preamble ............................................... 179
CPU bus ................................................ 179
Control Bus ......................................... 180
Address Decoder .................................... 182
I/OPorts ................................................ 183
I/O Instructions ..................................... 184
Keyboard Interface ...................................... 184
AT-Class Keyboard Port Enhancements ............. 186
PC Expansion Buses .................................... 187
Industry Standard Architecture (ISA) ................ 188
Peripheral Connect Interface (PCI) ...................... 19 1
Postamble .............................................. 194
8 BIOS, DOS, & Windows Low-Level Services 195
Preamble ............................................... 195
BIOS and DOS Services ................................ 197
Standard DOS Interrupts ............................ 200
DOS Protected Mode Interface (DPMI) ................. 203
INT-2Fh Extensions ..................................... 205
vii
Ch.
9
10
11
Page
Windows Functions ..................................... 207
Thunking ............................................... 219
Generic Thunking ................................... 219
More Win95 “Improvements” ........................... 222
Device I/O Control .................................. 222
Dynamically Loadable Drivers ...................... 223
Threads ............................................. 223
Memory Mapped Files .............................. 224
Postamble .............................................. 224
Direct Hardware Access 225
Preamble ............................................... 225
Initialisation ............................................ 226
Addressing Segments ................................... 227
Direct Video ............................................ 229
Restore Video ....................................... 23 1
Change Video Mode ................................ 232
A Direct-Video Text-Mode Routine ................. 232
Call REPAINTSCREEN .......................... 234
Ordinal Coordinates ................................. 235
To and From Text Mode ............................ 236
Video Output Issues ................................. 237
MessageInput ...................................... 238
Experimenting ...................................... 239
A Direct-Video Window Program ................... 239
I/O Ports ................................................ 244
Real-Time Events 249
Preamble ............................................... 249
TSRs ...................................................250
Hooking a Vector ................................... 251
Service Routine (ISR) ............................... 253
Testing ............................................. 255
Hardware Interrupts ..................................... 256
XT Hardware Interrupts ............................. 256
AT Hardware Interrupts ............................. 257
Windows’ Standard Mode Hardware Interrupts ...... 258
Interrupt Handler Code .............................. 260
Enhanced Mode Hardware Interrupts ................ 263
Direct Memory Access .................................. 264
Real Mode Access 267