flat assembler
Message board for the users of flat assembler.

Index > Main > XOR EAX,EAX

Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8  Next
Author
Thread Post new topic Reply to topic
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
How to optimize for the Pentium
family of microprocessors Agner Fog wrote:
The PPro, PII and PIII processors make a special case out of this combination to avoid a partial register stall when later reading from EAX. The trick is that a register is tagged as empty when it is XOR'ed with itself. The processor remembers that the upper 24 bits of EAX are zero, so that a partial stall can be avoided. This mechanism works only on certain combinations:

XOR EAX, EAX
MOV AL, 3
MOV EBX, EAX ; no stall

XOR AH, AH
MOV AL, 3
MOV BX, AX ; no stall

XOR EAX, EAX
MOV AH, 3
MOV EBX, EAX ; stall

SUB EBX, EBX
MOV BL, DL
MOV ECX, EBX ; no stall

MOV EBX, 0
MOV BL, DL
MOV ECX, EBX ; stall

MOV BL, DL
XOR EBX, EBX ; no stall
Post 14 Dec 2006, 22:01
View user's profile Send private message Reply with quote
MichaelH



Joined: 03 May 2005
Posts: 402
MichaelH
WytRaven wrote:
Assembly is not meant to be readable


If you continue to believe assembler code is not meant to be readable, that's exactly what you'll get! I think assembler can be the most readable language there is. It's never the language's fault for badly written code, it's always the coder's fault!


Quote:

Unfortunately I am starting to feel that your opinions on this matter are beginning to amount to pollution in the context of this message board and its subject.


I totally support Tom's quest for well written readable assembler code (although I like xor eax,eax) and if you think it's population then I say, Tom, pollute away!
Post 15 Dec 2006, 00:10
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 559
smiddy
Tom, because it is written doesn't make it right (or wrong). It just means that someone preferred it that way over another way. In any of those books are there explanations as to why they chose one way or another? In Berry Brey's books he uses XOR reg,reg to accomplish an initialization of that register to zero and so does Allen Wyatt. I still contend it is a matter of choice, and if you are whimsical or pragmatic about it it doesn't matter so long as it accomplished the end result, solving the problem.

I think what has happened is there was an unintended result, however they analyzed it further and understood what it was doing and the benifits of using it, smaller code and faster processing (at least in the old days).
Post 15 Dec 2006, 02:26
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
MichaelH: Then HLLs are pointless, right?

ps.apparently I had some problems and haven't been on these forums for quite a while
Post 15 Dec 2006, 20:59
View user's profile Send private message Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost
Plue wrote:

Code:
count := count + 1    
our compiler generates code similar to:
Code:
mov ax, WORD PTR count_083
push ax
mov ax, 1
pop dx
add ax, dx
mov WORD PTR count_083, ax    

That is one shitty compiler.

NTOSKRNL_VXE wrote:

Code:
ZER reg 

Zeroizes a register 

LOK reg

Looks at the value in a register, and sets flags, same effect as CMP reg,0
    



Intel did do this, it's "mov r, 0"

xor is a different instruction all together. xor enumerates and compares each bit of the dst and src operands and sets each corresponding bit in the dst to 0 if they are equal, or 1 if they differ. If you do "xor eax, eax", each bit is obviously the same in both operands as its the same value. xor wasn't designed to clear registers, it just has that functionality, as does "sub r, r" and "and r, 0". I think the old saying is "There is more than one way to skin a cat."

I don't know if you are familiar with C, but I wrote a small routine to try and explain this ->

Code:
void xor( int *dst, int src ) {
    int i = 0, m = 1;

    for( ; i <= 31; i++ ) { /* an iteration for each bit */
        if( (*dst & m) == (src & m) )
            *dst &= ~m;     /* clear bit */
        else
            *dst |= m;      /* set bit */

        m <<= 1; /* next bit mask */
    }
}
//---

.......

