• Aucun résultat trouvé

Progra~ Flow -

Dans le document II II II II II (Page 78-81)

-a

JCXZ and the WOP Instructions _

Unfortunately, the LOOP instructions decrement CX before checking to see if it is zero. So, if you enter a LOOP structure

when CX is zero, the loop will be executed 65,536 times. If

II

this is what you intended, this is fine. If, on the other hand, you want the loop to be skipped when CX is zero, you can use

the JCXZ (Jump if CX is Zero). Place the JCXZ instruction

a

before the loop as shown below. Now the loop will be skipped when CX is zero.

JCXZ NO_LOOP DO_LOOP: (whatever)

LOOP DO_LOOP NO_LOOP: (continue)

The Unconditional Jump

JMP simply transfers control of the program from one place to another, just like the BASIC GOTO statement. There is no de-cision making involved with this instruction; in other words, the computer jumps unconditionally.

There are five kinds of unconditional JMPs. The assembler automatically selects the correct JMP on the basis of the op-erand (the label you are jumping to).

Near jumps. Near jumping (referred to as an Intra Seg-ment Direct jump by IBM literature) has the general format as shown below.

JMP label ;displacement to label

;is calculated by the

;assembler.

(some code)

label: (more code)

I

Near JMPs can jump anywhere within the code segment.

Near JMPs are called direct jumps because the position of the _ next instruction is stored with the JMP instruction.

Short jumps. A short jump, or an Intra Segment Direct

Short jump, is identical to a near JMP. A short jump can be

II

only 127 bytes forward or 128 bytes backward. Trying to jump

too far with a short jump will result in a Relative jump out of range error from the assembler. Note that, whenever possible, • the assembler will automatically use short jumps.

70

II

8

, I

,

a

5 Program Flow

Short jumps are important because all conditional jumps are short jumps, and all LOOP instructions use short jumps.

The range limitation on short jumps can become a problem when you need a conditional jump to skip a very large part of your program. You can overcome this limitation by reversing the logic of your jump condition and skipping over an un-conditional (near) jump. For example, if this jump resulted in a Relative Jump Out of Range error:

JGE SOMEJLACE (more program) You could replace it with:

JNGE SKIP ; (a negative condition) JMP SOME_PLACE

SKIP: (more program)

Remember that the unconditional JMP can jump anywhere within the current code segment. Unfortunately, there is no way to overcome the limitation on LOOP instructions. Just use short loops.

Far jumps. The far jump allows you to transfer control to another segment. This kind of jump is also known as an Inter Segment Direct Jump. Note IBM's careful use of the prefixes In-ter (between) and Intra (within).

The format of the far JMP is identical to that of near JMP;

however, the operand label must have a far attribute; that is, the label must be the name of a far procedure. You will need to use this instruction only if you write programs with more than one code segment, but the assembler will use far jumps automatically if the label has a far attribute.

Indirect jumps. Indirect jumps are jumps in which the address of the next instruction is not coded as the operand of the JMP operation, but is held in a data table or in a general register. There are two kinds of indirect jumps, one for Intra Segment jumps, and another for Inter Segment jumps. Ad-vanced programmers can use indirect jumps just as BASIC programmers use the ON-GOTO construction.

A Sample Program

"Flash," as its name implies, flashes the screen several times.

With a color/graphics screen adapter, the background color of the screen is changed as it is flashed. Flash_M (Program 5-1) is for IBM PC users who have the monochrome screen

71

5 Program Flow

adapter. Flash_C (Program 5-2) is designed for a PC computer with the color/graphics screen adapter and for the PCjr. They should work with any of the compatibles, as long as the screen adapters are fully compatible with the IBM boards. If you are using a PC with both monochrome and color/graphics adapters, try entering both programs. However, DOS 2.00 users should execute the MODE command to change to the appropriate adapter before running the program; otherwise, the results are unpredictable. DOS 1.10 users will have to load BASIC and change monitors according to the BASIC manual.

Users of noncompatible systems should still look at these pro-grams, as they are good examples of short machine language programs.

Flash uses the register DX as a counter; it determines how many times the screen should be flashed. The BX register acts as a pointer into the screen memory. We will use it to read and write the screen attributes. The CX register, the counter for the LOOP instruction, is used to determine how many attributes to change. It is initialized to the value of the constant

SCREENSIZE, the size of the screen page. AH is used to hold and check the attribute.

These programs introduce our first use of the SEGMENT command. The SEGMENT command is being used to locate the screen memory. The AT operand tells the assembler that we want the segment to be located at a specific segment ad-dress; BOOOH for the monochrome screen, and B800H for the color graphics screen. Note that these are not absolute ad-dresses (0 to FFFFF hex), but segment adad-dresses (0 to FFFF hex).

Notice the use of the assembler pseudo-op EQU. This pseudo-op is used to assign a constant value to a symbol (not a memory location, but an assembler value). The format is symbol EQU value

Symbol is equal to the value.

At this point it is important to understand how IBM computers handle screen memory. There are 2000 characters on an 80-column screen. IBM computers use 4000 bytes (note that this is 4000 bytes, not 4K bytes) to represent the charac-ters. The even-numbered bytes (0, 2, 4, etc.) hold the actual character. The odd-numbered bytes (I, 3, 5, etc.) hold the character's attribute. So the character in byte 0 has the

72

8

, a

,

a

I

II

II

I

I

II I

5 Program Flow

attribute defined by byte 1. The attribute byte of the mono-chrome screen adapter can be broken down as shown in Fig-ure 5-1. The F and I symbols show where flashing and intensity attributes can be set.

The attribute byte for the color adapter is used as shown in Figure 5-2.

Figure 5,1. Monchrome's Attribute

o o o o o o o o

- no display

Dans le document II II II II II (Page 78-81)