		ASSEMBLY LANGUAGE - WEEK I				October 26, 1986





People communicate with computers in many ways. Pulling a can of beans over an optical scanner at the supermarket, keying in a checking checking account number into an automatic banking machine, pushing the channel selector button on a tv tuner, and typing in a BASIC program are but a few of the ways people direct the operations of the computers. In all of these examples and most other applications the user doesn't speak directly to the microprocessor. The user speaks through an interpreting program which in turn speaks to the microprocessor. To use the Z-80 in the TRS-80 one must bypass the built-in interpreter and speak the z-80's native language. This chapter introduces that language and a special program that allows the user to speak it
.

A digital computer can be defines as follows: - An electronic device that is capable of accepting information which includes both data and the controlling program, comparing, storing, and arithmetically manipulating this information, and then supplying the results of these processes in an acceptable form. The major elements of a digital computer usually include memory, control, arithmetic, logical, and input and output facilities. It should be emphasized that a digital computer manipulates binary information.



A microcomputer is a fully operational digital computer that is based upon a microprocessor chip. A microprocessor is a single integrated-circuit chip that possesses at least 75% of the computing and data manipulation power of a digital computer. It usually cannot function without the aid of support chips and memory.



The elementary unit of information is called the bit, which is the abbreviation for Binary Digit. It is equal to one binary decision, or the designation of one or two possible and equally likely values or states (such as 0 or 1) of anything used to store or convey information.



The organization of the internal components of a microprocessor is known as its architecture. The Z-80 architecture is shown in the block diagram  Figure 1-1. Although only four blocks are included in the diagram, each block consists of several smaller blocks which would be explained in a more detailed discussion of the microprocessor design. The four blocks are collectively called the central processing unit, or CPU. The brain of the Z-80 CPU is the control unit which directs and synchronizes the actions of the other blocks and their components. The general purpose registers can serve as temporary storage locations for data that will be used in the program or data that point the control unit to the location in memory that holds the data needed. Note that the central processing unit (CPU) does not include memory in which it can store the instructions or data that make up the program. Instructions and data are stored in external memory (other ICs) and both are moved into the microprocessor as needed by the control unit during execution of the program. The special purpose registers are also temporary storage locations, but each holds a specific type of information about how the execution of the program is proceeding. The information is invaluable to both the programmer and the control unit in its execution of the program. 


The ALU, arithmetic-logic unit, performs all of the arithmetic and logic operations on data under direction of the control unit. Addition, subtraction, logical ANDing, etc., are performed in the ALU. Data cannot be stored in or loaded into the ALU but must first be placed in one of the general-purpose registers, where it is accessible to the ALU.



Since the instructions and data must be stored in other ICs, the microprocessor must have a way of communicating with those ICs. It does so via the three buses shown in Figure 1-2. A bus is simply a group of lines, or wires, that carries similar kinds of information between chips. Since the Z-80 is a digital device it handles all instructions and data in binary 1 or 0 form. the Z-80 is organized around the byte, which consists of eight parallel binary bits identified as 0-7, where bit 0 is the least significant and bit 7 is the most significant. A binary byte may represent any number between decimal 0 (binary 00000000) and decimal 255 (binary 11111111). The control unit interprets each byte as either part of an instruction or as data.


The DATA BUS is a group of eight lines, designated D0-D7, over which all instructions and data are transferred between the Z-80 and external memory or I/O. The byte-wide data bus is bidirectional, that is, data can flow from memory into the Z-80 or from the Z-80 into memory. The Z-80 dictates the direction of data flow to other chips in the system with two control lines. The movement of information from memory into the microprocessor is called a read operation. The movement of information from the microprocessor into memory is called a write operation. Although that data bus is bidirectional, data will only move in one direction at one time; it cannot move both ways simultaneously.


