Why use these Instruction Sequences?

Programming the 6502 microprocessor and its relatives in assembly and other languages.
Post Reply
Finn
Posts: 11
Joined: 05 Nov 2012

Why use these Instruction Sequences?

Post by Finn »

I was reading an old article in a BBC Micro magazine about hacking games to give you extra lives. One of the things they suggest is to search through the program for the sequence of 6502 instructions that would reduce the player's lives by one. What puzzled me and what my question here is about - is why programmers would use some of the sequences of instructions in the first place.

These specifically

LDX &70
DEX
STX &70

and

LDA &70
SEC
SBC #1
STA &70


Wouldn't it be more efficient (esp how tight they were for memory on a 32K BBC) to just do DEC memorylocation in this case
DEC &70? These programmers obviously know a lot more about 6502 code than me (I only have basic knowledge of it) so why do they do this?

The article says both Commando and Jet-Pac on the BBC use the 2nd method above. These may have been converted from Z80 code since i think Jet-Pac at least was originally in the Spectrum, could this be a reason?
Klaus2m5
Posts: 442
Joined: 28 Jul 2012
Location: Wiesbaden, Germany

Re: Why use these Instruction Sequences?

Post by Klaus2m5 »

A bad (non optimizing) compiler? Whatever the original programming language was, it most probably wasn't assembler.

Although I do not know the Z80 in detail, I am sure it has increment and decrement. The difference in the source code may be as simple as writing x=-1 instead of x--.

edit: typo, know = now :oops:
Last edited by Klaus2m5 on Tue Jan 08, 2013 6:16 pm, edited 1 time in total.
6502 sources on GitHub: https://github.com/Klaus2m5
User avatar
PaulF
Posts: 143
Joined: 08 Dec 2008
Location: Brighton, England

Re: Why use these Instruction Sequences?

Post by PaulF »

BBC Micro games were usually written in assembler for speed and compactness reasons.

One reason for using these sequences of instructions would be to leave the number of lives remaining in a register so it could then be displayed.

Another might be because they were converted from a Z80 system where decrementing a memory location directly didn't affect the CPU flags.
Shift to the left,
Shift to the right,
Mask in, Mask Out,
BYTE! BYTE! BYTE!
Finn
Posts: 11
Joined: 05 Nov 2012

Re: Why use these Instruction Sequences?

Post by Finn »

Thanks for the insights, I didn't even think about any of those things!
PaulF wrote:
One reason for using these sequences of instructions would be to leave the number of lives remaining in a register so it could then be displayed.
That's a very good point
User avatar
GARTHWILSON
Forum Moderator
Posts: 8775
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: Why use these Instruction Sequences?

Post by GARTHWILSON »

It would still be better to do DEC, LDX, wouldn't it?
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?
User avatar
PaulF
Posts: 143
Joined: 08 Dec 2008
Location: Brighton, England

Re: Why use these Instruction Sequences?

Post by PaulF »

GARTHWILSON wrote:
It would still be better to do DEC, LDX, wouldn't it?
Yes it would. But if the program was written by someone converting from a Z80 version, he might not have realised that both these instructions set flags on the 6502, unlike the Z80. Or he might simply have done a direct translation without bothering to optimise it. Who knows why programmers do what they do? (Which explains a lot of my programs!)
Shift to the left,
Shift to the right,
Mask in, Mask Out,
BYTE! BYTE! BYTE!
jgharston
Posts: 181
Joined: 22 Feb 2004

Re: Why use these Instruction Sequences?

Post by jgharston »

Inexperience? I've dug through loads of 6502 code and it still surprises me what sort of inefficiencies people use. One bit of code I saw used repeated instances of:

Code: Select all

LDX #cmd AND 255:LDY #cmd DIV 256:JSR oscli
...
.cmd:EQUS "FX 225,1":EQUB 13
instead of:

Code: Select all

LDA #255:LDX #1:LDY #0:JSR osbyte
It even still gets me. For years I've used this sort of code to increment a BCD value:

Code: Select all

  LDA num:AND #15    :\ Do a BCD inc. with &09->&10
  CMP #9:LDA #1      :\ &x0-&x8 - add 1
  BCC AddBCD:LDA #6  :\ &x9 - add 6+1
  .AddBCD
  ADC num:STA num    :\ num=num+1 or +7 
I only realised a few months ago, after 30 years, that I could just do:

Code: Select all

   SED         :\ set decimal mode
   CLC         :\ clear carry for add
   ADC   #1  :\ +1 gives x9 to y0
   CLD         :\ exit decimal mode
Edit: sorry, can't get the formatting to work. [Edited 4/1/13 by moderator to make it work, by unchecking the "Disable BBCode" box]
User avatar
Dr Jefyll
Posts: 3526
Joined: 11 Dec 2009
Location: Ontario, Canada
Contact:

Re: Why use these Instruction Sequences?

Post by Dr Jefyll »

jgharston wrote:
Edit: sorry, can't get the formatting to work.
I was able to reproduce that problem by checking the "Disable BBCode" box. I don't know what BB code is, but I suggest you see what's in the box, and if it's checked then try un-checking it. :D

-- Jeff
code formatting.gif
code formatting.gif (5.41 KiB) Viewed 1011 times
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html
Finn
Posts: 11
Joined: 05 Nov 2012

Re: Why use these Instruction Sequences?

Post by Finn »

jgharston wrote:
Inexperience? I've dug through loads of 6502 code and it still surprises me what sort of inefficiencies people use. One bit of code I saw used repeated instances of:

Code: Select all

LDX #cmd AND 255:LDY #cmd DIV 256:JSR oscli
...
.cmd:EQUS "FX 225,1":EQUB 13
instead of:

Code: Select all

LDA #255:LDX #1:LDY #0:JSR osbyte
when i was looking at 6502 programs in the main BBC magazines i'm sure they used that code, so maybe that's why - if people learnt from those? Or maybe they used it in programming books first? I suppose there was no internet in those days, so it was harder to learn other ways of doing things
jgharston
Posts: 181
Joined: 22 Feb 2004

Re: Why use these Instruction Sequences?

Post by jgharston »

Dr Jefyll wrote:
jgharston wrote:
Edit: sorry, can't get the formatting to work.
I was able to reproduce that problem by checking the "Disable BBCode" box.
Ah ha! Thanks. On a hunch I went to the Member Settings and there's a "enable BB code" setting there to enable it by default.
Post Reply