It is known algorithm to get ABS(N):

@@: neg bVar

js @@

But must pay attention - if byte variable may be equal 80h (or 8000h, or 80000000h) this algo gives infinite cycle.

@@: neg bVar

js @@

But must pay attention - if byte variable may be equal 80h (or 8000h, or 80000000h) this algo gives infinite cycle.

IMHO, solutions to this problem are case specific due to there being no proper absolute value for the maximum negitive number. So, I'm assuming you just want to get rid of the infinite loop and the value is okay for your work.

NEG iVal

...same as:

DEC iVal

NOT iVal

...same as:

NOT iVal

INC iVal

...therefor,

mov eax, iVal

sar eax, 31

xor iVal, eax

sub iVal, eax

...this is a conditional NEG based on high bit of iVal.

NEG iVal

...same as:

DEC iVal

NOT iVal

...same as:

NOT iVal

INC iVal

...therefor,

mov eax, iVal

sar eax, 31

xor iVal, eax

sub iVal, eax

...this is a conditional NEG based on high bit of iVal.

>= 686

```
mov eax, iVal ;iVal is ....
```

mov ecx, eax

neg eax

cmovs eax, ecx