6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Apr 26, 2024 8:48 pm

All times are UTC




Post new topic Reply to topic  [ 24 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Tue Dec 29, 2015 2:18 am 
Offline

Joined: Tue Dec 29, 2015 2:09 am
Posts: 18
I am hopeless with 6502... I come from the Apple II world but for the most part I think 6502 is universal, at least to my understanding. I have bought and read several books, but nothing sinks in.... but i'm crackerjack at Basic.. (whiptydo?!?)

Here is an example...the numbers are expressed in DEC although I know they need to be in hex when compiling...
LDA 950
CLC
ADC 44
STA 44

This adds the location of 950 to location 44... and the result is stored at 44
Exp 950: 10 44:12 after code 44:22

The next thing I need to be able to do, is add a memory location value to Xreg, say from the same address 951.
Xreg is 10.... 951: 20 I need the Xreg to = 30 (or)
Xreg is 20.... 951: 25 I need the Xreg to = 45 (something like that)

The last thing I need to be able to do, is add a memory location value to Yreg, say from the same address 952.
Yreg is 11.... 952: 26 I need the Yreg to = 36 (or)
Yreg is 53... 952: 35 I need the Yreg to = 88

I hope this makes sense... Thanks for any replies.... Tom


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 29, 2015 2:37 am 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3346
Location: Ontario, Canada
Welcome! :) May I suggest you use the Code text style? The mono-spaced font makes a tidier presentation. Like this:
Code:
LDA 950
CLC
ADC 44
STA 44

Quote:
The next thing I need to be able to do, is add a memory location value to Xreg
Alright. Y' reckon the TAX and TXA instructions might be helpful for that? :)

_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 29, 2015 2:41 am 
Online
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1926
Location: Sacramento, CA, USA
TAX, TXA, TAY, and TYA are your friends here, because the A register is the most efficient place to do arithmetic on the 6502. If you don't want to trash what's in A, you can start with a PHA and end with a PLA, like so:
Code:
    pha
    txa
    clc
    adc  951
    tax
    tya
    clc
    adc  952
    tay
    pla

Happy programming!

Mike B.


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 29, 2015 3:11 am 
Offline

Joined: Tue Dec 29, 2015 2:09 am
Posts: 18
Alright, I'm flipping thru my 6502 machine book to find out what these codes mean... writing lots of notes...
Thank you for the quick replies! Anybody use Apple II emulators? I have a game you can try out if you do!

Tom


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 29, 2015 3:46 am 
Offline

Joined: Tue Dec 29, 2015 2:09 am
Posts: 18
Ok... so let me get this straight.... if a number is in the accumulator, and then I add
another number to it, its an automatic addition (or subtraction), so ADC is not just
put a number there, its a Add a number there....


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 29, 2015 3:58 am 
Online
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1926
Location: Sacramento, CA, USA
LASERACTIVEGUY wrote:
Ok... so let me get this straight.... if a number is in the accumulator, and then I add
another number to it, its an automatic addition (or subtraction), so ADC is not just
put a number there, its a Add a number there....

Yes, I think you're on target with your thinking. If you want to "copy" a value to the A register, you would do a LDA something or a TXA or TYA or PLA. If you want to "accumulate" a value, you would use ORA something, AND something, EOR something, ADC something, SBC something, ASL, LSR, ROR, ROL, etc.

