A data register is a storage space for a number. Data registers can be used to store and record the number of executions of a certain task, speed values, etc. The number stored in a register can be an integer or a real number. It is important to emphasize that the data registers are globally accessible and identified by R[x]m where x is the number that identifies the data register. IF statements are used to introduce conditions and to branch the program. In our previous post, we have explained how to draw a square by using an infinite loop. In this post, we will explain how to draw a square by using an IF statement and data registers.
The following code will draw a square 5 times.
1: R[1]=0
2:
3: LBL[1]
4:J P[1] 100% FINE
5: L P[2] 200 mm/sec FINE
6: L P[3] 200 mm/sec FINE
7: L P[4] 200 mm/sec FINE
8: L P[1] 200 mm/sec FINE
9: R[1]=R[1]+1
10: IF R[1]<5, JMP LBL[1]
11: …
On code line 1, we set the register R[1] to 0. That is, the register R[1] has a value of zero. The code executes until line 8. On line 9, we increase the value stored in the data register R[1] by 1. In the first code execution, the value stored in the register is increased from 0 to 1. Then, on the code line, in the IF statement, we check if the value stored in the register is smaller than 5. If this condition is satisfied, we jump to label 1, on code line 3. If the condition is not satisfied, we exit the loop defined by JMP-LBL commands, that is, code line 11 is executed.