Question

Determining the sign bit of adder alu

Asked by: Newt6398

Hi all,

I am designing a simple 8 bit processor for a uni assignment. I have a full adder which I want to perform 2s complement adition/subtraction on. I (kinda) understand what's going on. However after searching around for ages I don't understand how I determine what the sign bit should be after a calculation.

For instances:

  11111101b  -3d
+00000010b  +2d
 
When the two words are added/subtracted how do i determine the sign bit???

I've attached a file with my adder so far, I've been trying to figure this out with just three bits to try and understand it better.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-12-09 at 06:32:46ID23969103
Tags

Simple processor design

Topics

Computer CPU Processors

,

Processor Programming for Specific Platforms

,

Assembly Programming Language

Participating Experts
2
Points
250
Comments
17

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Age calculation
    Hi, I have a method that calculates a person's current age in years (which I now have rewritten). Now I'd like to calculate the age in years, months and days as well. On the web I found a Javascript method that did this, but I found that it didn't calculate correctly every ti...
  2. how to calculate an age ?
    Let's say i've got a birthdate in a variable called $bdate: for example: 31-08-1975. How can I calculate the current age ?

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: DropZonePosted on 2008-12-09 at 10:37:26ID: 23131867

If you are using 2's compliment, then the left most bit of the result is the sign bit:  1=negative; 0=positive.

If the sign-bit is set, you know you have a 2's compliment of a negative number.  This is how binary arithmetic works.

Here's some reference on binary arithmetic:
     http://academic.evergreen.edu/projects/biophysics/technotes/program/2s_comp.htm

       -dZ.

 

by: Newt6398Posted on 2008-12-09 at 11:02:29ID: 23132207

If the sum of both inputs is large and has an overflow then wouldn't it be possible that my sign bit could change although it should be the opposite of what it's changed to. And then showing a positive when it should be a negative. If that makes any sense. Or have I just not followed this through fully?

Thanks for the reply

 

by: DropZonePosted on 2008-12-09 at 11:10:19ID: 23132300

There certainly can be an overflow.  If this happens, then the sign bit won't reflect the sign, and you won't have a way of knowing how many bits were "overflowed" and were lost.  For this reason, when an overflow is detected on an arithmetic operation, the result is bogus and an error condition is raised.

    Am I missing the point of your question?
     -dZ.

 

by: Newt6398Posted on 2008-12-09 at 11:36:39ID: 23132534

No that's right, so how do I get around this.

What I want to do is: in my language I have two different instructions for adding or subtracting signed and unsigned words. And so:

If it is a signed calculation then I want to perform the calculation on the mantissa [First 7 bits] and set the sign bit to what it should be. Negative or positive depending on the calculation.

The only thing I have come across in relation to this is a statement saying that the "two words need to be compared in order to determine the sign bit!. But the site I saw that (berkeley.edu) had no further explanation or logic gate examples.

 

by: DropZonePosted on 2008-12-09 at 11:50:10ID: 23132682

Hum, I must be missing something.  It seems to me that you want to retain the sign bit regardless of an overflow.  Why is this?

To avoid overflow, you just need higher resolution (more bits), or emulate it by splitting the inputs into smaller chunks and adding the parts in series, feeding the carry of each part to the next one.

Can you offer a link to the document to which you refer so that I may further understand what you are trying to do?

     -dZ.

 

by: Newt6398Posted on 2008-12-09 at 12:47:41ID: 23133502

Here's the link: http://inst.eecs.berkeley.edu/~cs150/fa05/Lectures/23-ArithI.ppt#261,6,Number Systems

Although it doesn't say a whole lot.

Attached is an image, basically three standard adders strung together with an XOR on the B input which allows for addition and subtraction. As you can see in the image it isn't working.

But from everything I read this is just a standard setup for performing addition / subtraction.

So i need to add the two Words together determining if the result is a negative or positive number somehow and set the MSB (Sign bit) to the relevant bit for a negative or positive result. Also if the penultimate bit has a carry then if performing signed calculation this carry should be spat out wherever (Overflow register), obviously this will need some selection mechanism to determine that we are calculating a signed number and if so not to perform calculation on the sign bit and to take the overflow from the penultimate bit instead.

It's making sense in my head it's just explaining it that i'm not too good at, my apologies.



 

by: DropZonePosted on 2008-12-09 at 13:38:05ID: 23134033

Perhaps your circuit design is faulty.  I'm a bit rusty on logic circuits, but I thought that a full adder should look like this:

http://en.wikipedia.org/wiki/Image:Full-adder.svg

 

by: DropZonePosted on 2008-12-09 at 13:42:25ID: 23134073

(Sorry, I submitted before I finished the comment.)

That image I sent was a quick composite of 4 full adders I just made, and it's missing the XOR gate to allow for addition and subtraction.

    -dZ.

 

by: Newt6398Posted on 2008-12-09 at 16:04:40ID: 23135151

Silly mistakes hey, thanks for pointing that out I wouldn't have gotten anywhere otherwise.

Ok so I have modified my design, am I going to have the same issues with the sign bit and overflow??? Can you envisage any problems with this. Obviously it is going to be an 8 bit adder not the three bits i'm showing at the moment.

This is always going to perform a twos complement isn't it, I need it to only perform the two's complement on a selection bases. If i'm using signed words constantly the highest I can go is -128 to 127 without the sign I can add up to 255 with my 8 bits. Just need some pointers to help me do this as I don't know where to begin.

 

