Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Please give reasons for your answer.

As I found, two additions are needed. This is because every number to be add needs to changed to binary. Then addition is done between two binary numbers where the XOR gate is used for addition. When a carry bit is required an AND gate is involved to add the carry bit to the next line. Therefore if more than 3 binary numbers are added together there can be more than one carry bit and the AND gate doesn't support that.

Am I right or I am confusing everything and nothing make sense?

Thank you for your time.

share|improve this question
    
Your question makes no sense to me. Are you talking about a hypothetical ALU which has 3 inputs and can add 3 numbers in 1 operation? If not, then your question makes no sense, ALU can not add three numbers... Are you perhaps confused about the existence of the intermediate result? It's just a number in a register, outside the ALU logic. –  hyde Aug 31 '13 at 7:47
1  
It's an A level Computing question. "How many ALU additions are needed to add three numbers ? Give reasons for your answer. [5 marks]" My answer is just what I searched on google and what I think the answer should be. –  user2695507 Aug 31 '13 at 7:55

1 Answer 1

The answer is "it depends," because the question is ambiguous as worded.

As you observed, you can add any two numbers (which I'll define as "unsigned and will fit in a CPU register") and get away with a single carry bit because that's as much as you need for adding the two largest possible values. In an 8-bit world, adding 0xFF and 0xFF will net you 0x1FE, a value larger than a register will hold, which is why CPUs have carry bits. Add an additional large value (0x1FE + 0xFF) and you're into 0x2FD, which requires a second bit of carry you don't have. This is where you quit fooling around with a single carry bit and use an entire register to count carries instead. This gives you the 16-bit number you need to hold the result.

Here's a bit of assembly for the 6502, an 8-bit CPU designed in the 1970s, that demonstrates how this is done. We're using two of its registers, X to hold the upper bits and A to hold the lower bits of the 16-bit result. We'll also go on the assumptions that we're writing code to add three numbers we know ahead of time and we're not doing anything fancy to save space.

        ; Preparation
        LDX  #0      ; Clear the high-order bits by loading zero into X.
        LDA  #11     ; Load first number directly into A, no add required.
        CLC          ; Clear the carry flag.

        ; Adding of second number
        ADC  #22     ; Add the second number to A, with carry.
        BCC  THIRD   ; If carry is clear, no overflow.  Branch to THIRD
        INX          ; Carry was set, increment the high-order bits.
        CLC          ; Leave the carry flag clear before the next addition

        ; Adding of third number
THIRD:  ADC  #33     ; Add the third number to A, with carry.
        BCC  THEEND  ; No carry.  We're done.
        INX          ; Carry was set.  Increment the high-order bits.

THEEND: RTS          ; Return with the 16-bit result in X and A.

If the constants (11, 22 and 33) can be any number, the worst case is that you execute two ADC and two INX instructions for a total of four uses of the ALU. Best case is no carries, which means only two additions. You'll notice that there are only two additions and not three, because the first is always added to zero and can therefore be loaded directly into a register without using the ALU.

In a more general-purpose routine, you'd start by zeroing out your accumulated 16-bit number and having to use the ALU once (no carry) or twice (carry) per addition. The worst case for adding any three numbers becomes six and the best case is three.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.