Programming Basic Loops in Assembly Language
Created | Updated Jan 28, 2002
Low Level Loop Programming
This is a section only for the more advanced amateur, or professional programmer.
It is possible to create loops at the assembly language level (commands to the processor, one level up from binary), to
explain how this works I will use simplified instructions from the MOTOROLA 68000 processor as an example.
Now before we begin I have to explain something about the nature of programming loops in assembly language. With higher level languages they have mnemonics dedicated to certain types of loops, eg: DO..WHILE, FOR..NEXT, REPEAT..UNTIL etc. These mnemonics make it instantly clear that a loop is intended. In general, this is not the case for loops written in assembly language. In assembly, loops are based on condition checking operations and jump (or 'branch') instructions, but so are conditional statements and many other structures at this level1. So at a glance at a section of assembly code you may be usure as to whether you have a loop there until you work through the logic. The confusing nature of these pieces of code is one of the main reasons it is recommended to only the more advanced programmer.
What do you mean by a condition checking operation?
Basically whenever a comparison or mathematic operation takes place in the processor the values of a set of "Flags" are set by the result. Now when writing loops you create a mathematic or comparison operation that will be used to control the loop by what "Flags" are or are not set.
A Brief Introduction to Flags
Flags are true or false values in the processors ALU2(in effect the calculator
of the processor). These values tell the processor things about the result of a mathematic operation that the number itself
will not obviously show. For example was there an overflow, or is it a zero(it is quicker than checking the number).
The following is a table of the flags:
FLAG NAME | MEANING |
Z | Zero Flag |
C | Carry Flag (carry as in mathmatics) |
N | Negative Flag |
V | Overflow Flag |
X | Extend Flag [not really used for comparisons] |
What is a Branch Instruction?
A branch instruction is basically an instruction that tells you to go to another line in the
program. There are two types, ones that depend on a condition to perform a branch (based on the values of flags), and others that branch no matter what.
Format of Branch Instructions
Example of the format:
BNE | Address |
The Op-code, this corresponds to a Hexadecimal3 number4 the processor recognises as a set of operations | Address this is an direct address, an indirect address5, a line label[the name given to a line, however this is not going to be explained in this entry] or an operation dependant on an address (e.g. an increment 6). The addresses are the address of a line of program code. |
Conditional Branches
These are branches that only branch if a condition is true, this condition is based
upon the value of the flags (TRUE=1 FALSE=0).
The following is a list of some branch instructions and the explanation of them, some of them relate to the results
of comparison operations:
INSTRUCTION | FLAG VALUE FOR BRANCH | WORD DESCRIPTION |
BPL | N=0 | Branch if there is no negative result. |
BMI | N=1 | Branch on positive result. |
BNE | Z=0 | Branch if the value is not a Zero, with comparisons this means the 2 compared values aren't equal. |
BEQ | Z=1 | Branch if the value is a Zero, with comparisons this means the 2 compared values are equal. |
BCC | C=0 | Branch if there is no carry resulting from the mathematical operation. |
BCS | C=1 | Branch if their is a carry resulting from the mathematical operation. |
BVC | V=0 | Branch if there is no overflow resulting from the mathematical operation. |
BVS | V=1 | Branch if there is an overflow resulting from the mathematical operation. |
BGT | (N=1,V=1 AND Z=0) OR (N=0,V=0 AND Z=0) | Branch if the comparison shows value A is greater than value B. |
BGE | (N=1 AND V=1) OR (N=0 AND V=0) | Branch if the comparison shows the value A is greater than or equal to value B. |
BLT | (N=0 AND V=1) OR (N=1 OR V=0) | Branch if the comparison shows value A is less than value B. |
BLE | Z=0 OR (N=0 AND V=1) OR (N=1 OR V=0) | Branch if the comparison shows value A is less than or equal to value B. |
Unconditional Branches
There are 2 main unconditional branch instructions, BRA (Branch Always) and GOTO, they
have the same format, the only real difference is their action, basically every time the program hits
the unconditional branch, it branches to the specified program code address.
Now there are 2 applications of this code, when used alongside conditional branches to create loops
example (this isn't exact code it only gives an idea):
00 MOVE #00,A
01 ADD #01,A
02 CMP #12,A
03 BEQ #05
04 BRA #01
05 The rest of the program.
Basically this program keeps adding 1 to the value in register7 A using a loop until the value in A is 12 in Hex, then the program continues.
Now the use of these unconditional branches in loops is an appropriate use of them, I'd just like to mention at this
point that these instructions are not useful for directing the flow of the program as it causes overly complex and confusing code
8.
Related Entries
Other Loop Entries
An Introduction to Programming: Programming LoopsProgramming Loops in C/C++/Java
Programming Entries
An Introduction to Programmingfor holding values in play.8for further details find the technical paper "GOTO Considered Harmful".