0% found this document useful (0 votes)
124 views

SBC (Subtract With Borrow)

The document describes the Z80 SBC (subtract with borrow) instruction. It has the following key points: 1. SBC subtracts the immediate 8-bit data or contents of an 8-bit register/memory location and the carry bit from the accumulator. It affects the S, Z, H, P/V, and CY flags. 2. Examples of SBC instructions like SBC A,data8 and SBC A,r are provided to demonstrate how the subtraction is performed and how flags are affected. 3. The 16-bit subtraction instruction SBC HL,rp that subtracts a 16-bit register pair from HL is also described along with an example. 4

Uploaded by

EnesVS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views

SBC (Subtract With Borrow)

The document describes the Z80 SBC (subtract with borrow) instruction. It has the following key points: 1. SBC subtracts the immediate 8-bit data or contents of an 8-bit register/memory location and the carry bit from the accumulator. It affects the S, Z, H, P/V, and CY flags. 2. Examples of SBC instructions like SBC A,data8 and SBC A,r are provided to demonstrate how the subtraction is performed and how flags are affected. 3. The 16-bit subtraction instruction SBC HL,rp that subtracts a 16-bit register pair from HL is also described along with an example. 4

Uploaded by

EnesVS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 36

Z80 instruction Set (SBC)

SBC (subtract with borrow) S,Z,H,P/V,CY are affected, N=1(subtraction


instruction)

SBC A,data8 ==> 2 bytes


Subtract the immediate 8-bit data and carry bit from
the accumulator. A=A-data8-carry
example : LD A,3AH
00111010 B
58 (decimal)
SBC A,7CH 01111100 B 124 (decimal) result = -66-1=-67
1 <== carry
1 10111101 = BDH ==> A (-67 signed decimal)
CY=1
Z=0 (result is nonzero) S=1 (MSB of result)
N=1 (subtraction)
P/V= carry XOR borrow bit to 6th bit = 1 XOR 1 = 0 (no overflow) : result is
correct
SBC A,r ==> 1 bytes
Subtract the contents of 8-bit register and carry bit from
the accumulator. r: A,B,C,D,E,H,L
A=A-r-carry
example : LD A,3AH
LD E,7CH
SBC A,E

; assume carry=1
; A <==BDH

Z80 instruction Set (SBC)


SBC A,(HL)
==> 1 bytes
SBC A,(IX+d) ==> 3 bytes
SBC A,(IX+d)

Subtract the contents of memory location and carry


bit from the accumulator. A=A-(HL)-carry
A=A-(IX+d)-carry

A=A-(IY+d)-carry

---------------------------------------------------------------------------------------------------------------------------16-bit subtraction with borrow:

SBC HL,rp ==> 2 bytes

rp: BC,DE,HL,SP

example: SBC HL,BC

Assume that HL=8536H, BC=1044H and carry=0

1000 0101 0011 0110


0001 0000 0100 0100
0 <== carry
00111 0100 1111 0010

HL=HL-rp-carry

CY=0 (no borrow to 15th bit)


S=0 (MSB of the result)
Z=0 (result is nonzero)
N=1 (subtraction)
P/V= carry XOR borrow bit to 14th bit
= 0 XOR
1
=
1
(overflow) result is wrong !

Z80 instruction Set (SUB)


SUB (subtract from accumulator without borrow)
S,Z,H,P/V,CY are affected, N=1 (subrtaction instruction)
SUB data8 ==> 2 bytes Subtract the immediate data from the accumulator.
A=A-data8
example :
LD A,3AH
00111010 B
58 (decimal)
SUB 7CH
01111100 B 124 (decimal) result = -66
- __________
1 10111110 = BEH ==> A (-66 signed decimal)
CY=1 Z=0 (result is nonzero)
S=1 (MSB of result)
N=1 (subtraction)
P/V= carry XOR borrow bit to 6th bit = 1 XOR 1 = 0 (no overflow) : result is
correct
Subtract the contents of 8-bit register from the
accumulator. r: A,B,C,D,E,H,L
A=A-r
SUB r
==> 1 bytes
Subtract the contents of memory location from the
SUB (HL) ==> 1 bytes
SUB (IX+d) ==> 3 bytes
accumulator.
A=A-(HL)
SUB (IY+d)
A=A-(IX+d)
A=A-(IY+d)

Z80 instruction Set


(RES/SET)

RES (reset) and SET

flags are not affected

RES b,r ==> 2 bytes


b: 0,1,2,....,7
MSB=> 7 6 5 4 3 2 1 0 <=LSB
r:A,B,C,D,E,H,L
Make bit b of register r logic zero.
example:
LD B,FFH ; B<== 11111111
RES 7,B ; B<== 01111111
RES b,(HL) ==>2 bytes
Make bit b of the contents of memory location logic
zero using address pointer HL or index registers.
RES b,(IX+d) ==>4 bytes
RES b,(IY+d)