int a = 50;
int b = 50;

xor( &a, b ); /* this is the same as a ^= b; */
printf( "%i", a ); /* zero is printed */
    

_________________
redghost.ca
Post 15 Dec 2006, 21:45
View user's profile Send private message AIM Address MSN Messenger Reply with quote
MichaelH



Joined: 03 May 2005
Posts: 402
MichaelH
Quote:

MichaelH: Then HLLs are pointless, right?


Hi The_Grey_Beast, yep, HLLs are totally pointless!

HLL languages were created by programmers so they could get imployed and get paid to program ..... pretty clever really Smile

Problem is there are far too many people like WytRaven who don't even want to consider writing well written clear assembler code and not enough people like Tom saying that if only you try, it can be done.
Post 15 Dec 2006, 23:15
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
Quote:
That is one shitty compiler.


Right Very Happy

Quote:
Intel did do this, it's "mov r, 0"


Unfortunately, it wastes 4 bytes storing the zero as 32-bit integer, and
invites hard-core coders to invent things like "XOR EAX,EAX".

Quote:
I don't know if you are familiar with C, but I wrote a small routine to


OK, understand the C code, also why "XOR EAX,EAX" saves memory
and zeroizes EAX.

So, are XOR and OR the only 2 instructions frequently used exposing such a syntax ?

_________________
Bug Nr.: 12345

Title: Hello World program compiles to 100 KB !!!

Status: Closed: NOT a Bug
Post 15 Dec 2006, 23:35
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
MichaelH: you are wrong about HLLs.
Post 16 Dec 2006, 00:03
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
MichaelH



Joined: 03 May 2005
Posts: 402
MichaelH
Quote:

MichaelH: you are wrong about HLLs.


Well Vid, you're about 20 years old, so that means I've been programming in various HLL's for your entire life span Shocked . When you've done 20 years programming in HLL's, think back too when you posted that remark and see what you've really achieved. If you head towards HLL programming, I'm picking it will be a shit load of sod all ..... but you'll be paid well for it and that's all that counts in the end Wink
Post 16 Dec 2006, 01:08
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7797
Location: Kraków, Poland
Tomasz Grysztar
RedGhost wrote:
xor enumerates and compares each bit of the dst and src operands and sets each corresponding bit in the dst to 0 if they are equal, or 1 if they differ.

There's also another possible interpretation of xor operation (though the one you give here is exactly the xor definition): it negates the bits in dst specified by the mask is src. That is, the bits that are set in src specify, which bits in dst gets inverted. For this reason xoring with -1 (all bits set) is the same as negation, and xoring with 0 does nothing. This makes xor useful for switching flags, and certainly it's good for beginners to understand this interpretation, too.
Post 16 Dec 2006, 03:14
View user's profile Send private message Visit poster's website Reply with quote
Kain



Joined: 26 Oct 2003
Posts: 108
Kain
It didn't take me long to learn most of the logical tricks that assembly programmers use. This is just a part of the assembly language that every asm programmer should know and it will eventually become natural. The only reason I can think of for using mov eax, 0 rather than the shorter form is to preserve flags.
Post 16 Dec 2006, 05:57
View user's profile Send private message Reply with quote
tom tobias



Joined: 09 Sep 2003
Posts: 1320
Location: usa
tom tobias
Tomasz wrote:

This makes xor useful for switching flags, and certainly it's good for beginners to understand this interpretation, too.

could not disagree more earnestly. Do NOT use Boolean operators to test flags, if you wish to write programs that others can understand.
Quote:

The only reason I can think of for using mov eax, 0 rather than the shorter form is to preserve flags.

