Here is very simple problem, but I think it would be fun to solve it here.
It teaches checking condition.
Given:
you have 9 variables let them be v1,v2,v3,v4,v5,v7,v8,v9.
Known that 8 variables of them are equal, 1 is different.
Task is to find the different variable.
You may ADD, MOV, any JCC and CMP.
You must put the var in eax and ret.
BUT! You can use no more than 2 CMP. You are free to use the rest instruction
as many times as you want.

Good luck!.
Posted on 2002-03-25 07:33:22 by The Svin
``````	mov		esi,offset array-4
mov		eax,[esi+4]
cmp		eax,[esi+8]
je		@f
mov		eax,[esi+12]
@@:
cmp		eax,[esi]
je		@b
mov		eax,[esi]``````

KetilO
Posted on 2002-03-25 07:56:58 by KetilO
Well...
Your code has just two cmp, all right.
But they may be executed more than 2 times.
Could your figure out code wich return right result and execute
cmp no more than twice? ;)
Posted on 2002-03-25 08:45:05 by The Svin
Since I needed to implement exactly this too, I'll join the vagon. :)

It's not particularly optimized (I've so many things to work on that it would be a waste of time to improve it), but works well on my Athlon (including that MOV,1):

NOTE: ESI must contain the address of the first variable to test; EAX will return the index of it (0..8 included).

``````
MOV             EBX,[ESI]
MOV             EAX,1
CMP             EBX,[ESI+4]
JE              .loop
CMP             EBX,[ESI+8]
JE              .exit
XOR             EAX,EAX
JMP             .exit
ALIGN           16
.loop:                          INC             EAX
CMP             EBX,[ESI+EAX*4]
JE              .loop
.exit:
``````

I just noticed that you imposed restrictions on the instructions that can be used.. I really see no point for them (as e.g. why enforcing the use of ADD instead of INC?), so I'm out of the vagon I realize. :)

It was worth a quick copy n'paste anyway :)
Posted on 2002-03-25 09:27:29 by Maverick

Could your figure out code wich return right result and execute
cmp no more than twice? ;)
Of course using ADD (accumulating real and foreseen results), but that wouldn't make it more optimized, so I'll not do it. ;)
Posted on 2002-03-25 09:29:04 by Maverick
hi!

I saved a CMP, and hope you allow me to use two SUB's instead. :grin:

``````
mov	eax, v1
cmp	eax, v2
jne	@F
mov	ecx, v1

sub	eax, ecx
ret
@@:
mov	ecx, v9

sub	eax, ecx
ret
``````

Maverick : Was the use of XOR and INC allowed ? ;)

Cu, Jens
----
http://www.emucheater.com
Posted on 2002-03-25 09:36:22 by Jens Duttke
Maveric, without given condition the problem is absolutly primitive.
The only reason I offered the problem - simple math logic problem.
So it is not about how to solve the problem, but how to solve it with given condition.
Condition not met - the problem not solved.
Posted on 2002-03-25 09:55:36 by The Svin
No, but since I was a little kid, I couldn't stand any rule. ;)

That's the reason of the handle "Maverick", after all. ;)
Posted on 2002-03-25 09:55:56 by Maverick
NOTE: my previous message was addressed to Jens.

Maveric, without given condition the problem is absolutly primitive.
The only reason I offered the problem - simple math logic problem.
So it is not about how to solve the problem, but how to solve it with given condition.
Condition not met - the problem not solved.
Well, I've no time for it just as an excercise in logic.. although I'm sure you'll get others in. :)
Posted on 2002-03-25 09:57:32 by Maverick
Jens,
intersting code!
Now lets change task - return index of the variable.
Posted on 2002-03-25 10:02:49 by The Svin
See? I should sleep more.. since I thought since the start that one had to return the index, not the var itself. In fact my code (which is out of the requeriments, I know.. but I wanted to post it anyway since it was fresh in my clipboard) returns an index, not the var..
Tonight I will sleep 30 minutes more, I promise. :grin:
Posted on 2002-03-25 10:46:00 by Maverick
hi!

Jens,
intersting code!
Now lets change task - return index of the variable.

I don't think, that this is possible with your given condition.
Or have you already found a solution for it ? :)

Cu, Jens
----
http://www.emucheater.com
Posted on 2002-03-25 11:13:14 by Jens Duttke
Modified Jen's code - No CMP :)
``````
mov	eax, v1
[color=blue]sub     eax, v2[/color]  ;I know I cheated :)
jne	@F
mov	ecx, v1

[color=blue]mov     eax, v1[/color]

sub	eax, ecx
ret
@@:
mov	ecx, v9

[color=blue]mov     eax, v1[/color]

sub	eax, ecx
ret
``````
Posted on 2002-03-25 11:31:38 by stryker

hi!

I don't think, that this is possible with your given condition.
Or have you already found a solution for it ? :)

Are you jocking? It's second grade problem :)
I solved that when I was 8.
Posted on 2002-03-25 11:45:45 by The Svin
Svin,

I have a solution but I have a few question regarding the rules. Is the frequent use of SUB Ok because technically this can replace CMP? And are the variables in array form or seperated like var1, var2...?
Posted on 2002-03-25 11:59:33 by stryker
Jens,
In task to return index instead of value I forget to say one more
thing given, sorry.
In that task is also known that different value is LESS than others.
stryker,
The main condition is not use cmp (or subs) more than 2 times.
That is whole point of problem, how to compare more things with less comparesions.
Training in that is one of most usefull things in programming.
Help to prepare for creation effective control blocks.
Posted on 2002-03-25 12:12:42 by The Svin

Are you jocking? It's second grade problem :)
I solved that when I was 8.

Nope, I just didn't thought about it. :grin:

But still ... this problem is harder than to return just the value.
It should be easy if you allow to use SUB, XOR, AND, NOT and anything else ... but not with just 2 cmp's, add's, some jumps, and even without a SUB.

I would like to see your solution for that. :)

Cu, Jens
----
http://www.emucheater.com
Posted on 2002-03-25 12:16:37 by Jens Duttke
hi!

Jens,
In task to return index instead of value I forget to say one more
thing given, sorry.
In that task is also known that different value is LESS than others.

Ohh ... that makes it damn easy. :grin:

I just wonder what you mean by 'index' ...

the number of the value ? v1 = 1; v2 = 2 ...
or simply the address of the value ?

Cu, Jens
----
http://www.emucheater.com
Posted on 2002-03-25 12:22:36 by Jens Duttke
Since you said only CMP and SUB, why not ADD? :)

Ah!!! I have solution, without cmp and subs...just add but there are a lot of jumps involve... :(
``````
.DATA

Bfr  DB 9 DUP(?)
var1 DD 2
var2 DD 3
var3 DD 2
var4 DD 2
var5 DD 2
var6 DD 2
var7 DD 2
var8 DD 2
var9 DD 2

.CODE

Start:

mov     eax, var1
[color=blue]jb      @F[/color]

invoke  MessageBox, 0, 0, 0, 0

@@:

@@Exit:

invoke  ExitProcess, 0

END Start
``````
This one will popup a messagebox. The magic jump is the jb if you change that to ja it will not popup a messagebox. :)

Still incomplete but this should be what Svin's looking for: Determine what is the odd variable among the rest.
Posted on 2002-03-25 12:44:38 by stryker
Jens,
It's upto you anything of variants is fine.
Posted on 2002-03-25 12:46:10 by The Svin