Translate MASM mnemonic into standard 6502

discuss pc<>acorn file transfer issues and the use of other utils
Post Reply
Coeus
Posts: 3557
Joined: Mon Jul 25, 2016 12:05 pm
Contact:

Translate MASM mnemonic into standard 6502

Post by Coeus »

Acorn MASM uses an alternative set of 6502 mnemonics in which the addressing mode is part of the mnemonic rather than part of the operand. It wasn't the only assembler to do this. Here is a Perl script that will convert this syntax into the standard 6502 syntax. Note is does not do anything with the assembler directives, i.e. it is not a tool to get MASM source code to assemble with something else, just to create a version that is easier to read/compare for someone unfamiliar with the Acorn syntax.

Code: Select all

#! /usr/bin/perl

sub masm2std {
	my ($label, $opcode, $operand, $comment) = @_;
	$opcode = uc($opcode);
	if ($opcode =~ /^(...)IM$/) {
		$opcode = $1;
		$operand = "#$operand";
	}
	elsif ($opcode =~/^(...)A([XY])$/) {
		$opcode = $1;
		$operand = "$operand,$2";
	}
	elsif ($opcode =~ /^(...)A$/) {
		$opcode = $1;
		$operand = 'A';
	}
	elsif ($opcode =~ /^(...)IX$/) {
		$opcode = $1;
		$operand = "($operand,X)";
	}
	elsif ($opcode =~ /^(...)IY$/) {
		$opcode = $1;
		$operand = "($operand),Y";
	}
	elsif ($opcode =~ /^(...)Z([XY])$/) {
		$opcode = $1;
		$operand = "$operand,$2";
	}
	elsif ($opcode eq 'JMI') {
		$opcode = 'JMP';
		$operand = "($operand)";
	}
	if (defined($comment)) {
		printf("%-16s%-8s%-16s%s\n", $label, $opcode, $operand, $comment);
	}
	elsif (defined($operand)) {
		printf("%-16s%-8s%s\n", $label, $opcode, $operand);
	}
	else {
		printf("%-16s%s\n", $label, $opcode);
	}
}

while (<>) {
	chomp;
	if (m/^\s*;/ || m/^\s*$/) {
		print "$_\n";
	}
	elsif (m/^(\S+?)\s+(\S+?)\s+(.+?)\s*(;.*)$/) {
		&masm2std($1, $2, $3, $4);
	}
	elsif (m/^(\S+?)\s+(\S+?)\s+(.*)$/) {
		&masm2std($1, $2, $3, undef);
	}
	elsif (m/^(\S+?)\s+(.*)$/) {
		&masm2std($1, $2, undef, undef);
	}
	elsif (m/^\s+(\S+?)\s+(.+?)\s*(;.*)$/) {
		&masm2std(undef, $1, $2, $3);
	}
	elsif (m/^\s+(\S+?)\s+(.*)$/) {
		&masm2std(undef, $1, $2, undef);
	}
	elsif (m/^\s+(.*)$/) {
		&masm2std(undef, $1, undef, undef);
	}
	else {
		print "$_\n";
	}
}
P.S. it is almost all regular expressions so should translate easily into any other language that supports them well.
Post Reply

Return to “software & utilities for the pc, mac or unix”