Since any program will have several instructions the control unit must have a way of telling the external memory which instruction or piece of data it needs. the address bus serves this purpose. The address bus is a group of 16 lines organized as two bytes, A0-A7 and A8-A15, that can hold any binary number between 0 and 65,536. Each storage location in memory is numbered so the Z-80 control unit can get the instruction or data it needs by furnishing the instruction or data location number, called its address, to the memory IC. Circuitry on the memory IC decodes the address supplied vial the address bus and makes the contents of the requested location available to the microprocessor on the data bus. Notice that the address bus is not bidirectional; normally only the microprocessor places an address on address lines A0-A15.


A simplified schematic of the TRS-80 is shown in Figure 1-3. Note that the memory block of Figure 1-2 has been replaced with two blocks labelled ROM and RAM. ROM is read-only memory that stores programs which are automatically executed by the Z-80 on power-up and are invisible to the user. ROM is read-only because the programs stored in it cannot be changed, but the programs stored in ROM are not lost when the system is turned off. Programs stored in RAM are lost when the system is turned off because RAM forgets when power is removed, but a program stored in RAM can be readily changed by the user. A program written in RAM can be permanently stored if it is transferred to a magnetic storage medium, such as a cassette or disk. A program stored on tape or disk must be transferred into RAM before it can be executed on the Z-80.


As indicated in FIG 1-3, RAM, ROM, the keyboard, the video monitor, and other I/O devices are all connected to the Z-80 via the three shared buses: data, address, and control. During read or write data transfers all devices except the Z-80 and the other device directly involved in the transfer must be removed from the data bus to prevent them from storing data not meant for them during write operations and to prevent them from placing data on the bus during a read operation. Memory and I/O devices that are not participating in the current operation are not physically unplugged from the data bus are are electrically isolated from the bus. 


Selection of a device is accomplished by addressing a certain memory cell or location. Since only one device can be enabled at a time and the address decoder supplies chip enable signals based on the Z-80 address lines, the Z-80's memory address space 0-65,535 is carved into chunks which are assigned to individual devices. A ROM in the TRS-80 exclusively occupies addresses 0-4095. The video display exclusively occupies addresses 15,360 to 16,383. Ram and the keyboard occupy other areas of the memory space. A table that lists the assignments of devices to particular memory spaces is known as a system's memory map.


Assignment of addresses to devices in the memory map of the TRS-80 or any other system is performed by the system designers. As a memory map is being designed the designers are free to assign devices to any addresses in the map, although design guidelines are often used. Once a memory map has been drawn and the system built it is not possible to change assignments in the map. As more memory or I/O devices are added to a system they must conform to the existing memory map by being placed in areas not already assigned. Fortunately for the system user, memory and I/O decoding are all performed with hardware (ICs) and the user must only be concerned with the assignment of addresses in the memory map. 



A register is a short-term storage circuit (memory location) within the microprocessor, where computation may be carried out. Single registers in the Z-80 microprocessor chip store a single byte, i.e., eight contiguous bits. A variety of registers exist within the Z-80 chip, some of which are used to store digital information and others which are used by the chip itself as it performs instructions. In general, the registers of the chip are divided into two different sets: those that you can address from a program and those that you cannot address from a program. The Z-80 contains two sets of eight internal general-purpose registers, four 16-bit registers, and two special-purpose 8-bit registers.



The Program-addressable registers include:
  
. two sets of 8-bit general purpose registers addressed singly or in pairs,
	
		Main Register Set	Alternate Register Set
	
	B register		B' register
	
		C register		C' register
	
		D register		D' register
	
		E register		E' register
	
		H register		H' register
	
		L register		L' register

  
. an 8-bit ACCUMULATOR for each set, also known as registers A and A'
  
. an 8-bit FLAG register for each set, also known as registers F and F'
  
. the 16-bit STACK POINTER register (SP)
. the 16-bit PROGRAM COUNTER register (PC)
  
. two 16-bit INDEX REGISTERS (IX) and (IY)
  
. the 8-bit INTERRUPT PAGE ADDRESS register (I)
  
. the 8-bit MEMORY REFRESH register (R)



These are the only registers with which you can directly exchange information with the aid of a suitably written microcomputer program.



One of the two sets of the general-purpose registers is called the main register set, and the other is called the alternate register set. The main set is what you always use in computations. The registers of the alternate register set are named similarly but are marked by a prime after their letters.  At any time only one set of the registers are active. The alternate set is accessed by only two instructions which exchange the contents of the main set with the alternate set.The Z-80 instructions select the current in-active set to become active and put the currently active into the inactive state. The instructions, therefore, are used to switch between the two sets as desired. A second set does not HAVE to be used, but simply makes more register storage available if required.


The two sets of six general-purpose registers - B,C,D,E,H, AND L and B',C',D',E',H',and L', - temporarily store single bytes of information. Since they are located within the Z-80 microprocessor chip, the exchange of information from one general-purpose register to another can be very fast. The exchange of information between any of these general-purpose registers and the accumulator is also fast. The registers B through L may be used either singly as 8-bit registers or in pairs for 16-bit quantities. In the latter case, B and C, D and E, and H and L are always used together, and in such cases are designated as BC, DE, and HL.


The HL register pair has two primary uses. First, it is the "accumulator" for 16-bit arithmetic operations. (There are no 16-bit logical operations.) All 16-bit arithmetic operations use HL as one of the operand registers and and the result register. Second HL can be used to contain an address pointing to a memory location whose contents are used in an 8-bit operation. Whenever this is done, the operand is indicated as "(HL)". while the BC and DE register pairs can sometimes be used in this manner, there are many more Z-80 instructions that involve (HL). When used for memory addressing, the H register corresponds to the HI memory address and the L register corresponds to the LO memory address.


Both individual register B and the BC register pair are often used to hold a count of the number of times something is to be repeated, so these are sometimes called the "count" registers. B is used as a count with the DJNZ instruction, the mnemonic for which is supposed to suggest the mellifluous phrase "decrement B and branch to the location specified if it is not zero". The BC register pair is used as a count for all block transfer instructions -- LDI, LDIR, etc. These operations are used to move an entire block of memory from one area to another, and they will be described later. Finally, the C register is the only register used for certain input and output operations. When used for memory addressing, the B register corresponds to the HI memory address and the C register to the LO memory address.


The DE register pair has many uses analogous to HL and BC, except that there are fewer such instruction. Both (BC) and (DE) can be used to specify addresses like (HL) but only loading to or from the accumulator is possible. thus,
	
	LD	A,(DE)

and
	
	LD	(BC),A

are legal, but not
	
	LD	H,(BC)

whereas
	
	LD	H,(HL)

is legal. When used for memory addressing, the D register corresponds to the HI memory address, and the E register to the LO memory address.


The A register is called the accumulator. It is the most important register in the computer, because it is where most of the action takes place. All 8-bit arithmetic and logical operations involve the accumulator containing one of the operands and the result of the operation. In addition, some instructions that fetch or store a byte in memory only allow A to be used; getting the byte into or out of another register requires and additional operation. The flag register F is the other "half" of the the A register. By having F grouped with A in the CPU, all registers can be treated in two-byte groups. F is called the FLAG register, or FLAGS, because it is where bits indicating various conditions are kept. F itself is never used in computations. It is automatically set according to the RESULTS of other computations. 



The flag register F is never used to hold data. It contains several bits logically called "flags", that are set according to the RESULTS of other calculations. It is an eight-bit register, even though there are only six flags, and only four of these are really important for most programming applications. These four flags are called the ZERO flag (Z), the SIGN flag (S), the CARRY flag (C), and the PARITY/OVERFLOW flag (P/V). The other two flags, the HALF-CARRY flag (H) and the ADD/SUBTRACT flag (N), are used only with the DAA (decimal adjust accumulator) instruction, which is used only for BCD numbers, a relatively rare application.



Two of the 16-bit registers are called index registers, designated IX and IY. They are used, more or less, as pointers to a memory location to which an offset value can be added or subtracted.



The other two 16-bit registers are called the STACK POINTER (SP) and the PROGRAM COUNTER (PC). The program counter, abbreviated PC, determines the order in which the instructions are executed. When an instruction is being executed, the PC contains the address of the next instruction to be executed. A branch or jump instruction actually modifies the PC.



The stack pointer, SP, contains an address that must point to a free area in RAM that is used for temporary storage of values as the computer is running. If the stack ever gets destroyed, or if it points to an area in ROM or nonexistant memory, disaster can occur!. 

The remaining 8-bit registers are called the interrupt (I) and refresh (R) registers. The refresh register makes it easy and practical to use low-cost dynamic ram rather than static ram in the computer. Otherwise, the refresh register is unimportant from the programmer's standpoint. The interrupt register provides a more flexible system of interrupts for the Z-80 than the 8080. Interrupts, however, are used only for more advanced real-time programming.



In general, it is the programmer's responsibility to keep track of all the registers he is using and whether their contents can be changed without causing the program to produce an error. The contents of any register pair can easily be saved and retrieved.



The registers of the Z-80 CPU must always be considered in relation to the operations that can be carried out in them. While there are may operations that can only be done in certain registers, there are many others that can be carried out in any register.


A computer program can be defined as a series of instructions or statements prepared in a form acceptable to the computer, the purpose of which is to achieve a certain result. Actually, a computer program includes more than instructions; it also contains the data and memory addresses that the microprocessor needs to accomplish the task defined by the instructions. Clearly, if the microprocessor is to perform an addition, it must have two numbers to add and a destination for the result. The computer program must determine the sources of the data and the destination of the result as well as specifying the operation to be performed. All microprocessors execute instructions sequentially unless one of the instructions changes the execution sequence or halts the computer. (i.e. the processor gets the next instruction from the next consecutive memory address unless the current instruction specifically directs it to do otherwise.



A computer instruction can be defined as a set of characters which define an operation. The instruction set of a microprocessor is the set of binary inputs which produce defined actions during an instruction cycle. An instruction is simply a binary bit pattern - it must be available at the data inputs to the microprocessor at the proper time in order to be interpreted as an instruction. Either alone or together with other information, an instruction causes a digital computer to perform the operation or manipulate the indicated quantities. Each computer instruction is written as a series of 1s and 0s that specifically characterize that instruction and no other. Such a binary representation of a computer instruction is called machine language or machine code. For example, the machine language instruction 00000111 rotates the contents of the accumulator within the Z-80 microprocessor chip one bit to the left. The instruction 00001111 rotates the contents of the accumulator one bit to the right. When the Z-80 microprocessor receives the 8-bit binary pattern, 10000000 as the input during an instruction fetch operation, the pattern means:
"Add the contents of register B to the contents of the Accumulator"



The microprocessor, (like any other computer) recognizes only binary patterns as instructions or data: it does not recognize words or octal, decimal, or hexadecimal numbers.



Ultimately every program becomes translated into a set of binary numbers. For example, this is the Z-80 program that adds the contents of memory locations &60H and &61H and places the result in memory location &62H.
00111010
01100000
00000000
01000111
00111010
01100001
00000000
10000000
00110010
01100010
00000000



This is a machine language, or object program. If this program were entered into the memory of a Z-80-based microcomputer, the microcomputer would be able to execute it directly.There are many difficulties associated with creating programs as object, or binary machine language programs. These are some of the problems:

1) The programs are difficult to understand or debug (binary numbers all look the same, particularly after you have looked at them for a few hours).

2) The programs are slow to enter since you must enter each bit individually.

3) The programs do not describe the task which you want the computer to perform in anything resembling a human readable format.

4) The programs are long and tiresome to write.

5) The programmer often makes careless errors that are are very difficult to find. For example, the following version of the addition object program contains a single bit error. Try to find it:

	          00111010

	          01100000

	          00000000

	          01000111

	          01110010

	          01100001

	          00000000
               10000000

	          00110010

	          01100010

	          00000000



Although the computer handles binary numbers with ease, people do not. People find binary programs long, tiresome, confusing, and meaningless. Eventually a programmer may start remembering some of the binary codes, but such effort should be spent more productively.We can improve the situation somewhat by writing instructions using octal or hexadecimal, rather than binary numbers. We will use hexadecimal numbers in this book because they are shorter, and because they are the standard for the microprocessor industry.The Z-80 program to add two numbers now becomes:

	          3A

	          60

	          00

	          47

	          3A

	          60

	          00

	          80

	          32

	          62

	          00



At the very least, the hexadecimal version is shorter to write and not quite so tiring to examine.Errors are somewhat easier to find in a sequence of hexadecimal digits. The erroneous version of the addition program, in hexadecimal form, becomes:

	          3A

	          60

	          00

	          47

	          72

	          61

	          00

	          80

	          32

	          62

	          00



The mistake is easier to spot. What do we do with this hexadecimal program? The microprocessor understands only binary instruction codes. The answer is that we must convert the hexadecimal numbers to binary numbers. This conversion is a repetitive, tiresome task. People who attempt it make all sorts of petty mistakes, such as looking at the wrong line, dropping a bit, or transposing a bit or digit.This repetitive, grueling task is, however, a perfect job of a computer. The computer never gets tired or bored and never makes silly mistakes. The idea is then to write program which takes hexadecimal numbers and converts them into binary numbers. This is a standard program provided with many microprocessors; it is called a "hexadecimal loader"
.


A hexadecimal loader certainly does not solve every programming problem. The hexadecimal version of the program is still difficult to read or understand; for example, it does not distinguish instructions from data or addresses, not does the program listing provide any suggestion as to what the program does. What does 32 or 47 or 3A mean? Memorizing a card full of codes is hardly an appetizing proposition. Furthermore, the codes will be entirely different for a different microprocessor, and the program will require a large amount of documentation.



An obvious programming improvement is to assign a name to each instruction code. The instruction codes name is called a "mnemonic", or memory jogger. Mnemonic is a term that describes something used to assist the human memory.



The instruction mnemonic should describe in some way what the instruction does. In fact, every microprocessor manufacturer (they can't remember hexadecimal codes either) provides a set of mnemonics for the microprocessor instruction set. They are standard for a given microprocessor and therefore understood by all users. These are the instruction names that you will find in manuals cards, books, articles, and programs. These mnemonic codes are computer instructions written in a form the programmer can easily remember, but which must be converted into machine language later by a computer or by the user.



The problem with selecting instruction mnemonics is that not all instructions have "obvious" names. Some instructions do have obvious names (e.g. ADD, AND, OR), and others have obvious contractions (e.g. SUB for subtraction, XOR for exclusive OR), while still others have neither. The result is such mnemonics as WMP, PCHL, and even SOB (try and guess what that means!). Most manufacturers come up with mostly reasonable names and a few hopeless ones.



Along with the instruction mnemonics, the manufacturer will usually assign names to the CPU registers. As with the instruction names, some register names are obvious (e.g. A for Accumulator) while other may have only historical significance. If we use standard Z-80 instruction and register mnemonics, as defined by Zilog, our Z-80 addition program becomes:

	          LD        A,(60H)

	          LD        B,A

	          LD        A,(61H)

	          ADD       A,B

	          LD        (62H),A



The program is still far from obvious, but at least some parts are comprehensible. ADD A,B is a considerable improvement over 80: LD does suggest loading data into a register or memory location. Such a program is an assembly language program. How do we get the assembly language program into the computer? We have to translate it, either into hexadecimal or into binary numbers. You can translate and assembly language program by hand, instruction by instruction. This is called hand assembly. Hand assembly of the addition program's instruction codes may be illustrated as follows:

	          Instruction Name    Hexadecimal equivalent

	          LD        A,(NN)	         3A NN NN

	          LD        B,A	    	   	    47
               ADD       A,B	              80

	          LD        (NN),A	         32 NN NN



As is the case of hexadecimal to binary conversion, hand assembly is a rote task which is uninteresting, repetitive, and subject to numerous minor errors. Picking the wrong line, transposing digits, omitting instructions, and misreading the codes are only a few of the mistakes that you may make. Most microprocessors complicate the task even further by having instruction with different word lengths. Some instructions are one word long, which others are two or three words long. Some instructions require data in the second and third words; others require memory addresses, register numbers, or who knows what? Assembly is another rote task that we can assign to the microcomputer. The microcomputer never makes any mistakes when translating codes. it always knows how many words and what format each instruction requires. The program that does this job is called an "assembler". 



The assembler program translates a user program, or "source" program written with mnemonics, into a machine language program, or "object" program, which the microcomputer can execute. The assembler's input is a source program and its output is an object program.



Assemblers have their own rules that you must learn to abide by. These include the use of certain markers (such as spaces, commas, semicolons or colons) in appropriate places, correct spelling, the proper control information, and perhaps even the correct placement of names and number. These rules typically are a minor hindrance that can be quickly overcome.



In describing the instructions executed by nearly all computers, the term LOAD is used to indicate a transfer of data between a memory location and a register in the CPU. STORE indicates the opposite transfer, from a register to memory, and MOVE indicates a transfer of data from within the instruction itself to a register. All instructions that specify a transfer of data between a register and a memory location on the Z-80 are called LOAD instructions, abbreviated by the mnemonic LD. The direction of the transfer is indicated by the ORDER of the operands.If register A is loaded from location 100, this would be specified by the mnemonic:

	          LD        A,(100)


whereas if location 100 were loaded from Register A, it would be:

	          LD        (100),A


The parentheses around 100 are necessary to show that 100 is the address of the memory location involved in the transfer.



Lack of parentheses would indicate a move instruction:

	          LD        A,100

means that A is loaded with the VALUE 100.

It is very important that you understand the meaning of the parentheses in these instructions, as this terminology is basic to descriptions of all instructions on the Z-80. Whenever  parentheses enclose an operand in a Zilog mnemonic, it means that the operand specifies an address rather than a data value. An unparenthesized "HL" specifies the HL register pair, whereas "(HL)" indicates that the CONTENTS of HL specify an address which is involved in a data transfer.



People are born in a decimal world. The monetary system, the metric system, and the Bill of Rights have ten basic units. The decimal system is so familiar to most people that they don't have to think about how it works when they use it. Yet the decimal system works like all other number systems and looking at it can lend insight to other systems. The decimal system has ten characters: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Each of these characters has two unique properties. Each has a unique shape when written and sound when spoken, and each represents a unique quantity. Any quantity from none to nine can be represented by one of the decimal characters. A quantity of more than nine must be represented by a combination of more than one of the decimal characters. Each place or position a character occupies represents an increasing power of 10. Starting at 10*0. at the decimal point the powers of 10, and the places a character can occupy, increase to the left. Mathematical convention defines any number raised to the 0 power is equal to one.



Up to this point in your programming career, you have probably used decimal values for such things as constants, memory addresses, and POKEs. Assembly-language programming makes extensive use of binary data and hexadecimal data. Don't let these terms frighten you. They're really more simple than decimal data. Binary representation is a way of expressing numeric values using the binary digits of 0 and 1, rather than the decimal digits of 0 through 9. Binary digits represent an "on" or "off" condition. A wall switch is either on or off. In similar fashion, the transistors with the CPU portion of the TRS-80 are either on or off and hold binary values.

Now we know that a decimal number such as 921  the 9 represents 9 hundreds, the 2 represents 2 tens, and the 1 represents 1 units.












Likewise, the number 65,535 can be represented as:





In a binary number, the position of the digits represent powers of two rather than powers of ten. Instead of units, tens, hundreds, and other powers of ten, a binary number is made up of digits representing units, two, four, eight, sixteen, and other powers of two. Since there are only two binary digits, the digit at each position represents either 0 or 1 times the power of two for that position.



For instance, 11001101 represents:






In order to convert a decimal number to a binary number, we continuously divide by 2, keeping track of the remainders. For instance, to convert decimal 11 to its binary equivalent, proceed as follows:







Converting 126 decimal to its binary equivalent involves:



























The Z-80 is organized around 8-bit binary numbers. Unfortunately 8-bit binary numbers are not very handy for people to work with. Besides occupying a lot of space they are hard to say and even more difficult to remember. A rather special relationship exists between the hexadecimal number system and the binary system. The hexadecimal system, henceforth called hex, is more like decimal than is binary so it is often used by people to deal with the binary numbers required by the processor.

The hexadecimal system has 16 unique characters which are listed with their decimal equivalents below
	
Hexadecimal		Decimal
	
0				0
	
1				1
	
2				2
	
3				3
	
4				4
	
5				5
	
6				6
	
7				7
	
8				8
	
9				9
	
A				10
	
B				11
	
C				12
	
D				13
	
E				14
	
F				15



Notice that decimal quantities 10 to 15 are represented in hex by the letters A to F. As in Binary and decimal, the hex system uses increasing powers of 16 in position to the left of the least significant digit to represent quantities greater than 15.

For instance 3A0E means:





Therefore, to convert a hexadecimal number to its decimal equivalent, you multiply each digit by its positional power of 16 and add the products.

For example the hex number AD represents:


















A decimal number can be expressed in hexadecimal form by breaking the decimal number into its powers of 16 components, a process like the decimal to binary conversion. Decimal 75 can be converted to hex:




Let's try to convert decimal 17,433 to hex:







If a binary number is treated as groups of four binary digits, the binary number can be converted into a hexadecimal number. Hexadecimal means nothing more than powers of sixteen. The groups of four bits represent 0000 through 1111. now, 0000 through 1001 correspond to the decimal digits 0 through 9, and the hexadecimal digits for 0000 through 1001 are similarly designated 0 through 9. This leaves the groups of bits from 1010 through 1111. Recalling that each numeric character must be unique in shape and represent a unique quantity, the digits greater than 9 are named A, B, C, D, E, and F.

The Hexadecimal-Binary Equivalents are listed below:


	Hexadecimal		Binary

	0		0000
	1		0001

	2		0010

	3		0011

	4		0100

	5		0101

	6		0110

	7		0111

	8		1000

	9		1001

	A		1010

	B		1011

	C		1100

	D		1101

	E		1110

	F		1111


In general we'll be working with groups of eight binary digits or sixteen binary digits within the TRS-80. Binary Digit was long ago shortened to bit to prompt shorter lunches in the computer science cafeteria when researchers started talking shop.  Whenever bit is used, then, it will mean one binary digit of either a 1 or 0. A group of four bits may be referred to as a hexadecimal digit of 0 through F. When this is done, the suffix H is added. The symbol EH, therefore, represents the hexadecimal digit E or the binary digits 1110. A group of eight bits is commonly called a byte. A byte is made up of two hexadecimal digits, since there are two groups of four bits.



Examine the binary equivalents of the hex character set in the Table above.



Notice that any hex character can be expressed as a 4-bit binary number. Also notice that all binary possibilities 0000 to 1111 are used in expressing the hex character set. These two conditions allow simple hex-to binary and binary to hex conversions. 



To convert a binary number to its hex equivalent, group the binary number into 4-bit segments starting with the rightmost bit. then convert each 4-bit segment into its hex equivalent.



Convert 11010111










Try 1010101











And - 1100000101101111




















To convert a hexadecimal number to its binary equivalent simply express each hex character as a 4-bit binary number. All four binary bits must be listed even when some or all of them are zeroes. 




Convert BA:













Try converting C7F



















And the hex number 4061



























The utility of expressing binary numbers in hex form should now be apparent. The hex-binary and binary-hex conversions are quite simple and, more importantly, hex numbers are simpler to say, write, and remember than are binary numbers. The number 75E4 is certainly more manageable by people than 111010111100100! Binary numbers will generally be expressed in their hex form by the monitor program through which we interact with the Z-80.

                                                                                         