flat assembler
Message board for the users of flat assembler.

Index > Main > what is twos complement

Author
Thread Post new topic Reply to topic
b1528932



Joined: 21 May 2010
Posts: 287
b1528932
Here and there i keep finding this term and i have no clue what it means.
Maybe its something really simple, but i cant imagine idea behind it.



Question 2, how do i implement signed multiplication/division? My current way is to NEG negative operands, do normal multiplication/division and then xor original signs. If xor is 1 neg result, if xor is 0, leave result. In case division also do it to reminder.
Post 01 Feb 2011, 21:59
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler
1:
The two's complement of a binary number is defined as the value obtained by subtracting the number from a large power of two (specifically, from 2N for an N-bit two's complement)


I don't really understand it the way they explain it. I see it as the negative is denoted by extending 1s instead of the implied 0s. Like, 2=00000010 (notice the 6 implied 0s), 8bit -2 = 11111110 (notice that those 0s were replaced with 1s.

2: imul and idiv?
Post 01 Feb 2011, 22:41
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17270
Location: In your JS exploiting you and your system
revolution
One's compliment: not eax
Two's compliment: neg eax
Post 01 Feb 2011, 22:55
View user's profile Send private message Visit poster's website Reply with quote
b1528932



Joined: 21 May 2010
Posts: 287
b1528932
1. so this compliment is way of representing negative and positive data or what.

neg eax is transformation like -6 <=> 6, not eax is -6 <=> 5. and what has to do with compliment, whats compliment, i dont understand that word maybe in that context. There is instruction btc, wich flips a bit and set cf to old value. Is that it? compliment = flip all bits, and in ones compliment you get negative value, while in twos you get negative - 1?


2. And what of data that doesnt fit into register? IMHO x86 ALU sucks, because it should support arithmetics on non-fixed length data. It should implement hardware operations as it do now, but simply over not fixed ammount of bytes. For example, it should take pointer to memory wich would contain pointer to data and their length. And even it could allow for more complex operations than basic 2. Whatever, why not additional chip designed just to do math, and not implement anything arithmetic on x86. We could go 1 step ahead, and strip x86 from logical functions as well. 1 chip pure io, 1 logic, 1 arithmetic.
Post 02 Feb 2011, 00:12
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17270
Location: In your JS exploiting you and your system
revolution
b1528932 wrote:
compliment?
Just another way of saying opposite.
b1528932 wrote:
Whatever, why not additional chip designed just to do math, and not implement anything arithmetic on x86. We could go 1 step ahead, and strip x86 from logical functions as well. 1 chip pure io, 1 logic, 1 arithmetic.
You want to go backwards? We had that with 8087s and the like. Going off chip is slow, better to have everything integrated as much as possible.
Post 02 Feb 2011, 00:23
View user's profile Send private message Visit poster's website Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler
1 Yes, positive and negative. Positive in twos complement is just like non-twos-compliment, just you can't use the most significant bit.

2. That's too complex for circuitry. Just do it in software. I realize it's a pain to try to code such things in asm, so use an HLL.
Post 02 Feb 2011, 00:37
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4237
Location: 2018
edfed
neg = not +1

a CPU is a Central Processing Unit, and is designed to compute things.
then, X86, as a CPU family, is able to compute things, in one single chip. non need to separate chips to do what can do a single 20mm² chip.
Post 02 Feb 2011, 00:38
View user's profile Send private message Visit poster's website Reply with quote
b1528932



Joined: 21 May 2010
Posts: 287
b1528932
1. thank you now i finally get it. Initialy i though it have something to do with ammount of states 1 bit can have.

2. No its not that hard if i use straightforward method, but why not make cpu do that for me? It would be however impossible in HLL, carry/borrow in C? Good joke. Signed division? Good luck with that.
Post 02 Feb 2011, 00:41
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17270
Location: In your JS exploiting you and your system
revolution
b1528932 wrote:
2. No its not that hard if i use straightforward method, but why not make cpu do that for me?
Use an FPGA.
Post 02 Feb 2011, 00:43
View user's profile Send private message Visit poster's website Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler
Actually, 64 bit integers make it pretty easy to carry. Not so efficient, but I don't care. If you want efficiency, use GMP. They have custom asm routines for pretty much everything.
Code:
// adds a + b and returns an array of ints, in least to most significant order.
void add(uint32_t a, uint32_t b, uint32_t buf)
{
     buf[0] = (uint64_t)a + b & 0xffffffff; // least significant dword
     buf[1] = ((uint64_t)a + b) >> 32; // put overflow into second int
}
    
Post 02 Feb 2011, 01:22
View user's profile Send private message Reply with quote
b1528932



Joined: 21 May 2010
Posts: 287
b1528932
Now try 4096 bit mod 512 bit.
Post 02 Feb 2011, 02:21
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler
The concept scales. I'm not going to waste my time proving what has already been proven. I only do that in math. Yet again, I'm going to defer to GMP. The have done it, so any statement saying it can't be done is false by default.
Post 02 Feb 2011, 03:01
View user's profile Send private message Reply with quote
b1528932



Joined: 21 May 2010
Posts: 287
b1528932
Ok maybe a little correction from me: When i ment 'impossible' i ment 'a huge waste of time, memory, and health'.
Post 02 Feb 2011, 03:13
View user's profile Send private message Reply with quote
idle



Joined: 06 Jan 2011
Posts: 359
Location: Ukraine
idle
Quote:
and then xor original signs. If xor is 1 neg result, if xor is 0, leave result. In case division also do it to reminder.

sign ADD sign does near same:
01 + 01 = 10 = '+'
01 + 00 = 01 = '-'

[quote=]...division...[/quote]
remember decimal div:
10 / 1, we have 10 subtract cycles max when dividend's and divisor's places differ by 1
100 / 1, 3 places versus 1 give 100 subtract cycles max
100 / 10, 3 places versus 2, give 10 max

for binary division difference in 1 place gives 2 sub cycles/iteration max:
10b / 01b
1010b / 111b
Post 02 Feb 2011, 06:05
View user's profile Send private message Reply with quote
idle



Joined: 06 Jan 2011
Posts: 359
Location: Ukraine
idle
myself wrote:

or binary division difference in 1 place gives 2 sub cycles/iteration max

2^places
Post 02 Feb 2011, 06:11
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-2020, Tomasz Grysztar.

Powered by rwasa.