



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Binary to hexadecimal conversion and hexadecimal to binary conversion is based on the fact that one hexadecimal digit can be replaced by four binary digits and ...
Typology: Study Guides, Projects, Research
1 / 27
This page cannot be seen from the preview
Don't miss anything!




















In order to understand how inform ation is stored in the memory of a computer we need to know about binary and h exadecimal numbers. In everyday l i f e we normally use de cim al numbers. However, computers store information in binary, and hexadecimal i s a compact way of representing binary.
Put simply , decimal is counting in tens, binary i s counting in twos and hexadecimal is counting in sixteens.
A.1 BINARY AND HEXADECIMAL NUMBERS
A decimal number, say 453 may be expressed in the following way
(4 x 10 ) + (5 x 10
Similarly, a hexadecimal number, say 974, is expressed as
and the binary number 101 is expressed as
The H at the end of the hexadecimal number 974 is there to indicate that that number is, in fact, hexadecimal rather than decimal or binary. Sim ilarly, a binary number is postfixed by the letter B.
Looking at the three numbers above you can see that de cimal numbers are expressed in terms of the powers of tens, hexadecimal numbers are expressed in terms of the powers of sixteens and binary numbers are expressed in terms of the powers of two. The
ten, sixteen and two are said to be the base or radix, of the numbers. Decimal numbers have a base of ten, hexadecimal numbers a base of sixteen and binary numbers a base of two. Any number can be used as a base, but in computing, and particularly for microprocessors, the most common bases are sixteen and two.
Throughout this appendix there are several exercises for you to do to give you practice in number systems. The answers to the exercises are at the end of this appendix.
Exercise 1 By working out the expressions above, what are 974H and 101B equivalent to as decimal numbers?
is, zero through to one less than the base value.
Exercise 2 Which digits do b inary numbers use?
We can use the same digits as are used for decimal numbers up to 9 but for the remaining six digits we need single-character symbols. The chosen symbols are the letters A, B, C, D, E and F, so that hexadecimal A is equivalent to decimal 10 and hexadecimal F is equivalent to decimal 15.
Look now at Figure A.l which shows the equivalent hexadecimal and binary numbers of the decimal numbers a to 15.
decimal hexadecimal
6 6 7 7 8 8 9 9 10 A 11 B 12 C 13 D 14 E 15 F
Figure A.l
binary
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
You will need to know the hexadecimal and binary numbers in Figure A.l by heart, so spend a short time making sure that you know them without having to think about it - especially the binary numbers.
the hexadecimal number in powers of 16, and then add the terms. For example, the conversion of 3AB2H to its equivalent decimal
number would look like
3AB2H
the equivalent decimal number being 15026.
A quicker way to convert a hexadecimal number to a decimal number (and vice-versa) is to use a conversion table, assuming that one is readily available.
Exercise 6 Using the converion tables in Appendix B, convert FBH and A3B2H to decimal and 142 and 9467 to hexadecimal.
A.5 BINARY-HEXADECIMAL CONVERSION
Binary to hexadecimal conversion and hexadecimal to binary conversion is based on the fact that one hexadecimal digit can be replaced by four binary digits and vice-versa.
So to convert a hexadecimal number, s ay 6BH, to binary, just replace each he xadecimal digit by its four dig it binary equivalent - according to the values in Figure A.1. Hence,
which is equal to (^) 1101011B
with the leading zero removed and the two sets of binary digits joined together.
To convert a binary number to a hexdecimal number, the binary number is separated into groups of four binary digits from the right. For example, the binary number 1111100111 would look like
11 1110 0111
Each group of bits is then converted to its equ ivalent he xadec imal digit, so the binary number above would look like
3 E 7
Hence, 1111100111B is equivalent to 3E7H.
Exercise 7 Convert 9AB3H to binary and 110011101111B to he xadec imal.
Appendi x A: Binary and Hexadecimal Numb er Systems 97
A.6 DECIMAL-BINARY CONVERSIONS
Conversions between decimal and binary numbers can be done in the same way as we did decimal and hexadecimal number conversions, except that 2 is used wherever we used 16.
However, those methods of conversion are rather tedious for decimal/binary conversions so, either
use hexadecimal as an intermediary, so that, for example, to convert from decimal to binary first convert from decimal to hexadecimal and then to binary,
or use a conversion table.
Exercise 8 Convert 1290 to binary and 101110111101B to decimal using both suggested methods.
A.7 BYTES
The basic unit of data in the Z80 microprocessor is a byte, which contqins eight binary digits (bits, for short) or two hexadecimal digits. The contents (O's and l's) of a byte may represent any one of several entities, such as
a character, a number (unsigned), or a signed number.
Representation of characters is dealt with in Ch apter 3.
Representation of a number in a byte refers to the contents of a byte being considered t o be the value of the bin ar y number cont ained in the byte. For example, a byte conta in ing 01100110B represents the number 11100110B, 66H or 102 decimal. The range of numbers which can be contained in a byte is 0 to 11111111B (FFH and 255 decimal). When it is necessary to do so, this representation is distingu i shed from another representation by referring to it as the unsigned number representation.
Exercise 9
What range of unsigned numbers can be represented in two bytes (that is, 16 bits)?
A.8 SIGNED (2's COMPLEMENT) NUMBERS
Numbers which may have negative values as well as positive values are held in computers in what is called '2's complement form'. This form of representation depends on numbers consisting of a fixed number of digits. As we are concerned with the Z microprocessor we will consider 2's complement numbers consisting of eight binary digits.
Append ix A: Binary and Hexadecimal Number Systems 99
There are other ways of representing negative numbers in computers, but the 2's complement method is the most common and the one used by the Z80 microprocessor. However, rather than using the inelegant phrase '2's complement' we shall refer to 'signed numbers' rather than 2's complement numbers from now on.
It may help you in your understanding of signed numbers and unsigned numbers to look at the weighting of the bits in a byte for each of the representations. They are
unsigned numbers
signed numbers
4
4
2
so that, for example, the unsigned number 10010001B is equ ivalent to
whereas, the signed number 10010001B is equivalent to
Exercise answers
974H is equivalent to 2420. 101B is equivalent to 5.
2 0 and 1.
4
OllOllOlB
1l001011B
100 Programming in Z80 Assembly Language
6 FBH is equivalent to 251 A3B2H is equivalent to 41906 142 is equivalent to 8EH 9467 is equivalent to 24FBH
7 9AB3H is equivalent to 1001101010110011B 110011101111B is equivalent to CEFH
8 1290 is equivalent to 50AH and 10100001010B 101110111101B is equivalent to BBDH and 3005
9 Unsigned numbers in the range 0 to 1111111111111111B (FFFFH and 64535) can be represented in two bytes.
10 -1 is equivalent to 11111111B -2 is equivalent to 11111110B -126 is equivalent to 10000010B 10000000B is equivalent to - 10000001B is equivalent to -
11 11000100
-------- [ 1) 11110101 -
- -------
00000101 +
102 Programming in Z80 Assembly Language
Hexadecimal
100 200 300 400 500 600 700 800 900
Decimal
256 512 768 1024 1280 1536 1792 2048 2304 2560 2816 3072 3328 3584 3840 4096 8192 12288 16384 20480 24576 28672 32768 36864 40960 45056 49152 53248 57344 61440
Appendix C: Summary of zao
This appendix contains a summary of the complete Z8D instruction set.
The first table, C.l, gives a summary of the flag operations.
In tables C.2 to C.12, the instructions are logically arranged into functional groups. Each table shows the assembly language mnemonic OP code, the numeric OP code, the symbolic operation, the content of the flag register following the execution of each instruction, the number of bytes required for each instruction as well as the number of memory cycles and the total number of T states (external clock periods) required for the fetching and execution of each instruction. Care has been taken to make each table self-explanatory without requiring any cross reference with the text or other tables.
The following pages have been reproduced by permission of Zilog, Inc. 1977. This material shall not be reproduced without the written consent of Zilog, Inc.
Sy mboli c Flap^ OP-Code^ No.or No .or M^ No.err Mn emonic Operation C Z P/V S N H 76 54 3 210 By tes Cycl es Cycles Com ments LO r. r' r _^ r'^ 0 0 0 0 0 0 0 1 r r' I I 4 r, ~ (^) R"l!. LD r.n r -n.^0 0 0 ·
00 r 110 2 2 7 000 B
II 0 11 10 1 3 5 19 011 E 01 r (^110 100) H
LD (IY+d), r (IY+d) -r 0 0 0 0 0 0 - I I II Id^ 10 1- 3 5 19 0 1 110 r
LD A,R I^01 010 III A -R 0 I IFF I 0 0 II 10 1 10 1 2 2 9 01 0 11 II I LDI ,A I -A 0 0 0 0 0 ·
II 101 101 2 2 9 01 000 III LD R, A R- A. (^0 0 0 0 0) II 101 101 2 2 9 0 1 00 1 III
No les : r, r' means any o f the registers A. B, C, D, E, H, L IFF the co ntent o f t he interrupt enab le flip-fl op (IFF) is co pied into the P/V !lag
fla B Notation : • = nag no t affec ted , 0 = flag reset, I = n ag set, X = nag is unknown, ; = nag is affected according to the result o f the oper at ion.
8-bit l oad group Tabl e C.2 Courtes y Zi l og , Inc.
106 Programming in Z80 Assembly Language
F n..r... (^) No. No. No. MnftMH&ic SymbolkOpcraltc.^ .Pt" S 01 oIM^ oIT C N H 76 54) 110 1)'leli ",<I.. Sbl. Commeatl t Odd.,n n dd ~nn · ··^ ···
00 ddO 00 1 3 3 10 ... (^) PIliI
" ' 0 HL 00 100 001 11 SP
·· (^) ··· · 00 101 010 3 S I. l-(nn ) (^) - n (^) - t o dd, tnn ) dd -^ n^ - ddH"" (nn +l ) · ·^ ·· ··^ I I^ 101 101^ •^ •^ ' L ....(nn^ )^ 0 1 ddl^ 01 1
IYH^ -^ (nn+^ 1) ·^ ··^ ··· II^ I I I^ tO I^ •^ •^ ' l -(^ n n )^00 101 0 10
··· (^) · ··
00 100 010 3 S I.
t o (nn),d d -^ n^ - IM+ I)-dd (^) H ·· (^) ···· 11 101 10 1 (^) • (^) • 20 {nn ) .... ddL 01 ddO 0 11
(nn ) -I XH^ ··^ ··· ·^ 11 011 101^ •^ •^ ' L 00 t OO 010
H (^) · · (^) ···· II^ I II^101 ·
(^6) ' I MI -IY (^) L 00 100 0 10
······ 11 III 001 I I s i o SP, IX SP-IX ······
11 011 ) 01 ,^ ,^10 11 111 00 1 t o SP, IV (^) S P- IV ··· (^) · ·· II III 101 ,^ ,^10 11 111 00 1 (^) qq Poi, PUSH qq (^) (SP- 2) - qql ······
It qqO 101 I 3 11 00 Be (SP-I ) - qqH (^) 0 ' DE PUSH IX (5P- 2) - IX (^) L ······ II 01 1 101 ,^4 IS 10 HL (5P-1) -I XH II 100 10 1 (^) II AF PUSH IY (SP- 21 -I YL ······
II III 10 1 ,^4 IS 1 51-I )-I YH II 100 10 1 POP qq q qH - (SP+ I) ······
II qqO 001 I J 10 qq l - (SP) pop IX IXH -~S P + 11 ······
II (^011) ' 0\ ,^4 IXl - (SP) II 100 OOL POPIY IYH -( S' +! ) ·· (^) · · (^) ·· II 111 101 ,^ • (^) " IYL -(S P) (^11 1000 ) No tes: dd is any of t he repU e' pain BC, DE, HL , SP qq is an y of the reJi ster pai n(PAI R) H' (PAI R) L refer to hilh AF, order BC, DE , HL and lo w or de r e ilh t bits of the r elu te, p air respec t ively. E... BeL c C, AFH " A Ftaa No ta nce: • c flaa nat aff ect ed , 0 c fll a reset , I "fla , set. X • flaa is unk nown , t flaa is af fec ted acco rd ina t o the rescn o r the o pe ratio n.
16-bit load group Table C.3 Courtesy Zilog, Inc.
10 8 Programm ing in Z 80 Assembly Language
),
0 '
r lags O p-Cod e ~
No. No. No. Sy mbo lic of of M^ o fT Mnemo nic Op era lio n C Z V S N H 76 543 2 10 Byt es Cy cles Sta les Com me nts ADDA , r A -A + t I I V I 0 I IO IQQQ] r I I 4 r Reg. ADD A, n A - A +n (^) I I V (^) I 0 I II IQQQ] (^110 1 1 7 000) 00 1^ CB
I
10 IQQQ] 110 101 L II I A
I V I 0 I on I 10 [[]ill] 1 3 I I I NC (I X+d J n X +d ) ... ·
I V I 0 I J 1 0 1 1 101 J b 2. (I X + d ) +l (^) UU 11n[@ill d I NC HY +d) HY+d ) - ·
I V I 0 I II III W I ] b 23 lIY "d ) + I (^) on ' I I U lTIE!l
; V I I I (^) lTIill m is any of r,^ (HL f lX+d) .lIY +dIJ
sno wn for INC Same f orm al .1l1lJ \ I; l t~·\ a \ I N C K,' p l.I" " 100 wi th 111 1 III UP dK h:
NOles: Th e V sy mbo l in urc P/ V O••g c o lumn indrca t c-, th at th e It V na~ co n t arn \ th e ove r flo w ot thc rv...ul t 01 th.; opera tion Simil ar ly t he P sym bo l ind u-atc x pan rv. V =: J mcunv ove rflow. V =0 mean ... l lll l l l' l ' r ll i llol. P =:I mean s pa rit y of th e re..u lt is eve n, p ..: 0 rncauv p ant y o t th c rcvul t ,...odd , Flag N otat ion : _ = flag no t affec ted. 0 :: n ag rc-et , I -= na t! <rt. X ;: n at! IS un kn ow n. t ;: flag hi affe c ted acco rdi ng 10 the tv ... iil l o f the o pc rau on
8-bit a r i t hme t i c and logical group Tabl e C.S Courtesy Zilog, Inc.
Appendi x C: Summary of Z80 Instructions 108
t)
FlaS' Op-{:o<Ie ~
No. No.^ No. Symbo lic of^ ofM^ o^ fT Mn emonic Operation^ C^ Z^ V^ S^ N^ H^ 76 543^210 Byte s^ Cycl es^ Sta tes^ Comments DAA Converts^ ace.^ I I P^ I ·
I 00 100 111 I I 4 Decimal adjust content into acc u m u lator pack ed BCD foll owing add or subtract with packed BCD operands CPL A-A · (^) · ··
I I 00 101 III I 1 4 Complement accumulator (one 's complemen NEG A-O^ -A^ I I V^ I^ I^ I^ I I^ WI^ 10 1^2 2 8 Negate ace. <two 's 0 1 000 100 comple me nt) CCF Cy -^ a^ I ···
0 X 00 III I II I I 4 Complement carry flag SCF CY - I I ···
0 0 00 110 III^ I^ I^4 Set carry^ flag NOP No operation · ·^ ··^ ··
00 000 00 0 I 1 4
HALT CPU^ ha^ lted · · ···^ ·
01 110 110 I I 4 DI IFF-O · ·^ · ···^
II 110 OIl I I 4 EI IFF^ -I ····· (^) ·
II III (^011) I I 4 IM O Set interrupt · ·····
11 WI 101 2 2 8 mode 0 (^01 000 ) IMI Set interrupt ·· · ···^
II 101 10 1 2 2 8 mode 1 (^) 01 010 110 1M2 Set interrupt · ··^ · ··^
II 101 101 2 2 8 mode2 (^) 0 1 OI l 110
No tes : IFF indicates the interrupt enable flip-flop CY indicate s the carry flip-flop.
Flll Notation: • = flag no t affected. 0 = flag reset. 1 = flag set, X = flag is unkno wn.
Genera l pur po se a r i t hm e t i c and CPU contr ol gro ups Tabl e C. 6 Co ur tesy Zi log , I nc.
Ap pendix C: Sum mary of Z80 Instructions 111
Flags Op-Code ~
No. No. No. Symbolic (^) 01 aIM olT Mnemoni c (^) Operation C Z V S N H 76 543 210 Bytes Cycles States Comments RLCA ~
I ···
0 0 00 000 III 1 I 4 Rotate left circular accumulator
RLA (^) ~ I · ·^ ·
0 0 00 010 III I I 4 Rotate left accumulator
RRCA (^) ~ I ···
0 0 00 00 1 I II I I 4 Rotate right circular accumulator
RRA ~ I · · (^) · 0 0 0001 1 III 1 1 4 Ro tate right accum ulator
RLCr I I P I 0 0 11 001 011 2 2 8 Rotat e left circular OO[QQQ]r register^ r RLC (HL) (^) I I P (^) I 0 0 I I 001 011 (^2 4 15) r (^) ~I·
00 [QQQ] 110 000 B RLC (IX'd ) I I P I 0 0 11 0 11 101 4 6 23 001010 CD •. \ HU. lI l1;....'. fIY••l! (^) 11 001 0 11 (^011) E
m '! • • IHLl. IIX"" I. lI YOdl states are as shown for RLC,m. To form
RRC m (^) I I P (^) I 0 0 lQill of RLC,m with m < r,(H l I.lI X. c1I, Il Yodl shown co de
m ~ r. f H LI. (I X. 4 1. I I Y " 1
SLA m ~ o I I P I 0 0 [iN]
. " I. l HLI.I IX"<l'. llY · c1,
SRA m (^) ~ I I P (^) I 0 0 llQI) ", · •. IH lI. lIX. ol" f1Y· c
" ",. I HLI, f IX· c1 1. / IY ."
RLD (^) ' ~ '" U.^ I P I 0 0 11 101 101 2 5 18 Rotate disit left and 01 101 III right accumulator^ between^ the
' ~'" U
and location (HL). RRD. (^) I p (^) I 0 0 11 101 101 2 5 18 The conten t of the 0 1 100 III upper half of theaccumulator is unaffected
All Notation : -,. tlag not affected, 0 = Oag reset, 1 = Oagset, X = flag is unknown. ; = nag is affected according to the result of the operation.
Rotate and shift group Table C.8 Courtesy Zilog, Inc.
11 2 Programming in Z80 Assemb ly Language
Flap Op{"ode 1"0. ~
No. No. Symbolic: oC^ orM^ oCT Mnemonic Opention C Z V S N H 76 543 210 Bytes Cycles States Comments BITb. , Z-rb ·
01 b r 000 B BITb,lHLl (^) Z-IH L. ·
I X X 0 I 11 001 011 2 3 12 001010 CD 01 b (^110 011) E BITb, (IX+d) (^) Z-(iX+djb ·
I X X 0 I 11 011 (^101) • 5 20 100 H 11 001 011 101 L 111 A
I X X 0 I 11 111 101 4 5 20 000 0 11 001 011 001010 I 2
11 001 011 2 2 8 IITIb r SETb , (HLI IHLlb -I ···· ··^
11 001 011 2 4 15 (j]b 110 SETb,IIX+dl (IX+d)b - I ··.·· (^) ·
II 011 101 4 6 23 II 001 011
11 III 101 4 6 23 11 001 011
RES b , m 'b^ -0^ [ill^ To fonn new OP· m=',(HL l , code repla ce or SET b,m with^ IIIl (IX+dl. (^) [QJ. Flags and time (IY+dl (^) sta tes for SET instruction
Not ..: The notatlon 'bindicate. bil b (0 10 71 or Iocatlon L
FIaa Notation : • = n., not affected. 0 • flag reset. I ... flag ~ 1. X • flag is unkn own.
Bit set, reset and test group Table C.9 Courtesy Zilog, Inc.