Compilation Basics (4) Program Control transfer

Source: Internet
Author: User
Tags bit set

Program Control transfer

For programming to control the direction is very important thing, it is your program according to the conditions

Make a judgment and jump to the corresponding bit value. Unconditional Jump

The most basic instructions for steering the control program are jjmp.

Use the following form:
JMP Label
The way to declare/label/in a program is simple, just add ":" after its name,
A label can be mixed by any character but cannot start with a number, for example, the following
It's a 3 legal label.
Label1:
Label2:
A:
A label can be declared before an instruction, for example:
X1:
MOV AX, 1

X2:mov AX, 2
Here is an example of a JMP directive:

ORG 100h

MOV AX, 5; Set AX to 5.
MOV BX, 2; Set BX to 2.

JMP Calc; Jump to ' calc '.

BACK:JMP stop; Jump to ' stop '.

Calc:
ADD AX, BX; Add BX to AX.
JMP back; Return ' back '.

Stop

RET; Back to operating system

END

Of course there is a simpler way of calculating the sum of these two numbers, but

The above is a good example of a JMP directive.

As you can see from the example, JMP can control both forward and backward. It can be

Move to any location (65535 bytes) of the current code snippet.

Short term transfer

Unlike the unconditional transfer directive in jmp, there are some conditional jumps

Directive (only if the condition of the directs jump). These directives are divided into

Three groups, the first group is only the detection of individual marker bits, the second set of comparative characters

Number, and the third group compares unsigned numbers.

Transfer instructions for detecting individual marker bits

Instructions

Description

Conditions

Opposite instruction

JZ, JE

If 0 (equal), transfer.

ZF =1

JNZ, JNE

JC, JB, Jnae

If carry (less than, not greater than or equal to), transfer

CF = 1

JNC, JNB, JAE

Js

If it is a negative number, transfer

SF = 1

JNS

JO

If overflow, transfer

of = 1

Jno

JPE, JP

If it is an even number, transfer

PF = 1

JPO

JNZ, JNE

If not 0 (not equal), transfer

ZF = 0

JZ, JE

JNC, JNB, JAE

If there is no carry (greater than, greater than or equal), the transfer

CF = 0

JC, JB, Jnae

JNS

If not negative, transfer

SF = 0

Js

Jno

If there is no overflow, transfer

of = 0

JO

JPO, JNP

If not even, transfer

PF = 0

JPE, JP

You can see that some instructions function the same, yes, they build after compiling

Same machine code so it's easy to understand why you compiled JE instructions

The anti-compilation is JZ. A different name is used to make the program easier to understand.

Comparing the transfer instructions with signed numbers

Instructions

Description

Conditions

Opposite instruction

JE, JZ

if equal to (=), if 0, jump

ZF = 1

JNE, JNZ

JNE, JNZ

If not equal to (<>), if not equal to 0, jump

ZF = 0

JE, JZ

JG, Jnle

If greater than (>) if not less than equals (not <=), jump

ZF = 0

And

SF = of

JNG, Jle

JL, Jnge

If the small and jump if less (<) is not greater than or equal to (not >=), jumps

SF <> of

JNL, Jge

Jge, JNL

If greater than or equal (>=), if not less than (not <), jump

SF = of

Jnge, JL

Jle, JNG

If less than equals (<=), if not greater than (not >), jump

ZF = 1

Or

SF <> of

Jnle, JG

<>-symbol indicates not equal.

Compare unsigned number transfer directives

Instructions

Description

Conditions

Opposite instruction

JE, JZ

if equal to (=)., if 0, jump

ZF = 1

JNE, JNZ

JNE, JNZ

If not equal to (<>), if not 0, jump

ZF = 0

JE, JZ

JA, Jnbe

If greater than (>), if not less than equals (not <=), jump

CF = 0

and

ZF = 0

JNA, Jbe

JB, Jnae, JC

If less than (<), if not greater than equals (not >=), if rounding, jump

CF = 1

JNB, JAE, JNC

JAE, JNB, JNC

If greater than or equal (>=), if not less than (not <), if no rounding, jump

CF = 0

Jnae, JB

Jbe, JNA

If less than or equal to (<=), if not greater than (not >), jump

CF = 1

Or

ZF = 1

Jnbe, JA

In general, you need to use CMP directives to compare numeric values (the directive and the SUB (subtraction)

The directive is similar, except that the result is not saved, but only the value bits are modified.

The above statement means, for example:

Need to compare 5 and 2, 5-2 =3

The result is not 0 (0 value bit set to 0)

Another example

Compare 7 and 7

7-7 = 0

The result is 0. (0 The standard value bit is set to 1.) JZ or JE will be transferred).

Here is an example of a CMP directive and conditional transfer directive:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.