flat assembler
Message board for the users of flat assembler.

Index > Main > There is no negative value ??

Author
Thread Post new topic Reply to topic
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 08 Feb 2011, 19:28
Hello everyone! I was thinking about negative values but I'm on mystery now! When I'm trying to "move eax,-1" it moves 0xFFFFFFFF value that means there is no negative value ? When I'll start counting from 0 till 0xFFFFFFFF then it loops again ? I don't understand.. Can someone explain me ? (: I'm asking this cause I'm stuck about using NOT instruction. Thanks.
Post 08 Feb 2011, 19:28
View user's profile Send private message Reply with quote
b1528932



Joined: 21 May 2010
Posts: 287
b1528932 08 Feb 2011, 20:24
there are no negative o rpositive values... you have bits, and you can treat them as signed or unsigned values.

signed values take highest bit to represent sign, while unsigned use all of them.


addition and subtraction are done in a way to give equal result for signed and unsigned desire.

When you use add or sub, yo set flags.
if you want to treat result as unsigned value, you test for CF and ZF.
When you want use signed - ZF and OF.

CF is set when you go higher than 0xFFFF, or lower than 0x0000.
ZF is set if result is 0x0000.
OF is set when you add 2 positive values and get negative result (msb set), or when you subtract 2 negative values and get positive value (msb clear).


note that multiplication and division works diffrently, in order to perform signed operation, you have to NEG 2nd operand, and neg it again if msb1 xor msb2 = true.
Post 08 Feb 2011, 20:24
View user's profile Send private message Reply with quote
idle



Joined: 06 Jan 2011
Posts: 440
Location: Ukraine
idle 08 Feb 2011, 20:32
imagine a gas counter set to 0,
roll it 1 position backward -> 9999 ie ffff etc
roll it 1 position forward -> 0000 ie 0000 etc
Smile
Post 08 Feb 2011, 20:32
View user's profile Send private message Reply with quote
MinhHung



Joined: 10 Sep 2010
Posts: 51
Location: Viet Nam
MinhHung 08 Feb 2011, 22:21
-NUMber=not(number)+1. ex: not 1=not 0000 0000 0000 0000 0000 0000 0000 0001 =1111...1110. -1=1111...1111=FFFFFFFF. 1-1=0 and set cary flag.
Post 08 Feb 2011, 22:21
View user's profile Send private message Yahoo Messenger Reply with quote
MinhHung



Joined: 10 Sep 2010
Posts: 51
Location: Viet Nam
MinhHung 08 Feb 2011, 22:25
-NUMber=not(number)+1. ex: not 1=not 0000 0000 0000 0000 0000 0000 0000 0001 =1111...1110. -1=1111...1111=FFFFFFFF. 1-1=0 and set cary flag. first bit is a signed bit, 1 mean negative number. sory for two post my mobile phone 'mat mat' carzy

_________________
sorry for my english
Post 08 Feb 2011, 22:25
View user's profile Send private message Yahoo Messenger Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 09 Feb 2011, 10:20
Oh well I got it. I should study about flags.. Thank you (:
Post 09 Feb 2011, 10:20
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 10 Feb 2011, 07:51
1 more question, can you explain me whats difference between SIGNED and UNSIGNED numbers ? and what the point of using them with specify ?
Sorry for my English ):
Post 10 Feb 2011, 07:51
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20343
Location: In your JS exploiting you and your system
revolution 10 Feb 2011, 07:58
There is no difference between signed and unsigned numbers. It is just a representation convention for humans to use. CPUs don't care, it is all just binary. The only "special" circuits in a CPU for considering a number signed or unsigned are the overflow flag and the sign flag. Neither flag will affect the actual computation, it is up to the programmer to decide if they wish to use the flag to change the flow of subsequent code.
Post 10 Feb 2011, 07:58
View user's profile Send private message Visit poster's website Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 10 Feb 2011, 08:03
revolution
Hmm.. I got that but I saw many examples on C that people were using unsigned char blabla or unsigned int blabla.. Well, I'm stuck about whats difference between IMUL and MUL)) what's difference between them or DIV/IDIV and etc.. Smile
Post 10 Feb 2011, 08:03
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20343
Location: In your JS exploiting you and your system
revolution 10 Feb 2011, 08:14
For C, int and unsigned relate to this sentence: "Neither flag will affect the actual computation, it is up to the programmer to decide if they wish to use the flag to change the flow of subsequent code." C uses the type information to generate different code. For one type the C code will treat the binary number as signed and for other type C will treat the binary number as unsigned.

MUL/IMUL/DIV/IDIV etc. do as the manuals say they do, they treat the number as signed or not signed. If you want to treat your binary numbers as signed then use IMUL/IDIV, if you want to treat your numbers as unsigned then use MUL/DIV.
Post 10 Feb 2011, 08:14
View user's profile Send private message Visit poster's website Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 10 Feb 2011, 10:34
revolution
Thanks for replying mate. Can you write some example for me please ? Smile) Well, I don't understand what the point of changing flags or not. For example,
EAX:EDX=9, ECX=3
DIV ECX = 3
IDIV ECX = ?
Or what you mean ? Neutral
Post 10 Feb 2011, 10:34
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20343
Location: In your JS exploiting you and your system
revolution 10 Feb 2011, 10:46
AL=0x7f
AH=0x02
add ah,al ;sign flag=1, overflow flag =1, carry flag =0, ah =0x81

If al & ah are intended to represent signed then the overflow flag tells you if the result if bogus. Overflow flag tells you if the result is too large to fit into the destination register.

If al & ah are intended to represent unsigned then the carry flag tells you if the result if bogus. Carry flag tells you if the result is too large to fit into the destination register.

It is the same operation, add ah,al, but depending upon what your numbers are meant to be then you will check different flags to detect if the result is sane.
Post 10 Feb 2011, 10:46
View user's profile Send private message Visit poster's website Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 10 Feb 2011, 12:42
Hmm, I got it kinda! If it's 0xFFFFFFFF (unsigned) then when adding 1 it would be 0. Unsigned numbers are for hex numbers, I mean limited numbers from 0 to FFFFFFFF (32 bit) and signed means it's from -n to +n.. and 1 more question, is there any way to set both flags, carry and of at the same time ? Smile and if it is, how it would work ? + how to set values to unsigned or signed in asm ?
Post 10 Feb 2011, 12:42
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20343
Location: In your JS exploiting you and your system
revolution 10 Feb 2011, 12:51
Overflowz wrote:
... is there any way to set both flags, carry and of at the same time ? Smile and if it is, how it would work ?
Sure there is. ah=0x80, al=0x80, add ah,al.
Overflowz wrote:
+ how to set values to unsigned or signed in asm ?
You don't. They are just binary numbers to the assembler.
Post 10 Feb 2011, 12:51
View user's profile Send private message Visit poster's website Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 10 Feb 2011, 13:01
revolution
Thank you very much. I got it now!
Post 10 Feb 2011, 13:01
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.