Chapter 7
Program Logic and Control


Short, Near, and Far Addresses
 

A jump operation reaches a short address by a one-byte offset, limited to a distance of -128 to 127 bytes.

A jump operation reaches near address by a one-word offset, limited to a distance of -32,768 to 32767 bytes within the same segment.

A far address may be another segment and is reached by a segment address and offset; Call is the normal instruction for this purpose.
 
 
 
Short Near Far
Instruction -127 to 127
Same segment
-32,768 to 32,767
Same Segment
Over 32K or in 
Another Segment
JMP yes yes yes
Jnnn yes yes (80386+) no
LOOP yes no no
CALL N/A yes yes

The JMP, Jnn (conditional jump), and LOOP instructions require an operand that refers to the label of an instruction.

Example:

        FOR:
                    .
                    :
                    JMP    FOR

 

THE JMP INSTRUCTION
 

Syntax:
 

[label:]     JMP short, near, or far address
 

This is an unconditional jump, which flushes the instruction fetch queue. Many jump operations may slow processing speed. Some assemblers automatically generate a three-byte address operations. This can be stopped by specifying a SHORT operator in front of the label to force a two-byte address.  A JMP may be either backward or forward.

See Figure 7-1 to see how the next instruction is calculated for a JMP.
 

THE LOOP INSTRUCTION
 

Syntax:
 

[label:]     LOOP short-address

Example:
                          MOV    CX, 10  ;WHILE CX > 0
        WHILE:      :
                          .
                          LOOP  WHILE ; -- CX; if CX > 0 JMP LOOP

The LOOP instruction uses the value in the CX register. If the value in the CX register is zero control falls through the LOOP instruction to the next instruction. If the value in the CX is nonzero control jumps to the short-address. If the jump exceeds -128 to + 127 bytes a 'relative jump out of range" message is given.

See Figure 7-2 to see how the next instruction is calculated for a LOOP.
 

LOOPE and LOOPZ loop while equal or loop while zero while the content of the CX is zero or the zero condition is set.
 

LOOPNE and LOOPNZ loop while not equal and loop while not zero as long as the value in the CX is not zero or the zero condition is not set.

Note: Neither LOOP nor its LOOPxx variations affect any flags in the flag registers, which would be change by other instructions within the loop routine. If this is the case a loop routine contains no instruction that affects the ZF flag then using LOOPNE/LOOPNZ would be equivalent to using LOOP.
 

FLAG REGISTERS
 
 
Bit no.: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Flag: O D I T S Z A F C

Of the 16-bits for the flag register 9 are common to the 8086 family. This register indicates results of certain operations.
The common flag bits are
OF (overflow) Indicates overflow of a high-order (leftmost) bit following arithmetic.

DF(direction) Indicates left or right direction for moving or comparing string data.

IF(interrupt) Indicates that an external interrupt such as keyboard entry, is to be processed or ignored.

TF(trap) Permits operation of the processor in single step mode.  This is set by DOS DEBUG allowing a program to be stepped through.

SF(sign) Contains the sign result of an arithmetic operation, 0 = positive, 1 = negative.

ZF(zero) Indicates the result of an arithmetic or comparison operation (0=nonzero, 1=zero result)

AF(auxiliary carry) Contains a carry out of bit 3 on eight-bit data, for specialized operation.

PF(parity) Indicates even or odd parity of a low-order (rightmost) eight-bit data operation.

CF(carry). Contains carries form a high-order (leftmost) bit following an arithmetic operation; also contains the bit content of last bit of a shift or rotate operation.

The 80386 has Eflags which is a 32-bit extended flag register.  The O,S,Z, and C are the most relevant flags to the assembly language programmer.
 
 

THE CMP INSTRUCTION
 

[label:]     CMP  register/memory, register/memory/immediate

The CMP instruction is used to compare two data fields. The result of the CMP operation affects the AF, CF, OF, PF, SF, and ZF flags.
 

CONDITIONAL JUMP INSTRUCTIONS
 

[label:]     Jnnn short-address

The type of jump used is dependent upon the data being compared. Signed and unsigned data use different jumps. See page 117-118.
 

CALLING PROCEDURE

Organizing a program into procedures provides the following benefits:
 

CALL and RETn Operations

The CALL instructions provides for the transfer of control to a called procedure.  The RETn returns control back to the calling procedure.

[label:]    CALL    procedure-name

[label:]    RET[n]    [immediate]  ; Here n is either N for near or F for far or a positive integer.

The assembler can tell from the procedure whether RET is near or far and generates the appropriate object code or it can be explicity denoted by using RETN or RETF.

Near Call and Return

When a near procedure is called:
1. The IP is pushed onto the stack.
2. The IP is loaded with the address of the called procedure.
3. Upon executing the return the IP is popped off the stack.

Far Call and Return

