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

Farabi

 :lol
I laughed at some post.

There are so many ways to look for a value is it odd or even.


IsOdd proc value:dword
mov eax,value
and eax,1
ret
IsOdd endp


This one is the sortest I can get.

Here is example how to use it

Invoke IsOdd,1234567
.if eax==0
invoke MessageBox,0,CADD("Its Even"),0,0
.else
invoke MessageBox,0,CADD("Its Odd"),0,0
.endif
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

jj2007

Farabi,
Your version is short but it alters eax. No need for that:

mov eax, 12345
test eax, 1          ; same as and eax, 1 but without actually changing the register
.if zero?
            invoke MessageBox, 0, str$(eax), chr$("Even:"), MB_OK
.else
            invoke MessageBox, 0, str$(eax), chr$("Odd:"), MB_OK
.endif

NightWare

Quote from: jj2007 on October 17, 2008, 01:15:35 PM
mov eax, 12345
test eax, 1          ; same as and eax, 1 but without actually changing the register
.if zero?
            invoke MessageBox, 0, str$(eax), chr$("Even:"), MB_OK
.else
            invoke MessageBox, 0, str$(eax), chr$("Odd:"), MB_OK
.endif

jj, test reg,1 is NOT the fastest method... coz if test is fast in itself, you NEED a jump (je,jne,jz,jnz) to use it... so you will have a branch misprediction (except in speed test, or small app limited to this work...) look at drizz post to see how to do.

noobs, do not listen the macros lovers !!! they don't see what they are doing !  :lol

after software optimisations, we must pass to user optimisations, here how to proceed... in my opinion it's the GOTY, but guys, don't play at work...  :lol




[attachment deleted by admin]

herge

 Hi NightWare:

Neat program!

ML.exe is usually in C:\masm32\bin
It is looking for RESOURCE.H on a F drive
I am pretty sure I don't have a F drive.


Microsoft (R) Windows (R) Resource Compiler, Version 5.00.1823.1 - Build 1823

Copyright (C) Microsoft Corp. 1985-1998. All rights reserved.


Using codepage 1252 as default
Creating rsrc.RES

RC: RCPP -CP 1252 -f C:\masm32\bin\odd\OddOrEven\RCa03888 -g C:\masm32\bin\odd\OddOrEven\RDa03888 -DRC_INVOKED -D_WIN32 -pc\:/ -E -I. -I . -I C:\Program Files\Microsoft SDK\Include\.
rsrc.rc(1) : fatal error RC1015: cannot open include file 'F:\Programmation\MAsm32\INCLUDE\RESOURCE.H'.
Microsoft (R) Windows Resource To Object Converter Version 5.00.1736.1
Copyright (C) Microsoft Corp. 1992-1997. All rights reserved.

CVTRES : fatal error CVT1101: cannot open rsrc.res for reading
Assembling: OddOrEven.asm
Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

Volume in drive C has no label.
Volume Serial Number is 20B4-B172

Directory of C:\masm32\bin\odd\OddOrEven

10/17/2008  08:50 PM            11,478 OddOrEven.asm
10/17/2008  05:50 PM             1,594 OddOrEven.bat
10/17/2008  05:56 PM             4,096 OddOrEven.exe
05/06/2005  12:13 AM               727 OddOrEven.exe.manifest
10/17/2008  05:56 PM             4,388 OddOrEven.obj
               5 File(s)         22,283 bytes
               0 Dir(s)  218,251,354,112 bytes free
Press any key to continue . . .



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

NightWare

Quote from: herge on October 17, 2008, 10:01:13 PM
Neat program!

ML.exe is usually in C:\masm32\bin
It is looking for RESOURCE.H on a F drive
I am pretty sure I don't have a F drive.

:red yep, sorry i've forgotten to change those path... i update the code as soon as i can...

jj2007

Quote from: NightWare on October 17, 2008, 09:36:34 PM
jj, test reg,1 is NOT the fastest method... coz if test is fast in itself, you NEED a jump (je,jne,jz,jnz) to use it... so you will have a branch misprediction (except in speed test, or small app limited to this work...) look at drizz post to see how to do.

drizz's post gives correct results, but I don't think he was seriously proposing to squeeze out half a cycle for code that ends with a MessageBox.

Furthermore, I did not claim that test eax, 1 was the absolutely fastest way to do it. But it preserves all registers, is the shortest, and is usually pretty fast. By the way, in your post I missed timers.asm among the includes

