if a==-1 a=b;
if a==-1 a=0;

Another size optimization.

version for starter:
eax = a
edx = b


cmp eax,-1
jne exit
mov eax,edx
add eax,1
adc eax,-1
Posted on 2003-09-05 14:36:56 by The Svin
6 bytes:


again:
xchg edx,eax
inc edx
jz again
dec edx
xchg edx,eax
Posted on 2003-09-05 16:47:06 by Sephiroth3
Good code!
But it spoils edx.
With saving it's 8 bytes.
Anyway shorter than starter.
Thanks for your code.
Posted on 2003-09-05 18:31:46 by The Svin
	cmp	eax, -1

jne exit
sub eax, edx
jz exit
not eax
exit:
:grin: Have to know your instruction arithmatic: NEG == DEC + NOT :grin:

NEG == NOT + INC

(note: b + c != c + b, unless c==b)
Posted on 2003-09-05 21:50:13 by bitRAKE
BTW, Svin, why do you always need only size optimization? Isn't generally speed more important today?
Posted on 2003-09-06 02:51:54 by MazeGen


inc eax
jnz _out
inc eax ;eax = 1
add eax, edx ;eax = edx + 1
jz _realout
_out:
dec eax
_realout:

9 bytes. If only I could remove the jmps
Posted on 2003-09-06 03:56:39 by roticv

BTW, Svin, why do you always need only size optimization? Isn't generally speed more important today?

No I don't always offer size optimization.
Those who know me - remember that it was usually speed optimization, or + size without harm for speed.
Lately I just focus on opcode, size optimization help does two things - keeps me looking for some examples of usage of opcode special system, and second - makes sence of knowing opcode itself.

About what is important today - it's long talk, and better not here,
we try to keep this section extremly practical.

For short - size optimization understanding is always important, including importance of it for speed :).
We just exegerate importance of size with all this "size optimization tasks" for an obvious goal - to find and collect tricks, to finese our data analyzing skills etc.
The knowlege aquered lately to be used for other things, including speed optimization, when decreasing size we actually can increase speed.
I'd better stop right now - philosophy is not for the section.
Posted on 2003-09-06 06:58:11 by The Svin
Good point bitRake,
I usually havilly use the fact,
but in the particular case it escaped from
me.
Thanks for your code.
Sephiroth3 code is still shortest,
even if add 2 bytes for edx preservation.
Indeed interesting idea from algorithmic (as
sequence of steps) point of view.
Yours is interesting from arithmetic point of view.
Posted on 2003-09-06 07:16:09 by The Svin
Here is my try...

cmp eax, -1
jne exit
xor eax, edx
cmp eax, 0
jz exit
mov eax,-1
exit:

if i change mov eax, -1 for xor eax, edx is a little less....


If I can kick that -1 and 0 will be nice ... how ?? and eax, eax??? for set 0 flag(ZF)???
Posted on 2003-09-06 14:01:46 by rea
Another one


xchg eax, edx
xor eax, edx
jz salir1
or eax, edx
and eax, edx
salir1:


of 9 bytes, but trash edx (have the orig value of eax)

--edit--
one less byte and save edx.... ;)



xchg eax, edx
xor eax, edx
jz salir1
xchg eax, edx
xor edx, eax
salir1:

mmmmmm, only inside jz salir1 ..... and salir1: ops, the meaning of that is that i need one extra jump and a extra xchg :S

---edit---
ok, like i see is preserved ok, and my anterior conclusion was bad...
Posted on 2003-09-06 15:23:52 by rea
by the way, deleting that xchg have a short code.



xor eax, edx
jz salete
xor eax, edx
salete:

and 6 byte code ;)

Also with this 'cleared' code i see the potential problem, and this comes when eax == ebx not only when eax == -1 but will be the same for eax == edx == ...,-2-1, 0, 1, 2, 3 ...., and then that save some bytes :(

Nice day.
Posted on 2003-09-07 17:46:28 by rea
One that do the xpected of 8 bytes.



cmp eax, edx
jne sale
inc eax
jz sale
dec eax
sale:


Nice day.
Posted on 2003-09-07 19:17:14 by rea
hgb,

Your last code did not apply to the condition if a==-1, a=b;
Posted on 2003-09-08 00:41:16 by roticv
for point thatout, I wa watching the others that I put, for what, i remember that was the same result, and yea, if you watch from the first, I think tht my solutions is for some like: if (eax == -1 && eax == edx) eax = 0; else eax=-1 but tht waas not the question .....
---- or is more like if(a == b) a =0; else a = -1 in some cases :D----/edit/
I was trying to do some, and only obtain this...



cmp eax, -1
jne sali
xor eax, edx
jz sali
neg eax
dec eax
sali:
14 bytes :(




Nice day.
Posted on 2003-09-08 12:57:53 by rea