by: Newt6398Posted on 2008-12-09 at 17:37:06ID: 23135606

If i put an AND gate on the penultimate overflow to the xor gate and use that as my overflow selection mechanism. Then i should be able to happily add two full 8 bits together without there being a sign bit???
That seems to be right.

But if I do a subtraction on two unsigned words and that subtraction is a negative and doesn't overflow the first 7 bits than somehow I'd want to detect that and set the sign bit and possibly a "sign register" to indicate the two unsigned words have a negative subtraction value and so have been converted to a signed result.

 

by: BrianGEFF719Posted on 2008-12-10 at 02:36:54ID: 23137648

Remember that a full adder is combinational logic and you can always revert to a truth table to build and correct your logic, just build a truth table and a few karnaugh maps and the correct logic will reveal its self.

 

by: DropZonePosted on 2008-12-10 at 03:34:28ID: 23137926

If you are going to perform subtraction (or addition of negative numbers), you always need the sign bit, which means that you will be using "signed integers".  This will always limit your range, because you have one less bit to use.  If you want to allow for a full 8-bit ALU, you need to then use 9 bits (you need one for the sign).

I still do not understand what your problem is and why the concern about the overflow and the sign bit.  The risk of overflow will *always* be there.  This is true for any number of bits (if you add the maximum number of bits, the result will never fit).  When the Carry-Out bit of the last Full Adder is different than its Carry-In bit, an overflow occurs, it indicates an error condition.  This is encoded in any programming language that I know (including Assembly Language, where you always check the Overflow flag after operations, unless you can be sure that the values are small enough to fit).

To detect overflow (and thus, add an "Overflow Flag" to your circuit), you can just add an XOR gate at the end of the circuit whose inputs will be the Carry-In and Carry-Out of bits of the MSB Full adder.

Here's more information on this, including mathematical explanations:
http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Comb/overflow.html

Is one of your requirements to overcome overflows and try to assess the answer from the register?

     -dZ.

 

by: DropZonePosted on 2008-12-10 at 03:42:51ID: 23137964

I forgot to say something.  The reason that Overflow is detected and flagged, but not overcome, is because the Overflow condition means that the result cannot be represented in the available register, and thus, cannot be used by the machine.

For example, if you build an 8-bit computer and the addition of two 8-bit values results in an overflow, even if you were able to extract what the correct value is, what are you going to do with it? It doesn't fit in any of your registers; it cannot be stored in 8-bit memory, and cannot be transfered via 8-bit busses.  Thus, it is an invalid value for your machine.

This means, as you said before, that an 8-bit machine that operates on signed values, can only use values from -127 to +127 (because it needs the eighth bit for the sign), and if values are of the same sign, it may overflow.  So the effective range of the operands of addition is smaller than that.

If you want your machine to operate on 8-bit values (excluding sign), then you need to use 9-bit registers and Adders.  The result, still, cannot be equal to or larger than the sum of two values of the maximum magnitud, of the same sign.

    -dZ.

 

by: Newt6398Posted on 2008-12-10 at 14:50:02ID: 23144122

Detecting the overflow wasn't a requirement. My lecturer said you may want to use different mnemonics in your language for adding signed or unsigned numbers. So my language would have::

add               a,b
addsigned    a,-b
sub               a,b
subsigned    -a,-b
etc.

Now if I add two unsigned numbers and they're too large then I get overflow error, that's fine.
Adding two signed numbers works fine if it overflows then i get an error, fine.
Subtracting two signed numbers could be greater than 128 and an overflow occurs, that's fine.

So the logic circuit allows you to choose to add/subtract signed or unsigned numbers which I have done and from going through it it seems to work (I need to test it a bit more as I think the overflow has issues).

My only issue now is subtracting of two unsigned positive numbers. ie:

sub   2,8   (00000010 - 00001000)

Should produce -6, well that's what i want it to do. So i need to know if this happens and as long as the number isn't greater than -128 to detect it and set the sign bit to 1 and a register to indicate the result is a signed number.

However

sub  8,2  (00001000 - 00000010)

Should simply be 6 which it is.

 

by: DropZonePosted on 2008-12-11 at 03:29:09ID: 23147303

To subtract, all you need to do is take the Two's Compliment of the second operand, and add.  This will work whether it is positive or negative.

sub   2, 8
    00000010 =>    00000010  =  2
  - 00001000 =>  +11111000  = -8
                          ---------------
                           11111010  = -6  Carry = 0, Overflow = 0

sub 8, 2
    00001000 =>    00001000  =  8
   -00000010 =>  +11111110  = -2
                         ----------------
                        1 00000110  =  6  Carry = 1, Overflow = 0

In the second case, there is a Carry-Out value on the MSB, but since it is the same as the Carry-In of the MSB adder, then it does not signify an overflow, so it can be safely discarded.

Signed and unsigned arithmetic should not require different circuitry; the bits are moved exactly the same for both.  The only difference is their contextual meaning: an unsigned number uses the MSB as part of the value, while a signed number uses it as the sign bit.  Any operation should not care about this at all.  The only time you care about this is when you are taking input or providing output.

     -dZ.

 

by: DropZonePosted on 2008-12-11 at 03:30:13ID: 23147309

As I mentioned before, adding an XOR with inputs set to the Carry-In and Carry-Out of the MSB adder should serve as an Overflow flag.

    -dZ.

 

by: Newt6398Posted on 2008-12-12 at 08:42:53ID: 31524177

Thanks for all your help Dz, much appreciated

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...