SET b,r
==> 2 bytes
SET b,(HL) ==>2 bytes
SET b,(IX+d) ==>4 bytes
SET b,(IY+d)

Make bit b of register r logic one.


Make bit b of the contents of memory location logic
one using address pointer HL or index registers.

Z80 instruction Set


(rotate/shift)

RL, RLC, RR, RRC, RLD, RRD, SLA, SRA, SRL


S,Z,P/V (parity) flags are affected, H=0, N=0.
Rotate contents of specified register (or memory location addressed by HL or
index register) left one bit through carry.
RL r
==> 2 bytes
RL (HL) ==> 2 bytes
RL (IX+d) ==> 4 bytes
RL (IY+d)

RLA

==> 1 byte

Equivalent to RL A
but flags are not affected (except N=0).

Z80 instruction Set


(rotate/shift)
Rotate contents of specified register (or memory location addressed by HL or
index register) left one bit circularly, copying bit 7 (MSB) into carry.
RLC r
==> 2 bytes
RLC (HL) ==> 2 bytes
RLC (IX+d) ==> 4 bytes
RLC (IY+d)

RLCA

==> 1 byte

Equivalent to RLC A
but flags are not affected (except N=0).

Z80 instruction Set


(rotate/shift)
Rotate contents of specified register (or memory location addressed by HL or
index register) right one bit through carry.
RR r
==> 2 bytes
RR (HL) ==> 2 bytes
RR (IX+d) ==> 4 bytes
RR (IY+d)

RRA

==> 1 byte

Equivalent to RR A
but flags are not affected (except N=0).

Z80 instruction Set


(rotate/shift)
Rotate contents of specified register (or memory location addressed by HL or
index register) right one bit circularly, copying bit 0 (LSB) into carry.
RRC r
==> 2 bytes
RRC (HL) ==> 2 bytes
RRC (IX+d) ==> 4 bytes
RRC (IY+d)

RRCA

==> 1 byte

Equivalent to RRC A
but flags are not affected (except N=0).

Z80 instruction Set


(rotate/shift)
Rotate left one hex digit between the accumulator and memory location
addressed by HL.
RLD

==> 2 bytes

Rotate right one hex digit between the accumulator and memory location
addressed by HL.
RRD

==> 2 bytes

Z80 instruction Set


(rotate/shift)
Shift contents of specified register (or memory location addressed by HL or
index register) left one bit arithmetically, resetting the LSB.
SLA r
==> 2 bytes
SLA (HL) ==> 2 bytes
SLA (IX+d) ==> 4 bytes
SLA (IY+d)

Z80 instruction Set


(rotate/shift)
Shift contents of specified register (or memory location addressed by HL or
index register) right one bit arithmetically. The MSB is not changed.
SRA r
==> 2 bytes
SRA (HL) ==> 2 bytes
SRA (IX+d) ==> 4 bytes
SRA (IY+d)

Z80 instruction Set


(rotate/shift)
Shift contents of specified register (or memory location addressed by HL or
index register) right one bit logically. The MSB is reset.
SRL r
==> 2 bytes
SRL (HL) ==> 2 bytes
SRL (IX+d) ==> 4 bytes
SRL (IY+d)

Z80 instruction Set


(DJNZ)
DJNZ (dec B, jump relative if nonzero)

flags are not affected


DJNZ closelabel ==> 2 bytes Decrement register B by 1, if the result is nonzero jump to closelabel. Very useful for program
loops. closelabel must exist in the label field of an
instruction relatively close to the DJNZ
instruction..
machine code :
1st byte:
10H
(DJNZ op-code byte)
2nd byte:
disp
(displacement byte) in the range [ -128,127]
disp = jump range - 2
When the instuction is executed: PC=PC+disp+2
disp is a signed 8-bit number, therefore the jump range is -126 (bytes backward) 129 (bytes forward) from the DJNZ instruction.
Equivalent to :
DEC B
JR NZ, closelabel

3 bytes total, so using DJNZ is more efficient.

ASSEMBLY DIRECTIVES
(PSEUDO OPERATIONS)
These are not Assembly language instructions, but they are rather
directives to the compiler.
EQU (equate): allows the programmer to equate labels and names with addresses
or data.
example:
NUMBER1 EQU 5
ADDRESS EQU 5000H
LD A, NUMBER1
; A5, value of 5 is loaded into the accumulator
LD A, (ADDRESS) ; A (5000H), contents of memory location 5000H are
loaded into the accumulator

