Author |
Message |
|
DarkerLine ceci n'est pas une |
Super Elite (Last Title)
Joined: 04 Nov 2003 Posts: 8328
|
Posted: 22 Oct 2008 08:33:35 am Post subject: |
|
|
asdf wrote: @DarkerLine: Btw, "cp 0" and "ld a,b" are unnecessary because "dec b" will set the Z flag if b=0 after decrementing. Also, "ld a,hl" isn't a valid instruction, i think you mean "ld a,(hl)"?
[post="128136"]<{POST_SNAPBACK}>[/post] I just copied and pasted the code, I don't think I actually looked at it. You're right about the pop hl, though: fixed. |
|
Back to top |
|
|
FloppusMaximus
Advanced Member
Joined: 22 Aug 2008 Posts: 472
|
Posted: 25 Oct 2008 09:14:28 pm Post subject: |
|
|
In regard to the original topic (avoiding stack usage bugs): I have a personal system I developed to deal with this problem. I've been using it for quite some time and have found it very helpful.
In general, whenever I have a PUSH instruction, I add a space at the beginning of each subsequent line of code. Whenever I have a POP instruction, I remove a space. So a piece of code might look like this:
Code: VPutS:
push de
push ix
VPutS_Loop:
ld a,(hl)
or a
jr z,VPutS_Done
B_CALL VPutMap
jr nc,VPutS_Loop
VPutS_Done:
pop ix
pop de
ret
The obvious advantage of doing this is that you can tell how many pushes have been done at any given point in the program. As a result, it is easy to identify the corresponding push for each pop (as long as the control flow is straightforward) which makes the program easier to read.
Furthermore, doing any of the following is a bug, except under special circumstances (i.e., you already know that you're violating the rules, and you've thought hard about how and why you're violating them.)
- Jumping to a label that is at a different indentation level (e.g. if I had written 'jr nc,VPutS' rather than 'jr nc,VPutS_Loop'.)
- Calling a label that is not at indentation level zero (e.g. calling VPutS_Loop.)
- Returning if you're not already at indentation level zero (e.g. writing 'ret z' instead of 'jr z,VPutS_Done'.)
I wrote a perl program to check that all of these rules are satisfied; I make a point of running it on any large assembly program I write (and yes, it does find bugs from time to time.) I could release the program if anyone's interested. |
|
Back to top |
|
|
asdf
Advanced Newbie
Joined: 17 Aug 2008 Posts: 73
|
Posted: 04 Nov 2008 05:23:39 pm Post subject: |
|
|
That's how i structure my programs as well, and often for keeping track of which set of registers (shadow/normal) i'm using. |
|
Back to top |
|
|
adje
Newbie
Joined: 24 Dec 2007 Posts: 24
|
Posted: 10 Nov 2008 12:02:40 pm Post subject: |
|
|
thats pretty handy!
with shadow registers you mean BC' and DE' and so on (EXX ?)
i guess so.
i usually dont prefere these Push an POP instructions. rather use selfmodifin code. |
|
Back to top |
|
|
darkstone knight
Advanced Member
Joined: 07 Sep 2008 Posts: 438
|
Posted: 10 Nov 2008 12:54:30 pm Post subject: |
|
|
shadow registers = AF' BC' DE' and HL'
you can swap them by using EXX' |
|
Back to top |
|
|
DarkerLine ceci n'est pas une |
Super Elite (Last Title)
Joined: 04 Nov 2003 Posts: 8328
|
Posted: 10 Nov 2008 01:44:52 pm Post subject: |
|
|
adje wrote: thats pretty handy!
with shadow registers you mean BC' and DE' and so on (EXX ?)
i guess so.
i usually dont prefere these Push an POP instructions. rather use selfmodifin code.
[post="128612"]<{POST_SNAPBACK}>[/post] Using the stack is more efficient (in terms of size and speed) than self-modifying code. And it works in more situations too. |
|
Back to top |
|
|
darkstone knight
Advanced Member
Joined: 07 Sep 2008 Posts: 438
|
Posted: 10 Nov 2008 02:33:36 pm Post subject: |
|
|
if you want more undersanding of shadow registers, check this:
http://commons.wikimedia.org/wiki/Image:Z80_arch.svg (nodice A and F are labeled wrong...)
when an EXX instruction is called, the registers are "swapped" whit their shadows |
|
Back to top |
|
|
brandonw
Advanced Member
Joined: 12 Jan 2007 Posts: 455
|
Posted: 10 Nov 2008 05:42:18 pm Post subject: |
|
|
Just to have it said, EXX doesn't affect AF, "ex af,af'" does that. |
|
Back to top |
|
|
sgm
Calc Guru
Joined: 04 Sep 2003 Posts: 1265
|
Posted: 10 Nov 2008 06:41:14 pm Post subject: |
|
|
darkstone knight wrote: (nodice A and F are labeled wrong...)
[post="128615"]<{POST_SNAPBACK}>[/post]
No, they aren't.
See to the right. |
|
Back to top |
|
|
darkstone knight
Advanced Member
Joined: 07 Sep 2008 Posts: 438
|
Posted: 11 Nov 2008 03:46:46 am Post subject: |
|
|
i see...
wth are W and Z then? |
|
Back to top |
|
|
brandonw
Advanced Member
Joined: 12 Jan 2007 Posts: 455
|
Posted: 11 Nov 2008 06:50:08 am Post subject: |
|
|
About ten seconds of googling reveals:
"The Register-pairs BC and DE are mostly used for counters and storage of part-results. The Register-pair HL can be used in a wide range of instructions mostly as addressing (pointing to memory). The forgotten registers W and Z are only used for internal operations in the processor, like jump to new addresses. (The CPU can only transfer 8 bits at a time, so to transfer (load) a 16-bit address, it will first store it in WZ)."
They can't be used. |
|
Back to top |
|
|
benryves
Active Member
Joined: 23 Feb 2006 Posts: 564
|
Posted: 11 Nov 2008 08:41:49 am Post subject: |
|
|
This post on SMS Power! contains a snippet that can check for the presence of WZ' (it seems not all Z80 clones implement it). |
|
Back to top |
|
|
FloppusMaximus
Advanced Member
Joined: 22 Aug 2008 Posts: 472
|
Posted: 11 Nov 2008 04:57:38 pm Post subject: |
|
|
Wow. It never would have occurred to me to create a WZ' register. Or to write a program to test for its existence, for that matter. Having an extra backup copy of a completely useless register... the mind reels. |
|
Back to top |
|
|
|