 # Bitwise Assembly Language Commands

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:

## 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.

## 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

## 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.

### 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.

### 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.

### 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.

### 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.

### 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

### 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.

### 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

### 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.

### 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.

### 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.

### 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.

### 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.

### 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.

### 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

### 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.

### 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.

### 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.

### 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.

### 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.

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!