flat assembler
Message board for the users of flat assembler.
Index
> Main > There is no negative value ?? |
Author |
|
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 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 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 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 09 Feb 2011, 10:20
Oh well I got it. I should study about flags.. Thank you (:
|
|||
09 Feb 2011, 10:20 |
|
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 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 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 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 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 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 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 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 ? Overflowz wrote: + how to set values to unsigned or signed in asm ? |
|||
10 Feb 2011, 12:51 |
|
Overflowz 10 Feb 2011, 13:01
revolution
Thank you very much. I got it now! |
|||
10 Feb 2011, 13:01 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.