Truly shocking code (z80 asm)

A General Discussion forum for TI calculators

Moderator: MaxCoderz Staff

King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Truly shocking code (z80 asm)

Post by King Harold »

http://www.ticalc.org/archives/files/fi ... 34716.html
Frank Yaul (in "TI86 Z80 Quicksort Implementation") writes:

Code: Select all

 scf
 ccf
 sbc hl,de
:shock: :shock: :shock:

someone who can write a Quicksort Implementation (which works, I've tested it) does not know the side effects of 8bit logic operations? Or does the 86's z80 work different?
User avatar
DigiTan
Calc Master
Posts: 1069
Joined: Fri 04 Mar, 2005 12:52 am
Location: NW Wisconsin
Contact:

Post by DigiTan »

I always "xor a/sbc hl,de." But never bothered to look up which is faster.
My diet consists of nails, code-stealers, and HP fans.
Projects:

Robot War [TI-82, TI Flash App]
Sonic the Hedgehog [Multiplatform]
Liazon
Calc Guru
Posts: 962
Joined: Thu 27 Oct, 2005 8:28 pm

Post by Liazon »

i doubt it. I'm pretty sure it's the same exact processor.
Image Image Image
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

So was I, but I began to doubt it, seeing this .. code..
XOR A, OR A, and AND A all take 4 cc's and 1 byte instead of the 8 cc's and 2 bytes that "scf ccf" takes together..

SCF CCF differs in effect ONLY from OR A, because it sets H, but that will only ever matter in conjunction with daa, and SCF CCF has the exact same result as AND A.
I wouldn't recommend XOR A since it zero's A and could lead to bugs if you make a habit of using it.
CoBB
MCF Legend
Posts: 1601
Joined: Mon 20 Dec, 2004 8:45 am
Location: Budapest, Absurdistan
Contact:

Post by CoBB »

You love looking for such artifacts, King Harold, don’t you? I suggest looking through the source of Acelgoyobis then. It has enough of them to give you a heart attack, so be careful. ;)
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

I think I'll live, I'll give it a try :P

edit: do you mean this?

taken from "physics.inc" just after the label MB_HackX:, in Acelgoyobis:

Code: Select all

 ld a,0
 srl a
 ld d,0
 ld e,a
 add hl,de
ok, lets see.. first put a zero in A, then shift it, which does nothing else than reset all flags (yes all, even H and N), then zero D, put A in E( zero E) and add DE to HL( HL += 0)..
what does that do? It loads zero into register! With arithmetic :shock: ! At the end of this, both DE and A will be zero, and all the flags will be reset and HL will not have changed..

CoBB, why did you write this? Left-over code from when it used to do something? Do you change it with SMC? Does it do something I have overlooked?
User avatar
Jim e
Calc King
Posts: 2457
Joined: Sun 26 Dec, 2004 5:27 am
Location: SXIOPO = Infinite lives for both players
Contact:

Post by Jim e »

When you manage a program that consists of several thousands of lines of code you'll realize that little chunklet there doesn't mean squat. It doesn't build up and multiply into bad performance, nor does it waste kilobytes of memory. I can easily find more than a few example of code of mine that looks useless like that. It simply evolves into that while no one is looking.
Image
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

So that would be "left-over code that once did something" right?
Ofcourse I can perfectly understand that, but that does not mean it particularly useful.. Maybe someone could write a little filter that will warn you when you have code that acts like a practical NOP at compile-time?

edit: your sig picture is gone..
User avatar
tr1p1ea
Maxcoderz Staff
Posts: 4141
Joined: Thu 16 Dec, 2004 10:06 pm
Location: I cant seem to get out of this cryogenic chamber!
Contact:

Post by tr1p1ea »

Why do you keep doing this King Harold ... ? Find a nice routine/program and then belittle it with a tiny spinnet of code? Its a complete waste of time and its utterly disrespectful to publically single people's work out as well.

You have a suggestion for someone elses code? How about you email them instead of making new threads.

And quite obviously that snippet of CoBB's code you posted is modified by SMC. Especially with a label name like that.
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."
Image
Image
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