Speaking of Apple ][s:
I have a couple of examples of the real hardware in my attic, and they still work ... or at least they did the last time I checked, a few years ago. For hacking around, I prefer the convenience of AppleWin. It seems to mostly satisfy my 8-bit needs, and it looks pretty convincing in full-screen mode. It doesn't smell the same, though. :)

Mike B.

P.S. The PDP-8 didn't have a unique instruction to copy an arbitrary value into the accumulator, but it did have TAD (twos-complement add to accumulator). There was an instruction to clear the accumulator first (CLA), but it wasn't always necessary, because a preceding DCA (deposit and clear accumulator) automatically cleared it for you! Very baroque!


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 29, 2015 4:43 am 
Offline

Joined: Tue Dec 29, 2015 2:09 am
Posts: 18
If you have nothing to do, This is the Music Master RPG...
https://www.dropbox.com/s/tuecdmul18csm ... 9.zip?dl=0

My very first 'game'. There is a sequel actually 1/2 done that has 3 times the people, 3 times the music and a huge world/map/story. The first one is 'not an official finish' but good for 35-45 minutes of gameplay. Apple II disk images!

Ps. I got the PLOT code working!!! Now I have both the VLIN and HLIN codes to go, and then when its done I will be able to have BRUN-able sprites that have instant loading time. I even have ideas how to make an easy collision detection possible with two states (background or enemy attack)...creating different outcomes for the sprites. You made my day... but a tired one at that! I'll be up all night!


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 29, 2015 5:19 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8428
Location: Southern California
Welcome.

LASERACTIVEGUY wrote:
Here is an example...the numbers are expressed in DEC although I know they need to be in hex when compiling...

An assembler can take in decimal values too (or binary, and usually octal as well, although I've never seen any practical use in octal in this equipment). BASICs usually take decimal only; but hex and binary are much more practical for some situations. The assembler will convert when appropriate. For example, if you write LDA #10, the assembler will lay down A9 0A; but if you write LDA #$10 or LDA #%00010000, it will lay down A9 10. You could also have a named constant (or EQUate) with that value. For an example, we'll use the name LCD_WIDTH to tell how many characters are on a line in an intelligent character LCD, and assign it a value of 16 in the source code, with the line LCD_WIDTH EQU 16. Then LDX #LCD_WIDTH will lay down A2 10. Now if you ever go to a wider LCD, for example one that's 24 characters across, you don't have to change the number in every single display routine that loads the initial width—only change the line where LCD_WIDTH is defined. Another use would be to count up and do a CPX #LCD_WIDTH to determine whether to go for another loop iteration or not. (These aren't really the best examples, but they should make the point.)

The normal use for adding X or Y is for indexing, ie, adding their content to an address. If X contains 7 and the instruction LDA 950,X is encountered, the processor will load the accumulator with what's in address 957. X's contents and the 950 got added together, but X does not change. It is common to have a loop where the index register gets incremented or decremented once or twice per loop iteration, using INX or DEX or INY or DEY. Each of these is a single byte, and takes two cycles to complete. If you find yourself wanting arithmetic or logical operations on X or Y, you have probably approached the problem the wrong way. BigEd here observed, "With 6502, I suspect more than one beginner has wondered why they can't do arithmetic or logic operations on X or Y, or struggled to remember which addressing modes use which of the two. And then the intermediate 6502 programmer will be loading and saving X and Y while the expert always seems to have the right values already in place."

_________________
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?


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 29, 2015 5:45 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8144
Location: Midwestern USA
LASERACTIVEGUY wrote:
I am hopeless with 6502... I come from the Apple II world but for the most part I think 6502 is universal, at least to my understanding.

Have you read the official Western Design Center programming manual? It covers virtually all aspects of programming the 6502, 65C02 and 65C816 in assembly language and should be part of your library. It's a well-written manual that doesn't assume very much on the part of the reader.

Quote:
Here is an example...the numbers are expressed in DEC although I know they need to be in hex when compiling...

WARNING! Pedantry Alert!

One of the first steps in becoming a good programmer is understanding and using the correct terminology. Doing so, especially in print, will assist in getting across your thoughts when discussing programming topics in an on-line forum.

  • Assembly language is called assembly language, not assembly, assembler or machine language.

  • Your program as written in a text editor is the source code.

  • You assemble your source code, not compile it.

  • Assembly is the process of translating the human-readable instructions in your source code into the binary equivalents that are specific to the particular microprocessor being programmed.

  • The program that assembles your source code is referred to as an assembler, not a compiler (they are two very different animals).

  • The output from the assembler is (usually) written into an object file, which contains object code. Object code may be raw binary that can be executed if loaded into memory, or may be some sort of textual representation of the raw binary. If the object code is the latter, it must be processed by a loader to produce an executable binary.

As Garth noted, most assemblers will cheerfully accept numbers in decimal, hexadecimal, binary or more rarely, octal. In some cases, one number base may be preferable over another. For example, when writing code that will be processing I/O chip registers I often use binary, as it helps me to visualize which bits are being handled. Experience will eventually teach you when to use a particular number base.

GARTHWILSON wrote:
...although I've never seen any practical use in octal in this equipment.

My first encounter with octal was on the DEC PDP-11, which seemed to strongly favor octal notation. Linux and UNIX continue to perpetuate octal notation, for example, in the chmod command, which changes file mode bits. As the permissions bits are acted upon in groups of three, octal notation makes some sense, since the largest numeral in octal is 7, which is %111 in binary. However, I personally find it a nuisance to have to mentally convert binary to octal when I know which file mode bits I want to change.

As you noted, octal is very seldom used in 65xx programs. If writing in assembly language it's just as easy to express masks and such in binary.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 29, 2015 6:34 am 
Online
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1926
Location: Sacramento, CA, USA
BigDumbDinosaur wrote:
...
  • You assemble your source code, not compile it.
    ...
  • The program that assembles your source code is referred to as an assembler, not a compiler (they are two very different animals).
...

http://cs.stackexchange.com/questions/1 ... -assembler

Are they really that different, BDD? :wink:

Mike B.


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 29, 2015 7:11 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8428
Location: Southern California
Your linked page, Mike, says, "All assemblers are (simple) compilers, since they transform one language to another. Not all compilers are assemblers." I might be able to accept that, with difficulty. Assembly language and machine language could be considered to be the the same language in two different writing systems, one being the machine-readable version and the other being the human-readable version, with basically a 1:1 relationship.

The Wikipedia article for "compiler" says, "The name 'compiler' is primarily used for programs that translate source code from a high-level programming language to a lower-level language." There's not a 1:1 relationship there, as different compilers starting with the same source code could produce vastly different machine-language outputs.

Although there may not always be as clear of a line between the two terms as we would like, I think it will be helpful for the beginning programmer to understand these differences.

My (mis?)understanding is that octal was common on early computers that had 12-bit words, so they could have four digits of three bits each, and that four bits per digit did not seem to make sense before the idea of going to letters A-F to get beyond 9 caught on.

In any case, I, too, would strongly recommend the programming manual BDD references above. It's free, and very thorough. The tutorial should get the beginner up to speed on the basics and how the various features of the instruction set are used. A large section near the end devotes an entire page (and sometimes more) to explaining every instruction. Another section devotes an entire page to diagramming every address mode.

_________________
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?


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 29, 2015 7:39 am 
Online
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1926
Location: Sacramento, CA, USA
GARTHWILSON wrote:
... Assembly language and machine language could be considered to be the the same language in two different writing systems, one being the machine-readable version and the other being the human-readable version, with basically a 1:1 relationship.

From http://www.csn.ul.ie/~darkstar/assembler/manual/a01.txt
Quote:
6. A86 takes advantage of situations in which more than one set of opcodes can be generated for the same instruction. (For example, MOV AX,BX can be generated using either an 89 or 8B opcode, by reversing fields in the following effective address byte. Both forms are absolutely identical in functionality and execution speed.) A86 adopts an unusual mix of choices in such situations. This creates a code-generation "footprint" that occupies no space in your program file, but will enable me to tell, and to demonstrate in a court of law, if a non-trivial object file has been produced by A86. The specification for this "footprint" is sufficiently obscure and complicated that it would be impossible to duplicate by accident. I claim exclusive rights to the particular "footprint" I have chosen, and prohibit anyone from duplicating it.

A slightly interesting "twist" from Mr. Isaacson, right?

GARTHWILSON wrote:
The Wikipedia article for "compiler" says, "The name 'compiler' is primarily used for programs that translate source code from a high-level programming language to a lower-level language." There's not a 1:1 relationship there, as different compilers starting with the same source code could produce vastly different machine-language outputs.

Although there may not always be as clear of a line between the two terms as we would like, I think it will be helpful for the beginning programmer to understand these differences.

My (mis?)understanding is that octal was common on early computers that had 12-bit words, so they could have four digits of three bits each, and that four bits per digit did not seem to make sense before the idea of going to letters A-F to get beyond 9 caught on.

Agreed. Octal is an excellent fit for several of the older DEC machines, due to the fact that many of them had words with bit-widths that were multiples of three. The pdp-11 had 16-bit words, but the internal layout of the bit fields in the op-codes made more sense in octal.

GARTHWILSON wrote:
In any case, I, too, would strongly recommend the programming manual BDD references above. It's free, and very thorough. The tutorial should get the beginner up to speed on the basics and how the various features of the instruction set are used. A large section near the end devotes an entire page (and sometimes more) to explaining every instruction. Another section devotes an entire page to diagramming every address mode.

Agreed. I have a copy on my hard drive, and I refer to it frequently, even after all these years of tinkering with the 65xx (and a nerdy ability to instantly recognize most hex dumps containing 6502 machine language).

Mike B.


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 29, 2015 10:01 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10793
Location: England
(We'll always get a big detour when we indulge in pedantry... resist the temptation!)

Back to 6502 programming - there's a great diagram of the programmer's model for the 6502 by Bob Sander-Cederlof. Here it is as an image (from here) and as text (got to love ascii diagrams):

Image

Code:
                         TRANSFER OPERATIONS
                         -------------------

 +-----------------------------------------------------------------+
 |                             MEMORY                              |
 |                           $0000-$FFFF                           |
 +-----------------------------------------------------------------+
      ^    |                   ^    |                   ^    |
      |    |                   |    |                   |    |
     STX  LDX                 STA  LDA                 STY  LDY
      |    |                   |    |                   |    |
      |    V                   |    V                   |    V
 +--------------+         +--------------+         +---------------+
 |              |---TXA-->|              |<--TYA---|               |
 |  X-REGISTER  |         |  A-REGISTER  |         |  Y-REGISTER   |
 |              |<--TAX---|              |---TAY-->|       +       |
 +--------------+         +--------------+         +---------------+
      |    ^                   ^    |
      |    |                   |    |
     TXS  TSX                 PLA  PHA
      |    |                   |    |
      V    |                   |    V
 +--------------+         +--------------+         +---------------+
 |              |         |              |---PLP-->|               |
 |  S-REGISTER  |         |    STACK     |         |  P-REGISTER   |
 |              |         | $0100-$01FF  |<--PHP---|   NV*BDIZC    |
 +--------------+         +--------------+         +---------------+


                           OTHER OPERATIONS
                           ----------------

 +-----------------------------------------------------------------+
 |               A-REGISTER    X-REGISTER    Y-REGISTER    MEMORY  |
 |-----------------------------------------------------------------|
 |  Arithmetic:  ADC           INX           INY           INC     |
 |               SBC           DEX           DEY           DEC     |
 |                                                                 |
 |  Logical:     AND           ---           ---           BIT     |
 |               ORA           ---           ---           ---     |
 |               EOR           ---           ---           ---     |
 |                                                                 |
 |  Shift:       ASL           ---           ---           ASL     |
 |               LSR           ---           ---           LSR     |
 |               ROL           ---           ---           ROL     |
 |               ROR           ---           ---           ROR     |
 |                                                                 |
 |  Compare:     CMP           CPX           CPY           ---     |
 +-----------------------------------------------------------------+

 +----------------------------------------------------+
 |  Status:      SET           CLEAR         BRANCH   |
 |----------------------------------------------------|
 |       CARRY   SEC           CLC           BCC, BCS |
 |    OVERFLOW   ---           CLV           BVC, BVS |
 |     DECIMAL   SED           CLD           -------- |
 |   INTERRUPT   SEI           CLI           -------- |
 |        ZERO   ---           ---           BEQ, BNE |
 |       MINUS   ---           ---           BPL, BMI |
 +----------------------------------------------------+

    Jump:     JMP, JSR   -------------------------------------------
                         6502 Programming Model, Bob Sander-Cederlof
    Return:   RTS, RTI          Apple Assembly Line, May 1981
                         http://txbobsc.com/aal/1981/aal8105.html#a4
    Other:    NOP, BRK   -------------------------------------------


Attachments:
File comment: 6502 Programmer's model by Bob Sander-Cederlof.
6502-programming-model-bob-sander-cederlof.gif
6502-programming-model-bob-sander-cederlof.gif [ 28.71 KiB | Viewed 4868 times ]


Last edited by BigEd on Wed Mar 27, 2019 10:20 pm, edited 2 times in total.
Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 29, 2015 10:42 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8428
Location: Southern California
That was a good way to visualize it. Ed, would you like to post an updated version, perhaps under "Tutorials, References, and Primers" at http://6502.org/tutorials/, to reflect the added CMOS instructions and addressing modes. It would be a good addition. Glaring are the lack of PHX, PLX, PHY, and PLY. Also lacking are TSB, TRB, INA, DEA, STZ, and BRA, and to a lesser extent, BBS, BBR, SMB, and RMB.

_________________
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?


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 29, 2015 10:54 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10793
Location: England
BTW I posted that diagram previously, in Best way to start learning 6502 Assembler which might perhaps be a good sticky thread in Newbies? If not, it might contain some material for such a post.

(I'm pretty sure I also straightened up the image but I can't find that anywhere.)
(Edit: found it, and updated the post upthread.)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 24 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 22 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: