Select page Next page

Objectives

Our programs so far have been fairly dumb. They do one or two simple things but they are incapable of anything "intelligent". To manage this, we need the ability to evaluate current circumstances and make decisions based upon what we find. This has other effects, too. For example, we would then be able to make loop structures by performing a section of code, incrementing some sort of counter and then comparing this with a known upper value. This will be the subject of the next chapter but in this one, we will see how we can simulate the "IF ... THEN ... ELSE" structure of high-level languages. In addition, we will examine the shift operations which allow us to "slide" the bits in a register to the left or right. This is much more important than, at first glance, it appears to be ...

To enable us to do all of this, you will need to be able to:

Flags

The first thing I want to say is that Jeremy Gordon has done a nice little section on "Flags and conditional jumps" in the help manual for goAsm and I have no intention of re-writing his perfectly good article. You should read it for yourself but, for the beginners, it is useful to deal with the simpler aspects of this subject in order to allow the development of the kind of understanding which allows you to deal with the more advanced material in the help - especially when backed up with a worked example. That's what I aim to do in this chapter.

So let's look at flags and the first question is - what are they? Flags are one-bit memory boxes inside the CPU. They can therefore hold a 1 or a 0. We say the former is "set" and the latter "clear". Some instructions are interested in these flags - some can actually change them - and others don't care. There are five flags in which we are interested, each with a single-character name:

Flag NameComments
Z - Zero FlagThis flag is set if the result of the previous operation was zero.
S - Sign FlagIf the leftmost bit of the result of an operation is set, this flag will be set. Think back to our discussion of negative numbers in Chapter 2. We saw that in signed numbers a value of "1" in the most significant bit was regarded as the sign for the rest of the bits to be taken as a negative number. So, if the sign flag is set, we can interpret the number, if we wish, as a negative value.
C - Carry FlagLet's imagine that we have the value 156 in the register AL and we then use the ADD instruction to add 100 to it. In decimal, that would give us 256 but we know that AL can only hold one byte and a byte encodes numbers in the range 0-255. In this case, the result would be 0 and the carry flag would be set to show "carry one".
O - Overflow FlagThis one's a little harder to understand. Again, think back to the subject of negative numbers in Chapter 2. It is perfectly possible that, when you add two numbers together, the carry flag could be set, as we discussed above. But if you were regarding the numbers as signed, the result would not, in fact be greater than 255 but the resultant bits would still cause the carry flag to be set. The question is, then, how do we show when the addition of two signed numbers causes an overflow? We've just said it - the overflow flag. Look at the example in Jeremey's help manual.
P - Parity FlagThe parity flag does not really concern us in this series of articles. It is set if the number of bits in a piece of data are even and cleared if thy are odd. For example, 101010102 would set the flag because there are four "1s" in the data. The use of this flag stems back to the earliest days of data communications with mechanisms like RS232 lines and allowed a basic sort of error checking to be made.

There are, in fact, another two flags - the Auxiliary Carry flag, which is used in Binary Coded Decimal, and the Direction flag. Neither of these is of interest to us here.

Select page Next page