Instruction use frequency?
Instruction use frequency?
Hi all!
Does anyone know if there's some sort of analysis on instruction frequency for the 6502? Particularly interested in indirect,x and perhaps a lot more common (just curious though) about cpx / cpy.
Thanks!
Yvo
Does anyone know if there's some sort of analysis on instruction frequency for the 6502? Particularly interested in indirect,x and perhaps a lot more common (just curious though) about cpx / cpy.
Thanks!
Yvo
Re: Instruction use frequency?
It's an interesting question. There's a small table at
http://www.slack.net/~ant/nes-emu/6502.html#frequent
[Edit: may be offline, so see archived version here.]
Shouldn't be too hard to tweak an emulator to produce these stats for a given program - I'm sure I've intended to do it, but I can't find a record of any results, so I'm pretty sure I never did it.
(For some purposes, dynamic frequency is what you want (so instructions inside loops are counted many times) but for other purposes static frequency might be more useful, or as useful (so you can just count the output of an assembly listing or disassembly listing.))
http://www.slack.net/~ant/nes-emu/6502.html#frequent
[Edit: may be offline, so see archived version here.]
Shouldn't be too hard to tweak an emulator to produce these stats for a given program - I'm sure I've intended to do it, but I can't find a record of any results, so I'm pretty sure I never did it.
(For some purposes, dynamic frequency is what you want (so instructions inside loops are counted many times) but for other purposes static frequency might be more useful, or as useful (so you can just count the output of an assembly listing or disassembly listing.))
Last edited by BigEd on Mon May 23, 2016 7:34 pm, edited 1 time in total.
- GARTHWILSON
- Forum Moderator
- Posts: 8775
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: Instruction use frequency?
It will also depend on the language and what you're doing with 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: Instruction use frequency?
yzoer wrote:
Particularly interested in indirect,x
-
White Flame
- Posts: 704
- Joined: 24 Jul 2012
Re: Instruction use frequency?
While instruction popularity is very usage specific, I'd say that ROM-based systems like the NES (used in the link above) would weight heavily against STA, especially STA abs, as opposed to RAM-based programs and tables.
(Indirect,x) is only found in some programs, often interpreters or other higher-level tools that do a lot of dynamic memory management... and then not at all in the majority of other programs. However, its most common usage that I've seen is LDA(0,X), where .X is a pointer to a ZP stack, as in many Forth interpreters. It would dereference a byte whose address is held in the top of the stack. I've also used it in some sprite multiplexers, where X is a multiple of 2, shuffling around a table of pointers in zeropage. It's not super efficient, but is well organized and keeps the code pretty small.
cpx/cpy I'd consider to be pretty rare. While their obvious use would be in loops, often they're optimized out so that dex/dey, bne is used instead, which is less runtime overhead.
(Indirect,x) is only found in some programs, often interpreters or other higher-level tools that do a lot of dynamic memory management... and then not at all in the majority of other programs. However, its most common usage that I've seen is LDA(0,X), where .X is a pointer to a ZP stack, as in many Forth interpreters. It would dereference a byte whose address is held in the top of the stack. I've also used it in some sprite multiplexers, where X is a multiple of 2, shuffling around a table of pointers in zeropage. It's not super efficient, but is well organized and keeps the code pretty small.
cpx/cpy I'd consider to be pretty rare. While their obvious use would be in loops, often they're optimized out so that dex/dey, bne is used instead, which is less runtime overhead.
Re: Instruction use frequency?
As an aside, I'm curious why they used a really big CASE statement for the emulator. I'm throwing together some experiments for a possible 65816 emulator (because there doesn't seem to be a good one for Linux around), and I thought it would be obvious to use a jump table with the opcode value as the index.
Then again, I'm using Forth and he's using C, so maybe that's the difference?
Then again, I'm using Forth and he's using C, so maybe that's the difference?
Re: Instruction use frequency?
Jump table vs case statement may have to do with the instruction set. I wrote two emulators, both in C, one used case statements (because the instruction set kind of made sense for that), while the other used a jump table due to the emulated CPU's huge number of opcodes.
- BitWise
- In Memoriam
- Posts: 996
- Joined: 02 Mar 2004
- Location: Berkshire, UK
- Contact:
Re: Instruction use frequency?
scotws wrote:
As an aside, I'm curious why they used a really big CASE statement for the emulator. I'm throwing together some experiments for a possible 65816 emulator (because there doesn't seem to be a good one for Linux around), and I thought it would be obvious to use a jump table with the opcode value as the index.
Andrew Jacobs
6502 & PIC Stuff - http://www.obelisk.me.uk/
Cross-Platform 6502/65C02/65816 Macro Assembler - http://www.obelisk.me.uk/dev65/
Open Source Projects - https://github.com/andrew-jacobs
6502 & PIC Stuff - http://www.obelisk.me.uk/
Cross-Platform 6502/65C02/65816 Macro Assembler - http://www.obelisk.me.uk/dev65/
Open Source Projects - https://github.com/andrew-jacobs
- BigDumbDinosaur
- Posts: 9428
- Joined: 28 May 2009
- Location: Midwestern USA (JB Pritzker’s dystopia)
- Contact:
Re: Instruction use frequency?
theGSman wrote:
yzoer wrote:
Particularly interested in indirect,x
The 65C816 also has JSR (<addr>,X).
x86? We ain't got no x86. We don't NEED no stinking x86!
- barrym95838
- Posts: 2056
- Joined: 30 Jun 2013
- Location: Sacramento, CA, USA
Re: Instruction use frequency?
If zero-page is already crowded, you can use (zp,x) to slide blocks up and down up to 255 bytes with a single vector, like in my VTL02 interpreter:
(zp,x) is also used in fetch-type operations (FigForth's @):
Mike B.
Code: Select all
...
tay y = length of line to delete
eor #-1
sec
adc ampr {&} = {&} - y
sta ampr
bcs delt
dec ampr+1
delt lda at
sta under {_} = {@}
lda at+1
sta under+1
delt2 lda under
cmp ampr delete the line
lda under+1
sbc ampr+1
bcs insrt
lda (under),y
sta (under,x)
inc under
bne delt2
inc under+1
bcc delt2 (always taken)
insrt ...
Code: Select all
L773 .BYTE $81,$C0
.WORD L762 ; link to TOGGLE
AT .WORD *+2
LDA (0,X)
PHA
INC 0,X
BNE L781
INC 1,X
L781 LDA (0,X)
JMP PUT
-
White Flame
- Posts: 704
- Joined: 24 Jul 2012
Re: Instruction use frequency?
scotws wrote:
As an aside, I'm curious why they used a really big CASE statement for the emulator. I'm throwing together some experiments for a possible 65816 emulator (because there doesn't seem to be a good one for Linux around), and I thought it would be obvious to use a jump table with the opcode value as the index.
Then again, I'm using Forth and he's using C, so maybe that's the difference?
Then again, I'm using Forth and he's using C, so maybe that's the difference?
Re: Instruction use frequency?
Thanks for the input guys! Aside from indirect,x I was actually surprised that cpx/cpy wasn't really used that much. Personally, I haven't really used it that much and generally used them either as loop (down) counters or primarily as index registers.
-Yvo
-Yvo
Re: Instruction use frequency?
White Flame wrote:
The C compiler will put all of the case bodies' variables in the same scope, reusing variable slots and potentially using CPU registers for many of them. I guess that is pretty Forth vs C specific.
This is why nostalgia sites like 6502.org exist; so that members can experience computing and programming like it was in the pioneering days of the 80's.
Re: Instruction use frequency?
A quick and dirty disassembly of the BASIC and OS for Acorn's BBC micro, which are 16k each, show the following static frequencies:
OS:
,X) 89
cpx 67
cpy 38
bne 338
Basic:
,X) 69
cpx 72
cpy 51
bne 356
As expected, cpx and cpy are usually followed by a branch:
cpx:
10 bcc
24 bcs
31 bne
32 beq
cpy:
7 bcc
13 bcs
17 beq
31 bne
OS:
,X) 89
cpx 67
cpy 38
bne 338
Basic:
,X) 69
cpx 72
cpy 51
bne 356
As expected, cpx and cpy are usually followed by a branch:
cpx:
10 bcc
24 bcs
31 bne
32 beq
cpy:
7 bcc
13 bcs
17 beq
31 bne
- BigDumbDinosaur
- Posts: 9428
- Joined: 28 May 2009
- Location: Midwestern USA (JB Pritzker’s dystopia)
- Contact:
Re: Instruction use frequency?
theGSman wrote:
White Flame wrote:
The C compiler will put all of the case bodies' variables in the same scope, reusing variable slots and potentially using CPU registers for many of them. I guess that is pretty Forth vs C specific.
As for relays, billions are produced and used every year. Railroad signalling is a big user of relays, which are generally more trustworthy than their electronic counterparts in the harsh operating conditions encountered in such service.
Regarding the output of a C compiler versus an expert assembly language programmer, I disagree. A human programmer can always do a better job of optimizing code than any automatic code generator—which description applies to a language compiler. The programmer can often see patterns that a compiler cannot, which means the programmer can use that information to develop a tighter and faster routine.
That said, modern MPUs (especially the x86 family) have gotten so complicated and opaque, it's generally not economical anymore to write in assembly language.
Quote:
This is why nostalgia sites like 6502.org exist; so that members can experience computing and programming like it was in the pioneering days of the 80's.
x86? We ain't got no x86. We don't NEED no stinking x86!