mov eax,-5

why it is -5 and not 11111111111111111111111111111100 b <===Very huge number.

if i load -5 or 11111111111111111111111111111100 b how the program know what i wont to have in eax.

how can i make |-5| = 5 if i do not know that ^:eek:
Posted on 2002-11-28 14:40:04 by drakoforma
answer:

when u refer to -5, its really a very long binary number as u pointed out.
ITS THE SAME NUMBER !!!
You can put it in binary, hex, decimal or any other number system u want, its the Same Number. k?
Posted on 2002-11-28 16:25:24 by Homer

how can i make |-5| = 5 if i do not know that ^:eek:

Absolute:
mov	eax, [number]

mov ecx, [number]
sar ecx, 31
xor eax, ecx
sub eax, ecx
Posted on 2002-11-28 16:41:55 by comrade
not eax
add eax 001b ; || or was it >> inc eax ?


?

Just guessing here :) I did that once for an trackbar...
Posted on 2002-11-28 16:52:22 by JimmyClif
Or just:

NEG EAX

:)
Posted on 2002-11-28 18:15:27 by S/390


xor eax,eax
mov ecx, [number]
cmp eax,ecx
ja out
neg ecx
out:
Posted on 2002-11-28 19:01:07 by roticv
Weird... somehow I thought neg didn't change the sign if the op was already negative...

Well, it does.. Good to know :stupid:
Posted on 2002-11-28 19:46:43 by JimmyClif
[size=12]   mov eax, [number]

cdq
add eax, edx
xor eax, edx[/size]
Posted on 2002-11-28 20:11:36 by iblis
Yup, NEG is short for NEGate. Makes positive numbers negative, and negative numbers positive, by subtracting from 0. :)
Posted on 2002-11-28 20:56:25 by S/390



xor eax,eax
mov ecx, [number]
cmp eax,ecx
ja out
neg ecx
out:

perhaps "out" might not be the best label name... might confuse some assemblers!
Posted on 2002-11-28 22:15:52 by jademtech
My mistake, I could not think of any better label at that point of time :grin:
Posted on 2002-11-28 22:40:44 by roticv
Hello all.....

Let me clear you all about negative number storage.

suppose you want to store -5 , then what processor do it calculate it's 2's complement and then store it or do any operation after that.
By saying 2's complement I mean.

to represent -5 we do.

5 ===> 0000 0000 0000 0000 0000 0000 0000 0101 ( this is 5 )
1111 1111 1111 1111 1111 1111 1111 1010 ( after complement )
-5 ==> 1111 1111 1111 1111 1111 1111 1111 1011 ( add 1 )

so if you write
mov eax, 11111111111111111111111111111011b

you will this instruction at run time as in any debugger ( like SoftIce )

mov eax, FFFFFFFB <===== that is -5

hope this help you

cya
Posted on 2002-11-30 03:54:33 by processingspeed
i've seen a snipplet to get the absolute of a number:



mov eax,[number]
@@:
neg eax
js @b ; if the sign flag is set after negating, negate again, cuz it WAS already a positive number
Posted on 2002-11-30 04:45:06 by hartyl
That snippet has hiden bug. If eax = 80000000h it goes to endless loop!!!
Use jl instead of js.
abs MACRO r:REQ

@@: neg r
jl @B
ENDM

If you have to compare signed value you can do this:
.if SDWORD PTR eax < 0

; do something
.endif
or
assume eax:SDWORD

.if eax < 0
; do something
.endif
assume eax:nothing

If you don't do this masm will treat value in eax as unsigned.
So say not -5, but 0FFFFFFFBh.
And 0FFFFFFFBh is > 0.
Posted on 2002-11-30 07:24:22 by Four-F

mov eax,-5

why it is -5 and not 11111111111111111111111111111100 b <===Very huge number.

if i load -5 or 11111111111111111111111111111100 b how the program know what i wont to have in eax.

how can i make |-5| = 5 if i do not know that ^:eek:

There is no short answer but I give you some hints to
understand this your problem and many others simular to
this.
(In details I'm going to discuss it in "Position numeric system for lowlevel programmers")
Keep in mind that
1. Any numeric format exist not just a rules how to represent value in this
format but also by rules how this representation is used in math operations.
For example if you studied in scool how to divide or multiply numbers in
decimal numeric system using just pen and paper you learnt those algos
only for this system and can not apply them to make calculation with Romen
numeric system (format)
2. Doing math in computer you deal not only with value of operand, but
also in discretion of those values, in other words each operand has size
(particular number of digits (bits) it can have)
For better understanding the following info let's call it not "size" but
limits.
For example byte is limited by 0000 0000 to 1111 1111.
In any positioning numeric system base x (radix x)
number of digits can tell us
1. Number of values that can be represented in this number of digits
This number of values = redix in power of number of digits
for example in binary system radix = 2
number of digits in byte = 8
so that byte can have 2^8=256 different values (from 0 to 255)
2. Max value = (radix in power of number of digits)-1
in byte
2^8 - 1 = 255.

Example: in 3 bit operand
number of values = 2^3=8
max number = 2^3-1=7

Now important note to understand the whole thing of
negative number representaion in integers in computer:

What if number of digits in result of operation exceed the upper
limit?
Let's say we add to bytes and result we write in a byte also:
1111 1111
+
0000 0001
------------------
10000 0000

Result has 9 digits but operant of result limited to 8 digits.
So it will have only lower 8 digits and they represent 0, not 10000 0000b

Now we store this effect in our mind and go to next thing
important for our understanding:

If we have 7 bit operand the number of values in can represent = 2^7 = 128
If 8 bit operand number of values = 2^8 = 256.

Suppose we need to devide all values in 8 bit operand in two groups, each group having
the same number of values.
2^8=256/2 + 256/2 = 128 + 128 = 2^7+2^7 = 2 (2^7)


You can see that if we need to represent only one of the group
we need just 7 bits cause number of values in each group = 2^7
But we have 2 groups and we also have one extra bit.
It can have only 2 values 1 or 0. That means that number of
values of bit is equal to numbers of our groups.
Let us use this extra bit to destinct whether value
belongs to group 1 or group 2.
We use upper bit in 8 bit operand for this destinction.
So values from 0 000 0000 to 0 111 1111 belong to first group
And those from 1 000 0000 to 1 111 1111 belong to second group.
As you can see both groups freely use low 7 bits and have that for
the same number of values.

Now let's try to find a way to use first group (with upper bit = 0) as
positive numbers and second group (with upper bit = 1) as negative.
(0 this way is finding its place in positive numbers though it is
nor positive neither negative.)
Then we can call this upper bit (bit[7] in byte in zero based enumaration)
sign bit, and if this bit is =1 then we have negative number, if = 0 - positive.

But its not enough just to separate 256 possible values by some rules -
those rules must allow up to find some algos to do math operations
and have right results.

Now we close to end of this introduction, and possible understanding
of positive\negative int values representation in computers.
(So to called "complimentary 2 code")

Let's up recall two things
1. All 4 math operation can be represented as just one - addition
a+b=a+b
a-b =a+(-b)
a*b=a+a+a.. b times or b+b+b... a times
a/b=times of a-b untill reminder < b = times a+(-b) untill reminder < b

So if we can find way how find corresponence between representation of positive x in first
group and negative x in second group so that result of addition will produce right result
we can use the system.
2. x + (-x) =0

Now remember example when add 1111 1111 + 0000 0001 and get in used 8 bits
of result 0000 0000 ? Cause upper bit was out of size of our operand?

Then if corresponding to positive 0000 0001 we assign as -1 1111 1111 we've got
the right answer for x+(-x)=0 in bits that is used in 8 bits operands.
Let's check this "-1" about addition of other positive values:
-1+2=1
1111 1111 + 0000 0010 = 1 | 0000 0001 (in meaningfull low 8 bits = 0000 0001 = 1)
-1+5=4
1111 1111 + 0000 0101 = 1 | 0000 0100 (in meaningfull low 8 bits = 0000 0100 = 4)

So what binary number would be for -2? -120? etc.
We added to 0000 0001 number 1111 1111 and got 1 0000 0000.
So for any x from positive group (0000 0000 to 0111 1111) 0??? ????
as -x will serve binary number so that
0??? ???? + 1??? ???? = 1 0000 0000
For example positive 5 = 0000 0101
negative 5 = 1 0000 0000 - 0000 0101 = 11111011b
and low 8 bits in 1111 1011b+0000 0101 = 1| 0000 0000 = 0

So that we can have in byte 128 values from -1 to -128 with sign bit.
And 128 values from 0 to 127 with sign bit = 0
In byte we don't have corresponding positive value for -128 as
well there is no - 0 too.

As I said before in computers there is no such things as
add some value to othervalue
there are always add two values of SPECIFIED SIZE.

What if our operand is 16 bits or 32 or xx.. bits?
The rule of format is the same.
All values of with high bit =1 are negative and high (let us now call it "sign") bit = 0 are
positive.
And corresponding values are found the same way, the only difference
is that in 32bits operands sum of them equal lowest 33 bit value
in 16 bits = lowest 17bits value
For example in 32 bit -1 = 11111111 11111111 11111111 11111111b
in 16bits 11111111 1111111b
How comp knows size? :)
It is in opcode. Opcode always specifys size of operands in operations.
If you write push -5 it pushes dword FFFFFFFBh
if you write mov al,-5 it mov FBh in al 'cause al is 8 bits operand.