1. The CS and IP are pushed onto the stack.
2. The IP and CS of the procedure are placed in the IP and CS registers.
3. Upon executing the return the IP and CS are popped off the stack.

A procedure can also be entered by means of a JMP to the beginning address of procedure.  However, the execution of the return would not work unless the return address is pushed on the stack before the JMP.

Passing Parameters to Procedures

A parameter can be either passed by value or by reference to a procedure.
It can be passed by placing its value or reference in a register or by pushing the value or reference on the stack before the call is made.
 

Passing Parameters by Value

Example 1:     Pass Values in Registers

                      MOV    AX, MULTICAND
                      MOV    BX,MULTIPLER
                      CALL    M30MULT

                        :
                        .
M30MULT    PROC    NEAR
                     MUL    BX
                     RET
M30MULT    ENDP
 

Example 2    Pass Values in Stack

                    PUSH    MULTICAND
                    PUSH    MULTIPLER
                    CALL    M30MULT
                            :
                            .

M30MULT    PROC    NEAR
                     PUSH    BP
                     MOV     BP,SP
                     MOV     AX,[BP+6]
                     MUL     WORD PTR [BP+4]
                     POP       BP
                     RET       4            ;ADDS 4 TO SP AFTER POPPING IP
M30MULT    ENDP

                                                                    STACK
 

MULTICAND
MULTIPLER
IP
BP

 

Passing Parameters by Reference

Example 3    Addresses in Registers

                    LEA        BX,MUTICAND
                    LEA        SI,MULTIPLER
                    CALL      M30MULT
                            :
                            .
M30MULT    PROC    NEAR
                     MOV     AX,[BX]
                     MUL     WORD PTR  [SI]
                     RET
M30MULT    ENDP
 

Example 4    Addresses in Stack
 

                    PUSH    OFFSET  MULTICAND
                    PUSH    OFFSET  MULTIPLER
                    CALL    M30MULT
                        :
                        .
M30MULT    PROC    NEAR
                     PUSH    BP
                     MOV     BP,SP
                     MOV     BX,[BP+6]
                     MOV     DI, [BP+4]
                     MOV    AX,[BX]
                     MUL    WORD PTR [DI]
                     POP       BP
                     RET       4
M30MULT    ENDP
                                                                STACK
 

OFFSET OF MULTICAND
OFFSET OF MULTIPLER
IP
BP

BOOLEAN OPERATIONS

The Instructions for Boolean logic are AND, OR, XOR, TEST, and NOT, all of which can be used to clear, set, and test bits.  The syntax

    [label:]    operation    register/memory, register/memory/immediate

The first operand references one byte, word, or doubleword in a register or memory and is the only value that is changed.  The second operand references a regsiter, memory, or immediate value, but a memory-to-memory operation is invalid.
The operation matches the bits of the two referenced operands and sets the CF, OF, PF, SF, and ZF flags accordingly. AF is undefined.

The AND Instruction:  If matched bits are both 1, the operation sets the result to 1; all other conditions result in 0.

The OR Instruction:  If either or both matched bits are 1, the operation sets the result to1; otherwise result is 0.

The XOR Instruction: If matched bits differ, the operation sets the result to 1; otherwise the result is 0.

The TEST Instruction: Same result as AND but the first operand is unchanged from the operation.

The NOT Instruction: NOT reverses the bits in a byte, word, or doubleword in a register or memory.

            [label:]        NOT    register/memory

SHIFTING BITS

The shift instructions, which are part of the computer's logical capability, can perform the following actions:
 

SHR/SAR/SHRD: Shifting Bits Right

    [label:]    SHR/SAR    register/memory, CL/immediate ; 2nd operand can be only 1 for 8088/8086
                                                                                    ;and up to 31 for 80286+

SHRD Instruction (80386+):
Can be used to shift 16-bit and 32-bit values.

    [label:]    SHRD    register/memory,register, CL/immediate

The first operand receives the bits that are shifted from the second operand.  The number of bits to be shifted is determined by the third operand.
 

SHL/SAL/SHLR: Shifting Bits Left

[label:]    SHL/SAL    register/memory, CL/immediate

[label:]    SHLD     register/memory,register, CL/immediate
 

ROTATING BITS

The rotate instructions, which are part of the computer's logical capability, can perform the following actions:

[label:]    rotate    register/memory, CL/immediate
 

ROR/RCR: Rotating Bits Right

Rotate right and Rotate right with carry.

RCR differs from ROR in this way: Each bit that RCR rotates off on the right first moves into the CF, and the CF bit moves into the vacated bit position on the left.
 

ROL/RCL: Rotating Bits Left

Rotate left and Rotate left with carry.

RCL differs from ROL in this way: Each bit that RCL rotates off on the left moves into the CF, and the CF bit moves into the vacated bit position on the right.  After an RCL or RCR operation, you can use the JC (Jump if Carry) instruction to text the bit rotated into the CF.