Does this only apply to explictly declared arrays (ie int stuff[5]) or does it even apply if you malloc it and use pointers?
Roflmao....You've obviously never used HuC!
[malloc()?? What's that??There's only 8K of Ram. The overhead for a heap/stack would kill things....]
How does huc implement arrays?
As contiguous bytes of memory. Just like most compilers.
The problem is a combination of things:
1) The CPU is byte oriented. Any address updates (ie, offsets) are done via additions. Yes, there are index registers.. But Huc doesn't use them for arrays
2) The CPU has -very- limited indirection instructions. You have to have the address in the zero page area, and do the indirection from there.
Which means, accessing array[5] goes something like:
...load low byte of array address.
...save in zp array addresss
...load high byte of array address
...save in zp array address
...load low byte of offset (5)
...add to zp address low
...save zp address low
...load high byte of offset
...add to zp address high (with carry)
...save zp address high
...indirect load of low byte of array entry
... save it where you can use it.
... add 1 to zp address low
... save zp address low
... add 0 to zp address high (with carry)
... save zp address high
... indirect load of high byte of array entry
... save it where you can use it.
There are a few things you can do to speed it up, but it still takes quite a few instructions to do an array access.
And that's not even discussing the screwed up way HuC actually does it
When I do things that need arrays in assembler, I split the ints into high/low bytes. That lets me use an index register as the offset (and allows me to reach 256 bytes, not just 128 for ints) from known addresses. Indexed offsets are faster than indirection (which is what HuC uses everywhere an array is involved), and because things are split, I don't have to update the offset register/base address.
.................
And before everyone starts picking on HuC, remember this: HuC is "small C", not a complete C implementation. It's based on code someone wrote for a 1 semester compiler class. And it shows.
I assure you arrays are faster for element access and iteration than linked lists in just about every language I've used
I vaguely remember (a long time ago) writing assembler stuff for a 68xxx processor. It had a very odd syntax for certain operations: ldi [var],reg
That loaded the value at the address given by reg bytes past the address in var. A 2-level offset indirect. -That- made linked lists a breeze, and bloody fast, too
Shame I we don't see things like that in use anymore