EMAC PRIMER Instruction Manual page 37

Table of Contents

Advertisement

LESSON 8: Sixteen-Bit Subtraction Using Two's Complement Addition
NEW INSTRUCTIONS
DAD
B
Add the 16 bit number in BC to the 16 bit number in HL and store the result in HL. The carry flag is 1 if the
result is too big to fit in HL.
CMA
Complement the bits in the A register (i.e. change ones to zeros and zeros to ones). No flags are affected
INX
B
Increment the value in the BC register pair. No flags are affected
The 8085 microprocessor's DAD instruction provides a quicker method of 16 bit addition. The following is a simple
program that adds the BC register pair to the HL register pair. Try running the program with HL = 01FF and BC = 7001,
the result in HL will be 7200 hex.
ADDRESS
FF01
FF02
There is no instruction for 16 bit subtraction, but this can be simulated by changing the register that you want to subtract to
its two's complement form and then executing a DAD instruction. A number is changed to two's complement form by
complementing every bit and then incrementing the number.
Below is a program that subtracts BC from HL by changing BC to its two's complement form, then the program adds BC to
HL repeatedly. Note that the result in the carry flag will not indicate a borrow as it did in the SUB and SBC instructions.
org
mov
cma
mov
mov
cma
mov
inx
loop:
dad
jmp
end
ADDRESS
FF01
FF02
FF03
FF04
FF05
FF06
FF07
FF08
FF09
FF0A
FF0B
Enter the above program into memory, then load HL with FFFF and BC with 00FF. Set a breakpoint at FF08 then run the
program. When the program breaks, you will see that BC is now the two's complement of 00FF which is FF01. Single step
the DAD instruction and HL will be FF00 (FFFF - FF), single step the JMP and DAD again and HL will be FE01 (FF00 -FF)
and so on. With each DAD instruction 00FF is subtracted from HL.
op code = 09
op code = 2F
op code = 13
DATA
INSTRUCTION
09
DAD
B
FF
RST
7
0ff01h
a,b
; put B in A
; so it can be complemented
b,a
; put the new value in B
a,c
; Do the same for C
c,a
; BC has been complemented (1's complement)
b
; now increment the BC register pair which will make
;
it 2's complement
b
; add BC to HL
loop
; add it again
DATA
INSTRUCTION
F3
MOV
A,B
2F
CMA
47
MOV
B,A
79
MOV
A,C
2F
CMA
4F
MOV
C,A
03
INX
B
09
DAD
B
C3
JMP
FF08
08
FF
37

Advertisement

Table of Contents
loading

Table of Contents