A2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SETR |
1 |
R1 |
|
|
|
R1 is the counter value that increments after every loop |
|
SETR |
N |
R2 |
|
|
|
R2 is the given N value, which is the length of the array |
|
SETR |
x |
R4 |
|
|
|
R4 is teh given x value, which is the target number |
|
SETR |
R1 |
A |
R5 |
|
|
R5 is the temporary max value before x, initialized to be A(R1) |
START |
|
|
|
|
|
|
Start point of loop |
|
BRI |
RI |
R2 |
>END |
|
|
If R1 is less than or equal to R2, increment R1 and continue. If not, go to END |
|
CLA |
|
|
|
|
|
Clears accumulator |
|
SETR |
R1 |
A |
R3 |
|
|
R3 is A(R1), it changes in every loop |
|
ADD |
R3 |
|
|
|
|
Adds R3 to accumulator |
|
SUB |
R5 |
|
|
|
|
Subtracts R5 (the max value) from the accumulator |
|
|
BLT |
A |
>START |
|
|
If accumulator is less than 0, this means the current max value is greater |
|
|
|
|
|
|
|
than R3. There will be no need to change the value of R5, go back to START |
|
|
|
CLA |
|
|
|
Clears accumulator |
|
|
|
ADD |
R4 |
|
|
Adds R4 to accumulator |
|
|
|
SUB |
R3 |
|
|
Subtracts R3 from the accumulator |
|
|
|
|
BLT |
A |
>START |
If the result of R4-R3 is less than 0, this means R3 is less than the target |
|
|
|
|
|
|
|
but bigger than the current max. The max value is then |
|
|
|
|
|
|
|
replaced along with the index value. If statement is false, return to START |
|
|
|
|
SETR |
R3 |
R5 |
Overwrites the value of R3 into the current max, R5 |
|
|
|
|
SETR |
R1 |
R6 |
The current R1 value is also the index value of the current max, so it can be |
|
|
|
|
|
|
|
stored as the max's array index |
|
BUN |
|
|
|
|
|
Unconditional branch: go back to START |
END |
|
|
|
|
|
|
END point |
|
PRT |
|
|
|
|
|
Prints the value of max |
|
PRT |
|
|
|
|
|
Prints the index value of max |
|
HLT |
|
|
|
|
|
Stop program. |