Chapter 1.2: Number Systems
Introduction
Learning Outcomes
Compute the value of decimal, binary and hexadecimal numbers from their digit weights.
Convert numbers between bases by division and by grouping.
Write, add and subtract negative numbers in two's complement.
Explain how BCD, IEEE 754, Gray code and byte order (endianness) look in memory.
Representing Numbers
Ways to Represent Numbers
- There are many ways to represent a numeric value.
- Long ago people counted with sticks; later they learned to draw the sticks on paper.
- So the number 5 was first written as
| | | | |. - In the Roman Empire
| | |still meant three sticks, while V came to stand for five and X for ten. - In the positional system we use today, a digit's value also depends on its position.
Representing Numbers
Roman Numerals
| Symbol | Value |
|---|---|
| I | 1 |
| V | 5 |
| X | 10 |
| L | 50 |
| C | 100 |
| D | 500 |
| M | 1000 |
- A number system used in ancient Rome.
- Built on addition and subtraction: a smaller symbol after a larger one is added (VI = 6), one before it is subtracted (IV = 4).
- Subtraction is used only in these pairs: IV, IX, XL, XC, CD, CM.
- There is no zero and no place value, so addition and multiplication are very tedious.
Representing Numbers · Step by step
Reading Roman Numerals
MCMLXXXVI
Split the number into value groups from left to right.
M = 1000.
CM: C comes before M, so it is subtracted → 1000 − 100 = 900.
LXXX = 50 + 10 + 10 + 10 = 80.
VI = 5 + 1 = 6.
Total 1000 + 900 + 80 + 6 = 1986.
MMVII
MMVI = 2006, MMVII = 1000 + 1000 + 7 = 2007.Positional Systems
The Decimal System
- Today most people count in decimal (base 10).
- The decimal system has 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
- These digits can represent any value, for example 754.
- A number's value is the sum of each digit times the base raised to the digit's position.
Positional Systems
Position Matters
- Each digit's position matters: move the 7 to the end and you get 547, a different value.
- Any number to the power zero is 1: x0 = 1. That is why the rightmost digit is the “ones” place in every base.
- 00 is undefined in mathematics; in number systems and polynomials it is taken as 1 by convention.
- Unlike Roman numerals, the same digit (7) is worth 700, 70 or 7 depending on its place, and an empty place needs a 0.
Positional Systems
The Binary System
- Electronic machines are easiest to build with two states: on/off, high/low voltage, 1/0.
- That is why computers use the binary system.
- Binary has 2 digits: 0 and 1; the base is 2.
- The digit weights are powers of 2: 1, 2, 4, 8, 16, 32, 64, 128 …
Positional Systems
Bit, Nibble, Byte, Word
| Unit | Bit | Unsigned range | On the 8086 |
|---|---|---|---|
| bit | 1 | 0 … 1 | flag |
| nibble | 4 | 0 … 15 (0h … Fh) | one hex digit, one BCD digit |
| byte | 8 | 0 … 255 | AL, BL …, DB |
| word | 16 | 0 … 65535 | AX, BX …, DW |
| doubleword | 32 | 0 … 4,294,967,295 | DX:AX, DD |
Each digit of a binary number is called a bit (binary digit). n bits can show 2n different values.
Positional Systems
Reading Binary Numbers
- In assembly, binary numbers usually end with the letter b:
101b= 5. - Example:
10100101bequals decimal 165.
Positional Systems · Step by step
Binary to Decimal: 1111011b
(1111011)2: 7 digits, weights 2⁰ … 2⁶.
Digit 0: 1 × 2⁰ = 1 → running sum 1.
Digit 1: 1 × 2¹ = 2 → running sum 3.
Digit 2: 0 × 2² = 0 → running sum 3.
Digit 3: 1 × 2³ = 8 → running sum 11.
Digit 4: 1 × 2⁴ = 16 → running sum 27.
Digit 5: 1 × 2⁵ = 32 → running sum 59.
Digit 6: 1 × 2⁶ = 64 → running sum 123.
Positional Systems
The Hexadecimal System
| base 10 | base 2 | base 16 |
|---|---|---|
| 0 | 0000 | 0 |
| 1 | 0001 | 1 |
| 2 | 0010 | 2 |
| 3 | 0011 | 3 |
| 4 | 0100 | 4 |
| 5 | 0101 | 5 |
| 6 | 0110 | 6 |
| 7 | 0111 | 7 |
| base 10 | base 2 | base 16 |
|---|---|---|
| 8 | 1000 | 8 |
| 9 | 1001 | 9 |
| 10 | 1010 | A |
| 11 | 1011 | B |
| 12 | 1100 | C |
| 13 | 1101 | D |
| 14 | 1110 | E |
| 15 | 1111 | F |
- The hexadecimal system uses 16 digits: 0–9 and A, B, C, D, E, F (10–15).
- One hex digit corresponds to exactly 4 bits (a nibble).
- So hex numbers are shorter than binary and easier to read: 1010 0101b = A5h.
Positional Systems
Writing Hex Numbers
- Hex numbers usually end with h:
5Fh= 5×16 + 15 = 95. - Numbers that start with a letter (A…F) get a leading 0:
0E120h. Otherwise the assembler takesE120hfor a variable or label name. - Example:
1234hequals decimal 4660.
| Notation | Meaning |
|---|---|
MOV AL, 0FFh | AL = 255 ✓ |
MOV AL, FFh | error: FFh is an undefined name |
MOV AX, 10 | decimal 10 (= 0Ah) — no suffix means decimal |
Positional Systems · Step by step
Hex to Decimal: A12Fh
(A12F)16: A = 10, F = 15. Weights 16⁰ = 1, 16¹ = 16, 16² = 256, 16³ = 4096.
F × 16⁰ = 15 × 1 = 15.
2 × 16¹ = 2 × 16 = 32 → 47.
1 × 16² = 1 × 256 = 256 → 303.
A × 16³ = 10 × 4096 = 40960 → 41263.
Positional Systems · Step by step
Binary ↔ Hex: Grouping by Four
Split the binary number into groups of 4 starting from the right (pad with 0s on the left if needed).
1010 = 8 + 2 = 10 → A.
1011 = 8 + 2 + 1 = 11 → B.
0101 = 4 + 1 = 5 → 5.
1100 = 8 + 4 = 12 → C. Result: 0AB5Ch.
- The reverse works the same way: each hex digit expands to 4 bits:
3Eh→ 0011 1110b. - Example:
1101011b→ 0110 1011 → 6Bh (a 0 was added to the left group). - That is why programmers always write binary data in hex: one byte = exactly 2 hex digits.
Positional Systems
Number Notation in Assembly
| Notation | Base | Value |
|---|---|---|
MOV AL, 65 | decimal (no suffix or d) | 65 |
MOV AL, 41h | hexadecimal | 65 |
MOV AL, 01000001b | binary | 65 |
MOV AL, 101o | octal (o or q) | 65 |
MOV AL, 'A' | ASCII character | 65 |
MOV AL, -1 | decimal, signed | FFh |
B0 41. The base is just a notation for people; in memory everything is binary.Base Conversion
Decimal to Other Bases
- Divide the decimal value by the target base.
- Each time, keep the quotient and take the remainder (the remainder is a digit, 0 … base−1).
- Keep dividing until the quotient is zero.
- Write the remainders last to first (bottom up) to get the result: the first remainder is the lowest digit.
Example: convert 39 to hex.
39 ÷ 16 = 2, remainder 7 (the ones digit).
2 ÷ 16 = 0, remainder 2. Quotient 0 → stop.
Remainders bottom up: 27h. Check: 2 × 16 + 7 = 39 ✓
Base Conversion · Step by step
43868 → Hex
How is 43868 written in hex?
43868 ÷ 16 = 2741, remainder 12 → C.
2741 ÷ 16 = 171, remainder 5.
171 ÷ 16 = 10, remainder 11 → B.
10 ÷ 16 = 0, remainder 10 → A.
Result 0AB5Ch (leading 0 because it starts with A). Check: 10·4096 + 11·256 + 5·16 + 12 = 43868 ✓
Base Conversion · Step through the code
Code Trace: Hex Digits with DIV
ORG 100h MOV AX, 2989 ; number MOV BX, 16 ; base MOV SI, 0 ; counter again: MOV DX, 0 ; DX:AX DIV BX ; quot, rem MOV rem[SI], DL INC SI CMP AX, 0 ; quotient 0? JNE again RET rem DB 4 DUP(0)
AX = 2989 = 0BADh. The hex digits are found by repeatedly dividing by 16.
BX = 16: the target base. With BX = 2 or 8 the same code would produce that base's digits.
SI = 0: where the next remainder goes in the rem array.
DX = 0. A 16-bit DIV BX divides the 32-bit DX:AX; without clearing DX the dividend would be wrong.
2989 ÷ 16 = 186, remainder 13: AX = 00BAh (quotient), DX = 000Dh (remainder). The first remainder is the lowest digit: D.
rem[0] ← DL = 0Dh.
SI = 1; quotient 186 ≠ 0 → ZF = 0, JNE goes back to again.
Pass 2: DX cleared, 186 ÷ 16 = 11, remainder 10 → A.
rem[1] ← 0Ah. Quotient 11 ≠ 0: the loop continues.
Pass 3: 11 ÷ 16 = 0, remainder 11 → B.
rem[2] ← 0Bh.
SI = 3. Quotient 0 → ZF = 1, JNE does not jump: the loop ends with 3 digits.
Memory holds the remainders 0D 0A 0B; read last to first they give 0BADh. Check: 11·256 + 10·16 + 13 = 2989 ✓
The same loop works in any base: the remainder is always between 0 and base−1, and it is one digit.
Base Conversion · Step by step
45 → Binary (Dividing by 2)
The same method works for binary: divide 45 by 2.
45 ÷ 2 = 22, remainder 1.
22 ÷ 2 = 11, remainder 0.
11 ÷ 2 = 5, remainder 1.
5 ÷ 2 = 2, remainder 1.
2 ÷ 2 = 1, remainder 0.
1 ÷ 2 = 0, remainder 1.
Result 101101b. Check: 32 + 8 + 4 + 1 = 45 ✓ Alternative: for a large number, convert to hex first (2Dh) and expand each digit to 4 bits; it is faster.
Base Conversion · Step through the code
Code Trace: The Remainders Come from CF
ORG 100h MOV AL, 45 ; dividend MOV CX, 8 ; 8 bits bit: SHR AL, 1 ; rem → CF RCR BL, 1 ; CF → BL LOOP bit RET
AL = 45 = 0010 1101b. Shifting right one bit divides by 2; the bit that falls out is the remainder.
CX = 8: one pass for each of the 8 bits.
SHR AL, 1: 45 ÷ 2 = 22 (AL = 16h); the bit that drops out, the remainder 1 → CF = 1.
RCR BL, 1: CF enters BL from the left (bit 7); the remainder is saved.
LOOP: CX = 7 ≠ 0 → back to the bit label.
Pass 2 (summary): 22 ÷ 2 = 11, remainder 0. The earlier remainders shift one place right in BL; CX = 6.
Pass 3 (summary): 11 ÷ 2 = 5, remainder 1. The earlier remainders shift one place right in BL; CX = 5.
Pass 4 (summary): 5 ÷ 2 = 2, remainder 1. The earlier remainders shift one place right in BL; CX = 4.
Pass 5 (summary): 2 ÷ 2 = 1, remainder 0. The earlier remainders shift one place right in BL; CX = 3.
Pass 6 (summary): 1 ÷ 2 = 0, remainder 1. The earlier remainders shift one place right in BL; CX = 2.
Pass 7 (summary): 0 ÷ 2 = 0, remainder 0. The earlier remainders shift one place right in BL; CX = 1. Leading zeros.
Pass 8 (summary): 0 ÷ 2 = 0, remainder 0. The earlier remainders shift one place right in BL; CX = 0. Leading zeros.
The first remainder reached bit 0 of BL, the last one bit 7: BL = 0010 1101b = 2Dh = 45. Reading the remainders last to first gives the binary form.
To divide by a power of 2, a shift is enough: SHR gives the quotient, CF the remainder. That is why binary digits are the remainders of dividing by 2.
Base Conversion · Step by step
The Fractional Part: Multiplying by 2
- Multiply the fractional part by 2; the integer part of the product (0 or 1) is one bit.
- Continue with the remaining fraction; stop when the fraction is 0 or you have enough bits.
- Convert the integer and fractional parts separately and join them: 25.625 = 11001b + 0.101b = 11001.101b.
Example: 0.625 → binary. Multiply the fractional part by 2 again and again.
0.625 × 2 = 1.25: integer part 1 → the first fraction bit (2−1). Continue with 0.25.
0.25 × 2 = 0.5: integer part 0 → the 2−2 bit.
0.5 × 2 = 1.0: integer part 1, fraction left 0 → stop.
Result 0.101b. Check: 1/2 + 1/8 = 0.5 + 0.125 = 0.625 ✓ The bits are read top down (the opposite of the division method).
Base Conversion · Step by step
0.1 → Binary: A Repeating Fraction
0.1 → binary: the same method.
0.1 × 2 = 0.2 → 0.
0.2 × 2 = 0.4 → 0.
0.4 × 2 = 0.8 → 0.
0.8 × 2 = 1.6 → 1, fraction 0.6.
0.6 × 2 = 1.2 → 1, fraction 0.2: we have seen this fraction before (row 2)!
0.2 is multiplied again: rows 2–5 repeat forever.
Result 0.1 = 0.0 0011 0011 0011…b: it does not terminate in binary. With 8 fraction bits, 0.00011001b = 0.09765625 is stored.
Fractions whose denominator is not a power of 2 (0.1 = 1/10) cannot be written exactly in binary; with a limited number of bits they are rounded. IEEE 754 also stores 0.1 approximately, which is why 0.1 + 0.2 ≠ 0.3 in many languages.
Base Conversion · Interactive
Base Converter
Signed Numbers
Signed Numbers
- Is a number like
0FFhpositive or negative? The bit pattern alone cannot tell you: it could be 255 or −1. - 8 bits give 256 different combinations (including zero).
- The first 128 combinations (0 … 127) stand for positive numbers, the next 128 (128 … 255) for negative numbers.
- To get −5, subtract 5 from the number of combinations (256): 256 − 5 = 251 = FBh.
- General rule (n bits): −x ↔ 2n − x. This representation is called two's complement.
Signed Numbers
The Number Circle
- All 4-bit patterns sit on a circle: moving clockwise means +1.
- Between 0111 (7) and 1000 the signed reading jumps from +7 to −8 → signed overflow (OF).
- Between 1111 and 0000 the unsigned reading jumps from 15 to 0 → carry (CF).
- Every negative number has its top bit (MSB) set to 1: the MSB is read as the sign bit.
- The add and subtract circuit is the same for both readings; only the flags tell the readings apart.
Signed Numbers
4-Bit Representations
| Binary | Un- signed | Sign- mag. | ones' compl. | two's compl. |
|---|---|---|---|---|
0000 | 0 | 0 | 0 | 0 |
0001 | 1 | 1 | 1 | 1 |
0010 | 2 | 2 | 2 | 2 |
0011 | 3 | 3 | 3 | 3 |
0100 | 4 | 4 | 4 | 4 |
0101 | 5 | 5 | 5 | 5 |
0110 | 6 | 6 | 6 | 6 |
0111 | 7 | 7 | 7 | 7 |
| Binary | Un- signed | Sign- mag. | ones' compl. | two's compl. |
|---|---|---|---|---|
1000 | 8 | −0 | −7 | −8 |
1001 | 9 | −1 | −6 | −7 |
1010 | 10 | −2 | −5 | −6 |
1011 | 11 | −3 | −4 | −5 |
1100 | 12 | −4 | −3 | −4 |
1101 | 13 | −5 | −2 | −3 |
1110 | 14 | −6 | −1 | −2 |
1111 | 15 | −7 | −0 | −1 |
Signed Numbers
The Ones' Complement Method
- Ones' complement: to negate a number, flip all its bits (1 → 0, 0 → 1).
- 8-bit example: 00101101 → 11010010.
- The catch: there are two zeros (00000000 = +0, 11111111 = −0), and after an addition the carry must be added back (end-around carry).
- On the 8086 the
NOTinstruction produces the ones' complement.
Start.
+45 = 0010 1101b.
All bits are flipped: 1101 0010b = D2h. In ones' complement this is −45; in two's complement it is −46!
Signed Numbers
The Two's Complement Method
- The standard way to represent negative numbers: flip all the bits, then add 1.
- On the 8086 the
NEGinstruction does this in one step (0 − x). - Shortcut: keep the bits up to and including the first 1 from the right; flip every bit to its left.
Start.
+45 = 0010 1101b.
All bits are flipped (NOT): 1101 0010b.
Add 1: 1101 0011b = D3h = 211 = 256 − 45 ✓ This is the 8-bit form of −45.
Check: D3h + 2Dh = 100h → 00h in 8 bits. The two's complement of a two's complement is the number itself.
Signed Numbers · Step through the code
Code Trace: NOT + 1 and NEG
ORG 100h MOV AL, 45 ; +45 MOV BL, AL NOT BL ; ones' comp. INC BL ; + 1 NEG AL ; one instr. CMP AL, BL ; equal? ADD AL, 45 ; -45 + 45 MOV AX, 45 NEG AX ; -45 in 16 bits RET
AL = +45 = 0010 1101b (2Dh).
BL = AL: a copy of the same value; the complement will be taken on BL in two steps.
NOT BL: every bit flips → 1101 0010b = D2h (ones' complement). NOT does not change the flags.
INC BL: + 1 → 1101 0011b = D3h. Unsigned 211 = 256 − 45; signed −45.
NEG AL (0 − AL) gives the same result in one instruction: AL = D3h. The operand ≠ 0 → CF = 1.
CMP AL, BL: D3h − D3h = 0 → ZF = 1. Both routes gave the same bit pattern.
ADD AL, 45: D3h + 2Dh = 100h. Bit 9 falls out (CF = 1), AL = 00h, ZF = 1: −45 + 45 = 0 ✓
AX = 45 = 002Dh: the same number in 16 bits.
NEG AX: FFD3h = 65536 − 45. The low byte is D3h again; the high byte is FFh, MSB = 1 → negative (SF = 1).
−x = NOT x + 1 = 2n − x. NEG does it in one instruction; the same rule works at every width (8, 16 bits).
Signed Numbers
−5 + 5 = 0
- The method must agree with the arithmetic rule −5 + 5 = 0.
- Adding the bytes 5 and 251 gives more than 255 (256).
- The ninth bit does not fit in the byte and falls out; the remaining 8 bits are zero.
- So the same adder gives the right answer for both unsigned and signed numbers.
Signed Numbers
Signed Ranges
| Width | Combinations | Unsigned | Signed (two's complement) | Negatives |
|---|---|---|---|---|
| 8 bits | 256 | 0 … 255 | −128 … +127 | 80h … FFh |
| 16 bits | 65536 | 0 … 65535 | −32768 … +32767 | 8000h … FFFFh |
| 32 bits | 4,294,967,296 | 0 … 4,294,967,295 | −2,147,483,648 … +2,147,483,647 | 80000000h … |
- In the upper half of the combinations the top bit is always 1; this bit gives the sign of the number (the SF flag).
- 16 bits give 65536 combinations: the first 32768 (0 … 32767) are positive, the other 32768 (32768 … 65535) negative.
- The negative range has one more value than the positive: the positive of −128 does not fit in 8 bits (NEG 80h = 80h, OF = 1).
- Sign extension: when an 8-bit signed number moves to 16 bits, the MSB is copied to the left: FBh (−5) → FFFBh. 8086:
CBW(AL → AX),CWD(AX → DX:AX).
Signed Numbers · Step by step
Fixed-Point Signed Fractions
Let the last 3 of 8 bits be fraction bits: the Q5.3 format (5 integer bits including the sign + 3 fraction bits). Example: −6.75.
Digit weights: the leftmost bit is −16 (the sign bit); after the point ½, ¼, ⅛. Range −16 … +15.875, step 1/8 = 0.125.
+6.75 = 4 + 2 + ½ + ¼ → 00110.110.
Two's complement (flip + 1; ignore the point): 00110110 → 11001001 + 1 = 11001.010.
Check: the sign bit is worth −16: −16 + 8 + 1 + 0.25 = −6.75 ✓
Shortcut: multiply by 23 = 8 and take the two's complement as for an integer: −54 → CAh, then put the point 3 bits from the right.
Signed Numbers · Interactive
Bit Explorer
Click a bit to flip it; the unsigned, signed, BCD and Gray code readings of the same 8 bits appear together.
Binary Arithmetic
Binary Addition
- Each digit pair has four cases: 0+0, 0+1, 1+0 and 1+1.
- Starting from the lowest digit, add the matching digits of the two numbers.
- A digit sum of 0 or 1 is written directly.
- A sum of 2 (10b) produces a carry: write 0 and add 1 to the next digit.
- With the previous carry the sum can be 3 (11b): write 1, carry 1.
| a | b | carryin | sum | carryout |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 0 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 | 1 |
This table is the truth table of a full adder; chain 8 of them and you get an 8-bit adder.
Binary Arithmetic · Step by step
Addition: 110001101 + 100001011
110001101b (397) + 100001011b (267). Add column by column, right to left.
Digit 0: 1 + 1 = 2 → write 0, carry 1.
Digit 1: 0 + 1 + 1 (carry) = 2 → write 0, carry 1.
Digit 2: 1 + 0 + 1 (carry) = 2 → write 0, carry 1.
Digit 3: 1 + 1 + 1 (carry) = 3 → write 1, carry 1.
Digit 4: 0 + 0 + 1 (carry) = 1 → write 1.
Digit 5: 0 + 0 = 0 → write 0.
Digit 6: 0 + 0 = 0 → write 0.
Digit 7: 1 + 0 = 1 → write 1.
Digit 8: 1 + 1 = 2 → write 0, carry 1.
Digit 9: 0 + 0 + 1 (carry) = 1 → write 1. Result 1010011000b = 664 ✓ (the 10th bit did not fit the 9-bit addition → carry).
Binary Arithmetic · Step by step
Binary Subtraction
- Subtraction becomes addition of the complement: A − B = A + (−B).
- Flip every bit of the number being subtracted.
- Add 1 to the complemented number (two's complement).
- Add the result to the first number; a carry beyond the bit width is discarded.
- So the processor needs no separate subtractor circuit.
The first number is 1011 (11), the second 0110 (6).
A = 1011.
B = 0110.
Flip the bits of B: 1001.
Add 1: 1010 = −6 (4 bits).
1011 + 1010 = 10101 (5 bits).
The leftmost 1 that does not fit in 4 bits is dropped: 0101 = 5 = 11 − 6 ✓
On the 8086, SUB writes this dropped carry to CF inverted: a carry of 1 means no borrow (CF = 0).
Binary Arithmetic · Step by step
Hex Addition: 9A7Ch + 5B96h
Hex addition: digit by digit, right to left. If a digit sum exceeds 15, subtract 16 and carry 1 to the left.
C + 6 = 18 ≥ 16 → write 2, carry 1.
7 + 9 + 1 = 17 → write 1, carry 1.
A + B + 1 = 22 → write 6, carry 1.
9 + 5 + 1 = 15 → write F, no carry.
Result F612h. On the 8086, ADD AX, BX gives the same result; no carry leaves the last digit, so CF = 0.
Think of digit values in decimal (A = 10 … F = 15). If a sum is 16 or more, write sum − 16 in that digit.
Binary Arithmetic · Step by step
Hex Subtraction: 9A7Ch − 5B96h
Hex subtraction: if the top digit is too small, borrow 1 from the digit to its left; that adds 16 to this digit.
C − 6 = 6.
7 < 9 → borrow from the left: 7 + 16 − 9 = 14 → E; the A on the left becomes 9.
9 < B → borrow again: 9 + 16 − 11 = 14 → E; the 9 on the left becomes 8.
8 − 5 = 3.
Result 3EE6h. No borrow is needed from the leftmost digit, so after SUB AX, BX on the 8086, CF = 0.
The “−1” marks show the digits that lent. The same operation can also be done as addition of the complement.
Binary Arithmetic · Step by step
15's and 16's Complement
The 15's complement of 5B96h: subtract each digit from F (F−5, F−B, F−9, F−6) → A469h. In binary this is NOT.
16's complement = 15's complement + 1 = A46Ah. This is −5B96h in 4 digits (NEG).
9A7Ch − 5B96h = 9A7Ch + A46Ah = 13EE6h. The 1 beyond 4 digits is dropped: 3EE6h, the same as direct subtraction.
The other way round: 5B96h − 9A7Ch = 5B96h + 6584h (the 16's complement of 9A7Ch) = C11Ah. There is no overflowing digit → the result is negative.
To get the magnitude, take the 16's complement again: C11Ah → 3EE6h, so the result is −3EE6h (−16102).
SUB AX, BX (AX = 5B96h, BX = 9A7Ch) gives C11Ah directly, with CF = 1 because of the borrow; read as signed, C11Ah is −16102.Binary Arithmetic
The Signed Overflow Rule
| Operation | Signs | Example (8 bits) | Signed reading | OF |
|---|---|---|---|---|
| A + B | + with + | 50h + 40h = 90h | 80 + 64 → −112 ✗ | 1 |
| A + B | − with − | 9Ch + CEh = 6Ah | −100 + (−50) → +106 ✗ | 1 |
| A + B | different | 64h + CEh = 32h | 100 + (−50) = 50 ✓ | 0 (never) |
| A − B | + minus − | 64h − CEh = 96h | 100 − (−50) → −106 ✗ | 1 |
| A − B | − minus + | 9Ch − 32h = 6Ah | −100 − 50 → +106 ✗ | 1 |
| A − B | same | 9Ch − CEh = CEh | −100 − (−50) = −50 ✓ | 0 (never) |
- Overflow can happen only when adding two numbers of the same sign or subtracting two numbers of different signs; the symptom is a result whose sign is the opposite of what you expect.
- Hardware rule: OF = 1 when the carry into the sign bit differs from the carry out of it. CF means nothing in the signed reading (row 3 has CF = 1 but the result is correct).
- Before adding signed numbers of different widths, sign-extend the shorter one: FBh (−5) + 0100h → FFFBh + 0100h = 00FBh = 251 ✓; zero-extending gives 01FBh = 507 ✗.
Binary Arithmetic
Supported Operators
| Op. | Meaning | Example → value |
|---|---|---|
~ | not (flips all bits) | ~0Fh → F0h (8 bit) |
* | multiply | 3*4 → 12 |
/ | divide (integer) | 17/5 → 3 |
% | mod (remainder) | 17%5 → 2 |
+ | add | 40h+2 → 42h |
- | subtract (unary −) | -5 → FBh |
<< | shift left | 1<<4 → 16 |
>> | shift right | 80h>>3 → 10h |
& | bitwise AND | 0F0h&3Ch → 30h |
^ | bitwise XOR | 0Fh^05h → 0Ah |
| | bitwise OR | 0F0h|0Fh → FFh |
- Operators the emu8086 assembler supports in constant expressions.
- Evaluated at assembly time:
MOV AX, 3*4+1→MOV AX, 13. - The CPU does not run these; their run-time counterparts are the
NOT,MUL,DIV,ADD,SUB,SHL,SHR,AND,XORandORinstructions. - Constants only:
MOV AX, BX*2is invalid.
Encodings and Circuits
Digital Systems and Binary Signals
- In a digital system, software is a collection of digits grouped into data and instructions.
- These digits are physically realized in circuits as voltage and current.
- Digital computers use two kinds of signal, high and low: 1 (high), 0 (low).
- The voltage need not be exactly 5 V or 0 V; there are threshold ranges (TTL: ≥ 2.0 V → 1, ≤ 0.8 V → 0). This tolerance makes digital circuits robust against noise.
- Binary signals are the basic language of computers; a “bit” is one digit of a binary number.
Encodings and Circuits
Flip-Flop Circuits
- The basic memory element of digital electronics: it stores 1 bit.
- It holds a value until the next clock pulse.
- It takes an input and a clock signal and produces an output (Q and its inverse Q′).
- The clock signal marks a moment in time; the input is loaded into the flip-flop during that clock pulse.
- Registers (AX, BX …) are made of 16 flip-flops side by side.
Encodings and Circuits
Flip-Flop Types
| Type | Inputs | Behavior (on the clock pulse) | Characteristic |
|---|---|---|---|
| SR (Set-Reset) | S, R | S = 1 → Q = 1 (set); R = 1 → Q = 0 (reset); S = R = 0 → no change | S = R = 1 forbidden |
| D (Data) | D | Loads its single data input to the output on the clock: Q ← D | register/memory cell |
| JK | J, K | J = 1 → set, K = 1 → reset, J = K = 0 → no change, J = K = 1 → toggle | no forbidden state, unlike SR |
| T (Toggle) | T | With T = 1 the output flips on every pulse; T = 0 → no change | counters, frequency dividers |
Encodings and Circuits
D Flip-Flop Timing
- The D input may change any number of times between clock pulses; it does not affect the output.
- On the rising edge D is sampled and held in Q until the next edge: 1 bit of memory.
- All internal operations of the 8086 are divided into clock cycles this way (e.g. 5 MHz → a 200 ns cycle).
Encodings and Circuits
Floating Point: IEEE 754
- Fractional numbers use the IEEE 754 standard; a single-precision number is 32 bits.
- The 32 bits form three fields: the sign (1 bit), the exponent (8 bits) and the mantissa (fraction, 23 bits).
- The first bit is the sign bit: 0 positive, 1 negative.
- The exponent is stored with a bias of 127: true exponent = e − 127.
- The mantissa is the fractional part of the normalized number; the leading 1 is not stored (the hidden bit): value = (−1)s × 1.f × 2e−127.
The 8086 has no floating-point instructions; these operations are done by the 8087 math coprocessor or in software. 64-bit double: 1 + 11 + 52 bits, bias 1023.
Encodings and Circuits · Step by step
Decoding IEEE 754
Example: 0 01111100 01000000000000000000000 = 3E200000h.
Sign bit 0 → the number is positive: (+1).
Exponent 01111100b = 124 → true exponent 124 − 127 = −3 → 2−3.
Mantissa 0100…0 = 0×2⁻¹ + 1×2⁻² = 0.25 → with the hidden bit 1.25.
Result: (+1) × 2−3 × 1.25 = 1.25 / 8 = 0.15625.
Encodings and Circuits · Step by step
Encoding IEEE 754: −6.5
Let us convert −6.5 to 32-bit IEEE 754.
Negative → sign bit 1.
6.5 = 110.1b (4 + 2 + 0.5). Normalize: 1.101b × 2².
Exponent: 2 + 127 = 129 = 10000001b.
Mantissa: drop the hidden 1 and pad the remaining 101 with 0s on the right.
Result: 1100 0000 1101 0000 0…0b = C0D00000h.
| Pattern | Meaning |
|---|---|
| e = 0, f = 0 | ±0 |
| e = 255, f = 0 | ±∞ (e.g. 1/0) |
| e = 255, f ≠ 0 | NaN (not a number, e.g. 0/0) |
| e = 0, f ≠ 0 | denormalized (very small) number |
Encodings and Circuits
BCD: Binary-Coded Decimal
- BCD (binary-coded decimal) represents each decimal digit with four bits.
- Each digit from 0 to 9 takes 4 bits; the patterns 1010 … 1111 are invalid.
- Example: 25 →
0010 0101(BCD), while in binary it is0001 1001(19h). - Packed BCD: two digits per byte (25h). Unpacked: one digit per byte (02h 05h).
- Uses: clock chips (RTC), 7-segment displays, calculators, financial arithmetic.
| Decimal | BCD |
|---|---|
| 0 | 0000 |
| 1 | 0001 |
| 2 | 0010 |
| 3 | 0011 |
| 4 | 0100 |
| 5 | 0101 |
| 6 | 0110 |
| 7 | 0111 |
| 8 | 1000 |
| 9 | 1001 |
Encodings and Circuits · Step by step
BCD Addition and Adjustment
Let us add BCD 38 and BCD 45.
38 → 0011 1000.
45 → 0100 0101.
Add in binary: 0111 1101 = 7Dh. The low digit 1101 (13) is invalid in BCD.
Add 6 to an invalid digit (or when AF = 1): this skips the 16 − 10 = 6 unused patterns.
Result 1000 0011 = 83 ✓. On the 8086, ADD AL, BL followed by DAA does this adjustment.
A carry out of the low nibble (e.g. 29 + 18 = 41h, AF = 1) also adds 6 → 47. If the high digit exceeds 9, 60h is added to it as well and CF = 1.
Encodings and Circuits
Little-Endian and Big-Endian
- Little-endian: the least significant byte (LSB) is at the lowest memory address. Intel x86 (the 8086 included) is little-endian.
- Big-endian: the most significant byte (MSB) is at the lowest address. PowerPC (mostly), the Motorola 68000 and network communication (“network byte order”) are big-endian.
04 03 02 01; the big-endian layout is 01 02 03 04.Encodings and Circuits
Gray Code
| Decimal | Binary | changed bit | Gray |
|---|---|---|---|
| 0 | 000 | – | 000 |
| 1 | 001 | 1 | 001 |
| 2 | 010 | 2 | 011 |
| 3 | 011 | 1 | 010 |
| 4 | 100 | 3 | 110 |
| 5 | 101 | 1 | 111 |
| 6 | 110 | 2 | 101 |
| 7 | 111 | 1 | 100 |
- In Gray code two consecutive numbers differ in only one bit.
- In binary, going from 3 to 4 (011 → 100) changes three bits at once; if they do not change at the same instant, intermediate values (e.g. 111) can be read.
- In Gray code that step is 010 → 110: one bit, no false intermediate value.
- Used in rotary encoders, analog-to-digital converters and Karnaugh maps; robust against electrical noise.
- The 3-bit sequence: 000, 001, 011, 010, 110, 111, 101, 100.
Encodings and Circuits · Step by step
Binary to Gray Code
Conversion formula: G = B XOR (B >> 1).
B = 0110 (6).
B is shifted right one bit: 0011.
Bitwise XOR: 0110 ⊕ 0011 = 0101 → the Gray code of 6.
- Rule: the top Gray bit equals the binary one; each following bit is the XOR of the binary bit and the bit to its left.
- On the 8086 in three instructions:
MOV BL, AL/SHR BL, 1/XOR AL, BL. - Back again: Btop = Gtop, then Bi = Bi+1 XOR Gi.
Encodings and Circuits · Step through the code
Code Trace: Gray Code with SHR and XOR
ORG 100h MOV AL, 1011b ; 11 MOV BL, AL SHR BL, 1 ; B >> 1 XOR BL, AL ; G MOV AL, 1100b ; 12 MOV BH, AL SHR BH, 1 XOR BH, AL ; G RET
AL = 1011b = 11. The Gray code formula: G = B xor (B >> 1).
BL = AL: the shift is done on a copy, so B (AL) is kept.
SHR BL, 1: 0101b. The rightmost bit falls out (CF = 1) and a 0 enters on the left.
XOR BL, AL: 1011 xor 0101 = 1110b: the Gray code of 11. The top bit stayed the same as in binary.
AL = 1100b = 12, the next number.
BH = AL.
SHR BH, 1: 0110b.
XOR → 1010b. From 11 to 12, 3 bits change in binary (1011 → 1100); in Gray code only 1 bit (1110 → 1010).
Summary
Summary
Summary
Exercises
- Convert MCMXCIV and MMXXVI to decimal.
- Convert 11001010b to decimal and hex.
- Write 2024 in hex and binary.
- Write −100 as an 8-bit two's complement number. What does it look like in 16 bits?
- What are the 8-bit result, CF and signed reading of 0F0h + 20h?
- Do the BCD addition 47 + 36 with adjustment.
- What is the value of the IEEE 754 number 0 10000010 01100000000000000000000?
- What is the 3-bit Gray code of 5? If 0ABCDh is written as a word at address 0200h, what is [0200h]?
Answers
- 1000 + 900 + 90 + 4 = 1994; 2026.
- 128 + 64 + 8 + 2 = 202 = CAh.
- 7E8h = 0111 1110 1000b.
- 256 − 100 = 156 = 9Ch = 1001 1100b; FF9Ch in 16 bits.
- 110h → 10h, CF = 1; signed −16 + 32 = +16 is correct (OF = 0).
- 0100 0111 + 0011 0110 = 0111 1101 (7Dh) → +6 → 83h = BCD 83.
- +1.011b × 2³ = 1011b = 11.0.
- 101 ⊕ 010 = 111; [0200h] = CDh, [0201h] = ABh.
Summary
Exercises (2)
- Convert 0.375 and 0.2 to binary (8 fraction bits for 0.2).
- Write −3.625 in 8-bit Q5.3 format (binary and hex).
- Compute 7C5Eh + 3BA9h; compute 3BA9h − 7C5Eh with the 16's complement.
- In 8 bits, what are the results and OF for 70h + 30h, 90h − 20h and 90h − F0h?
Answers
- 0.375 = 0.011b (0.75 → 0; 1.5 → 1; 1.0 → 1). 0.2 = 0.0011 0011…b → 0.00110011b.
- 3.625 × 8 = 29 → −29 = 256 − 29 = E3h = 11100.011b. Check: −16 + 8 + 4 + 0.25 + 0.125 = −3.625.
- B807h. The 16's complement of 7C5Eh is 83A2h; 3BA9h + 83A2h = BF4Bh, no overflow → −(10000h − BF4Bh) = −40B5h.
- A0h, OF = 1 (112 + 48 = 160); 70h, OF = 1 (−112 − 32 = −144); A0h, OF = 0 (−112 − (−16) = −96: two numbers of the same sign were subtracted).
Summary
References
- M. Morris Mano, Michael D. Ciletti, Digital Design, 5th ed., Pearson, 2013.
- Barry B. Brey, The Intel Microprocessors, 8th ed., Pearson, 2009 — Chapter 1: number systems.
- IEEE Std 754-2019, IEEE Standard for Floating-Point Arithmetic.
- emu8086 documentation: Numbering systems tutorial.
- Mehmet Bodur, Mikroişlemcilere Giriş: Assembler ile Yazılım ve Arayüz, EMO, 2014 (in Turkish).
- Şadi Çağatay Öztürk, Oğuz Ergin, Intel 8086 ile Mikroişlemci Programlamaya Giriş, 2009 (in Turkish).