flat assembler
Message board for the users of flat assembler.

 Index > Main > There is no negative value ??
Author
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.
08 Feb 2011, 19:28
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.
08 Feb 2011, 20:24
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
08 Feb 2011, 20:32
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.
08 Feb 2011, 22:21
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
08 Feb 2011, 22:25
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 (:
09 Feb 2011, 10:20
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 ):
10 Feb 2011, 07:51
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20299
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.
10 Feb 2011, 07:58
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..
10 Feb 2011, 08:03
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20299
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.
10 Feb 2011, 08:14
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 ? ) 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 ?
10 Feb 2011, 10:34
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20299
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.
10 Feb 2011, 10:46
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 ? and if it is, how it would work ? + how to set values to unsigned or signed in asm ?
10 Feb 2011, 12:42
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20299
revolution 10 Feb 2011, 12:51
Overflowz wrote:
... is there any way to set both flags, carry and of at the same time ? 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.
10 Feb 2011, 12:51
Overflowz

Joined: 03 Sep 2010
Posts: 1046
Overflowz 10 Feb 2011, 13:01
revolution
Thank you very much. I got it now!
10 Feb 2011, 13:01
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum