News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Odd or Even

Started by herge, October 13, 2008, 11:35:59 AM

Previous topic - Next topic

herge


Hi All:

What's the quickest way to find if EBX is Odd
or even?


isOdd proc
push ebx
and ebx, 1
pop ebx
ret
isOdd endp


Regards herge
// Herge born  Brussels, Belgium May 22, 1907
// Died March 3, 1983
// Cartoonist of Tintin and Snowy

jj2007

Not bad, really, but why so complicated?

include \masm32\include\masm32rt.inc

.code
start: mov ebx, 123
test ebx, ebx
.if parity?
print str$(ebx), " is odd"
.else
print str$(ebx), " is even"
.endif
print chr$(13, 10, "That was short and crispy, right?")
getkey
exit
end start

:bg

Jimg

Parity is the total number of bits on being odd or even, not the number itself.  However, just  "test ebx,1", .if zero? then even will work.

jj2007

Quote from: Jimg on October 13, 2008, 02:01:13 PM
Parity is the total number of bits on being odd or even, not the number itself.  However, just  "test ebx,1", .if zero? then even will work.

I feel embarrassed :red
Thanxalot, Jim.

herge


Hi All:


; BIT0.ASM Monday, October 13, 2008 11:18 AM
include \masm32\include\masm32rt.inc
.code
start: mov ebx, 122
      mov ecx, 12
Next:
test ebx, 1
      push ecx
.if zero?
print str$(ebx), " is Even"
.else
print str$(ebx), " is Odd"
.endif
      print chr$( 13, 10)
      pop ecx
      inc ebx
      dec ecx
      jnz Next
print chr$(13, 10, "That was short and crispy, right?")
getkey
exit
end start


Regards herge
// Herge born  Brussels, Belgium May 22, 1907
// Died March 3, 1983
// Cartoonist of Tintin and Snowy

fearless

How about using mod to determine if odd or even? something like this:

_mod MACRO val1:REQ, val2:REQ
push ecx
mov  eax,val1
mov  ecx,val2
xor  edx,edx
div  ecx 
pop ecx
exitm <edx>
endm


Given that all odd numbers return a 1 when divided by 2, and even numbers return 0:

.IF _mod(ebx,2) == 0
   ; even
.ELSE
   ; odd
.ENDIF
ƒearless

BogdanOntanu

Quote
How about using mod to determine if odd or even?

DIV is many times slower than test eax,1 :D
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

jj2007

Quote from: fearless on October 13, 2008, 03:51:53 PM
How about using mod to determine if odd or even? something like this:
..
Given that all odd numbers return a 1 when divided by 2, and even numbers return 0:
That sounds interesting and innovative. Given that div is a relatively slow instruction, as Bogdan rightly notes, could we optimise it by replacing the div with a multiply? Here is a first attempt, it seems to work perfectly:

123 is odd
122 is even
121 is odd
120 is even
119 is odd
118 is even
117 is odd
116 is even
115 is odd

include \masm32\include\masm32rt.inc

.data?
f2sConW dw ?

.code
Point5 REAL10 0.5
start:
fstcw f2sConW ; status word could be saved as fstsw ax,
mov ax, f2sConW ; but no such instruction for Control word
or ax, 011100000000b ; rounding mode DOWN,
mov f2sConW, ax ; precision max=64
fldcw f2sConW ; set new control word

mov ebx, 123
.While ebx>100
push ebx
fild dword ptr [esp] ; push an integer (reg or immediate)
fld Point5
fmul
frndint
fadd st, st
fild dword ptr [esp]
fsub
fistp dword ptr [esp]
pop eax
inc eax
.if zero?
print str$(ebx), " is odd", 13, 10
.else
print str$(ebx), " is even", 13, 10
.endif
dec ebx
.Endw
print chr$(13, 10, "That was short and crispy, right?")
getkey
exit
end start

BogdanOntanu

No way... MUL is also very slow when compared with TEST :D

Basically the bitwise operators: AND, XOR, OR, NOT and TEST(hidden AND)  are the fastest operations that electronics can do on a register. You can not get much faster than this because it is just a single gate with 2 inputs performing the operation... anything else is slower ...
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

jj2007

Quote from: BogdanOntanu on October 13, 2008, 04:27:52 PM
No way... MUL is also very slow when compared with TEST :D

Hey Bogdan, where is your sense of humour???

BogdanOntanu


AI_FLAG_HAS_HUMOR EQU 1

test eax,AI_FLAG_HAS_HUMOR
.IF zero?
   invoke On_Humor,LIFE_CTX
.ENDIF

ret


I think I have missed a JNZ somewhere .. :D
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

herge


Hi ALL:

Rotating the wrong way thru carry.


; ODD.ASM Monday, October 13, 2008 6:50 AM
  include \masm32\include\masm32rt.inc
.data
buf dd 0
evenA db " Even ", 13, 10, 0
odd db " Odd ", 13, 10, 0
buffer db 20 dup(0)
      db 0
.code
isOdd proc
push ebx
rcr ebx, 1
pop ebx
ret
isOdd endp
printEbx proc
push ebx
push ecx
push edi
invoke crt__ultoa, ebx, buf, 10
invoke StdOut, offset buffer
pop edi
pop ecx
pop ebx
ret
printEbx endp
.code
Start:
lea eax, offset buffer
mov buf, eax
mov ecx, 25
mov ebx, 1
@@:
call printEbx

call isOdd
jnc isEven
      push ebx
      push ecx
invoke StdOut, addr odd
      pop ecx
      pop ebx
jmp Next
isEven:
      push ebx
      push ecx
invoke StdOut, addr evenA
      pop ecx
      pop ebx
Next:
inc ebx
dec ecx
jnz @B
inkey
exit
ret
end Start


The div instruction is going to slow I can remember doing biniary
to Ascii Decimal before we had the div instruction!

Regards herge
// Herge born  Brussels, Belgium May 22, 1907
// Died March 3, 1983
// Cartoonist of Tintin and Snowy

Mark Jones

"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

jj2007

Folks, this thread is getting increasingly odd.

herge


Hi jj2007:

It is suppose to be ODD or EVEN so
far!

Regards herge
// Herge born  Brussels, Belgium May 22, 1907
// Died March 3, 1983
// Cartoonist of Tintin and Snowy