How about REDUCING errors?
XOR eax, ecx. ooops, meant to write XOR eax, eax. Do you have any idea how MUCH time it takes to debug someone else's CODE, (usually undocumented, like FASM)? Here's a hint:
HIRE SOME PROGRAMMERS, pay them some money from your own pocket, and then, a few months later, about half way through the project, when they have all departed your employ, to commence work at M$, for more money, hire some more programmers, and give them the previous group's efforts. THEN, COUNT how many of the new folks, working with the previous group's CODE, suggest to you, that it would be CHEAPER, i.e. MORE COST EFFECTIVE, if they THREW AWAY the efforts of the first group, because it was essentially an undecipherable tangle of unmarked, unlabeled, illogical wires, that would require more time and effort to figure out, than rewriting the whole mess themselves. I will bet you one bottle of Pilsner Urquel, that AT LEAST 90% of the second group will make that statement, to you, within one week of receiving the CODE from the first group. My comments focus on XOR, only because it is the most obvious, most egregious error on this forum, but believe me, the same could be said about MANY other instructions, where PRECIOUS execution time is saved, or EVEN MORE PRECIOUS MEMORY is saved by using "TRICKS", like:
Quote:

xoring with -1 (all bits set) is the same as negation, and xoring with 0 does nothing.

for what? The REAL time that one wishes to SAVE, is the time (and money!!) required for a team of programmers to work on the same project, successfully. This time, i.e. MONEY, requires TRANSPARENCY, not subtlety; programs, NOT CODE. Trickery may win prizes at the university, but not in industry.
Kain wrote:

it will eventually become natural

Yes, doing things improperly can indeed become "natural", history is full of illustrations. Very appropriate comment, "Kain".
Tomasz wrote:
xoring with 0 does nothing

