6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Nov 08, 2024 10:32 pm

All times are UTC




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Tue Jan 08, 2013 3:24 pm 
Offline

Joined: Mon Nov 05, 2012 4:40 pm
Posts: 11
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?


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 08, 2013 4:20 pm 
Offline

Joined: Sat Jul 28, 2012 11:41 am
Posts: 442
Location: Wiesbaden, Germany
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:

_________________
6502 sources on GitHub: https://github.com/Klaus2m5


Last edited by Klaus2m5 on Tue Jan 08, 2013 6:16 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 08, 2013 5:07 pm 
Offline
User avatar

Joined: Mon Dec 08, 2008 6:32 pm
Posts: 143
Location: Brighton, England
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!


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 08, 2013 9:00 pm 
Offline

Joined: Mon Nov 05, 2012 4:40 pm
Posts: 11
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


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 08, 2013 10:00 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8538
Location: Southern California
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?


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 10, 2013 1:14 pm 
Offline
User avatar

Joined: Mon Dec 08, 2008 6:32 pm
Posts: 143
Location: Brighton, England
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!


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 17, 2013 3:08 pm 
Offline

Joined: Sun Feb 22, 2004 9:01 pm
Posts: 104
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:
LDX #cmd AND 255:LDY #cmd DIV 256:JSR oscli
...
.cmd:EQUS "FX 225,1":EQUB 13

instead of:
Code:
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:
  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:
   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]

_________________
--
JGH - http://mdfs.net


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 17, 2013 6:12 pm 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3367
Location: Ontario, Canada
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
Attachment:
code formatting.gif
code formatting.gif [ 5.41 KiB | Viewed 888 times ]

_________________
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: Thu Jan 17, 2013 8:47 pm 
Offline

Joined: Mon Nov 05, 2012 4:40 pm
Posts: 11
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:
LDX #cmd AND 255:LDY #cmd DIV 256:JSR oscli
...
.cmd:EQUS "FX 225,1":EQUB 13

instead of:
Code:
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


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 17, 2013 10:16 pm 
Offline

Joined: Sun Feb 22, 2004 9:01 pm
Posts: 104
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.

_________________
--
JGH - http://mdfs.net


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


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: