Showing posts with label assembler. Show all posts
Showing posts with label assembler. Show all posts

Wednesday, November 24, 2021

A List of Cross-Assemblers for Retrocomputing

 In my retrocomputing projects with various processors I have used a number of cross-assemblers to build code. I've had to use several in order to support different processors as well as for compatibility with different software packages. For the most part they are similar, but have their own unique features and quirks.

I put this table together, mostly for my own reference, that others may also find useful. It is by no means a complete list -- just those that I have used.

Name Author Source Supported Processors Key Features Comments
as9 Motorola, with changes by Albert van der Horst and others https://home.hccnet.nl/a.w.m.van.der.horst/m6809.html 6809 macros 100% compatible with Motorola assembler
asl Alfred Arnold and others http://john.ccac.rwth-aachen.de:8000/as/ many (over 50) Macros I have used with several processors
asm6809 Ciaran Anscomb https://www.6809.org.uk/asm6809/ 6809, 6309 macros Good choice if 6309 support needed.
cc65 John R. Dunning, Ullrich von Bassewitz, and others https://www.cc65.org/ 6502, 65C02 C compiler, assembler, linker, librarian Supports several 8-bit computer platforms.
crasm Leon Bottou https://github.com/colinbourassa/crasm 6800, 6801, 6803, 6502, 65C02, Z80 macros Included in most Linux distributions.
gcc Richard Stallman and others https://gcc.gnu.org/git/ Many Compilers and assemblers I used for 68000. Included in most Linux distributions.
lwasm William Astle and others http://lwtools.projects.l-w.ca/ 6809, 6309 Cross-assembler and linker. macros
vasmm68k_mot Volker Barthelmann http://sun.hasenbraten.de/vasm/ Many Cross-assembler I used for 68000.
z80asm Bas Wijnen http://savannah.nongnu.org/projects/z80asm Z80 Z80 cross-assembler Included in most Linux distributions.

Sunday, May 11, 2014

Apple II Monitor Ported to Apple 1

As announced here,  a cassette tape was found that contains a port of the Apple II Monitor to the Apple 1. It was done by Winston Gayler. Additional work was done by Wendell Sander, who posted documentation, cassette tape sound files and Woz Mon binaries here .

I've tested the code on a Briel Replica 1 and it works quite well. The only issue I found on the Replica 1 is that the commands that require Control keys do not work as the Replica 1 does not emulate Control keys when using a PS/2 keyboard. They will work if entered from the serial port.

This weekend I went a little further adapted the original monitor source from the "Red Book" to build under the CA65 assembler, then applied the changes for the Apple 1. The result is code that can be assembled from source and easily relocated.

References:

  1. www.apple1notes.com/old_apple/Monitor_II_on_1.html
  2. www.applefritter.com/content/apple-ii-monitor-ported-apple-1
  3. github.com/jefftranter/6502/tree/master/asm/Apple%5D%5BMonitor


Monday, March 17, 2014

Using the CP/M Assembler

I'll continue giving more details on the programs I demonstrated in the YouTube video The Briel Altair 8800 Kit, Part 5: Advanced Topics.

This time, the CP/M Assembler.

To follow the demos in the video, boot up a CP/M 2.2 image that has the assembler (ASM.COM), loader (LOAD.COM), and optionally the DDT debugger (DDT.COM). The Briel CPM22.DSK image on the CD will work.

If you use CP/M 3, there is a more sophisticated macro assembler called MAC.COM. I have not tried it with these examples.

Create a file HELLO.ASM with the following source code and copy it to your SD card. Use TREAD to copy it to the CP/M disk (or enter it locally on CP/M if you have a text editor such as ED or WordStar).

; This is an example of the "Hello World" program.
; Uses 8080 assembler mnemonics.
ORG 100h ; cpm programs start address.
JMP START ; go to program start.

; Variable storage space
MsgStr: DB 13,10,'Hello world.',13,10,0
Stack1: DW 0 ; place to save old stack.
Sbot: DS 32 ; temp stack for us to use.

; Constants
STOP: EQU $-1 ; top of our stack.
BDOS: EQU 5 ; address of BDOS entry.

; Start of code segment
START: LXI H, 0 ; HL = 0.
DAD SP ; HL = SP.
SHLD Stack1 ; save original stack.
LXI H, STOP ; HL = address of new stack.
SPHL ; stack pointer = our stack.
LXI H, MsgStr ; HL = address of string.
LOOP1: MOV A, M ; read string char.
ORA A ; set cpu flags.
JZ EXIT ; if char = 0 done.
MOV E, A ; E = char to send.
MVI C, 2 ; we want BDOS func 2.
PUSH H ; save HL register.
CALL BDOS ; call BDOS function.
POP H ; restore HL register
INX H ; point to next char.
JMP LOOP1 ; do next char.

; Exit and return code
EXIT: LHLD Stack1 ; HL = entry stack address.
SPHL ; SP = value on entry.
RET ; return control back to CPM.
END

Since I recorded the video, I found an even simpler hello world program which you can use instead if you like. The source code is below. I saved it as HELLO1.ASM and used it in the examples that follow.

        org 100h
bdos    equ    0005h    ; BDOS entry point
start:  mvi    c,9      ; BDOS function: output string
        lxi    d,msg$   ; address of msg
        call   bdos
        ret             ; return to CCP
msg$:   db    'Hello, world!$'
        end

Here is an example session assembling, linking, and running it, and disassembling it with DDT. Commands typed by the user are in bold. Note that in the video, when I demonstrated DDT, I did not correctly load the file, so the disassembly in the video was incorrect. The example below is correct. Note that this session was run on drive B so I needed to specify the path to the tools on drive A.

B>a:asm hello1

CP/M ASSEMBLER - VER 2.0
0117
000H USE FACTOR
END OF ASSEMBLY

B>a:load hello1

FIRST ADDRESS 0100
LAST  ADDRESS 0116
BYTES READ    0017
RECORDS WRITTEN 01

B>type hello1.hex
:100100000E09110901CD0500C948656C6C6F2C20E2
:07011000776F726C6421247B
:0000000000

B>type hello1.prn

 0100                   org 100h
 0005 =         bdos    equ    0005h    ; BDOS entry point
 0100 0E09      start:  mvi    c,9      ; BDOS function: output string
 0102 110901            lxi    d,msg$   ; address of msg
 0105 CD0500            call   bdos
 0108 C9                ret             ; return to CCP
 0109 48656C6C6Fmsg$:   db    'Hello, world!$'
 0117                   end

B>a:ddt hello1.com

DDT VERS 2.2
NEXT  PC
0180 0100
-l
  0100  MVI  C,09
  0102  LXI  D,0109
  0105  CALL 0005
  0108  RET  
  0109  MOV  C,B
  010A  MOV  H,L
  010B  MOV  L,H
  010C  MOV  L,H
  010D  MOV  L,A
  010E  INR  L
  010F  ??=  20
-^C
B>

Note that the assembler is for the Intel 8080. If you find CP/M programs for the Zilog Z80 and try to build them, they will not assemblem as it uses different mnemonics. They also won't run on the
Briel Altair 8800 as it only emulates Intel 8080 instructions.

The source for the "hello world" 8080 assembler program I listed above, as well as more information on ASM, can be found here.

Monday, July 9, 2012

Implementing A Mini Assembler


One of the features missing from JMON was a "mini assembler" like the one included in the Apple II computers.

I implemented one with roughly the same features and limitations as the one in the Apple II, i.e. it assembles simple 6502 assembly language code where all numeric values are in hex and there is no support for symbols or labels.

It is very handy for writing short assembly language programs where you don't want to fire up a cross-assembler or even Krusader.

In conjunction with the disassembler you can verify the program you entered and then run it.

The code leveraged the data tables in the JMON disassembler. Most of the work is parsing the entered code to validate it and determine the addressing mode.

While I only implemented 6502 support in this first version, because it uses the tables from my disassembler, it does accept 65C02 and even 65816 instructions that use the standard 6502 addressing modes. It just doesn't yet understand the new addressing modes for the 65C02 or 65816.

A sample session is shown below, where the user entered text is in bold.

? A 6F00
6F00: CLD
6F01: CLI
6F02: LDY #7F
6F04: STY D012
6F07: LDA #A7
6F09: STA D011
6F0C: STA D013
6F0F: LDA #5C
6F11: JSR 6FEF
6F14: JSR 6F1B
6F17: BCC 6F0F
6F19: BCS 6F14
6F1B: JSR 6EE5
6F1E: LDY #01
6F20: DEY
6F21: BMI 6F1B
6F23: JSR 6EBE
6F26: STA 0200,Y
6F29: CMP #0D
6F2B: BEQ 6F38
6F2D: CMP #5F
6F2F: BEQ 6F20
6F31: CMP #1B
6F33:

It provides meaningful error messages and checks the validity of the code. Below are some error messages:

6000: LDA #1234
INVALID OPERAND
6000: LDA (1234)
INVALID ADDRESSING MODE
6000: BNE 7000
RELATIVE BRANCH OUT OF RANGE
6000: LDB
INVALID INSTRUCTION

I tested it as I built up the code, but the final test was to disassemble about a thousand lines of JMON code and then feed that back to the assembler and confirm that it accepted it and generated the same code.

I'll probably add support for the rest of the 65C02 instructions in future and maybe also the 65816.

As always, the code is available here.

Tuesday, June 26, 2012

Debugging Tips for 6502 Programing


After working on some 6502 assembler code recently I was thinking about some useful tips for debugging that I could pass on. Here are a few thoughts based on my experience. Many of these are not specific to the 6502 but valid for assembly language or any software debugging in general.

First, here are some suggestions on things to to to minimize your time spent debugging:

1. Use a cross-assembler

While some programmers can write machine code in their head and change code in memory on the fly, with assembler code you can't afford to make simple mistakes like entering the wrong hex code for an instruction. For any significant amount of code I recommend you use a cross-assembler (or at last a resident assembler, like Krusader).

2. Comment your code

It really helps to comment code, even if you are the only person who may ever look at it. A few weeks or months from now you may not understand what your code is doing if it is uncommented. As compared to high-level languages like C or C++ (or BASIC), I think it is good practice to comment almost every line of code, with additional comments describing what each routine does and tricky features and algorithms, etc. Here is an example:

; Get character from keyboard
; Returns character in A
; Clears high bit to be valid ASCII
; Registers changed: A
GetKey:
        LDA KBDCR               ; Read keyboard control register
        BPL GetKey              ; Loop until key pressed (bit 7 goes high)
        LDA KBD                 ; Get keyboard data
        AND #%01111111          ; Clear ms bit to convert to standard ASCII
        RTS

3. Use a source code management system

Even if you are the only person who may ever use your code, using a source code management system will help you keep different versions of your code during development. This is really useful for the times when you break something that was previously working and you need to figure out what changed. If you use a remote server it also protects you against losing all your work due to human error or hardware failure. There are many services now like github.com, many of which are free for free software projects, that will host your code so you don't have to set up a server.

4. Build up and test small routines

Don't write several hundred lines of code and then attempt to debug it. Write code in small routines and test them individually. It is much easier to debug and your code will likely end up being more modular as well.

5. Do a desk check

A desk check is a manual process of running through your code, as if you were the computer, verifying that it works correctly. It is often helpful to actually step through the code and keep track of values of variables and registers on paper. You will often find errors in your code this way and can more efficiently find and correct the problems.

I have to admit that I sometimes only go back and do a desk check when my code doesn't work the first time (which is usually the case).

6. Print statement debugging

Often you need to figure out where your code is going, and what the values of certain variables are. A tried and true method of debugging is to put instructions in to print information at key points in the code.

You may want to write functions that can print strings and 8 and 16-bit values that you can call for this purpose. In simple code it may be enough to just print single characters or values. If your hardware is such that you don't have a device to send characters to (screen, serial port, etc.) then even a LED or output port can be used to output debug information.

7. Use a simulator.

If you don't have a good debugger on your system you should consider using one of the available 6502 simulators to run and test your code. They may provide a better environment for debugging and testing and eliminate the possibility of hardware problems.

8. Use a debugger

On the Replica 1 the Krusader assembler provide a "mini-monitor" which lets you change registers and single step through code.

My JMON monitor  also has a breakpoint feature that works in conjunction with the mini monitor.

An assembler listing generated by your cross-assembler is also very helpful when single stepping and using breakpoints so you can relate the code to addresses.

Common Errors

Here is a list of what in my experience are some of the more common errors made, even by experienced programmers, when writing in 6502 assembler.

Off by one errors


This is the classic error where a condition test is off by one, such as a loop which runs one time too few or too often.

Branching on the wrong condition

It is easy to use a "BEQ" when you meant "BNE" and hard to notice the error when looking at the code you wrote. Good comments can help here.

Forgetting to clear/set carry before addition/subtraction

This is specific to the 6502 and most programmers soon learn not to make this mistake.

Use # when not needed or missing it when needed

It is easy to forget to use immediate addressing mode mode when that is what you intended or inadvertently use immediate addressing when you shouldn't. For example, the two code examples below are probably wrong but you can easily overlook the mistake, particularly in your own code:

  LDA '$'               ; probably should have been #'$'
  JSR PrintChar


  LDA #VAL              ; if VAL is a memory location, this is wrong
  JSR PrintByte

Registers being changed by called routines

A common error is to call a subroutine and forget that it changes some registers containing values that you subsequently use. I try to always document what registers are changed and try to preserve all registers in code unless I need to optimize them for efficiency.

Conflicts in memory locations (usually page zero)

This is a common error, especially when calling other code that might use memory locations used by your code.

Branching to the wrong part of loop

It is easy to branch to the wrong location in a loop. How many times does the code below run through the loop?

loop: LDX #10
      DEX
      BNE loop

I recently had an error like this (but a little more subtle):

loop1:
        JSR GetKey              ; Get character from keyboard
        CMP #CR                 ; key pressed?
        BEQ EnterPressed        ; If so, handle it
        CMP #ESC                ; key pressed?
        BEQ EscapePressed       ; If so, handle it
        JSR PrintChar           ; Echo the key pressed
        STA IN+1,X              ; Store character in buffer (skip first length byte)
        INX                     ; Advance index into buffer
        CPX #$7E                ; Buffer full?
        BEQ EnterPressed        ; If so, return as if was pressed
        BNE loop                ; Always taken

You can ignore most of what the code is doing. The problem is the last instruction. I meant to branch to label "loop1" but I branched to "loop", which was in a completely different routine (so it assembled okay). In part it happened because I copied and pasted the code from a similar routine and then changed it but left the wrong label. The program ran but the behaviour left me scratching my head and I had to single step through the code before I realized the problem.

Here are some additional errors which in my experience are less common but you should watch out for:

Pushing data on the stack and not correctly restoring it

A particularly bad error is pushing data on the stack and then doing an RTS, which will cause your code to return to the wrong address.

Leaving the CPU in decimal mode

If you use decimal mode, be sure it set it back to binary mode when done.

Jump indirect across page boundary

The 6502 has a bug when using jump indirect across a page boundary. The 65C02 CPU fixes this.

There are many good books on 6502 programming, some of which are freely available on the Internet on sites like http://www.6502.org.

A good reference on 6502 hardware and software advice that was recently posted on the 6502.org forum is here.

Thursday, April 26, 2012

Porting 2KSA to the Replica 1


2KSA is a symbolic assembler written in 1979 by Robert Denison. A PDF document with source code and description is available here.

Cover Page of 2KSA Manual

It was primarily written for KIM-1 systems.

It is a somewhat primitive assembler by today's standards but amazingly it is only about 2 kilobytes in size, written in assembler, allowing it to run resident on very small systems like the KIM-1. It is also well documented mostly portable. It is able to assemble itself.

I thought it would be fun to try to port it to the Replica 1. The first task is to enter the source code. The PDF file is all images, and while I have found some text versions of the document they are OCRed and are incomplete and have errors.

I am in the process of entering the code and getting it to assemble using cc65 to produce the same binary code as the dumps in the documentation. KIM-1 users may find this useful so I will make the original KIM-1 version available.

The next step will be to port it to the Replica 1 which should only involve replacing the I/O routines and some addresses. If it works well I may make some enhancements, like extending the error messages from the current one character messages.

So far I have found a few errors in the document but I can resolve them by looking at the memory dumps.

The source code can be found here.

Saturday, April 21, 2012

Latest CC65 Patches for Replica 1

There have been various patches for the cc65 assembler / compiler tools to support Apple 1 and Replica 1 systems but I haven't seen any patches for the latest release. I took patches for 2.12 (thanks to  Jeremy Rand for these) and got them working with 2.13.3. The patches can be found here.

I am hoping to get these patches applied back to the original source at cc65.org so future releases will support Apple 1 / Replica 1 systems out of the box.