2

Is it possible to get sign flag equal 1 with these instructions?

movzx ecx, byte ptr[eax]
and ecx, 8000000Fh
jns short loc_401073

There is an and between the ecx register and 8000000Fh. In ecx can be a number in the range 00h-FFh, but I am not sure if it is possible to get SF=1. Could someone help me?

Fifoernik
  • 9,411
  • 1
  • 18
  • 26
martin
  • 25
  • 2

2 Answers2

5

If the range of ecx is indeed limited to 00h-FFh, SF is never going to be set as a result of and ecx, 8000000Fh because the most significant bit is always clear.

Fifoernik
  • 9,411
  • 1
  • 18
  • 26
fuz
  • 76,641
  • 24
  • 165
  • 316
1

For example (if you need to load a unsigned integer from an 8 bit value in ECX's register), you can write:

MovZX ECX,byte ptr[EAX]
Test  CL,080H
JZ    short loc_401073

In this case, instead of the jump instruction, if you need to get the sign of the input, you can write:

SetNZ AL  ; AL's register contains 1 if sign else contains 0

If you want to extend the sign of input value, you can write:

MovSX ECX,byte ptr[EAX]
Or    ECX,ECX
JNS   short loc_401073

In this case, instead of the jump instruction, if you need to get the sign of the input, you can write:

SetS  AL  ; AL's register contains 1 if sign else contains 0
Paolo Fassin
  • 315
  • 1
  • 10
  • `Or ECX,ECX` is worse than `test ecx,ecx`: It can't macro-fuse with `jcc` (but test/jns can fuse on AMD at least). It also writes a register so it uses up a physical register (for register-renaming). It also introduces an extra cycle of latency in the dep chain for anything that reads `ecx`. https://stackoverflow.com/a/33724806/224132 – Peter Cordes Oct 17 '17 at 04:23
  • I have read the documentation on alternatives to CMP ECX, 0, as OR ECX, ECX and you are right: it is much better TEST ECX, ECX; by now I will use that. – Paolo Fassin Oct 18 '17 at 21:04