Posted on 10-17-2006, by Tim
To perform an if statement in LC-3, you should use the branch instruction (BR). There are three flags you can pass to it (n, z, and p). The main difference between BR and higher-level languages' if statements is that if statements compare two numbers, while BR compares one number to see if it is negative (n), zero (z) and/or positive (p). (Note: the nzp flags must be entered in order. For example, using zn is correct, but zn is not.)
The statement directly before your BR instruction will be tested. Testing for all three nzp flags will always return true. If it is true, the branch will jump to a point in the program specified by you. For example:
|ADD R1, R1, #1
; if the value is negative or zero, this code will by skipped
; this code will be executed for all values of R1, even negative values and zero
Remember: only the last statement executed before the branch will be evaluated for nzp.
- Views: 27923