Now if we use the same operator for both sign and unsign values
how we ensure right result?
FFh can be treated as -1 sign and 255 unsign byte value?

That's is what CF,SF and OF flags for.
CF = 1 if bits in result is 1 more then operand size.
SF = 1 if highest (sign bit) in result is 1
OF treat SF as another carry flag and if CARRY FROM SF to CF <> to SF
it is set to 1.
If you need explonations why - I'll try to write examples to understand.

Anyway I will post materials about the issue to the board in more details
and you can wait untill it happens.

There are actually 3 very important things for lowlevel programming
that don't have good explonation in current asm books:
1. Operations in positioning numeric system in details
2. Understanding machine code specifics
3. Machine code algorithm in details.

Most questions I read on the board would have never been asked
if somewere it were explained in details with examples and exersizes.
It usually was different in old books, but instead of expanding the practice
in good old books contemporary books mostly look like reference of
recepies books and don't teach understanding of the very important basics.

I've decided to fill the gap, but I can not be sure for now that I'll be capable
to carry the task.
Posted on 2002-11-30 08:30:48 by The Svin
Nice explanation :grin:
Posted on 2002-11-30 09:58:28 by roticv
mov al,-5 <=== for example but i can also dont know if it is -5 or sth else
mov ebx,eax

;ebx !=-5


What can i do with this problem if i do not know if the al was -5 or some positive number:confused:
Posted on 2002-12-01 12:34:00 by drakoforma
Originally posted by drakoforma
mov al,-5 <=== for example but i can also dont know if it is -5 or sth else
movsx eax, al ; conversion to 32-bit signed!
mov ebx,eax

; ebx =-5
Posted on 2002-12-01 12:45:53 by bitRAKE

mov al,-5 <=== for example but i can also dont know if it is -5 or sth else
mov ebx,eax

;ebx !=-5


What can i do with this problem if i do not know if the al was -5 or some positive number:confused:

I'm absolutly buffled by code you posted
and your questions :(
Where this code did come from?
Is it from your app or what?
al and eax have different operand size.
why if you need to add ebx and eax
you load -5 in al not in eax?
Anyway from your code there is absolutly
no way to predict what could be in eax after all
mov al,-5
equal to
mov al,0FBh
and after that we don't know what is in eax
if before mov al,0FBh we have in eax ???? ????h
then after mov al,0FBh we have in eax ???? ??FBh
but what is in upper 24 bits? - God knows.
Read my post again - I wrote several times there
in computers there is no ABSOLUTE number representation
each number in particular format is formated for size of operand.
For 8 bit operand (al) it is FBh for 32 bit format it is FFFFFFFBh.
Rules are explained in my post.
I repeat - I wrote it several times and yet you are giving examples
were you load -5 in 8 bit operand and waiting that 32 bit register
that in addition to bits in al has also more 24 bits will magically
to be converted to something.
If you are going to use value in 8 bit operand now with 32 bit operand
you need to convert it to 32 bit operand first.
I'll give you the same stupid example.
eax is 32 bit and have 4 bytes as digits.

Now let I have some number in deciamal -
I also as you in your example - have know idea what the
number is and what digits it has. So this number is ????
I put in last digit - 5. Guess what number is now?
???-5.
If you load in AL - 5 (FBh) and add it with 8(!!!!!!!!!!!!!!!!!!!!) bit operand
then you don't need to know anything, it will work correctly.
Numbers in computers represented depending on 3 things
1. Value
2. Format
3. Operand size.
Not only integer compliment code.
0.1 in float also formatted differently according to OPERAND SIZE.
Posted on 2002-12-01 16:47:56 by The Svin
Pretend al = some 8 bit signed number

[size=12]movsx ebx, al[/size]


Now ebx = al, except ebx is a signed 32 bit version of it.
Posted on 2002-12-01 17:44:23 by iblis