Programming Basic Loops in Assembly Language

2 Conversations

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 NAMEMEANING
ZZero Flag
CCarry Flag (carry as in mathmatics)
NNegative Flag
VOverflow Flag
XExtend 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:
BNEAddress
The Op-code, this corresponds to a Hexadecimal3 number4 the processor recognises as a set of operationsAddress 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:

Branch Instructions [Flag Dependant]
INSTRUCTIONFLAG VALUE FOR BRANCHWORD DESCRIPTION
BPLN=0Branch if there is no negative result.
BMIN=1Branch on positive result.
BNEZ=0Branch if the value is not a Zero, with comparisons this means the 2 compared values aren't equal.
BEQZ=1Branch if the value is a Zero, with comparisons this means the 2 compared values are equal.
BCCC=0Branch if there is no carry resulting from the mathematical operation.
BCSC=1Branch if their is a carry resulting from the mathematical operation.
BVCV=0Branch if there is no overflow resulting from the mathematical operation.
BVSV=1Branch 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.
BLEZ=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 Loops
Programming Loops in C/C++/Java

Programming Entries

An Introduction to Programming
1There are exception, for example the LOOP mnemonic of the Intel 80x86/Pentium family.2Arithmetic Logic Unit3Base 16: 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f4it is the hexadecimal number that is entered when programming.5The address of the memory location that holds the address.6increasing the memory address by one7An area within the processor
for holding values in play.
8for further details find the technical paper "GOTO Considered Harmful".

Bookmark on your Personal Space


Entry

A533530

Infinite Improbability Drive

Infinite Improbability Drive

Read a random Edited Entry


Written and Edited by

Disclaimer

h2g2 is created by h2g2's users, who are members of the public. The views expressed are theirs and unless specifically stated are not those of the Not Panicking Ltd. Unlike Edited Entries, Entries have not been checked by an Editor. If you consider any Entry to be in breach of the site's House Rules, please register a complaint. For any other comments, please visit the Feedback page.

Write an Entry

"The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."

Write an entry
Read more