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?
Why use these Instruction Sequences?
Re: Why use these Instruction Sequences?
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
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
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
Re: Why use these Instruction Sequences?
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.
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!
Shift to the right,
Mask in, Mask Out,
BYTE! BYTE! BYTE!
Re: Why use these Instruction Sequences?
Thanks for the insights, I didn't even think about any of those things!
That's a very good point
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.
- GARTHWILSON
- Forum Moderator
- Posts: 8775
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: Why use these Instruction Sequences?
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?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
Re: Why use these Instruction Sequences?
GARTHWILSON wrote:
It would still be better to do DEC, LDX, wouldn't it?
Shift to the left,
Shift to the right,
Mask in, Mask Out,
BYTE! BYTE! BYTE!
Shift to the right,
Mask in, Mask Out,
BYTE! BYTE! BYTE!
Re: Why use these Instruction Sequences?
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:
instead of:
It even still gets me. For years I've used this sort of code to increment a BCD value:
I only realised a few months ago, after 30 years, that I could just do:
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]
Code: Select all
LDX #cmd AND 255:LDY #cmd DIV 256:JSR oscli
...
.cmd:EQUS "FX 225,1":EQUB 13Code: Select all
LDA #255:LDX #1:LDY #0:JSR osbyteCode: 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 Code: Select all
SED :\ set decimal mode
CLC :\ clear carry for add
ADC #1 :\ +1 gives x9 to y0
CLD :\ exit decimal mode--
JGH - http://mdfs.net
JGH - http://mdfs.net
Re: Why use these Instruction Sequences?
jgharston wrote:
Edit: sorry, can't get the formatting to work.
-- Jeff
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html
https://laughtonelectronics.com/Arcana/ ... mmary.html
Re: Why use these Instruction Sequences?
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:
instead of:
Code: Select all
LDX #cmd AND 255:LDY #cmd DIV 256:JSR oscli
...
.cmd:EQUS "FX 225,1":EQUB 13Code: Select all
LDA #255:LDX #1:LDY #0:JSR osbyteRe: Why use these Instruction Sequences?
Dr Jefyll wrote:
jgharston wrote:
Edit: sorry, can't get the formatting to work.
--
JGH - http://mdfs.net
JGH - http://mdfs.net