简体   繁体   中英

Predicting branches

Assume that a processor has a feature that enables the compilier to specify the initial prediction state as either LT or LNT for a branch instruction. Consider a statement of the form

IF A > B THEN A = A + 1 ELSE B = B + 1

(a) Generate assembly-language code for the statement above.

(b) In the absence of any other information, discuss how the compiler should specify the initial prediction state for the branch instructions in the assembly code.

(c) A study of the execution behavior of the program containing the above statement reveals that the value of variable A is often larger than the value of variable B. If this information is made available to the compiler, discuss how it would influence the initial prediction state for the branch instructions.

I think I have part a and b. For part a, wouldn't this be something like:

Assume R0 contains the value in A and R1 contains the value in B.

       Branch_if_R0>R1   LOOP            //Compares A and B
       Add               R1, R1, #1      //A>B is false so B=B+1
LOOP   Add               R0, R0, #1      //A>B is true so A=A+1

As for part b, since the loop conditional is at the beginning of the loop, wouldn't this be LNT? And for part c, if we knot that A is often greater then B, wouldn't this then be LT?

Your code have bug.

 IF A > B THEN A = A + 1 ELSE B = B + 1

to assembly language

       cmp               R0, R1
       jg                LOOP             //Compares A and B
       Add               R1, R1, #1      //A>B is false so B=B+1
       jmp               LOOP1
LOOP:  Add               R0, R0, #1      //A>B is true so A=A+1
LOOP1:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM