Jump to content

Z80 Assembly/Printable version

From Wikibooks, open books for an open world


Z80 Assembly

The current, editable version of this book is available in Wikibooks, the open-content textbooks collection, at
https://en.wikibooks.org/wiki/Z80_Assembly

Permission is granted to copy, distribute, and/or modify this document under the terms of the Creative Commons Attribution-ShareAlike 3.0 License.

Introduction

Why Learn z80?

[edit | edit source]

While most computers today are 64 bit with Intel x86 or AMD processors, there are still many devices that use Z80 processors. Most notable of such devices are GameBoy, Sega Genesis and Handheld Calculators such as Casio and the TI-series. Several other consumer electronics devices such as MIDI synthesizers also have a Z80 processor.

But why 8-bits?

[edit | edit source]

The Z80 microprocessor was designed in the 70s. Its competitor was Intel's 8008. Most computers in the 70s were 8-bit.


Z80 Architecture

The Z80 architecture is based on that of the Intel 8080.

Registers

[edit | edit source]

The Z80 has eight main registers, six of which are paired to form three 16-bit general-purpose registers.

  • A (accumulator) is the main 8-bit general-purpose register.
  • F (flags) stores flags used to control the flow of a program that are set based on the last arithmetic instruction:
    • Bit 7 (S; sign bit) indicates whether the result was negative.
    • Bit 6 (Z; zero bit) indicates whether the result was zero.
    • Bit 4 (H; half-carry bit) indicates whether the result required a carry from a lower to an upper nybble.
    • Bit 2 (P; parity bit) indicates the parity of the result.
    • Bit 1 (N; subtraction bit) indicates whether the operation was a subtraction.
    • Bit 0 (C; carry bit) indicates whether a carry occurred.
  • BC is a 16-bit register, made up of B (most significant byte) and C (least significant byte).
  • DE is a 16-bit register, made up of D (most significant byte) and E (least significant byte).
  • HL is a 16-bit register, made up of H (most significant byte) and L (least significant byte). HL is generally used for addressing locations in memory, as there are a number of instructions that use HL for this purpose that don't have counterparts for BC and DE.

Each of these main registers has a shadow register counterpart. These registers are accessed by swapping with the main registers using the EX and EXX instructions. EX can be used to swap A and F with their shadow counterparts A' and F', while EXX swaps the three pairs BC, DE, HL with their counterparts BC', DE', HL'.

In addition to the 8-bit registers there are a number of special 16-bit registers:

  • IX and IY are the index registers, designed to be used as base offsets for addressing memory.
  • PC is the program counter, indicating the location of the currently executing instruction.
  • SP is the stack pointer, indicating the location of the top of the processor stack.


What You Need

To begin programming, you will require the following.

  1. Calculator: a TI-82, TI-83x, TI-84x, TI-85, or TI-86 or newer model.
  2. TASM: This program "assembles" the assembly code you will write to code that the computer understands.
  3. TI-83 Plus Include File: this text file assists TASM by giving it necessary information when assembling.
  4. DevPac8x: this program changes the code that TASM has assembled into a file that you can transfer to your calculator.
  5. TI SDK: also known as the TI 83 Plus Flash Debugger, this program allows programs to be tested on the computer for errors without having to transfer them to the calculator.
  6. Link: this will allow you to transfer your programs to the calculator from the computer.

Now that you have everything, you will need to set up the environment so that everything works together. It is highly advisable to follow examples given.

  1. Make a folder where you will put all of your Assembly files (e.g. C:\ASM).
  2. Unzip the TASM files into a subfolder of ASM titled 'TASM' (e.g. C:\ASM\TASM).
  3. Put 'ti83plus.inc' into your TASM folder.
  4. Unzip the DevPac8x files into a subfolder of ASM titled 'devpac8x' (e.g. C:\ASM\devpac8x).
  5. Make another subfolder of ASM titled 'Programs' (e.g. C:\ASM\Programs). This folder will be where all of your programs will be placed.
  6. Install the TI SDK.
  7. Open Notepad and type the following:
 @echo off
 echo Compiling...
 move %1.z80 ..\tasm
 cd ..\tasm
 tasm -t80 -i -b %1.z80 %1.bin
 move %1.z80 ..\programs
 move %1.bin ..\devpac8x
 cd ..\devpac8x
 devpac8x %1
 move %1.bin ..\programs
 move %1.8xp ..\programs
 cd ..\programs
 echo Done!

Save this as 'compile.bat' into your Programs folder. Make sure to set 'Save as type:' to 'All files' or it will save as 'compile.bat.txt'.

Now, just verify that you know how to use the link and turn on your calculator.


Hello World

In this section you will make a program to display "Hello, World!" on the screen.

Writing the Program

[edit | edit source]

Make a new text file in Notepad and type in the following text:

.NOLIST
#define   EQU   .equ
#define   equ   .equ
#define   END   .end
#define   end   .end
#include "ti83plus.inc"
.LIST

     .org 9D93h
     .db $BB,$6D
      ld a,0
      ld (CURCOL),a
      ld (CURROW),a
      ld hl,text
      B_CALL(_PutS)
      ret
text:
      .db "Hello, World!",0

.end
end

The tabbed lines need to be tabbed! Save this file as myprog.z80 into your "Programs" subfolder.

Compiling the Program

[edit | edit source]

Compiling essentially means translating text code into code that a machine can read.

To compile your program myprog.z80 first open a Command Prompt (Start > Run... > command in Win9x, or Start > Run... > cmd in WinNT). You should be faced with a black window with some white text saying what folder you are in. The first step is to change the folder to your programs folder. To do this use the following commands:

  1. cd subfoldername (This will go to the subfolder that you tell it to.)
  2. cd .. (This will instantly go to the folder that holds the folder you are in.)

Afterwards, you need to type in:

  1. compile myprog

This should automatically compile myprog.z80 if you have followed all instructions correctly.

Screenshot

The finished program should be located in your "Programs" folder as myprog.8xp.

Testing the Program

[edit | edit source]

On your computer, open up the TI 83 Plus Flash Debugger to make a fake calculator for testing purposes. Click on the white paper icon to make a new fake calculator and choose the 83+ calculator. Then click on Load... RAM File and open your compiled program (myprog.8xp). Finally, click on the triangle-shaped play button to begin emulation. On the fake calculator, press [2nd]+[Catalog] to bring up the catalog and arrow down to Asm(. Press enter to insert the command into the screen and then select your program ([PRGM],[ENTER]). The screen on the calc should say Asm(prgmMYPROG). Now press enter, and "Hello World" should be displayed on the screen.

Always test your programs on the Flash Debugger before you use them on your real calculator because there is a large chance of your real calculator crashing.

Commands of the Program

[edit | edit source]

The only lines of importance now are the middle ones:

      ld a,0
      ld (CURCOL),a
      ld (CURROW),a
      ld hl,text
      B_CALL(_PutS)
      ret
text:
      .db "Hello, World!",0

When the calculator executes a program, it follows the commands line by line. The first line (ld a,0) loads zero into a, which is a commonly used variable. So now the variable a=0. The next two lines load that number into the cursor row and column. So now the cursor is located at 0,0 and the next text will be displayed at that location. The fourth line (ld hl,text), loads the location of .db "Hello... into hl, another variable. B_Call(_PutS) takes the text that hl has specified and displays it at the current cursor location. Finally, ret tells the calculator to exit out of the program. You should now have a vague understanding of the functions of most of the lines in your first program.