Example: air traffic controller

PC Assembly Lanugage - Distributed operating system

PC Assembly LanguagePaul A. CarterJanuary 15, 2002 Copyrightc 2001 by Paul CarterThis may be reproduced and Distributed in its entirety (including this au-thorship, copyright and permission notice), provided that no charge is madefor the document itself, without the author s consent. This includes fairuse excerpts like reviews and advertising, and derivative works like that this restriction is not intended to prohibit charging for the serviceof printing or copying the are encouraged to use this document as a class resource; however,the author would appreciate being notified in this Number Systems .. Computer Organization .. CPU .. 80x86 family of CPUs .. 16-bit Registers .. 32-bit registers .. Mode .. Protected Mode.

Other PC assembly language books still teach how to program the 8086 processor that the original PC used in 1980! This book instead discusses how to program the 80386 and later processors in protected mode (the mode that Windows runs in). There are several reasons to do this: 1. It is easier to program in protected mode than in the 8086 real mode

Tags:

  Language, Assembly, Pc assembly language, Pc assembly

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of PC Assembly Lanugage - Distributed operating system

1 PC Assembly LanguagePaul A. CarterJanuary 15, 2002 Copyrightc 2001 by Paul CarterThis may be reproduced and Distributed in its entirety (including this au-thorship, copyright and permission notice), provided that no charge is madefor the document itself, without the author s consent. This includes fairuse excerpts like reviews and advertising, and derivative works like that this restriction is not intended to prohibit charging for the serviceof printing or copying the are encouraged to use this document as a class resource; however,the author would appreciate being notified in this Number Systems .. Computer Organization .. CPU .. 80x86 family of CPUs .. 16-bit Registers .. 32-bit registers .. Mode .. Protected Mode.

2 Protected Mode .. Assembly language .. language .. language .. operands .. instructions .. and Output .. Creating a Program .. program .. dependencies .. the code .. the C code .. the object files .. an Assembly listing file .. Skeleton File ..242 Basic Assembly Working with Integers .. representation .. extension .. s complement arithmetic .. program .. precision arithmetic .. Control Structures .. instructions .. loop instructions .. Translating Standard Control Structures .. statements .. loops .. while loops .. Example: Finding Prime Numbers ..413 Bit Shift Operations .. shifts .. of shifts .. shifts .. shifts .. application.

3 Boolean Bitwise Operations .. of boolean operations .. Manipulating bits in C .. bitwise operators of C .. bitwise operators in C .. Counting Bits .. one .. two .. Three ..55 CONTENTSiii4 Indirect Addressing .. Simple Subprogram Example .. The Stack .. The CALL and RET Instructions .. Calling Conventions .. parameters on the stack .. variables on the stack .. Multi-Module Programs .. Interfacing Assembly with C .. registers .. of functions .. parameters .. addresses of local variables .. values .. calling conventions .. C functions from Assembly .. Reentrant and Recursive Subprograms .. subprograms .. of C variable storage types ..855 Introduction.

4 Arrays .. elements of arrays .. advanced indirect addressing .. Array/String Instructions .. and writing memory .. prefix .. string instructions .. prefixes ..1006 Floating Floating Point Representation .. binary numbers .. floating point representation .. Floating Point Arithmetic .. and division .. for programming .. The Numeric Coprocessor .. formula .. array from file .. primes ..1257 Structures and C++ Structures .. alignment .. structures in Assembly .. Assembly and C++ .. and Name Mangling .. functions .. and Polymorphism .. C++ features ..157A 80x86 Non-floating Point Instructions .. Floating Point Instructions ..165 PrefacePurposeThe purpose of this book is to give the reader a better understanding ofhow computers really work at a lower level than in programming languageslike Pascal.

5 By gaining a deeper understanding of how computers work, thereader can often be much more productive developing software in higher levellanguages such as C and C++. Learning to program in Assembly languageis an excellent way to achieve this goal. Other PC Assembly language booksstill teach how to program the 8086 processor that the original PC usedin 1980! This book instead discusses how to program the 80386 and laterprocessors in protected mode (the mode that Windows runs in). There areseveral reasons to do is easier to program in protected mode than in the 8086 real modethat other books modern PC operating systems run in protected is free software available that runs in this lack of textbooks for protected mode PC Assembly programming is themain reason that the author wrote this alluded to above, this text makes use of Free/Open Source software:namely, the NASM assembler and the DJGPP C/C++ compiler.

