Danh mục

PC Underground: Phần 1

Số trang: 228      Loại file: pdf      Dung lượng: 2.10 MB      Lượt xem: 13      Lượt tải: 0    
Thư viện của tui

Phí tải tài liệu: 4,000 VND Tải xuống file đầy đủ (228 trang) 0
Xem trước 10 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Part 1 Pc Underground give readers the knowledge overview: Assembly language the true language of programmers, assembly language in practice, mode X the secret to great graphics, split-screen and other hot effects,... document serve you specialized information technology and related industries.
Nội dung trích xuất từ tài liệu:
PC Underground: Phần 1 1. Assembly Language: The True Language Of Programmers Assembly PC und erg r o u n d Language: The True Language Of Programmers Chapter 1 There are many high-level, structured languages for programming today's PCs. Two popular examples are C++ and Pascal. However, assembly language still has its place in today's programming world. Since it mimics the operations of the CPU at the machine level, assembly language lets you get right to the heart of your PC. In fact, there are some tasks that you can do only by using assembly language. While it's true that the Pascal language is capable enough to handle interrupts, it can't be used to pass keyboard input to DOS, for example. Since Pascal has no native way to do this, you must still insert an assembler module routine to perform the function. Likewise, you can't easily remove a high-level resident program from memory. Once again, you have to write the routine in assembly language to do this. For many applications, programming code must still be as compact as possible. For example, in programming resident programs, each kilobyte of RAM below the 640K boundary is vital. Programs written in high-level languages usually require a runtime library which may add several additional kilobytes to the size. Assembly language programs don't need these bulky library routines. However, the most important advantage of assembly language is speed. Although high-level languages can be optimized for speed of execution, even the best optimization cannot replace the experience of a programmer. Here's a simple example. Let's say that you want to initialize two variables in Pascal to a zero value. The compiler will generate the following assembly code: xor ax,ax mov var1,ax xor ax,ax mov var2,ax Here, the Pascal compiler optimized the execution speed by using the XOR instruction to zero the ax register (the fastest way to do this) and storing this value as var1. However, due to compiler's limitations, the AX register was again zeroed before the second assignment although this was redundant. For truly time-critical tasks such as sprite movement and high-speed graphics, the only choice may be to use assembly language. There are two basic ways to do this: 1. Use an internal assembler such as the one built into Borland Pascal and its asm directive. 2. Use a stand-alone assembler such as Turbo Assembler or Microsoft Assembler. Each way has its own advantages and disadvantages but using the stand-alone assembler is usually the better choice. 1 Chapter 1 Assembly Language: The True Language Of Programmers PC und ergr oun d The stand-alone assembler is designed from the ground up for writing full assembly language programs - not as an add-on to a high-level language. A stand-alone assembler has a complete programming environment with many convenient features. For example, it has directives such as db 20 dup that makes programming easier. Only a limited number of directives are available from built-in assemblers. Stand- alone assemblers also offer the advantage of macros which speed up assembly language programming tasks. We've chosen to use a stand-alone assembler in this book wherever possible. Of course there are exceptions such as if the assembly language routine module has to access a procedure's local variables as in Borland's GetSprite and PutSprite procedures. Multiplication And Division In Assembly Language Today's 486 DX4es and Pentiums are fast. These speed demons can perform a multiplication operation in only six clock cycles. This is a far cry from the 100+ cycles that were required using the ancient 8086 processors or about 20 cycles using yesterday's 286es. However, if you really want to impress people with fast multiplication, you can use the shift instructions. The number of bits by which you're shifting corresponds to the exponent of the multiplicand to base 2; to multiply by 16, you would shift 4 bits since 16 equals 2 to the 4th power. The fastest method of multiplying the AX register by 8 is the instruction SHL AX,3 which shifts each bit to a position eight times higher in value. Conversely, you can perform division by shifting the contents to the right. For example, SHR AX,3 divides the contents of the AX register by 8. In the early days of computing, numerical analysts suggested other ways to speed up computations. One common technique was to use factoring. For example, multiplication by 320 can be factored like this: 1. Multiplication of the value by 256 (shift by 8 bits) 2. Multiplication of a copy of the value by 64 (shift by 6 bits) 3. Addition of the two results from above Mathematicians call this factoring according to the distributive law. Fixed Point Arithmetic The preceding examples assume that the values you're working with are integers. But for many applications, it's not always appropriate or possible to use integers. In programming graphics, for example, to draw a line on the screen you need to know the slope of the line. Practically speaking, the slope is seldom an integral number. Normally, in such cases, you would use real (Pascal) or float (C) values which are based on floating point representation. Floating point numbers allow a variable number of decim ...

Tài liệu được xem nhiều: