Make your own free website on Tripod.com

Bitwise Assembly Language Commands

Table of Contents

Flags
AND
BIT
CCF
NEG
OR
RES
RL
RLA
RLC
RLCA
RLD
RR
RRA
RRC
RRCA
RRD
SCF
SET
SLA
SRA
SRL
XOR
Back to the lesson index


Flags

Flags... Anyone who has programmed before knows that computers make decisions based on comparisons of variables and/or constants. Anyone who has programmed assembly language before knows that this is a two part process. First the comparison is performed, at which point the results are stored using serveral flags. After this conditional jumps, calls, and returns either do or don't branch to other places based on the values of these flags. An example of how this works ported to 82BASIC:
If A=3
Then
F=1
Else
F=0
End
If F=1
Goto G

In assembler this is:
CMP (10B8), 3
JZ G

The code of: If A=1
Then
0\->\A
Else
1\->\A
End

Could translate to:
CMP (39DE), 1
JZ L1
LD (39DE), 1
JP L2
L1:
LD (39DE), 0
L2:
end of if-then-else

The obvious questions from this are what is a flag, what does it mean, and how do I use it. The answers are:

  1. A flag is a single bit in the F register of the Z80. Six of these are defined. They are the Sign flag, Zero flag, Half carry flag, Parity flag, N Add/Subtract flag, and the Carry flag. Usually you don't access these directly, but set them with instructions for that purpose (usually CP) and use them to control jumps, returns, and calls. A flag is either called by its name, or its name's first letter. The add/subtract flad with the letter N is the only exception.
  2. A flag means that when you were doing something a condition was met. Here is a list of what the flags mean:

    bit 7 S/Sign: This is normally examined after an ADD, SUB, ADC, SBB, or CP. If the result in a math operation was negative this bit will be set, otherwise it will be cleared. This is used after math instructions to see what the number's sign was, and after CP's to see which of the numbers compared is greater. A CP is actually a subtraction that only affects the F register. So CP A, 3 acts like SUB A, 3 except that A doesn't change. That means if the Z flag isn't set (a common test) then if S is clear the first number was larger, and if S is clear the second number was larger.

    bit 6 Z/Zero:This flag tells you (usually after an INC, DEC, ADD, ADC, SUB, SBC, or CP) whether the result was zero. This is most useful with CP because if the Z flag is set the numbers compared were equal.

    bit 4 H/Half Carry:If a byte is added or subtracted with another byte so that the first four bits arithmetic requires a carry to or a borrow from the other four bits, this flag is set. The same thing happens when adding or subtracting word registers except the dividing line is when the rightmost 8 bits require a carry or borrow.

    bit 2 P/Parity/Overflow: This flag tests parity (used often with communications) and overflows. This is most often used for overflows. If the sum of two bytes is greater than 127, the difference is less then -128, the sum of two words is greater than 32767 or their differance is less than -32768 this flag is set, otherwise it is cleared.

    bit 1 N/Add/Subtract: This flag is set by some instructions, cleared by others, and ignored by the rest. You shouldn't have to worry about this flag.

    bit 0 C/Carry: This flag is similar to the Half Carry flag except for bytes this occurs if the result is greater than 255 or less than 0. This occurs with word registers if a sum is greater than 65535 or less than 0.

  3. The last question, how do I use a flag has serveral answers depending on what flag you want to use. When using the Carry flag you can use the ADC (ADd with Carry) and SBB (SuBtract with Borrow) instructions which will add or subtrat an extra digit if the Carry flag is set. There are also opcodes like CCF (Complement Carry Flag) that set the carry flag if it is clear, and clear it if it is set. Most often however, conditional jumps are used to act on the settings of flags. For instance, a JP Z, RED would jump to RED if and only if the Z flag was set. According to a popular document the conditions available are C, M, NC, NZ, P, PE, PO, and Z. No mention is made of what M is, or whether P corresponds to PE or PO. Since this is so screwy I believe it to be in error and this section will be updated when I'm done testing this for myself.

Back to top


AND

This is a simple command that ANDs the bits of the first argument with the second and stores the result in the first. For those who don't know what and is, it works as follows:
1 and 1 = 1
1 and 0 = 0
0 and 1 = 0
0 and 0 = 0
So 10101010 and 11110000 would be 10100000, or AAh and F0h = A0h. The syntax is:
AND ?
where ? is one of the following: A, B, C, D, E, H, L, 0-255, (HL), (IX+d), or (IY+d) where d = -128 to 127. The Sign, Zero, and Half-Carry flags are set based on the results of the AND. AND is used for masking as well as tests. Masking is usually ANDing or ORing a byte with another byte called the mask to set or clear some bits while leaving others intact. For instance, to set all the bits except bit 0 of A to 0 we would use the command AND A, 00000001b. 00000001b is a mask. ANDing with a mask clears bits and ORing with a mask sets bits. This is useful when you want to see if a certain bit is 1 or 0.

Back to top


BIT

This operand lets you test a single bit of a byte without using an and. In other words, it's and AND that doesn't store the result to the first argument. The syntax is:
Bit 0-7, ?
where ? is one of the following: A, B, C, D, E, H, L, 0-255, (HL), (IX+d), or (IY+d) where d = -128 to 127. The Zero flag is set or cleared depending on whether the bit in question is set or clear. To test bit 7 of C use BIT 7, C

Back to top


CCF

The syntax for this command is:
CCF
If the carry flag was set it will be cleared. If the carry flag was clear it will be set. The add/subtract flag is cleared.

Back to top


NEG

This command takes the two's complement of A. This means that if the number in A negative it will become positive, and if the number was positive it will become negative. It's syntax is:
NEG
The Sign, Zero, Carry and Half-Carry flags are set accordingly. To take a two's complement you reverse all the bits in a number, then increment the number. With binary numbers this is how you convert between positive and negative.

Back to top


OR

This is a another simple command that ORs the bits of the first argument with the second and stores the result in the first. For those who don't know what or is, it works as follows:
1 or 1 = 1
1 or 0 = 1
0 or 1 = 1
0 or 0 = 0
So 10101010 or 11110000 would be 11111010, or AAh and F0h = FAh. The syntax is:
OR ?
where ? is one of the following: A, B, C, D, E, H, L, 0-255, (HL), (IX+d), or (IY+d) where d = -128 to 127. The Sign, Zero, and Half-Carry flags are set based on the results of the OR. OR is used for masking and tests.

Back to top


RES

RESet bit.
This command will reset a bit in a register or memory, the syntax is:
RES #, ? where # is a bit from 0 to 7 and ? is (hl), a, d, c, d, e, h, l, (ix+d) or (iy+d). d is -128 to 127. To reset bit 3 in a you would use the command res 3, a. This command has no effect on the flags.

Back to top


RL

Rotate Left.
This command will move all of the bits in a register or a byte of memory left, then take the bit that was on the left and put it in the carry flag. The bit that was in the carry flag will go on the right. The syntax is:
RL ? Where ? is (hl), a, d, c, d, e, h, l, (ix+d) or (iy+d). d is -128 to 127.
To rotate the bits in b to the left once you would use the command rl b. The sign, zero, and carry flags are updated by this command.

Back to top


RLA

Rotate Left Accumulator.
This command is equivalent to RL A. Given the choice you should use this command for all RL's of A. The Carry flag is updated by this command. The syntax of this command is:
RLA

Back to top


RLC

Rotate Left Circular
This command is like RL except instead of the bit 7 going to the carry flag and the carry flag going to bit 0, bit 7 goes to bit 0. The sign, zero, and carry bits are updated by this command. The syntax is: RLC ? Where ? is (hl), a, d, c, d, e, h, l, (ix+d) or (iy+d). d is -128 to 127.

Back to top


RLCA

Rotate Left Circular Accumulator.
This command is equivalent to RLC A. Given the choice you should use this command for all RLC's of A. The Carry flag is updated by this command. The syntax of this command is:
RLCA

Back to top


RLD

Rotate Left ?????
This command performs an RL on A and (HL) with bit 7 or (HL) becoming bit 0 of A and bit 7 of A becoming bit 0 of (HL). The syntax is:
RLD
The Sign and Zero flags are updated.

Back to top


RR

Rotate Right
This command acts like RL, except that the bits are rotated right instead of left. The syntax is:
RR ? Where ? is (hl), a, d, c, d, e, h, l, (ix+d) or (iy+d). d is -128 to 127.
The Sign, Zero, and Carry flags are updated.

Back to top


RRA

Rotate Right Accumulator
This command acts like RLA, except that the bits are rotated right instead of left. The syntax is the same as RLA
The Carry flag is updated.

Back to top


RRC

Rotate Right Circular
This command acts like RL, except that the bits are rotated right instead of left. The syntax is:
RRC ? Where ? is (hl), a, d, c, d, e, h, l, (ix+d) or (iy+d). d is -128 to 127.
The Sign, Zero, and carry flags are updated.

Back to top


RRCA

Rotate Right Circular Accumulator
This command acts like RLCA except that the bits rotate right instead of left. The syntax of this command is:
RRCA
The Carry flag is updated by this command.

Back to top


RRD

Rotate Right ?????
This command acts like RRD except that the bits rotate right instead of left.
The syntax of this command is:
RLD
The Sign and Zero flags are updated.

Back to top


SCF

Set Carry Flag
This command sets the carry flag amd clears the half-carry and add/subtract flags. The syntax of this command is:
SCF

Back to top


SET

SET bit.
This command will set a bit in a register or memory, the syntax is:
SET #, ? where # is a bit from 0 to 7 and ? is (hl), a, d, c, d, e, h, l, (ix+d) or (iy+d). d is -128 to 127. To reset bit 3 in a you would use the command res 3, a. This command has no effect on the flags.

Back to top


SLA

Shift Left Arithmetic
This command acts like RL except that the carry flag will not be placed at bit posistion 0, instead a 0 is always placed there. The syntax is:
SLA ? Where ? is (hl), a, d, c, d, e, h, l, (ix+d) or (iy+d). d is -128 to 127.
The Sign, Zero and Carry flags are updated.

Back to top


SRA

Shift Right Arithmetic
This command acts like SLA except that the bits shift right instead of left. The left bit (bit 7) is unchanged. The syntax of this command is:
SRA ? Where ? is (hl), a, d, c, d, e, h, l, (ix+d) or (iy+d). d is -128 to 127.
The Sign, Zero and Carry flags are updated.

Back to top


SRL

This command acts like SLA except that the bits shift right instead of left. The left bit (bit 7) is always filled with a 0. The syntax of this command is:
SRA ? Where ? is (hl), a, d, c, d, e, h, l, (ix+d) or (iy+d). d is -128 to 127.
The Sign, Zero and Carry flags are updated.

Back to top


XOR

This is a another simple command that XORs the bits of the first argument with the second and stores the result in the first. For those who don't know what xor is, it works as follows:
1 xor 1 = 0
1 xor 0 = 1
0 xor 1 = 1
0 xor 0 = 0
So 10101010 or 11110000 would be 01011010, or AAh and F0h = 5Ah. The syntax is:
XOR ?
where ? is one of the following: A, B, C, D, E, H, L, 0-255, (HL), (IX+d), or (IY+d) where d = -128 to 127. The Sign, Zero, and Half-Carry flags are set based on the results of the OR. OR is used for masking and tests.

Back to top


Questions, comments, flames? Send them in!
If you need help (more simple or more technical) tell me and I'll send it to you by e-mail. Also, is there anything in specific you want to see examples of? Let me know!