z80:Getting Started with Asm

From Learn @ Cemetech
Jump to: navigation, search

Existing Tutorials

Here's a link to some tutorials that can get you started on learning assembly.

For more information, read on.

What is Asm?

What exactly is the z80 assembly language? I'm sure many of you who have roamed through the ticalc.org archives, and have found an extensive library of z80 assembly programs. Before we learn how to program it, it is quite important to get an understanding of what assembly language is.

History of Computer Languages

Machine language

In the beginning of programming, programmers had to write code by manually setting on/off switches. Depending on whether the switch was set on or off, the computer would then do something. Obviously this form of programming is extremely time intensive and not very practical. Why did the 1st computer designers choose only 2 different elementary data types? It turns out that before micro processors, computers were designed to interpret signals through vacuum tubes, so the only method of communication was whether these signals were coming or not coming (hence the on/off idea). But enough about the basic elements of programming, on to...

Assembly language

Not long after the development of microchips came the development of compilers. These were primitive programs that took user inputs (the code) and turned them into on/off data that the computer could use. The basis of assembly language is the use of primitive operations to accomplish tasks more quickly than with writing in machine language, as well as develop a language that resembles native language (what you speak and write). One of these languages, called z80, is the basis of programming for the Texas Instruments (TI) series of graphing calculators, the TI-73, -82, -83, -83+, -84+, -86, etc. However, one of the problems with ASM programming is that it still lacked structured programming and syntax that more closely resembles native language. So, this led to...

Higher Level languages

The zenith of language design (at least for now), higher level languages provide a powerful mixture of structure as well as functionality. Examples of higher level languages include C, C++, Java, Pascal, and Visual Basic. Since higher level languages aren't included in the scope of this site, let's move on.

Assembly vs. TI-Basic

Why should anyone bother to learn assembly? How does assembly compare to TI-Basic? These questions will be answered in this section.

Advantages of Assembly over TI-Basic

  • Speed. If programmed correctly, assembly code can run many times faster than TI-Basic. This is because assembly code directly sends instructions to the processor or hardware, allowing for direct response.
  • Functionality. Because assembly can poll hardware and memory directly, it has far greater functionality than TI-Basic. This allows assembly programmers to do things that TI-Basic programmers could only dream of.
  • Protection. Assembly programs can not normally be altered by a user. This prevents users from accidentally deleting crucial code from the program, as sometimes happens in TI-Basic programs. It also allows you to keep the source a secret if you ever wish to do so.

Disadvantages of Assembly over TI-Basic

  • Size. For the most part, assembly programs might be larger than TI-Basic programs. This is because TI-Basic programs are composed of tokens that take up ~1 byte each. These tokens are parsed on runtime to perform functions pre-programmed into the TI-OS, saving space in the actual program. On the other hand, assembly programmers have to manually code advanced functions by themselves, and sometimes will even re-write basic functions.
  • Learning curve. Because of the complexity of assembly, it has a rather high learning curve. In order to truly understand how to program efficient assembly code takes a great deal of effort.
  • Stability. Because assembly can change itself as well as anything else in memory, it is very unstable and very prone to crashes. Also, assembly programs do not have any way of error checking at runtime and you cannot normally break out of an assembly program. This results in having to reset the RAM many times. It is suggested that assembly programs be tested first on emulators before sending them to a real calculator.

Required stuff

It's almost time to start writing assembly code, but before you do, you will need some things.

  • Computer. This will be necessary to actually write the code. I will assume you have access to a computer if you are reading this.
  • Calculator. Necessary if you ever want to debug/test a program. Again, I will assume you have one.
  • Calculator-Computer link cable. Necessary if you want to transfer data between your computer/calculator.
  • Text Editor. You'll need this to write the code.
  • Compiler and Linker. Necessary to get change your code from assembly into machine language, then into a file that can be run on your calculator.

Integrated Development Environments (IDEs)

IDEs are programs that help you program, compile/link, and debug your program. Although not necessary for programming, it is recommended that you become familiar with a good IDE so that you can take your programming to the next level. Generally IDEs have a graphical user interface (GUI) that will allow you to perform tasks without having to type commands in. For more information on IDEs, see here.

