Today's assembly mistakes

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
User avatar
Turtle_Quality
Manic Miner
Posts: 511
Joined: Fri Dec 07, 2018 10:19 pm

Today's assembly mistakes

Post by Turtle_Quality »

1)

PUSH HL before a loop

POP HL within the loop

2)

Having too similar labels, Drop_Loop and Drop_Loops, one storage the other is a code label. Then I wonder who keeps updating my code while it's running

3) Use of LD(IY),0 , or LD (HL),0 ... not seeing that it's going to blank 2 bytes instead of one. The answer is obviously to load (HL) with some spare register set to zero, when obviously I don't have any spare registers
Definition of loop : see loop
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2644
Joined: Mon Nov 13, 2017 3:16 pm

Re: Today's assembly mistakes

Post by Ast A. Moore »

Turtle_Quality wrote: Thu May 13, 2021 9:18 pm Use of LD(IY),0 , or LD (HL),0 ... not seeing that it's going to blank 2 bytes instead of one.
Not sure what you mean here exactly. This will write 0 to the memory address pointed to by HL. Just one byte.
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
Turtle_Quality
Manic Miner
Posts: 511
Joined: Fri Dec 07, 2018 10:19 pm

Re: Today's assembly mistakes

Post by Turtle_Quality »

:?

You're right thanks, that was another wild goose chase. Found what was causing the nasty data and crashes now, HL expected to point to a label at the start of a long loop, and wasn't getting pointed there before I looped back.

Now I can write some more code and cause more crashes.
Definition of loop : see loop
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2644
Joined: Mon Nov 13, 2017 3:16 pm

Re: Today's assembly mistakes

Post by Ast A. Moore »

Of all the bugs, I find those related to the stack to be the most pernicious and difficult to track down.
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
cmal
Manic Miner
Posts: 637
Joined: Fri Jun 05, 2020 1:05 am
Location: California

Re: Today's assembly mistakes

Post by cmal »

A common mistake I make is to forget to save and restore the IY register before returning to BASIC. Similar to forgetting to re-enabling interrupts.

Another one: getting the bit position wrong. E.g. Bit 7,A instead of Bit 0,A. I always have to remind myself about this.
User avatar
Turtle_Quality
Manic Miner
Posts: 511
Joined: Fri Dec 07, 2018 10:19 pm

Re: Today's assembly mistakes

Post by Turtle_Quality »

cmal wrote: Fri May 14, 2021 5:16 am A common mistake I make is to forget to save and restore the IY register before returning to BASIC. Similar to forgetting to re-enabling interrupts.

Another one: getting the bit position wrong. E.g. Bit 7,A instead of Bit 0,A. I always have to remind myself about this.
With this program any return to Basic is a sign something went seriously wrong, but I did forget yesterday to LD IY,0x5c3a before calling the Rom print routine, and that produces interesting crashes. I think I managed a pretty comprehensive list of assembly errors yesterday
Definition of loop : see loop
User avatar
Morkin
Bugaboo
Posts: 3335
Joined: Mon Nov 13, 2017 8:50 am
Location: Bristol, UK

Re: Today's assembly mistakes

Post by Morkin »

Ast A. Moore wrote: Thu May 13, 2021 11:12 pm Of all the bugs, I find those related to the stack to be the most pernicious and difficult to track down.
Definitely..! Possibly the hardest bug I've had is when I'd been playtesting a game and it'd would randomly crash after about 10-15 minutes. Not when I'd done anything particular, in any place, or at any time. Just playing the game.

Spent forever digging around in just about every game routine, looking for a possible un-PUSHed POP (or vice versa). Nothing in life nowadays beats the elation I felt when I found it :lol:
My Speccy site: thirdharmoniser.com
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2644
Joined: Mon Nov 13, 2017 3:16 pm

Re: Today's assembly mistakes

Post by Ast A. Moore »

Morkin wrote: Fri May 14, 2021 11:33 am Nothing in life nowadays beats the elation I felt when I found it :lol:
Amen to that!
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
Mpk
Dynamite Dan
Posts: 1022
Joined: Tue Feb 09, 2021 8:10 am

Re: Today's assembly mistakes

Post by Mpk »

I lost an hour or so to this recently :

PUSH HL
PUSH DE
...stuff
POP HL
POP DE

