Compilation Basics: Sequence, branching and cyclic programming

Source: Internet
Author: User

An algorithm is described in an orderly way by combining the statements of programming language, which is called the control structure of the program or simply the program structure. The basic structural forms of the program are sequential structure, branch structure and cyclic structure.

First, sequential structure

The sequence structure is the simplest, is also the most basic form of the program structure, the most important feature of the program is that the program runs from beginning to end in the order of the written instructions, and each instruction executes only once, with sequential structure of the program or program section, become sequential program.

Let's take a simple procedure to illustrate the sequential programming of the Assembly.

Example: Set two word storage variables x and Y, and programmatically implement the exchange of these two variables.

Analysis: Because the storage units can not be directly between the data exchange, so the use of general-purpose register AX as an intermediary exchange. I'll give you a code example here:

Data    SEGMENT    
    X   DW  1032H    
    Y   DW  2043H    
DATA    ENDS    
STACK1  SEGMENT PARA STACK    
    DW   20H   DUP (0)    
STACK1  ENDS    
CODE    SEGMENT    
assume Cs:code, Ds:data, SS: STACK1    
BEGIN:  mov     ax, DATA    
    mov     DS, ax    
    mov ax, x    
    xchg    ax, Y    
    mov x, ax    
    MOV AH, 4CH    
    INT 21H     
CODE    ENDS    
    End

Code Analysis: The previous section is a definition of the data and the stack, here I will not say more, we start from the beginning, because for MOV, the data can not be directly as the source operand, DS as the purpose of the operand, so it is through a universal register to achieve the assignment between the two, MOV AX, X means to save the value of x first in Ax and then execute Xchg Ax, y to exchange the value of Ax and y, where Y stores the value of X, and the value of y stored in ax. Immediately after MOV x, Ax assigns x to the value of y that is stored in AX, which means that x and Y are worth swapping.

Second, the branch program design

The branch program needs to have the support of the transfer instruction, and the transfer instruction is divided into unconditional transfer instruction and conditional transfer instruction, in conditional transfer instruction, different conditions are often reflected by the different states of the condition mark in the sign register. Therefore, one of the most important problems in branch programming is how to use appropriate transfer instruction to realize program transfer according to the different states of flag registers. See my other article about the transfer instructions (http://yiluohuanghun.blog.51cto.com/3407300/940123).

Let's take the example: x is the symbol data in the storage unit, the program implementation calculates its absolute value and saves it to its original place.

Analysis: When x>=0, the absolute value of x is itself, otherwise the use of the negative instructions will be x, and put back to the original place, I gave a program, you can refer to the following:

Data    SEGMENT    
    X   DW  0f874h    
data    ENDS    
STACK1  SEGMENT    STACK    
    DW  20H DUP  (0)    
STACK1  ENDS    
CODE    SEGMENT    
assume    
cs:code, Ds:data, Ss:stack1 BEGIN:  mov ax, DATA    
    mov DS, ax    
    mov ax, x    
    TEST    ax, ax      
    jns done    
    NEG x   negative instruction NEG against X Done    
:   MOV AH, 4CH    
    INT 21H    
CODE    ENDS End    
    BEGIN

Program Analysis: Testax, AX; The purpose of this is to reset the values of the flag registers so that you can use the flag registers for the next instruction Jns

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.