Computer Systems Org I - Prof. Grishman

Lecture 7 - Sept. 29, 2004

Condition codes and branch instructions
        branch address is offset from current PC

Example:  absolute value (put absolute value of R1 into R2)

in 'Java'
    int R1, R2;
    R1 = -5;
    if (R1 < 0)
       R2 = -R1;
    else
       R2 = R1;

in LC-3, with 'labels' on instructions to show where we want to branch
         BRZP    positive
         NOT     R2, R1         ; R1 < 0,
         ADD     R2, R1, 1      ;    R2 = -R1
         BRNZP   done
positive AND     R2, R1, R1     ; R1 >= 0,  R2 = R1
done     (next instruction in program)

and then replacing the labels by offsets (less clear, but closer to machine code):
         BRZP    3
         NOT     R2, R1         ; R1 < 0,
         ADD     R2, R1, 1      ;    R2 = -R1
         BRNZP   1
         AND     R2, R1, R1     ; R1 >= 0,  R2 = R1
         (next instruction in program)