How can I determine even or odd number?


:stupid:
Posted on 2004-06-05 22:55:22 by johnny_d
With the least significant bit. If is set, the number is odd, if clear, the number is even.
the_number dd 125


test [the_number],1
je is_even
is_odd:
;do some stuff
is_even:
;do other stuff
Posted on 2004-06-05 23:34:21 by pelaillo
		mov eax, Number32

shr eax, 1
.if( CARRY? )
@invoke MessageBox, hWin, "Number is Odd", "Quick Test", MB_OK
.else
@invoke MessageBox, hWin, "Number is Even", "Quick Test", MB_OK
.endif


Regards,
:NaN:
Posted on 2004-06-06 00:09:05 by NaN
All right, Thanks for fast reply..
It worked well for me...
Posted on 2004-06-06 00:29:36 by johnny_d
TEST should be faster than SHR, and is the same size... On the other hand, with SHR you can do tricks similar to this one:


mov eax,Number32
mov ecx,QuickTest
shr eax,1
sbb eax,eax
push ecx
and eax,91feee0ah
add [ecx+21],eax
add ecx,11
push ecx
push hWin
call MessageBox
...
QuickTest db "Quick Test",0,"Number is Even",0
Posted on 2004-06-06 07:23:43 by Sephiroth3
It would help if you explained what you are doing.
This code makes no sense at first glance.
Why are you anding with 91FEEE0Ah?
What is the significance of ?
etc etc.
If you share code, explain what and why please. Else it's just noise.
Posted on 2004-06-06 07:29:17 by Scali
I would say that it is brillant code :alright: Comments on the code



shr eax,1 ;sets carry flag depending on value in eax
sbb eax,eax ; eax becomes 0 or -1 depending on cf
and eax,91feee0ah ; eax becomes 0 or 91eee0ah depending on cf
add [ecx+21],eax ; Changes "Even" to "Odd" depending on cf


If you do not understand add , eax

"Even" = 6e657645h

6e657645h + 91feee0ah = 64644fh

64644fh = "Odd"
Posted on 2004-06-06 07:47:53 by roticv
Well the I'd guess 91feee0ah is 'Odd' - 'Even', neat trick if a bit silly in this particular case. The code would be more legible if you could use something like:

and eax,'Odd' - 'Even'
add ,eax

Though different assemblers would interpet that differently. I know Fasm interpets those string intutitivly, but don't know if it allows you to subtract them. Comments would certainly have helped.

Also, instructions like cmove or sete allow a similar technique to be used with test.

Beat me to it roticv :)
Posted on 2004-06-06 07:52:14 by Eóin
Exactly, and nobody mentioned yet that 21 is the offset of "Even" in the string.
So that is 4 posts wasted on something that the author should have mentioned right away.
As someone once said: "Everyone can write code that computers can understand. Great coders can write code that humans can understand".
The idea of posting code is not that it works, but that people can understand what it means. Remember that please. I see uncommented, almost incomprehensible code here too often, and nobody will learn from that. After all, the only people who can understand it, are the people who are familiar with it anyway.
Posted on 2004-06-06 08:15:32 by Scali
Come on Scali stop getting your knickers in a knot.
Posted on 2004-06-06 08:27:14 by Eóin
He has a point though - it's better to learn from code than just to rip and use it...
Posted on 2004-06-06 08:41:36 by f0dder
Pherapsh will be better write a thread on "tricks"... that in fact they are not tricks.. a trick will be that the cpu start burning only watching at this page ;).


QuickTest db "Quick Test",0,"Number is Even",0


is like:
QuickTest db "Quick Test",0

offsetSecond equ $- QuickTest
(secondString) db "Number is"
changeThisOffset equ $-secondString
(changeThis) db " Even",0


Also some labels not are necesary if you not whant access them directly ()...

And instead of use or calculate numbers.. you can use symbols. Also is more work.... should it be done? for pheraphs easy modify or should be done with numbers?


Have a nice day or night.
Posted on 2004-06-06 09:04:23 by rea
May I remind you that the person who started this thread is not a very experienced coder... I doubt that this person understood much of the given code.
If you try to help someone, please help him in a way that he can understand it aswell, else you might aswell not say anything.
Posted on 2004-06-06 09:27:14 by Scali
The discovery method of learning challenges the student to branch on a topic in an exploritative way - I really enjoy this method of learning myself. We move from a simple problem of ODD/EVEN to other ideas and new questions. Never is there not more to say and more to learn.
Posted on 2004-06-06 11:50:12 by bitRAKE
Everybody, Thank you very very much..
Thouhgh It's hard for me.. It must be helpful for me..
Thanks all...
Posted on 2004-06-06 13:16:23 by johnny_d
I, too, believe in this way of learning... the understanding of something by analysis and investigation can be a much more rewarding experience than being spoon fed with all the answers. It encourages the learner to think for themselves and possibly gain insights they would not have got from reading. This pedagogical approach is unfortunately not common in school textbooks over here to my knowledge - the math and chemistry textbooks, for example, that we have used at the school I have attended, contains many overly complicated explanations of simple material. This contributes to the popular habit of being dependent on others' work and being unable to think for oneself.