Setup

If you are using an IDE, follow its instructions on setting it up and skip the rest of this section, or if you prefer to use the more common TASM setup, read on.

For those who want it, I've set up Brass for you. If you want, download the Doors CS SDK, which includes Brass and a script to make it easier to assemble programs. Just pick a folder to extract it to and you can skip the rest of this page. Or, if you want, read the rest just to get some basic information on setting up Brass.

Step 1: getting the necessary files

Here's a list of links for things you'll need:

Download all of them and put them all into a folder you'll use for all of your ASM programming stuff (Example: "C:\Asm\")

Step 2: Setting up the Folder Structure

Create 4 folders inside of "C:\Asm\" named: "tasm", "exec", "list", and "source". As the name implies, tasm is where all of the compile/linking programs and includes go. So, move the two zip files you downloaded into it and extract them. If you want, delete the extra files included with the last one or move them somewhere else if you want to keep them. Keep everything else.

Step 3: Creating the Assembly Batch File

Use Notepad or Notepad++ to create a new file named "compile.bat" in the main Asm directory. Make sure that you have file extensions turned on, and that you don't save as a .txt file: you don't want compile.bat.txt! Put the follow contents (taken from the Doors CS SDK) into compile.bat:

echo Syntax: asm [NAME (w/o extension)]
   
@echo off
cls
echo               Doors CS Assembler/Compiler    
echo     Adapted for z80 Heaven Tutorials by Christopher Mitchell
echo ----- Assembling %1 for the TI-83/84 Plus...
echo #define TI83P >tasm\zztemp.asm
echo .binarymode TI8X >>tasm\zztemp.asm
set output=%~n1.8xp
set varname=%~n1
call :UpCase varname
echo .variablename %varname% >>tasm\zztemp.asm
if exist source\%1.asm (
	type source\%1.asm >>tasm\zztemp.asm
) else (
	if exist source\%1.z80 (
		type source\%1.z80 >>tasm\zztemp.asm
	) else (
		if exist source\%1 (
			type source\%1 >>tasm\zztemp.asm
		) else (
			echo ----- '%1', '%1.asm', and '%1.z80' not found!
			goto ERRORS
		)
	)
)
cd tasm
brass zztemp.asm ..\exec\%output% -l ..\list\%1.list.html
if errorlevel 1 goto ERRORS
cd..
rem cd exec
rem ..\tasm\binpac8x.py %1.bin
color 02
echo ----- %1 for the TI-83/84 Plus Assembled and Compiled.
color 07
echo TI-83 Plus version is %output%
goto DONE
:ERRORS
color 04
echo ----- There were errors.
color 07
rem cd..
:DONE
del tasm\zztemp.asm >nul
rem del %1.bin >nul
rem cd..
GOTO:EOF
:UpCase
:: Subroutine to convert a variable VALUE to all UPPER CASE.
:: The argument for this subroutine is the variable NAME.
FOR %%i IN ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I" "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R" "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z") DO CALL SET "%1=%%%1:%%~i%%"
GOTO:EOF

save and close "asm.bat".

Compiling

To compile your code, open up command prompt (start, run... command prompt) and navigate to the location where you extracted the zip file.

Command Prompt instruction What it does
cd "directory" Look for folder "directory" (without quotes) and open it
cd "directory1/directory2" jump to directory2 inside of directory1
cd .. Jump to the "parent" folder
C:, Z:, etc. Jump to a different hard disk
dir list all folders and files in the current folder you're in

Once you get to "../asm", type in "compile sourcename". Sourcename is the name of your source file, which you put in "../asm/source". Don't include the ".z80" (or the ".asm", if that's the extension you use) at the end.

If there were no errors during compilation, your program should be ready for you in the "../asm/exec" folder. Just send it to your calculator and run "Asm(prgmname)".

Conclusion

That's all there is to setting it up. If you want to actually learn the z80 language, see the other topics in this section to get a glimpse of what it can do.