flat assembler
Message board for the users of flat assembler.

Index > Heap > Best coding practices? (the offspring of XOR vs. MOV debate)

Goto page Previous  1, 2, 3
Author
Thread Post new topic Reply to topic
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17272
Location: In your JS exploiting you and your system
revolution
My Pentium M doesn't have a trace cache. Nor do the new chips in the new Intel cores.
Post 15 Mar 2006, 11:33
View user's profile Send private message Visit poster's website Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1605
Location: Ukraine
shoorick
btw: yesterday i've been asked to help to do homework in pascal... i caught myself when i typed "mov i," instead of "i:=0;" Very Happy
Post 17 Mar 2006, 08:09
View user's profile Send private message Visit poster's website Reply with quote
Raedwulf



Joined: 13 Jul 2005
Posts: 375
Location: United Kingdom
Raedwulf
Well hell to english semantics.....kinda gave up on its coherency long ago.
Well...... in terms of communication we pretty much understood what vid said - and that is the main object of language. (Please don't relate this the computers Smile )

Well, english even includes the word 'Jedi' now.....isn't that sad - though I am a star wars fan Very Happy.

Cheers.

_________________
Raedwulf
Post 24 Mar 2006, 13:47
View user's profile Send private message MSN Messenger Reply with quote
Azu



Joined: 16 Dec 2008
Posts: 1160
Azu
tom tobias wrote:
revolution:
Quote:

You would do well to research your answers before proclaiming knowledge of all English.

http://dictionary.reference.com/search?q=rax
"No entry found for rax.
Did you mean tax?"
Quote:

Let's take your argument further, it seems to me that the AAD instruction is woefully underused, therefore we all need to deliberately design our algorithms to make use of this instruction even if it means having to make our code/program twice as long and half as fast.

My argument has NOTHING to do with frequency of useage. Your rejoinder is non-sequitur.
ONCE again, for the record, maybe for the VERY LAST TIME, one hopes!
My argument AGAINST use of XOR to clear a register, is this:
XOR eax, eax DOES clear EAX, everyone agrees,
however,
MOV eax, zero is preferable for READABILITY.
Illustration of this point:
Suppose we wanted to put the simple integer value ONE, 1, into a register, let's say, EAX.
We want EAX to contain the value of 1.
How shall we accomplish this?
I would propose:
mov EAX, zero
mov EAX, 1
BUT, I acknowledge, that it is possible instead, to use:
xor EAX,EAX
mov EAX,1
(and I suppose it is also possible to simply write: mov eax,1, i.e. without first clearing the register, though I am so oldfashioned, and behind the times, I would include the (probably unnecessary) EXTRA step of clearing the register.)
So, what have we got:
two ways to accomplish the same goal
xor eax, eax
mov eax, 1
versus
mov eax,zero
mov eax,1
Which method is SIMPLER for a neophyte to comprehend?
End of story
oops, no, not quite the end of story.
no, because now, you see, just to be a little fussy,
now, we insert a REAL BOOLEAN operation.
in fact, an exclusive or, operation

mov eax, zero
mov eax, 1
xor EAX, EDX

WHOOA.
now, all of a sudden, we are using the Boolean operator for its INTENDED purpose. Not to clear a register, but to perform an exclusive or operation. Here's how the same sequence appears using the FASM forum method:

xor EAX,EAX
mov EAX, 1
xor EAX, EDX

Yes, for those well trained, EXPERIENCED programmers, no problem to detect the SINGLE ROMAN LETTER difference between the clearing operation, and the exclusive or operation. HOW IS THE NEOPHYTE to distinguish between that situation, and
xor EDX, EDX
?
Only one roman letter, A, instead of D
EAX
versus
EDX
But, if one has been using xor reg, reg, for the past UMPTEEN thousands of instructions, covering hundreds of pages of code, do you think it is easy to pick out that SINGLE letter, which signifies NOT clearing the EDX register, but rather, performing an exclusive or operation involving both edx, and eax?
So, in summary, the ONLY advantage of using MOV to clear a register, rather than XOR, is that MOV is used for assignment of all other integer values, why not use it to assign the value of zero, as well? For a beginner, a neophyte, a novice, a trainee, an apprentice, THERE IS NO NEED to introduce the complexities of XOR, for the simple task of CLEARING a register, i.e. FOR THE TASK OF ASSIGNMENT OF THE INTEGER VALUE ZERO.
Madis, and apparently Agner Fog, are CONCERNED about times, execution times, wait states, and microoperations.
Hmm.

"But when I see code where ADD eax,eax | ADD eax,eax is used instead of IMUL eax,eax,4, that guy/girl can get a pat on the sholder from me."

That's fine Madis. But, NO WAY does your trainee receive a pat on any anatomical part from me, for writing AND twice, instead of mul once. I don't care if it takes 1000 clock cycles MORE time to perform one multiplication, instead of two additions. THIS IS NOT 1960
Hello, anybody home?
This is 2006.
We are no longer working in the 1970's with 8 bit cpu's
Hello, this is no longer 1980, working with an 8086, which had a performance hit for using certain instructions, compared with others.
WE DON'T NEED to worry about SAVING time in our programs.
WE DO NEED to worry about writing programs that
(a) people can understand--> so that they can modify them at a later date, and
(b) fulfil the requirements of the task, WHICH, IN MY 30 years of assembly language programming NEVER, NOT ONCE, have EVER exceeded the ability of the cpu.
In my experience, without exception, EVERY SINGLE PROJECT, whether in aerospace, or manufacturing, or industry, or for the military, or in health care, or education, EVERY project that failed, (and there were MANY in that unhappy circumstance, especially if I played a significant role!!) FAILED NOT because of code which executed too slowly, BUT BECAUSE NO ONE COULD UNDERSTAND what the predecessor team had been trying to accomplish--and as a result, we, those of us who inherited this unreadable code, were often obliged to jetison entire projects, some of them costing MILLIONS of dollars. Inevitably, the few successes resulted from starting over, and rewriting the entire project from scratch. The kind of silliness above, where we see:
Quote:

lea ecx,[ecx*2+ecx]

instead of mul ecx, 3
is not that atypical, in my experience.
I cannot tell you how many times I have seen, even some senior most programmers on this forum, using shift instructions, instead of simple multiplication or division--using those boring instructions would be too obvious, too easy for everyone to understand!!!
Rolling Eyes
First attempt:
What the hell? "xor eax,eax" makes no sense but "mov eax,0 mov eax,1" is easy to understand?

Second attempt:
Okay.. yes.. one letter. So what? If you can't even see the difference between eax and edx, how exactly do you plan to have any chance of making anything more complicated than a "Hello world" program?

Third attempt:
You do know that there are these things called "equ" and "macro" that you can use, right? Just make a macro called zero to set things to zero, and make equs for the registers to rename eax to register11111111, ebx to register222222222, etc, if you have trouble telling them apart.

Fourth attempt:
Haven't you noticed how slow and bloated everything is becoming without any new functional being added? How much power desktops are taking just to browse the web? How the size of every program is mushrooming for no apparent reason? This kind of idiocy is the reason for that. If you can't handle making decent assembly code yourself, just do everyone a favor by letting C do it for you.

Fifth attempt:
Your post made my eyes bleed. Why are you even here if you can't stand the idea of assembly-level optimizations? If you don't want to even consider implementing even the most basic of optimizations, then all of the fine-grained control that this kind of language offers you is completely wasted.

Final:
Nothing personal, but I can't find words to describe what a load of crap your post was. Really. I sat here trying for a long time, but I give up. Sorry. I'm just going to try to erase it from my memory and hope I don't have nightmares. Sad

_________________
Post 04 Sep 2009, 19:16
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
Azu mate, I tried. Honestly, too much, more than I needed to. I even gave him a freaking SIMPLE QUESTION, as to why he doesn't use HLLs instead? (because, with his argument, HLL=more readable, and it also takes care of "obfuscations" like "xor eax, eax" and such... Rolling Eyes)

He never answered it, on the other hand, he pops out of nowhere into all "xor eax, eax" discussions and rants about it.

Sad


maybe I should use bigger text, so he can tell the letters apart? Confused

how about this:






Tom, I have a question I need answered

Why
don't
you
use...


High-Level Languages????

_________________
Previously known as The_Grey_Beast
Post 04 Sep 2009, 20:22
View user's profile Send private message Reply with quote
Azu



Joined: 16 Dec 2008
Posts: 1160
Azu

_________________
Post 05 Sep 2009, 00:06
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
WytRaven



Joined: 08 Sep 2004
Posts: 45
Location: Canberra, Australia
WytRaven
Azu wrote:
Final:
Nothing personal, but I can't find words to describe what a load of crap your post was. Really. I sat here trying for a long time, but I give up. Sorry. I'm just going to try to erase it from my memory and hope I don't have nightmares. Sad


Best post ever. Laughing

_________________
All your opcodes are belong to us
Post 15 Dec 2009, 02:35
View user's profile Send private message Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak
I don't know if it's been said yet (these arguments are mostly long and mudslinging anyway so i'm not going to bother reading every one or even skim over most of them), but i'm gonna say my peace here and now.

The whole debate always will go back to the stupid "goto debate." Honestly, when alot of compilers will still compile "while(true)" to "or eax, eax" only moving 1 or 0xFFFFFFFF to eax first, is it really that unreadable when that's all anyone use the goto for anyway? This "complex process" goes into all the other stupid debates on whether to use a bool array or if it should be acceptable to teach students the bitwise operators or not. Is it really worth the trouble of having to manage the extra brackets or deal with the massive tabulation when it's embedded?

Truth is, if the person can make a large program with his/her style and manage the code without getting a headache, leave the project for a year, then return to it and be able to figure out where everything goes, then his coding style is good enough. Why should people curb their own coding style just so less experienced people can read it?

If they're qualified to take your job or take over your project, they can read it. If they can't read it, they don't deserve the recognition they have. Example programs can easily be written in "a readable way," but regular programs don't need to ruin algorithms and performance just because the future reader may be too lazy to figure it out. If your code is reasonably documented, he won't have to read the whole program just to find out what's broken.

I remember someone having a problem, and why? His problem (i forget the exact problem) was that he didn't understand how you could pass a "string" to a function. To him, when he was taking VB, this was illogical, because a string could be really large, and it wouldn't make much sense to send something so large to a function. So I explained to him what was "too hard for students to understand and should therefore never be taught." I explained pointers, and that his "ByVal" was actually a "ByRef" of a copy. Suddenly, it dawns on him, and he fixed his code without me telling him how to do it. Wow, by teaching things that are "unreadable" and "hard," I managed to get someone to make less mistakes that required the teacher to come by his computer and try to guess what was wrong with his code (since the teacher can't read "readable" code of other programmers either).
Post 15 Dec 2009, 20:55
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
Azu



Joined: 16 Dec 2008
Posts: 1160
Azu
Wtf

Shouldn't while(true) just be a non-conditional jump?

The way you mentioned is disgusting!



I agree with the rest of your post, though.

_________________
Post 15 Dec 2009, 21:09
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak
Azu wrote:
Wtf

Shouldn't while(true) just be a non-conditional jump?

The way you mentioned is disgusting!



I agree with the rest of your post, though.


You're right, it SHOULD be. It's more of a side argument on another topic. Only compilers which are smart enough to do notice it's an infinite loop will treat it as one, instead of oring 1 or 0xF...F. Unfortunately, compilers are rarely as smart as we give them credit. One of the most common arguments for doing stupid practices because they're "more readable" is often that "well, all compilers are smart enough to optimize X into Y." I remember one guy arguing with me (i forget where) that compilers are supposedly smart enough to convert "tail recursion" into a regular loop. Honestly, if compilers were so smart, code like "for(int x=0; x<endtime; x++);" wouldn't work.
Post 15 Dec 2009, 23:49
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
mattst88



Joined: 12 May 2006
Posts: 260
Location: South Carolina
mattst88
I agree with you, Azu. But did you really feel it necessary to respond to a three year old thread?
Post 16 Dec 2009, 18:24
View user's profile Send private message Visit poster's website Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak
Wow, I didn't even catch that one. Laughing

It's still as valid as ever though. Rolling Eyes
Post 16 Dec 2009, 21:34
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2, 3

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.