Quote
noobs, do not listen the macros lovers !!! they don't see what they are doing !  :lol

Those who write macros need to understand every little detail of their code.

Quote
after software optimisations, we must pass to user optimisations, here how to proceed... in my opinion it's the GOTY, but guys, don't play at work...  :lol

noob, don't trust people who send zip archives with nine files for a tiny little project. Especially if the code looks confused, and the exe is not working on XP.

NightWare

Quote from: jj2007 on October 17, 2008, 11:25:52 PMThose who write macros need to understand every little detail of their code.
exactly !!!

Quote from: jj2007 on October 17, 2008, 11:25:52 PMnoob, don't trust people who send zip archives with nine files for a tiny little project. Especially if the code looks confused
confused ? in what ?

Quote from: jj2007 on October 17, 2008, 11:25:52 PMand the exe is not working on XP.
hey ! it's unfair, it's vista here ! not me...  :lol

jj2007

Quote from: NightWare on October 17, 2008, 11:46:40 PM
Quote from: jj2007 on October 17, 2008, 11:25:52 PMand the exe is not working on XP.
hey ! it's unfair, it's vista here ! not me...  :lol
Actually, this was bothering me a lot, so I tested intensively why it didn't work:
- When I double-click on OddOrEven.exe in the archive, it works.
- When I double-click on OddOrEven.exe in its folder, it does not work.

The solution is... but why make things unnecessarily easy, after all, we are trying to replace test eax, 1 with something more sophisticated, right? So here is a riddle for you:

http://shell.windows.com/fileassoc/0409/xml/redir.asp?EXT=xxx <-guess which file type is unknown to Microsoft :toothy
File Type: Unknown
Description: Windows does not recognize this file type.

Hint: The code assembles fine without this file.

hutch--

 :bg

Ain't nuthin' wrong with macros. (AS LONG AS YOU UNDERSTAND THEM)  :P


; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    ifevn MACRO value
      mov eax, value
      test eax, 00000000000000000000000000000001b
    ENDM

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    mov eax, 12345

    ifevn eax
    je nxt

    print str$(eax)," odd",13,10
    jmp quit1

  nxt:
    print str$(eax)," even",13,10

  quit1:

    mov eax, 123456

    ifevn eax
    jne nxt1

    print str$(eax)," even",13,10
    jmp quit2

  nxt1:
    print str$(eax)," odd",13,10

  quit2:

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

NightWare

Quote from: jj2007 on October 18, 2008, 12:10:39 AMhttp://shell.windows.com/fileassoc/0409/xml/redir.asp?EXT=xxx <-guess which file type is unknown to Microsoft :toothy
hmm, in fact the problem happen during uncompressing, it seams the old winzip version i use "hate" double extension... but it's strange it happen on xp (i already had the problem...) and not on vista...

jj2007

Indeed. The archive contains a OddOrEven.exe.manifest

What is plain ridiculous is that
a) Windows XP cannot interpret the double extension correctly, a dozen years after the introduction of long filenames
b) when you lookup the "unknown" extension, M$'s own site has never heard of .manifest
Imho they lost already years ago control over their pile of.

herge


Hi hutch:

Slightly less verbose:


    ifevn MACRO value
      mov eax, value
      test eax, 1
    ENDM


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

hutch--

herge,

The reason for the binary notation is so you know what the macxro does, a TEST and mask without saving the results.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

jj2007

Quote from: herge on October 18, 2008, 02:10:48 AM
Slightly less verbose:


    ifevn MACRO value
      mov eax, value
      test eax, 1
    ENDM


Regards herge



There is no need to move the value into eax. Test value, 1 is sufficient.


include \masm32\include\masm32rt.inc

.data
VarOdd dd 12345
VarEven dd 123456

.code
start: test VarOdd, 1
jz @F
MsgBox 0, str$(VarOdd), "Odd:", MB_OK
jmp Test2
@@: MsgBox 0, str$(VarOdd), "Even:", MB_OK

Test2: test VarEven, 1
jz @F
MsgBox 0, str$(VarEven), "Odd:", MB_OK
jmp Test3
@@: MsgBox 0, str$(VarEven), "Even:", MB_OK

Test3: mov eax, 12345678
test eax, 1
jz @F
MsgBox 0, str$(eax), "eax is odd:", MB_OK
jmp Test4
@@: MsgBox 0, str$(eax), "eax is even:", MB_OK

Test4: exit

end start

hutch--

It fails on "TEST IMMEDIATE, 00000000000000000000000000000001b"
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php