When I see code I don't comprehend, I sometimes make a diagram to get an idea about what bits go where and what eventually happens to them to produce a result. But most of the time, one can recognize right away the programmer's intent. The SBB+AND+ADD (or XOR) sequence used above is actually quite common in code produced by a few C compilers.

It might be that due to my background in taking apart programs to find ways to enhance them, I forget that it is not obvious to everyone what a program that is 11 instructions long is doing, but it is nevertheless a good idea to make a habit out of always trying to figure out what's going on in every piece of machine code you see. It will almost certainly be helpful in becoming a better computer programmer.
Posted on 2004-06-06 13:39:56 by Sephiroth3
blabla yea right.
Someone who doesn't know how to check if a number is odd or even obviously doesn't have much experience in asm. I suppose he doesn't even know all of the instructions used in your program. Let alone that he understands the way in which you used them. Your code was incredibly terse. Even for me it wasn't obvious right away, because you use numerical constants instead of more comprehensible forms. In short it is just bad code. Bad to maintain for yourself, and bad for others to learn from.
If you look at this code in a few years time, will you still know why you used that value to and with, or why you use ecx+21? No way, you'd have to reverse-engineer your own code.
And this is only 11 instructions in a huge program. If every set of 11 instructions requires that much effort to understand, you might aswell just throw your old code away and start fresh.
Let me put it this way: if you were working for me, I'd fire you, because you'd cost too much money.
Posted on 2004-06-06 15:04:55 by Scali
The best way to quickly become a skillful computer programmer is to become accustomed to figuring out things for oneself - you won't always have the luxury of having commented source code, and therefore I like to make code examples speak for themselves. If the reader does not know the instructions used, then he/she has an interesting and enriching task ahead. Writing learning material, whose purpose is to facilitate the student's development of high competence through encouraging him/her to actively involve himself/herself the topic at hand, is very different from writing code for a joint commercial project. If this were real code, I would have made sure to fit the code in the smallest possible space, but I didn't, just to illustrate this particular method of doing conditional operations.

I am looking at a few of my old programs right now, from before I discovered that NASM supported structs :tongue:, and it still seems quite clear, with minimal work involved in reading the code. Currently I am working on a game project with another fellow who has no problems understanding my code, even though I never or seldom make use of comments. It could be related to the fact that I assisted him in learning to program, but it is also because he actively expands his knowledge by trying to make sense out of what is not quite clear at first sight.

Training to be a good programmer is much like physical exercise. You have to push yourself to the limits to progress quickly.
Posted on 2004-06-06 17:16:11 by Sephiroth3
The code snippet was fun, sure... but I think it's a bad idea to post things like that for someone who looks like he's taking some of the very first steps in assembly programming. The risk is that it looks too complex and elitistic. A piece of uncommented code like that belongs under "programmer puzzles", and nowhere else.

You never comment your code? Bad. Sure, it will still be readable, but how much easier (and faster) would it be to read with some well-chosen comments? Even if you only program for yourself, it still makes sense, in case you want to _quickly_ get an overview of wtf you were doing some months ago - or coding sh1tfaced some saturday night ;)

Clarity, correctness, efficiency - in that order. Once you write clear code, your amount of bugs will tend to drop anyway - or at least be much easier to find.
Posted on 2004-06-06 17:29:05 by f0dder
If the reader does not know the instructions used, then he/she has an interesting and enriching task ahead.


Or he gets discouraged, gives up, and thanks to you, will never look at asm again.

just to illustrate this particular method of doing conditional operations.


You didn't 'illustrate' anything, you just dumped some incomprehensible code without even explaining what it is supposed to do, let alone how it accomplishes this task.

Currently I am working on a game project with another fellow


What kind of project? How big is it? Where is the design? How are the tasks delegated etc?

Training to be a good programmer is much like physical exercise. You have to push yourself to the limits to progress quickly.


So you tell a 12-year old to bench-press 250 kg on his first day at the gym?
Posted on 2004-06-06 17:42:38 by Scali