now now tr1p, come one man, I know CoBB writes nice code, but he asked for it. (notice that I did take in the possibility that he changes it with SMC)
that other code however, in the first post, is completely terrible, no matter how you look at it. You can think that 4 cc's and 1 byte aren't going to matter and be right about it, but that's not the point. I'm merely wondering how someone who does not know about logic operations has managed to write a working quicksort routine.
That program contains loads of "scf ccf"'s, it's not just an isolated mistake. The entire program never uses OR A or AND A to reset the carry flag.
When you need to preserve some flags then of course SCF CCF is okay, but before a SBC it is absolutely useless to use it instead of OR A or AND A.
CoBB
MCF Legend
Posts: 1601
Joined: Mon 20 Dec, 2004 8:45 am
Location: Budapest, Absurdistan
Contact:

Post by CoBB »

King Harold wrote:CoBB, why did you write this? Left-over code from when it used to do something? Do you change it with SMC? Does it do something I have overlooked?
As pointed out above, it’s SMC, and the modifying code is in the very same file, which you could have easily found by looking for the hack label. ;) That’s not the kind of thing I had in mind, since that’s conscious coding.

I sort of expected you to point out this piece of code in interact.inc (or even the whole EventHandler subroutine for not using a jump table):

Code: Select all

 jp z,Event_SetTimer   ; SetTimer Timer N

; If we arrive here, that's an error... It should never happen!

 jp Exit               ; Returning to the OS
I’m sure it must be giving you shivers. :P
King Harold wrote:Ofcourse I can perfectly understand that, but that does not mean it particularly useful.. Maybe someone could write a little filter that will warn you when you have code that acts like a practical NOP at compile-time?
This is impossible with assembly precisely for its self-modifying capabilities and context sensitivity. Well, you can write a program that matches well-known little code snippets in a dumb way and proposes well-known alternatives, but that wouldn’t be very useful in practice.
User avatar
tr1p1ea
Maxcoderz Staff
Posts: 4141
Joined: Thu 16 Dec, 2004 10:06 pm
Location: I cant seem to get out of this cryogenic chamber!
Contact:

Post by tr1p1ea »

Err King Harold, you are yet to prove yourself as an ASM master ... why should everyone else be? I personally think it is ok for some people to not know everything about programming. Whats to say that good ol' Frank Yaul was only learning assembly when he released that routine? 'scf \ ccf' isnt even that terrible, a lot of us here are pretty bewildered as to why you would even go to the trouble of pointing it out.

A lot of people who learn assmebly make mistakes. Ive seen some pretty bad code over my time. Instead of gloating about how lame someone else is, how about you offer them a helping hand? You know, be constructive?
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."
Image
Image
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

I never said I was an ASM master :)
and I don't think i'll ever be an ASM master anyway..

but

Code: Select all

 scf
 ccf
 sbc hl,de
is quite shocking if it's used constantly, as done by Frank. As I've said before, I find it strange that someone who does that is able to pull off a quicksort routine, seeing as you'd still be learning..

@CoBB: I just gave it a look, no in-depth search for oddness, I open a file and gave it a look :P you can't expect much to come off it :)
for the second part: It should be possible to write such a program nevertheless, I know I can't do it but maybe someone else could?
A lot of people who learn assmebly make mistakes.
Of course they do, but should you write Quicksort in a language you're new to?

Oh and Frank's code contains more strangeness than just "scf cff", not to noob-bash him, but yea it's not optimal to say the least. It was the weirdest thing I could find though.
User avatar
tr1p1ea
Maxcoderz Staff
Posts: 4141
Joined: Thu 16 Dec, 2004 10:06 pm
Location: I cant seem to get out of this cryogenic chamber!
Contact:

Post by tr1p1ea »

Why are you explictly looking for weird and strange code to go on about in the first place?
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."
Image
Image
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

I wasn't really, I thought "oh let's see how someone could implement quicksort in asm" and then I saw "scf ccf sbc x,x" and more and though "damn that's worse then, say, rl a (the 2byte 8cc opcode) because this is completely useless" rl a atleast modifies the flags in a different way, "scf ccf" also modifies them in a different way - but if won't matter unless you DAA after it (which Frank doesn't)
I'm not as evil as you think tr1p..
Post Reply