ASSEMBLY DIRECTIVES
(PSEUDO OPERATIONS)
DB (Define byte): locates 8-bit immediate data in memory.
DW (Define word): locates 16-bit immediate data in memory.
example for DB:
LD A, (NUM1) ; A 12H
LD E, A
LD A, (NUM2) ; A 2FH
ADD A, E
; A 12H + 2FH=41H
HALT
NUM1 DB 12H
NUM2 DB 2FH
During compiling, the assembler assigns
each label the address value for the memory
location into which a DB pseudo operation
stores a byte. Then symbols in an instruction's
operand field, that match a label, are replaced
by the address value assigned to that label.

ASSEMBLY DIRECTIVES
(PSEUDO OPERATIONS)
example for DW:
LD HL, (NUM1) ; HL 22FFH
LD BC, (NUM2) ; BC AB36H
ADD HL,BC
HALT
NUM1 DW 22FFH
NUM2 DW AB36H
Each label is assigned the address value
for the two memory locations into which
a DW pseudo operation stores two bytes.
Then symbols in an instruction's operand
field, that match a label, are replaced by
the address value assigned to that label.

ASSEMBLY DIRECTIVES
(PSEUDO OPERATIONS)
ORG (Origin): allows the programmer to locate programs, subroutines, or data
anywhere in memory. Programs and data may be located in different areas of
memory.
example:
ORG 2000H ; locates program in memory starting from 2000H
LD A, 1
.
.
ORG 3000H ; locates data in memory starting from 3000H
NUM1 DB 28H
NUM2 DB 05H

ASSEMBLY DIRECTIVES
(PSEUDO OPERATIONS)
DS (Define storage): allows the programmer to locate memory locations for a
block of data.
example:
ORG 2000H
VALUE DS 1
TABLE DS 20

LD A,3FH
LD (VALUE),A ;
(2000H)3FH
LD IX,TABLE
LD (IX+1),A

ASSEMBLY DIRECTIVES
(PSEUDO OPERATIONS)
END: marks the end of the assembly language source program for a compiler.
The HALT instruction suggests the end of a program, but that does not necessarily
mean the end of assembly
example:
.
.
.
HALT
END
This is my first Z80 Assembly language program
I really worked hard for it! :
_________________________________________________________________

You can write any comments, or story, after the END directive.
Assembler will just ignore them.

How to write a program?


Phases:
Problem Statement Write a program to load two hex bytes into registers B
and C, and add the bytes. If the sum is larger than 8 bits, display 00H as the
overload condition at the LED output port PORT1; otherwise, store the sum in
memory location OUTBUF.

Analysis divide the problem into small steps:


1. Load the bytes into the registers
2. Add the bytes
3. Check the sum
4. If the sum > FFH, display 00H at the output port
5. Store the sum in memory

Flowchart the sequence above can be represented in a block diagram called a


flowchart

Write the Assembly


Execute

20

Flowchart Components

21

Example Flowchart

Simple Programs

Simple Programs

Simple Programs

Simple Programs

Simple Programs

in the next page

Simple Programs

to find the square of numbers upto 255

Simple Programs

Simple Programs

Simple Programs

Z80 instruction Set (IN)


IN (input from an I/O port)
IN A,(portaddress) ==> 2 bytes

Flags are not affected.

Load a byte of data into the accumulator from the I/O port identified by 8-bit
portaddress.
example:
assume that data byte 36H is available at the data bus of the port with
I/O address 80H.
IN A,(80H) ; A<== 36H

In Z80 microprocessor, I/O port addresses are 8-bit long,


not 16-bit
8-bit I/O address is placed at the A0-A7 address lines of Z80 from which a
device select signal is generated by logic circuits to enable the input
device.

Z80 instruction Set (IN)


IN r,(C) ==> 2 bytes

S,Z,P/O are affected. H and N=0, Carry is not affected


Load a byte of data into the 8-bit register r from the
I/O port identified by the contents of register C.
Register C contains the device code (I/O port address)
In paranthesis no other 8-bit register can be used.

example: assume that data byte 36H is available at the data bus of the port with
I/O address 80H.
LD C,80H
IN D,(C) ;

D<== 36H

Z80 instruction Set (OUT)


OUT (output to an I/O port)
OUT (portaddress),A ==> 2 bytes

Flags are not affected.

Output the contents of the accumulator to the I/O port identified by 8-bit
portaddress.
example:
LD A,36H
OUT (80H),A ; (80H)<== A
data byte 36H is sent to the data bus of the port
with I/O address 80H.

Z80 instruction Set (OUT)


OUT (C),r ==> 2 bytes

S,Z,P/O are affected. H and N=0, Carry is not affected

Send the contents of the 8-bit register r to the I/O port


identified by the contents of register C.
Register C contains the device code (I/O port address) .
In paranthesis no other 8-bit register can be used.
example:

LD D,36H
LD C,80H
OUT (C),D ;

(80H)<== D

data byte 36H is sent to the data bus of the port


with I/O address 80H.

Reading Assignment
Please Read
Chapter 6
Chapter 7
Chapter 8
Chapter 9

You might also like