---
title: "Sum of digits processor"
subject: "Embedded System"
chapter: 2
key: "semester_6/embedded_system:spp-sum-of-digits"
source: https://www.bctengineer.com/q/semester_6/embedded_system/spp-sum-of-digits
site: BCT Engineering
---

# Sum of digits processor

Embedded System · Chapter 2: Hardware Design Issues

- Asked in: 2076 Baisakh, 8 marks
- Video: [Designing Single Purpose Processor To Calculate GCD (HCF).](https://www.youtube.com/watch?v=Wy344fMRnOo) — AFlick
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/embedded_system/spp-sum-of-digits
- Whole chapter: https://www.bctengineer.com/md/semester_6/embedded_system/chapter-2

## Question

Design a single purpose processor to determine the sum of digits of an integer. Start the design from the function computing the desired result, FSMD, datapath and controller.

## Short answer

Design uses repeated subtraction to extract digits: while N>0, set R=N, Q=0, subtract 10 from R until R<10 incrementing Q; add R to sum, set N=Q. Datapath has registers N,R,Q,SUM, subtractor, incrementer, adder, comparators, and muxes. Controller is a 6-state FSM. For n=345, sum is 12.

## Answer

**Assumption:** n is an unsigned 16-bit integer (0 to 65535). The decimal digits are extracted with "mod 10" and "div 10", implemented by repeated subtraction (there is no hardware divider).

#### Step 1: Function
```text
 SumDigits(n):
   sum = 0
   while (n > 0):
       q = 0; r = n
       while (r >= 10): r = r - 10; q = q + 1    // q = n div 10, r = n mod 10
       sum = sum + r
       n = q
   output sum
```

#### Step 2: FSMD
| State | Condition | Actions | Next |
|---|---|---|---|
| S0 wait | start = 0 | done = 0 | S0 |
| S0 | start = 1 | N = n, SUM = 0 | S1 |
| S1 test | N > 0 | none | S2 |
| S1 | N = 0 | none | S5 |
| S2 init | always | R = N, Q = 0 | S3 |
| S3 divide | R >= 10 | R = R - 10, Q = Q + 1 | S3 |
| S3 | R < 10 | none | S4 |
| S4 accumulate | always | SUM = SUM + R, N = Q | S1 |
| S5 output | always | out = SUM, done = 1 | S0 |

```text
 S0 -> S1 -(N>0)-> S2 -> S3 -(R>=10, loop)-> S3
        |                 |(R<10)
       (N=0)              v
        v               S4 -> S1
       S5 -> S0
```

#### Step 3: Datapath
```text
 n -> [N] <- mux(n, Q) ;  [N > 0] comparator -> gtz
 [R] <- mux(N, R-10) <- subtractor(R, const 10) ; [R >= 10] -> ge10
 [Q] <- mux(0, Q+1) <- incrementer
 [SUM] <- mux(0, SUM+R) <- adder
 SUM -> out
```
Components: registers N, R, Q, SUM; a subtractor (R - 10), an incrementer, an adder, comparators (N > 0, R >= 10), and muxes for N, R, Q and SUM.

#### Controller
Six-state FSM, 3-bit state register. Inputs: start, gtz, ge10. Outputs: N_ld, R_ld/R_sel, Q_clr/Q_inc, SUM_clr/SUM_ld, done.
```text
 start, status flags --> +-------------+   +-----------+
 (from datapath)         | Next-state  |-->| State     |
                         | logic       |   | register  |
                         +------^------+   +-----+-----+
                                |   current state |
                                +-----------------+
                                                  v
                                           +-------------+
                                           | Output logic|--> control
                                           +-------------+    signals
```

**Trace n = 345:** (Q, R) = (34, 5): sum 5; n = 34 -> (3, 4): sum 9; n = 3 -> (0, 3): sum 12; n = 0 -> output **12**.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How are digits extracted without a divider?
   A. Multiply n by 10
   B. Shift n by 4 bits
   C. Use a lookup of all numbers
   D. Repeated subtraction of 10 gives q = n div 10 and r = n mod 10
   Answer: D. R is reduced by 10 while counting Q; R is the last digit and Q the rest. (Key point: Digit extraction)

2. What does S4 do?
   A. SUM = SUM + R and N = Q
   B. Subtract 10 from R
   C. Output SUM
   D. Clear N
   Answer: A. The extracted digit is added and the remaining number becomes N. (Key point: Accumulate step)

3. When does the processor finish?
   A. When R >= 10
   B. When N = 0
   C. When SUM = 0
   D. When Q = 1
   Answer: B. N = 0 means no digits remain, so S1 goes to the output state S5. (Key point: Stopping)

4. What is the result for n = 345?
   A. 5
   B. 345
   C. 12
   D. 9
   Answer: C. Digits 5, 4, 3 sum to 12. (Key point: Trace)

## Related in Embedded System

- [Single-purpose processor for power](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Aspp-power) — Embedded System, chapter 2, asked 2×
- [Factorial single-purpose processor](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Aspp-factorial) — Embedded System, chapter 2
- [Fibonacci single-purpose processor](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Aspp-fibonacci) — Embedded System, chapter 2, asked 2×
- [Design LCM single-purpose processor](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Aspp-lcm-datapath-controller) — Embedded System, chapter 2
- [GPP operation: datapath and controller](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Agpp-datapath-controller) — Embedded System, chapter 2, asked 4×
- [Prime number checking processor](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Aspp-prime-check) — Embedded System, chapter 2

## Related in other subjects

- [Sum of digits until single digit](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adigit-sum-single-digit) — Computer Programming, chapter 5, asked 2×
- [Sum of digits using recursion](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asum-digits-recursive) — Computer Programming, chapter 6