:(

Also, finding out that Zeus is fine with LD DE,HL when it really shouldn't be, and having to go back and replace each occurrence with 2 lines to get pasmo to compile.
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2644
Joined: Mon Nov 13, 2017 3:16 pm

Re: Today's assembly mistakes

Post by Ast A. Moore »

Mpk wrote: Fri May 14, 2021 4:41 pm Also, finding out that Zeus is fine with LD DE,HL when it really shouldn't be, and having to go back and replace each occurrence with 2 lines to get pasmo to compile.
Yes, many assemblers are fine with this shorthand. I don’t particularly approve of it, because it leads to lazy coding, but many people like it because it saves them a few keystrokes.
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
Turtle_Quality
Manic Miner
Posts: 511
Joined: Fri Dec 07, 2018 10:19 pm

Re: Today's assembly mistakes

Post by Turtle_Quality »

That one makes more sense to me with an index register... LD DE,(IX+2) ... because it can specify that E will be loaded from +2 and D from +3 and there's no further impact

When the assembler "takes care of" LD DE,HL for you, it's hiding an increment of HL, changes of flags, don't know if they DEC HL afterwards.
Definition of loop : see loop
User avatar
Einar Saukas
Bugaboo
Posts: 3217
Joined: Wed Nov 15, 2017 2:48 pm

Re: Today's assembly mistakes

Post by Einar Saukas »

Turtle_Quality wrote: Fri May 14, 2021 8:03 pm When the assembler "takes care of" LD DE,HL for you, it's hiding an increment of HL, changes of flags, don't know if they DEC HL afterwards.
"LD DE,HL" is just a common abbreviation for "LD D,H / LD E,L".
User avatar
Turtle_Quality
Manic Miner
Posts: 511
Joined: Fri Dec 07, 2018 10:19 pm

Re: Today's assembly mistakes

Post by Turtle_Quality »

:oops:

I was seeing imaginary brackets, might explain my recent coding issues

Zeus does actually accept LD HL,(IX+d) (providing d<127) - Simon Brattel pointed that out to me about a month ago when I was trying to make an assembler function for index displacements.

I don't have a problem with LD HL,BC as it's the same as typing LD H,B ; LD L,C , it's tidier in the source but you should know that it's a pseudo-op

It would be nice if the highlighter could highlight that it's a pseudo-op but I think that would be quite tricky to implement.
Definition of loop : see loop
User avatar
Mpk
Dynamite Dan
Posts: 1022
Joined: Tue Feb 09, 2021 8:10 am

Re: Today's assembly mistakes

Post by Mpk »

I only built mine in Pasmo because I couldn't figure out how to export a .tzx from Zeus.

It does seem to make sense to compile in different compilers though, and to test periodically in different emulators. I'd hate to get to the end of a project to find out that it misbehaves on real hardware or something. And so, I'll try and avoid deceptively handy shortcuts.
User avatar
Bubu
Manic Miner
Posts: 542
Joined: Fri Apr 02, 2021 8:24 pm
Location: Spain

Re: Today's assembly mistakes

Post by Bubu »

Today I've been almost an hour tryng to figure out why my program doesn't work, I only have to do a comparison... and finally the mistake was de Carry flag:

Code: Select all

ld l, 250
cp l
jp nc, geater
I wanted the program to jump to "greater" label if a > 250. But obviously, it's "jp c", and not "jp nc" as I did.
This mistake, the carry flag's control when comparing, ussually gives me some headaches...
If something works, don't touch it !!!! at all !!!
User avatar
Einar Saukas
Bugaboo
Posts: 3217
Joined: Wed Nov 15, 2017 2:48 pm

Re: Today's assembly mistakes

Post by Einar Saukas »

Bubu wrote: Sat Sep 18, 2021 3:22 pm

Code: Select all

ld l, 250
cp l
jp nc, geater
I wanted the program to jump to "greater" label if a > 250. But obviously, it's "jp c", and not "jp nc" as I did.
Obviously not :)

Using "jp c" will jump if a<250, and "jp nc" will jump if a>=250.
Alcoholics Anonymous
Microbot
Posts: 194
Joined: Mon Oct 08, 2018 3:36 am

Re: Today's assembly mistakes

Post by Alcoholics Anonymous »

Mpk wrote: Sat May 15, 2021 2:13 pm I only built mine in Pasmo because I couldn't figure out how to export a .tzx from Zeus.
Surely tap is sufficient? tzx is an audio format for storing tape noises (and suitable for tape preservation purposes) whereas tap stores the actual data you are saving which is what programmers want. Unless you're trying to put in speedloaders or some protection scheme that mangles the normal tape loading process.

By going tzx you are making it harder to load on real spectrums because an awful lot of real users are using devices like the divmmc which supports tap and not tzx. Users less commonly have things like the tzxduino which can turn the tzx into audio that you can load as if from a tape deck but you are going to suffer through loading times when the purpose of things like the divmmc is to allow instant loading.
It does seem to make sense to compile in different compilers though, and to test periodically in different emulators.
I hate the the shorthand macros with passion :) It completely disrupts how I read code; standard z80 I know how many bytes every instruction takes and how fast every instruction is at a glance. I can quickly imagine and evaluate alternative instructions. When people start using pseudo-instructions, it disrupts the thinking because you have to pause to figure out what the actual instructions really are and unwind them in your mind to know how long they take. Stuff like "LD HL,BC" is pretty clear about what is going on but sjasm, eg, is notorious for having allowed this sort of thing to the extreme. I have seen source code that I can't even read without expanding the pseudo-instructions in full and some of them I had to go into documentation to know what the proper equivalent was because there would be more than one way to do it. Some of these pseudo-instructions can also lead to bugs because they can end up accepting what looks like an illegal z80 opcode that gets turned into something a little bit different. So a person reading the code thinks they have a standard z80 opcode and wonder why the program doesn't work. Anyway, rant over.
User avatar
Mpk
Dynamite Dan
Posts: 1022
Joined: Tue Feb 09, 2021 8:10 am

Re: Today's assembly mistakes

Post by Mpk »

Alcoholics Anonymous wrote: Sat Sep 18, 2021 4:47 pm Surely tap is sufficient?
I couldn't figure that out either.
I hate the the shorthand macros with passion
Yeah, agreed. Makes learning anything much harder if code appears tied to a particular emulator.
Post Reply