6 Both ofthese are available to download off the Internet. The text also discusses howto use NASM Assembly code under the Linux operating system and withBorland s and Microsoft s C/C++ compilers under aware that this text does not attempt to cover every aspect of assem-bly programming. The author has tried to cover the most important topicsthatallprogrammers should be acquainted author would like to thank the many programmers around the worldthat have contributed to the Free/Open Source movement. All the programsand even this book itself were produced using free software. Specifically, theauthor would like to thank John S. Fine, Simon Tatham, Julian Hall andothers for developing the NASM assembler that all the examples in this bookare based on; DJ Delorie for developing the DJGPP C/C++ compiler used;Donald Knuth and others for developing the TEX and LATEX 2 typesettinglanguages that were used to produce the book; Richard Stallman (founder ofthe Free Software Foundation), Linus Torvalds (creator of the Linux kernel)and others who produced the underlying software the author used to producethis to the following people for corrections: John S.

7 Fine Marcelo Henrique Pinto de Almeida Sam Hopkins Nick D Imperio Jeremiah LawrenceResources on the InternetAuthor s author welcomes any feedback on this Number SystemsMemory in a computer consists of numbers. Computer memory doesnot store these numbers in decimal (base 10). Because it greatly simplifiesthe hardware, computers store all information in a binary (base 2) let s review the decimal DecimalBase 10 numbers are composed of 10 possible digits (0-9). Each digit ofa number has a power of 10 associated with it based on its position in thenumber. For example:234 = 2 102+ 3 101+ 4 BinaryBase 2 numbers are composed of 2 possible digits (0 and 1). Each digitof a number has a power of 2 associated with it based on its position in thenumber.

8 (A single binary digit is called a bit.) For example:110012= 1 24+ 1 23+ 0 22+ 0 21+ 1 20= 16 + 8 + 1= 25 This shows how binary may be converted to decimal. Table showshow the first few binary numbers are shows how individual binary digits ( , bits) are s an example:12 CHAPTER 1. INTRODUCTIOND ecimalBinaryDecimalBinary000008100010001 9100120010101010300111110114010012110050 1011311016011014111070111151111 Table : Decimal 0 to 15 in BinaryNo previous carryPrevious carry00110011+0 +1 +0 +1+0 +1 +0 +101101001ccccFigure : Binary addition (c stands forcarry)110112+1000121011002 Consider the following binary division:11012 102= 1102r1 This shows that dividing by two in binary shifts all the bits to the rightby one position and moves the original rightmost bit into the remainder.

9 (Analogously, dividing by ten in decimal shifts all the decimal digits to theright by one and moves the original rightmost digit into the remainder.)This fact can be used to convert a decimal number to its equivalent binaryrepresentation as Figure shows. This method finds the rightmost digitfirst, this digit is called theleast significant bit(lsb). The leftmost digit iscalled themost significant bit(msb). The basic unit of memory consists of8 bits and is called HexadecimalHexadecimal numbers use base 16. Hexadecimal (orhexfor short) canbe used as a shorthand for binary numbers. Hex has 16 possible digits. NUMBER SYSTEMS3 DecimalBinary25 2 = 12r1 11001 10 = 1100r112 2 = 6r01100 10 = 110r06 2 = 3r0110 10 = 11r03 2 = 1r111 10 = 1r11 2 = 0r11 10 = 0r1 Figure : Decimal conversion589 16 = 36r1336 16 = 2r42 16 = 0r2 Figure :creates a problem since there are no symbols to use for these extra digitsafter 9.

10 By convention, letters are used for these extra digits. The 16 hexdigits are 0-9 then A, B, C, D, E and F. The digit A is equivalent to 10in decimal, B is 11, etc. Each digit is a hex number has a power of 16associated with it. Example:2BD16= 2 162+ 11 161+ 13 160= 512 + 176 + 13= 701To convert from decimal to hex, use the same idea that was used for binaryconversion except divide by 16. See Figure for an , 589 = 24D16. The reason that hex is useful is that there is avery simple way to convert between hex and binary. Binary numbers getlarge and cumbersome quickly. Hex provides a much more compact way torepresent convert a hex number to binary, simply convert each hex digit to a4-bit binary number.


Related search queries