Computer Systems 2nd Edition
J Stanley Waford
Chapter 6 Section 1.
Branching Instructions and Flow of Control
PEP/7 has 7 branch instructions
BRLE Branch on less than or equal to checks N and Z
BRLT Branch on less than checks N
BREQ Branch on equal to checks N
BRNE Branch on not equal to checks Z
BRGE Branch on greater than or equal to checks N
BRGT Branch on greater than checks N and Z
BRV Branch on V checks V
BRC Branch on C checks C
In order for these to be done, the A register is loaded with the information needed and then the N, V, Z or C bits are checked as necessary. Based on the BIT checking the branch will take place. P. 222 has a table as to how the will branch.
LOADA num,d
BRLT place
would load the value of num into register A and then branch to location place if the value was less than 0.
Program 6.1
A simple C++ program that takes a value in ( cin >> number ) . and then compares it with if number < 0 . If condition is met we negate the number number = - number. We then output the number with cout << number . Of course if the number is less than zero we branch around that and just do the output.
In assembler these translate to
main: DECI number,2 ; ask for number input
if: LOADA ;put the number in register A
BRGE endIF ; branch to the endIf label if N is 0 (if A is Greater than or equal to)
LOADA number ;not really necessary but reload the register with number
NOTA ; get 1's compliment to start negation process
ADDA d#1,i ; add 1 to it to get ones compliment
STOREA number,d ; store the negative back in to number
endIf DECO number,d ; do the cout statement to output the answer
Parts not important to discussion have been left out
notice that is the number is negative to start with the loop takes place and we go to output the number.
Optimizing Compilers
As noted the line to reload number into register A was not needed, but it could have been. What is something had changed the register. For that reason this is not optimized code, but it is nonoptimized. A compiler from a higher level language to the assembler can be of either type. Nonoptimized will be created quicker but optimized will be tighter and smaller code. Generally go for the first in creating the code and the second in creating the program to be distributed.
Program 6.2
We add to the discussion and else to go with the if. If a condition is met we do one thing, if not we do the other and in the end we do the stuff after it. The example is used in comparing two numbers. If one is larger than the other (which is a constant) then we print "high" and if not we print "low". After that the program terminates. In this program we:
main: DECI num,d ; input a number
if: LOADA num,d ;load number into A
COMPA limit,i ; compare register A with the value in limit(subtract it from register)
BRLT else ; go to the else label if it is less than 0 we are checking if the number is greater
; than limit by subtracting limit from it and going to the low area.
lines omitted here for output that say HI
BR endIf ; we are done go past next section
else: lines omitted here for output that say LOW ; this is where we go if compare is lower
endIf STOP ;end program
Program 6.3
Introduces the while statement and how it is done in a program. Pretty much the same thing except that the program checks to see if a condition is met and then drops through if it has not, and loops back if it has. It uses a technique of branching around the end of a loop when it checks the condition and goes to a position outside the loop. Other wise the loop just goes back up to an earlier part of the program to be done again. Nothing new code wise.
Program 6.4 is a do .. while loop. same as above just little fancier program.
Program 6.5 introduces the for loop. In C++ one can put a loop up that starts with a fixed value, compares the value and each time through the loop it will add a value to it (in this case 1 with the i++). In this program as long as the condition is not met (i<3) then we will keep looping through. Again the looping is fairly simple it is just translating it from a C++ program that gets complicated.
Program 6.6 shows us what can be done with branching and what should not be done. It is a classic case loops done because they can not because they should be.
I will not explain it in any detail as one of the homework assignments is to say what it does.
Flow Control in Early Languages
Early computers had no higher level languages to work with. So all code was in assembler and branching was the only way to do things. When FORTRAN was introduces it used statement numbers and introduced an unconditional branch as well as a conditional branch. While not comparing a status bit (at least at higher level) it let the user branch around code if conditions were right.
Structured Programming Theorem
The debate now (1966) was started. Was it possible to write code without GOTOs using only nested if and while loops. While theoretically proven, it was ignored.
The GOTO controversy
In 1968 the debate returned. It was proven that a person trying to follow a program with a lot of GOTOs in it became lost quickly. For that reason many suggested using loops to get the job done and structured programming has pretty much become the standard. remember though, just because there are no GOTOs does not mean that the program is well structured. And even if a language only has GOTO statements they can be used to create a structured style program.
No comments:
Post a Comment