Page 1 of 1
Why use these Instruction Sequences?
Posted: Tue Jan 08, 2013 3:24 pm
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?
Re: Why use these Instruction Sequences?
Posted: Tue Jan 08, 2013 4:20 pm
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

Re: Why use these Instruction Sequences?
Posted: Tue Jan 08, 2013 5:07 pm
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.
Re: Why use these Instruction Sequences?
Posted: Tue Jan 08, 2013 9:00 pm
by Finn
Thanks for the insights, I didn't even think about any of those things!
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
Re: Why use these Instruction Sequences?
Posted: Tue Jan 08, 2013 10:00 pm
by GARTHWILSON
It would still be better to do DEC, LDX, wouldn't it?
Re: Why use these Instruction Sequences?
Posted: Thu Jan 10, 2013 1:14 pm
by PaulF
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!)
Re: Why use these Instruction Sequences?
Posted: Thu Jan 17, 2013 3:08 pm
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:
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]
Re: Why use these Instruction Sequences?
Posted: Thu Jan 17, 2013 6:12 pm
by Dr Jefyll
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.
-- Jeff

- code formatting.gif (5.41 KiB) Viewed 1026 times
Re: Why use these Instruction Sequences?
Posted: Thu Jan 17, 2013 8:47 pm
by Finn
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:
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
Re: Why use these Instruction Sequences?
Posted: Thu Jan 17, 2013 10:16 pm
by jgharston
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.