Assembly programming

Programming the 6502 microprocessor and its relatives in assembly and other languages.
Kris1978
Posts: 41
Joined: 21 May 2018

Re: Assembly programming

Post by Kris1978 »

Quote:
....You need to be aware that every assembler has different syntax....
WHAT? WHY? 6502 Assembly is....6502 assembly!!!! Isn't it???

Oh come on now guys! What means that "different syntax"?

Is it a waste of time the books I'm reading?
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Assembly programming

Post by BigEd »

It's true, but don't worry too much! For example, some assemblers like ASL A and others like ASL when the operation is on the accumulator. Some like labels to have a trailing colon and some don't. Some might care about indentation. Kowalski uses () only to signify the addressing mode and uses [] to make up complex expressions. There are some which are even odder (Forthish or Lispy) but you're unlikely to meet them. BBC Basic's assembler allows multiple instructions on one line, and allows mingling of Basic and assembly. It also uses & for hex numbers where everyone else uses $.

So, these are little differences in detail, mostly. They do mean that you might not be able to copy and paste code, but you will usually easily be able to edit it into shape.
Chromatix
Posts: 1462
Joined: 21 May 2018

Re: Assembly programming

Post by Chromatix »

By and large, the actual 6502 instructions are written consistently. Some assemblers recognise more "alternative mnemonics" than others, eg BGE (Branch if Greater or Equal) becomes BCS (Branch if Carry Set). An exception is that most assemblers use $ as the prefix for hexadecimal constants, but BBC BASIC uses &.

The major differences lie in the assembler directives that go around the code, telling it where to put your code and defining labels among other things. BBC BASIC is perhaps the most extreme example, as you literally embed your assembly code in a BASIC program that acts as a macro processor for you. Other assemblers differ wildly in their support for directives and macros. Just about all you can count on is .ORG to locate your code and a : suffix to define a label, but even these don't work in BBC BASIC!
Kris1978
Posts: 41
Joined: 21 May 2018

Re: Assembly programming

Post by Kris1978 »

Phew! :shock:

I was scared for a moment. Now I understand. Okay, I think an afternoon reading of the manual of the x assembler will be okay....
Kris1978
Posts: 41
Joined: 21 May 2018

Re: Assembly programming

Post by Kris1978 »

I know it's too early for me to worry about macros but since you mentioned here, I would like to ask:

What IS a macro?

I bet it has to do with the assembler and not the actual 6502 assembly instructions/code

Can you give me an example?
User avatar
BitWise
In Memoriam
Posts: 996
Joined: 02 Mar 2004
Location: Berkshire, UK
Contact:

Re: Assembly programming

Post by BitWise »

The ZIP I attached to this post (viewtopic.php?f=1&t=5176) contains the source for a simple example 6502/65C02 program, the JAR containing my assembler/linker, an executable for my 65C816 emulator (that can be used to run and trace 6502/65C02 programs that don't use illegal or bit instructions) and some scripts to build and run them.

The source for the emulator is on GitHub (https://github.com/andrew-jacobs/emu816) and can be compiled on Windows or Linux. The assembler/linker needs a Java runtime. The Dev65 link in my signature goes to some documentation.
Andrew Jacobs
6502 & PIC Stuff - http://www.obelisk.me.uk/
Cross-Platform 6502/65C02/65816 Macro Assembler - http://www.obelisk.me.uk/dev65/
Open Source Projects - https://github.com/andrew-jacobs
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Assembly programming

Post by BigEd »

There's plenty of info and examples of macros in Garth's primer:
http://wilsonminesco.com/StructureMacros/
Chromatix
Posts: 1462
Joined: 21 May 2018

Re: Assembly programming

Post by Chromatix »

Kris1978 wrote:
I know it's too early for me to worry about macros but since you mentioned here, I would like to ask:

What IS a macro?

I bet it has to do with the assembler and not the actual 6502 assembly instructions/code

Can you give me an example?
You're just about right in your assumption. A macro is basically a form of shorthand for dealing with the repetitive structures that frequently show up in assembly code. There's at least one 6502 assembler out there which provides macros to implement common forms of loops, so your code is more readable because it looks a bit more like a high-level language. Generally a macro is replaced during assembly with a sequence of machine instructions, with parameters substituted.
User avatar
commodorejohn
Posts: 299
Joined: 21 Jan 2016
Location: Placerville, CA
Contact:

Re: Assembly programming

Post by commodorejohn »

In simple form, a macro is generally just a way of telling the assembler "replace this with that any time you see it," just like the assembler already does with labels or constants. (In fact, some assemblers, as well as some high-level languages like the C pre-processor, use the same syntax for both macros and constants.) So if you find yourself typing, say, a 16-bit addition sequence a lot:

Code: Select all

lda value_a
clc
adc value_b
sta result
lda value_a + 1
adc value_b + 1
sta result + 1
...you can tell the assembler to define, say, ADD_WORD as an identifier that will expand to that code sequence. Frequently you can even set it up to accept parameters like a function in a high-level language, so that you can invoke it like ADD_WORD(value_a, value_b, result) and have the assembler make the necessary substitutions.
User avatar
GARTHWILSON
Forum Moderator
Posts: 8775
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: Assembly programming

Post by GARTHWILSON »

Kris1978 wrote:
Phew! :shock:

I was scared for a moment. Now I understand. Okay, I think an afternoon reading of the manual of the x assembler will be okay....
Yes, it's just some syntax details for the assembler, not any differences in the language itself. It would be nice if all assemblers were completely compatible; but differences may arise from various assembler writers' efforts to help the user make his code clear or to be able to adapt to various users' preferences, versus possible challenges like making the assembler fit in the very limited memory space of a smaller system. (To clear up some possible confusion in that statement: The assembler is the tool that converts assembly-language source code to the machine language that the processor can understand. The language itself is "assembly language," not "assembler." You'll see a lot of word misuse though.)
scotws wrote:
A big project forces you to learn how to structure the code at a whole different level.
+1. The beginner tends to not want to be bothered with things he regards as superfluous and making the source code "pretty." But as you gain experience and your projects get bigger and bigger, you'll find that it's easy to lose control of them and reach a point where you feel totally lost in your effort to find and fix a bug, make a change, etc., and you'll feel like the only solution is to start over! To minimize that growing pain, try to get into good habits early on. Admittedly, you won't initially know what these good habits even are; but do keep an open mind about the next strategies to learn.

Macros were mentioned earlier. I wouldn't spring them on the complete newbie, but I would encourage jumping in soon after the basics of assembly-language programming have become familiar. Ed linked above to my article on macros. It starts out with what a macro is, but the thrust of the article is to use them for the less-obvious things you can do with them, particularly program-flow-control structures.

A video I saw of a lecture on programming had a couple of great quotes:
  • Programs must not be regarded as code for computers, but as literature for humans.
  • Reducing size and complexity is the triumph.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
Kris1978
Posts: 41
Joined: 21 May 2018

Re: Assembly programming

Post by Kris1978 »

Thank you all your valuable answers. Each one helped me in one or another way. 6502.org and wilsonminesco.com sites are indeed great sources of information. All I need is free time to study. Unfortunately I have a full time job (I'm a policeman) which means my spare time is very limited. For the time being I will stick with a free cross-platform assembler. The Kowalski assembler is my choice. I will read the help section in order to learn the syntax. Of course I will keep reading Rodnay Zak's and Randal Hyde's books. As for a target machine I think the Enhanced Apple IIe is the ultimate version of the 8-bit architecture. It keeps the simplicity of the NMOS 6502 without adding complexity and eliminates all harware bugs. The latter reason is why I prefer it over a BBC Micro (which undoubtedly is a stronger machine when comparing to an Apple ][) Later on I will move to an emulator for the Enhanced Apple IIe using Lisa assembler or Merlin assembler and finally I will try to purchase on eBay, a real Enhanced Apple IIe computer and....Godspeed! :)

P.s.Hope the above scenario won't last for ages till it will come true
User avatar
GARTHWILSON
Forum Moderator
Posts: 8775
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: Assembly programming

Post by GARTHWILSON »

Kris1978 wrote:
(I'm a policeman)
Thankyou for your service!
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
Kris1978
Posts: 41
Joined: 21 May 2018

Re: Assembly programming

Post by Kris1978 »

You're welcome Garth although -unfortunately- I don't like my job. I do it just for the money (as millions of people do I guess)
User avatar
GARTHWILSON
Forum Moderator
Posts: 8775
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: Assembly programming

Post by GARTHWILSON »

Just stay true to your oath to uphold and defend the Constitution. Is your department the problem? The city near us has an outstanding police department, but we're slightly outside the city limits and we get the sub-standard "services" of the local sheriff's station. The deputies themselves say the problem is the Sheriff himself, the one running the department. Hopefully we'll have a new one soon who will turn things much for the better.

I've been asked to do another consulting task on the side, by a company that makes propulsion units for satellites. I like the engineering part, and it pays extremely well; but the down side is that everything has to have a load of red-tape certification processes which I hate. (As you can imagine, it's very expensive to launch a satellite, and once it's up there, if there's a failure, you can't exactly FedEx it back for repairs!) But I'm pushing through it because my wife lost her job. Otherwise I might tell them to go find someone else. I just want to finish the project and collect my ten grand or so.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
Kris1978
Posts: 41
Joined: 21 May 2018

Re: Assembly programming

Post by Kris1978 »

First of all, I'm a greek policeman. Just to make things clear. Second, I will disappoint you once again but it's the nature of the work that doesn't suit me. In addition, it's an absolutely non creative job at all. I admire people freelancers who go chase their future, creating useful stuff. Now a logical question must have be drawn in your head. Why I chose to enter the police academy in the first place (precisely 21 years ago)? First and foremost, I came from a poor family who couldn't support me if I attended a university. Secondly, there was no chance to attend a university since my grades were very low. I admit it, I'm lazy. Furthermore, in the beginning been a policeman seemed a very appealing job and been a young guy, I didn't have the smartness(?) to sit down seriously and think what I really like to do in my life and how to find a work around in order to succeed it. Anyway. End of drama. I think we are off-topic!!! :)
Post Reply