Are you sure? What does it do to the person who must try to figure out this "clever" rationale? Of course, perhaps that person is insignificant....
Post 16 Dec 2006, 07:26
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7797
Location: Kraków, Poland
Tomasz Grysztar
Tom: I was not talking about programming here, I was talking about knowledge. Understanding different aspects of some mathematical operations may be useful even as a simple mental excercise, not speaking about better insight into many problems.
Sorry for off-topic, but also understanding this interpretation of xor makes it easier to understand why it is so useful for symmetric encryptions (no, I'm still not speaking about programming), as xor-ing twice with the same mask will just negate twice all the bits specified by thisk mask, and because negation is an involution, we get the original value intact... And if we xor with any set of values, in any order, but with each one exactly twice, each bit gets switched an even amount of times, what means it stays unchanged, so we've got again the original value. This means you can apply many xor encryptions and decryptions in any order and it's still going to work.
Post 16 Dec 2006, 08:19
View user's profile Send private message Visit poster's website Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost
Tomasz Grysztar wrote:
RedGhost wrote:
xor enumerates and compares each bit of the dst and src operands and sets each corresponding bit in the dst to 0 if they are equal, or 1 if they differ.

There's also another possible interpretation of xor operation (though the one you give here is exactly the xor definition): it negates the bits in dst specified by the mask is src. That is, the bits that are set in src specify, which bits in dst gets inverted. For this reason xoring with -1 (all bits set) is the same as negation, and xoring with 0 does nothing. This makes xor useful for switching flags, and certainly it's good for beginners to understand this interpretation, too.


I think you should be writing some in-depth assembly tutorials. The number one question I get asked by new programmers is for me to point them towards a good set of tutorials, and besides iczelion's there isn't much and I usually say buck up for the Art of Assembly Language or download the Intel/AMD manuals -- and I think this sometimes discourages the new-to-asm programmer.

_________________
redghost.ca
Post 16 Dec 2006, 17:13
View user's profile Send private message AIM Address MSN Messenger Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7797
Location: Kraków, Poland
Tomasz Grysztar
RedGhost: have seen my already started tutorial in the Documentation section? It was forgotten for a long time, however I plan to get back to it after I finish "Understanding fasm".
Post 16 Dec 2006, 17:29
View user's profile Send private message Visit poster's website Reply with quote
El Tangas



Joined: 11 Oct 2003
Posts: 120
Location: Sunset Empire
El Tangas
Mmm, another xor reg, reg thread... Can't resist posting.

Tom, do you also object to sub reg, reg? That's more obvious than xor, since we learn subtraction in elementary school, maybe even before school, or is it too obscure that subtracting something from itself results in nothing remaining?


lea eax, [0] ;just because I can
Post 16 Dec 2006, 18:56
View user's profile Send private message Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost
El Tangas wrote:

Tom, do you also object to sub reg, reg? That's more obvious than xor, since we learn subtraction in elementary school, maybe even before school, or is it too obscure that subtracting something from itself results in nothing remaining?

Laughing

Tomasz Grysztar wrote:
RedGhost: have seen my already started tutorial in the Documentation section? It was forgotten for a long time, however I plan to get back to it after I finish "Understanding fasm".

I had forgotten this, but it needs a bit more work, I took a look at the other articles, seem to have grown a bit.

....

Before I even learned what xor did I was able to recognize "xor r, r" had the effect of clearing the register from reading forums/sources, can't see why anyone would object to it.

_________________
redghost.ca
Post 16 Dec 2006, 22:32
View user's profile Send private message AIM Address MSN Messenger Reply with quote
WytRaven



Joined: 08 Sep 2004
Posts: 45
Location: Canberra, Australia
WytRaven
Quote:
Problem is there are far too many people like WytRaven who don't even want to consider writing well written clear assembler code and not enough people like Tom saying that if only you try, it can be done.


I write clear comments and maintain high performance assembly.

Quote:
if you wish to write programs that others can understand.


I don't wish to write assembly others can understand. I wish to write high performance assembly with comments that others can understand.

@MichaelH: Yay tom's found a gimp! Confused

_________________
All your opcodes are belong to us
Post 17 Dec 2006, 00:48
View user's profile Send private message Reply with quote
tom tobias



Joined: 09 Sep 2003
Posts: 1320
Location: usa
tom tobias
El Tangas wrote:

is it too obscure that subtracting something from itself results in nothing remaining?
I imagine the process of "clearing a register" as comparable to emptying the wastebasket, or washing the laundry, or sweeping the floor, or vacuuming the carpet. The register itself does not disappear, only the trash within it. I have never, until today, thought of photographing a trash bin, and then turning it upside down, placing it on top of itself in the photograph, and then inverting both the real trash can, and its virtual image, with the aim of causing both the real and the virtual contents to disappear. To answer your question, directly, NO. When I was in elementary school, half a century ago, the USA was too busy training children to hide under their desks, in case of nuclear war, so we skipped that lecture in class about subtracting something from itself. Umm. NO, I would not consider SUB reg, reg to be a useful candidate for a submission to the forum in the category of EMINENTLY READABLE. A careless typographic mistake could lead to hours of effort searching for the unintended error. Perhaps MOV reg, zero is simply too facile for fluent FASM forumers to accept? If the FASM forumer's code is insufficiently cryptic, it may convey the false impression that mere beginners, not FASM FORUM experts, wrote it, correct? I wonder, which requires more effort: writing a program, or writing code? In my own case, I was always too lazy to write anything but code. I always admired folks who could think in terms of data structures and algorithms.
E.W.Dijkstra CACM 18 (1975) pgs 453-7, and "A Discipline of Programming" from Prentice Hall, published the following year, 1976.
WytRaven wrote:
Yay tom's found a gimp!
Had to look up a couple of words:
http://www.yiddishdictionaryonline.com/
"vay" apparently means "Gosh", or "OOPS", or "WOW"
http://dictionary.reference.com/browse/gimp
while, "gimp" apparently refers, in a derogatory sense, to someone with a disability, or infirmity. I suppose the intention here is to signal to FASM forumers disdain for another forum member, (because the person is "lame", and therefore deserves neither sympathy nor attention), simply because that member expressed a view contrary to the view of the majority....I believe that such ad hominem attacks are unproductive, particularly, in the MAIN section of the forum:
http://dictionary.reference.com/browse/ad%20hominem%20
but, whether the comment is regarded by others as productive and/or dignified, or simply a distraction, as I suspect is the case, it appears irrelevant to the central question of this thread.
RedGhost wrote:
can't see why anyone would object to it.

Well, I don't wish to annoy vid, so I will refrain from posting a recursive link to this thread in response. Perhaps I am writing too tersely again?
RedGhost wrote:
I think you should be writing some in-depth assembly tutorials.
RedGhost wrote:
but it needs a bit more work,
As my dad used to ask me, "Is your arm broken"?
The question then is this, Would a "tutorial" (whether in depth, or superficial, and whether authored by Tomasz, or someone (i.e. anyone!) else of lesser talent) yield a satisfactory answer to the original question posed by the author of this thread, i.e. why is it desirable to employ a Boolean (or arithmetic) operator to clear or test a register, rather than the intuitive instructions, MOV reg, zero; CMP reg, zero? My hypothesis, based upon what I have read, is that such a tutorial, if illustrated with these kind of comments could be ill suited for beginners:
Tomasz wrote:
Understanding different aspects of some mathematical operations may be useful even as a simple mental excercise, not speaking about better insight into many problems.
I sincerely applaud Tomasz' effort to educate us with this comment, and concurrently acknowledge with praise his suggestion to improve our mental hygiene with exercise, for some of us are intellectually slovenly, and in need of a bit of trimming up! I acknowledge my own dullness, I clearly require extensive sharpening. Reading this forum, for me, is akin to entering the blacksmith's workshop. I can almost feel those strong hands polishing my featureless cerebrum.
Smile
Post 17 Dec 2006, 09:58
View user's profile Send private message Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade
tom tobias wrote:
The question then is this, Would a "tutorial" (whether in depth, or superficial, and whether authored by Tomasz, or someone (i.e. anyone!) else of lesser talent) yield a satisfactory answer to the original question posed by the author of this thread, i.e. why is it desirable to employ a Boolean (or arithmetic) operator to clear or test a register, rather than the intuitive instructions, MOV reg, zero; CMP reg, zero? My hypothesis, based upon what I have read, is that such a tutorial, if illustrated with these kind of comments could be ill suited for beginners.


So you're fine with educational books, which are meant to teach assembly, to teach your view on the subject,
Code:
mov eax, 0    
whereas any book or tutorial using the syntax
Code:
xor eax eax    
could be "ill suited" for beginners.

Anyway, any good tutorial should (and do) first introduce the mov method for clearing a register, and then later on in the book (usually in the Optimizations section), it will introduce the xor method, as it is, clearly, an optimization on speed, whether you like it's "unclear" syntax or not.

As for the subject of why fasm programmers write in cryptic code, for me, it's not meaningful. I write code which i believe is the most optimized, if that gives a cryptic output, then so be it. I'll add comments to the cryptic section to describe the purpose of the code section, or put some high level equivalent. But I don't expect my code to be read by anyone else, seeing as I usually code assembly for fun. Sadly my university course is teaching me C, java, and with a tiny bit of assembly, but less than even the simplest assembly tutorial. I code assembly on the side, because I enjoy it's power, and simplicity. Maybe some day I'll write code for a company, and that day, I'll have to adhere to coding standards, and if they want mov instead of xor, then they'll get it, because they are paying me, and if I don't stick to the rules, I'll get fired. But at the moment, I'm coding for myself, because I enjoy it, and I want to make the most optimized code I can, just because I can.

I doubt this discussion will lead anywhere, because both sides (including me) are too stubborn to change their view on the subject (and I say rightly so Razz), but it should continue, and be saved somewhere on the internet, for other people to see the views on the subject, and make up their minds for themselves.

Toodles Mr. Green
Post 17 Dec 2006, 11:15
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8  Next

< 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 cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.