home | bit manipulation | data manipulation | addressing | loops | conditional execution/flags | about the author
useful documents | exercises | ARMulator | forum | links | c/c++ introduction | contact us | other websites by the author
Loops
An important feature of the ARM instruction set architecture is the presence of BRANCH instructions which, usefully can be configured to execute under certain conditions and enable the programmer to produce loops -like in high level languages such as C or Java. In order to branch the program counter (register R15) needs to be set to a new value, which will break the sequence of the program. The BRANCH instruction has the following format:
B{cc} < label >
An example of a simple loop follows:
MOV R0, #5
loop
SUB R0, R0, #1
;some useful code
CMP R0, #0
BNE loop
loop
SUB R0, R0, #1
;some useful code
CMP R0, #0
BNE loop
Here
- R0 is set equal to decimal 5.
- The 'label' loop is used to declare the start of a sub-section of code.
- Then decimal 1 is subtracted from R0 and the result put back into R0.
- R0 is then compared to decimal 0.
- If R0 is not equal to 0 the program will goto or jump to the 'loop' section of code again
- loop terminates after 5 executions
External Links: