1. Trang chủ
  2. » Công Nghệ Thông Tin

assembly language step by step programming with dos and linux PHẦN 7 doc

47 313 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 47
Dung lượng 761,89 KB

Nội dung

file:///D|/Agent%20Folders/Chapter%208%20Dividing%20and%20Conquering.htm file:///D|/Agent%20Folders/Chapter%208%20Dividing%20and%20Conquering.htm (44 of 50) [9/26/2002 12:42:58 PM] file:///D|/Agent%20Folders/Chapter%208%20Dividing%20and%20Conquering.htm file:///D|/Agent%20Folders/Chapter%208%20Dividing%20and%20Conquering.htm (45 of 50) [9/26/2002 12:42:58 PM] file:///D|/Agent%20Folders/Chapter%208%20Dividing%20and%20Conquering.htm file:///D|/Agent%20Folders/Chapter%208%20Dividing%20and%20Conquering.htm (46 of 50) [9/26/2002 12:42:58 PM] file:///D|/Agent%20Folders/Chapter%208%20Dividing%20and%20Conquering.htm file:///D|/Agent%20Folders/Chapter%208%20Dividing%20and%20Conquering.htm (47 of 50) [9/26/2002 12:42:58 PM] file:///D|/Agent%20Folders/Chapter%208%20Dividing%20and%20Conquering.htm file:///D|/Agent%20Folders/Chapter%208%20Dividing%20and%20Conquering.htm (48 of 50) [9/26/2002 12:42:58 PM] file:///D|/Agent%20Folders/Chapter%208%20Dividing%20and%20Conquering.htm END Start ; The procedure named Start becomes the main program You'll spot something odd in EAT5.ASM: instead of using ClrScr to clear the screen as I have been for the last several incarnations of EAT, I've replaced ClrScr with a new macro called Clear. Clear (defined in VIDLIB.MAC) uses some technology I haven't explained yet, but will return to in Chapter 10. The lesson is that there are numerous ways to skin a screen, and we've moved here from having the BIOS do it for us to doing it all on our own. Take it on faith for now, until I come back to it. More to the point for the current discussion is the use of the GotoXY and Write and Writeln macros. Additionally, if you look closely at the main program procedure in EAT5.ASM, file:///D|/Agent%20Folders/Chapter%208%20Dividing%20and%20Conquering.htm (49 of 50) [9/26/2002 12:42:58 PM] file:///D|/Agent%20Folders/Chapter%208%20Dividing%20and%20Conquering.htm something odd may occur to you: It's starting to look like something other than an assembly-language program. This is true, and it's certainly possible to create so many macros that your programs will begin to look like some odd high-level language. The danger there is that unless you name your macros carefully, and document them both in their macro-library files and on the lines where they are invoked, your programs will not be any more comprehensible for their presence. Dividing complexity into numerous compartments is only half the job— labeling the compartments is just as (or more) important! file:///D|/Agent%20Folders/Chapter%208%20Dividing%20and%20Conquering.htm (50 of 50) [9/26/2002 12:42:58 PM] file:///D|/Agent%20Folders/Assembly%20Chap9%20Revised.htm You don't take off until all your flight checks are made. file:///D|/Agent%20Folders/Assembly%20Chap9%20Revised.htm (1 of 58) [9/26/2002 9:20:32 PM] file:///D|/Agent%20Folders/Assembly%20Chap9%20Revised.htm That's the reason that we haven't done a lot of instruction arranging in this book up until now, here that we are on the third-to-last chapter. I've found that machine instructions aren't the most important part of assembly-language programming. What's most important is understanding your machine and your tools, and how everything fits together. Higher-level languages like Pascal and Modula-2 hide much of those essential details from you. In assembler you must see to them yourself. For some reason, authors of previous "beginner" books on assembly language haven't caught on to this fact. This fact (in fact) was the major motivation for my writing this book. If you've digested everything I've said so far, however, you're ready to get in and understand the remainder of the 8086/8088 instruction set. I won't teach it all in this book, but the phrase "ready to understand" is germane. You can now find yourself a reference and learn what instructions I don't cover on your own. The skills you need to build programming skills are now yours, and if this book has accomplished that much, I'd say it's accomplished a lot. So let the fun begin. 9.1 Bits is Bits (and Bytes is Bits) Assembly language is big on bits. Bits, after all, are what bytes are made of, and one essential assembly-language skill is building bytes and taking them apart again. A technique called bit mapping is widely used in assembly language. Bit mapping assigns special meanings to individual bits within a byte to save space and squeeze the last little drop of utility out of a given amount of memory. There is a family of instructions in the 8086/8088 instruction set that allow you to manipulate the bits within the bytes by applying Boolean logical operations to the bytes on a bit-by-bit basis. These bitwise logical instructions are: AND, OR, XOR, and NOT. Another family of instructions allows you to slide bits back and forth within a single byte or word. The most commonly used shift/rotate instructions are: ROL, ROR, RCL, RCR, SHL, and SHR. (There are a few others that I will not be discussing in this book.) Bit Numbering Dealing with bits requires that we have a way of specifying which bits we're dealing with. By convention, bits in assembly language are numbered, starting from 0, at the file:///D|/Agent%20Folders/Assembly%20Chap9%20Revised.htm (2 of 58) [9/26/2002 9:20:32 PM] file:///D|/Agent%20Folders/Assembly%20Chap9%20Revised.htm least significant bit in the byte, word, or other item we're using as a bit map. The least significant bit is the one with the least value in the binary number system. (Return to Chapter 1 and reread the material on base 2 if that seems fuzzy to you.) It's also the bit on the far right, if you write the value down as a binary number. It works best as a visual metaphor. See Figure 9.1. When you count bits, start with the bit on the right, and number them from 0. "It's the Logical Thing to Do, Jim " Boolean logic sounds arcane and forbidding, but remarkably, it reflects the realities of ordinary thought and action. The Boolean operator AND, for instance, pops up in many of the decisions you make every day of your life. For example, to write a check that doesn't bounce, you must have money in your checking account AND checks in your checkbook. Neither alone will do the job. ("How can I be overdrawn?" goes the classic question, "I still have checks in my checkbook!) You can't write a check you don't have, and a check without money behind it will bounce. People who live out of their checkbooks (and they always end up ahead of me in the checkout line at Safeway) must use the AND operator frequently. When mathematicians speak of Boolean logic, they manipulate abstract values called file:///D|/Agent%20Folders/Assembly%20Chap9%20Revised.htm (3 of 58) [9/26/2002 9:20:32 PM] [...]... the instruction) and replaces its first operand with the result of the operation (By first, I mean the operand closest to the mnemonic.) In other words, if you write this instruction AND AL, BL the CPU will perform a gang of eight bitwise AND operations on the 8 bits in AL and BL Bit 0 of AL is ANDed with bit 0 of BL, bit 1 of AL is ANDed with bit 1 of BL, and file:///D|/Agent%20Folders /Assembly% 20Chap9%20Revised.htm... register in assembly language The idea is to pass the address of the second two bytes of the string to Byte2Str as though they were actually the start of the string This means that when Byte2Str converts the low byte of AX, it stores the two equivalent digits into the second two bytes of the string For example, if the high byte was 0C7H, the digits C and 7 would be stored in the first two bytes of the... from the left Then, if the low byte were 042H, the digits 4 and 2 would be stored at the third and fourth bytes of the string, respectively The whole string would read C742 when the conversion was complete As I've said numerous times before: understand memory addressing and you've got the greater part of assembly language in your hip pocket Most of the trick of Byte2Str and Word2Str lies in the different... in action, with the 30H bit mask just described, and an initial value of 9DH The three binary values involved are shown laid out vertically, with the LSB (the righthand end) of each value at the top You should be able to trace each AND operation and verify it by looking at Table 9.2 The end result is that all bits except 4 and 5 are guaranteed to be 0 and can thus be safely ignored Bits 4 and 5 could... file:///D|/Agent%20Folders /Assembly% 20Chap9%20Revised.htm ( 17 of 58) [9/26/2002 9:20:33 PM] file:///D|/Agent%20Folders /Assembly% 20Chap9%20Revised.htm (Remember that Byte2Str converts the value passed in AL.) Byte2Str does the conversion and stores the two converted digits in the first two bytes of the string at DS:SI For the second call to Byte2Str, AH and AL are not exchanged Therefore the low byte will be the... file:///D|/Agent%20Folders /Assembly% 20Chap9%20Revised.htm so on Each AND operation generates a result bit, and that bit is placed in the first operand (here, AL) after all eight AND operations occur This is a common thread among machine instructions that perform some operation on two operands and produce a result: the result replaces the first operand Masking Out Bits A major use of the AND instruction is... XOR Instruction In a class by itself is the exclusive OR operation, embodied in the XOR instruction XOR, again, does in broad terms what AND and OR do: it performs a logical operation on two operands, and the result replaces the first operand The logical operation, however, is exclusive or, meaning that the result is 1 only if the two operands are different (1 and 0 or 0 and 1.) The truth table for... mechanism in assembly language file:///D|/Agent%20Folders /Assembly% 20Chap9%20Revised.htm (12 of 58) [9/26/2002 9:20:33 PM] file:///D|/Agent%20Folders /Assembly% 20Chap9%20Revised.htm Splitting a Byte into Two Nybbles Displaying the value stored in a byte requires two hexadecimal digits The bottom four bits in a byte are represented by one digit (the least significant, or rightmost digit) and the top four... belt, understanding exactly how the instructions test the flags can wait until you've gained some programming experience Some people have trouble understanding how it is that the JE and JZ mnemonics are synonyms, as are JNE and JNZ Think again of the way a comparison is done within the CPU: the second operand is subtracted from the first, and if the result is 0 (indicating that the two operands were in... everything, even when procedures are called for Unlike some high-level languages like Pascal and Modula-2, there is no way around jumps—(what they so derisively call "GOTOs")—in assembly language Sadly, some people then assume that jumps are "it," and don't bother imposing any structure at all on their assembly- language programs By teaching procedures first, I feel that I've at least made possible a . 9.1 Bits is Bits (and Bytes is Bits) Assembly language is big on bits. Bits, after all, are what bytes are made of, and one essential assembly- language skill is building bytes and taking them. part of assembly- language programming. What's most important is understanding your machine and your tools, and how everything fits together. Higher-level languages like Pascal and Modula-2. gang of eight bitwise AND operations on the 8 bits in AL and BL. Bit 0 of AL is ANDed with bit 0 of BL, bit 1 of AL is ANDed with bit 1 of BL, and file:///D|/Agent%20Folders /Assembly% 20Chap9%20Revised.htm

Ngày đăng: 12/08/2014, 08:23

TỪ KHÓA LIÊN QUAN