How the program executes (ii) the execution of the control structure

Source: Internet
Author: User

One, if else SELECT statement

1. Branch Jump Instruction

We will use the corresponding assembly instructions to indicate "compare X is less than Y", "Select jump to statement block B"

(1) SLT instruction-"compare X is less than Y"

Format: SLT r4,r1,r2. This instruction is used to indicate whether the value in the comparison register R1 is less than the value in R2, and if it is less than the Register R4 1, otherwise 0;

Format: SLT r4,r1,constant. This instruction is used to indicate whether the value in the comparison register R1 is less than the constant value constant, if less than the Register R4 1, otherwise set 0;

(2) SLE instruction-"Judging less than or equal"

Format: SLE r4,r1,constant. The instruction compares the register R1 and the constant value constant, if the value in R1 is less than or equal to constant, then the Register R4 1, otherwise 0;

(3) BEQZ command-"Select jump to statement block action"

Format: Beqz R4,label. Instruction "BEQZ" to see if the value in the register is 0. If 0,CPU will no longer execute the next statement sequentially, it jumps to another block of statements. For the block of statements that will jump to, we can tag it with a label. BEQZ requires two operands, the previous operand is the register that stores the comparison result, and the other is sent

The register is a label. (Some of the instruction blocks in the assembler are labeled Label1, Label2 and so on, and can be jumped according to the conditions when executed, or directly to the instruction block.)

Example: Beqz R4,label2, which indicates that if the value in register R4 is zero, jumps to the instruction block of the tag Label2 tag

(4) Goto instruction-"Jump directly to statement block"

Format: Goto label. Indicates a direct jump to the label of the tag tag (no judgment)

2. Execution of If else SELECT statement

For the case at the beginning of the picture, we assume that X, Y have been read into registers R1 and R2, and that the assembly instruction represents the operation of the CPU when executing the IF else SELECT statement, such as:

(1) Operation process

First, the SLT instruction compares the size of x and Y, if X is less than Y, the register R4 value 1, otherwise zero, the second step, the BEQZ instruction determines whether R4 is 0, if 0 jumps to the LABLE0 to execute the statement block B, and then executes the statement block C sequentially, if R4 is 1, executes the statement block A, Then jump to Label1, execute statement block C

(2) execution process in CPU and memory

  

A: Assuming that the IF Else statement is translated into assembly instructions stored in memory from address 304, X and Y have been read from memory address 1000, 1001 to register R1 and R2

B: Execute the SLT instruction, the CPU now reads the SLT instruction into the instruction register IR, then the CPU transfers the values in the R1 and R2 to the ALU, and for the comparison operation, the Alu is judged by subtraction, and the result is saved back to the register R4, the PC adds 1 and points to the next instruction Beqz.

C: Executes the BEQZ instruction, the CPU first reads the BEQZ instruction to the instruction register IR to interpret, then the CPU determines the value of the Register R4

D: If the R4 is 0 then perform steps e, F, instead perform steps g, h, I;

E:R4 to 0 jumps to label0 execution, if the PC value shown is 401, point to Label0, that is, statement block B, the first statement

F: End the If Else statement after executing all statements in B, after which the PC value is 501 and the statement block C is executed sequentially

G:R4 executes the first statement of statement block A in 1 order, when the value of the PC is 306, and the first statement in the statement block A is executed

H: After all statements in a are executed sequentially, the PC value is 400, pointing to the goto instruction

I: For example, the CPU performs a goto instruction, jumps to Label1, in dotted line 2, the PC value is 500, executes the execution statement block C ends if else SELECT statement

Second, while the execution of the loop statement

Iii. execution of A For loop statement

How the program executes (ii) the execution of the control structure

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.