Chapter 1.2: Number Systems

Microprocessors
Roman numeralsDecimal, binary, hexadecimalBase conversionSigned numbersOnes' and two's complementBinary addition/subtractionIEEE 754 floating pointFlip-flopsBCDLittle-endian / big-endianGray code
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Positional systems

Compute the value of decimal, binary and hexadecimal numbers from their digit weights.

Conversion

Convert numbers between bases by division and by grouping.

Signed numbers

Write, add and subtract negative numbers in two's complement.

Encodings

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.
5 sticksIII = 3VV = 5XX = 10754positional: 7·100 + 5·10 + 4

Representing Numbers

Roman Numerals

SymbolValue
I1
V5
X10
L50
C100
D500
M1000
  • 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

MCMLXXXVI

Split the number into value groups from left to right.

M1000CMLXXXVI

M = 1000.

M1000+CM900LXXXVI

CM: C comes before M, so it is subtracted → 1000 − 100 = 900.

M1000+CM900+LXXX80VI

LXXX = 50 + 10 + 10 + 10 = 80.

M1000+CM900+LXXX80+VI6

VI = 5 + 1 = 6.

M1000+CM900+LXXX80+VI6= 1986

Total 1000 + 900 + 80 + 6 = 1986.

MMVII

M1000+M1000+VII7= 2007
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.
710²700510¹50410⁰4700 + 50 + 4 = 754
General formula: dn−1…d1d0 = Σ di × bi, where b is the base and i the digit's position (from the right, starting at 0).

Positional Systems

Position Matters

710²700510¹50410⁰4754510²500410¹40710⁰7547
  • 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 …
101100105 V0 Vhigh = 1low = 0

Positional Systems

Bit, Nibble, Byte, Word

00010111000101110001011100010111bitnibble (4 bits)byte (8 bits)word (16 bits)doubleword (32 bits)high bit (MSB) →← low bit (LSB)
UnitBitUnsigned rangeOn the 8086
bit10 … 1flag
nibble40 … 15 (0h … Fh)one hex digit, one BCD digit
byte80 … 255AL, BL …, DB
word160 … 65535AX, BX …, DW
doubleword320 … 4,294,967,295DX: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: 10100101b equals decimal 165.
12⁷12802⁶012⁵3202⁴002³012²402¹012⁰1128 + 32 + 4 + 1 = 165
Shortcut: add up the weights of the digits that are 1. The 8-bit weights: 128 · 64 · 32 · 16 · 8 · 4 · 2 · 1.

Positional Systems · Step by step

Binary to Decimal: 1111011b

12⁶12⁵12⁴12³02²12¹12⁰

(1111011)2: 7 digits, weights 2⁰ … 2⁶.

12⁶6412⁵3212⁴1612³802²012¹212⁰1sum = 1

Digit 0: 1 × 2⁰ = 1 → running sum 1.

12⁶6412⁵3212⁴1612³802²012¹212⁰1sum = 3

Digit 1: 1 × 2¹ = 2 → running sum 3.

12⁶6412⁵3212⁴1612³802²012¹212⁰1sum = 3

Digit 2: 0 × 2² = 0 → running sum 3.

12⁶6412⁵3212⁴1612³802²012¹212⁰1sum = 11

Digit 3: 1 × 2³ = 8 → running sum 11.

12⁶6412⁵3212⁴1612³802²012¹212⁰1sum = 27

Digit 4: 1 × 2⁴ = 16 → running sum 27.

12⁶6412⁵3212⁴1612³802²012¹212⁰1sum = 59

Digit 5: 1 × 2⁵ = 32 → running sum 59.

12⁶6412⁵3212⁴1612³802²012¹212⁰1sum = 123

Digit 6: 1 × 2⁶ = 64 → running sum 123.

(1111011)2 = 1×2⁰ + 1×2¹ + 0×2² + 1×2³ + 1×2⁴ + 1×2⁵ + 1×2⁶ = 1 + 2 + 0 + 8 + 16 + 32 + 64 = 123

Positional Systems

The Hexadecimal System

base 10base 2base 16
000000
100011
200102
300113
401004
501015
601106
701117
base 10base 2base 16
810008
910019
101010A
111011B
121100C
131101D
141110E
151111F
  • 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 takes E120h for a variable or label name.
  • Example: 1234h equals decimal 4660.
116³4096216²512316¹48416⁰44096 + 512 + 48 + 4 = 4660
NotationMeaning
MOV AL, 0FFhAL = 255 ✓
MOV AL, FFherror: FFh is an undefined name
MOV AX, 10decimal 10 (= 0Ah) — no suffix means decimal

Positional Systems · Step by step

Hex to Decimal: A12Fh

A16³116²216¹F16⁰

(A12F)16: A = 10, F = 15. Weights 16⁰ = 1, 16¹ = 16, 16² = 256, 16³ = 4096.

A16³40960116²256216¹32F16⁰15sum = 15

F × 16⁰ = 15 × 1 = 15.

A16³40960116²256216¹32F16⁰15sum = 47

2 × 16¹ = 2 × 16 = 32 → 47.

A16³40960116²256216¹32F16⁰15sum = 303

1 × 16² = 1 × 256 = 256 → 303.

A16³40960116²256216¹32F16⁰15sum = 41263

A × 16³ = 10 × 4096 = 40960 → 41263.

(A12F)16 = 15 + 32 + 256 + 40960 = 41263

Positional Systems · Step by step

Binary ↔ Hex: Grouping by Four

1010101101011100

Split the binary number into groups of 4 starting from the right (pad with 0s on the left if needed).

1010101101011100A

1010 = 8 + 2 = 10 → A.

1010101101011100AB

1011 = 8 + 2 + 1 = 11 → B.

1010101101011100AB5

0101 = 4 + 1 = 5 → 5.

1010101101011100AB5C

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

NotationBaseValue
MOV AL, 65decimal (no suffix or d)65
MOV AL, 41hhexadecimal65
MOV AL, 01000001bbinary65
MOV AL, 101ooctal (o or q)65
MOV AL, 'A'ASCII character65
MOV AL, -1decimal, signedFFh
6541h01000001b101o'A'AL = 0100 0001b (B0 41)
The first five notations produce the same machine code: 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= 2remainder7

39 ÷ 16 = 2, remainder 7 (the ones digit).

39 ÷ 16= 2remainder72 ÷ 16= 0remainder2

2 ÷ 16 = 0, remainder 2. Quotient 0 → stop.

39 ÷ 16= 2remainder72 ÷ 16= 0remainder2read bottom up27h

Remainders bottom up: 27h. Check: 2 × 16 + 7 = 39 ✓

Base Conversion · Step by step

43868 → Hex

How is 43868 written in hex?

43868 ÷ 16= 2741remainder12=C

43868 ÷ 16 = 2741, remainder 12 → C.

43868 ÷ 16= 2741remainder12=C2741 ÷ 16= 171remainder5

2741 ÷ 16 = 171, remainder 5.

43868 ÷ 16= 2741remainder12=C2741 ÷ 16= 171remainder5171 ÷ 16= 10remainder11=B

171 ÷ 16 = 10, remainder 11 → B.

43868 ÷ 16= 2741remainder12=C2741 ÷ 16= 171remainder5171 ÷ 16= 10remainder11=B10 ÷ 16= 0remainder10=A

10 ÷ 16 = 0, remainder 10 → A.

43868 ÷ 16= 2741remainder12=C2741 ÷ 16= 171remainder5171 ÷ 16= 10remainder11=B10 ÷ 16= 0remainder10=Aread bottom up0AB5Ch

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)
dividing with DIV BX (base 16)AX0BADhDX0000hSI0000hBX = 0000hrem array00000000+0 +1 +2 +3

AX = 2989 = 0BADh. The hex digits are found by repeatedly dividing by 16.

dividing with DIV BX (base 16)AX0BADhDX0000hSI0000hBX = 0010h (16)rem array00000000+0 +1 +2 +3

BX = 16: the target base. With BX = 2 or 8 the same code would produce that base's digits.

dividing with DIV BX (base 16)AX0BADhDX0000hSI0000hBX = 0010h (16)rem array00000000+0 +1 +2 +3

SI = 0: where the next remainder goes in the rem array.

dividing with DIV BX (base 16)AX0BADhDX0000hSI0000hBX = 0010h (16)rem array00000000+0 +1 +2 +3

DX = 0. A 16-bit DIV BX divides the 32-bit DX:AX; without clearing DX the dividend would be wrong.

dividing with DIV BX (base 16)2989 ÷ 16 = 186rem 13DAX00BAhDX000DhSI0000hBX = 0010h (16)rem array00000000+0 +1 +2 +3

2989 ÷ 16 = 186, remainder 13: AX = 00BAh (quotient), DX = 000Dh (remainder). The first remainder is the lowest digit: D.

dividing with DIV BX (base 16)2989 ÷ 16 = 186rem 13DAX00BAhDX000DhSI0000hBX = 0010h (16)rem array0D000000+0 +1 +2 +3

rem[0] ← DL = 0Dh.

dividing with DIV BX (base 16)2989 ÷ 16 = 186rem 13DAX00BAhDX000DhSI0001hBX = 0010h (16)rem array0D000000+0 +1 +2 +3

SI = 1; quotient 186 ≠ 0 → ZF = 0, JNE goes back to again.

dividing with DIV BX (base 16)2989 ÷ 16 = 186rem 13D186 ÷ 16 = 11rem 10AAX000BhDX000AhSI0001hBX = 0010h (16)rem array0D000000+0 +1 +2 +3

Pass 2: DX cleared, 186 ÷ 16 = 11, remainder 10 → A.

dividing with DIV BX (base 16)2989 ÷ 16 = 186rem 13D186 ÷ 16 = 11rem 10AAX000BhDX000AhSI0001hBX = 0010h (16)rem array0D0A0000+0 +1 +2 +3

rem[1] ← 0Ah. Quotient 11 ≠ 0: the loop continues.

dividing with DIV BX (base 16)2989 ÷ 16 = 186rem 13D186 ÷ 16 = 11rem 10A11 ÷ 16 = 0rem 11BAX0000hDX000BhSI0002hBX = 0010h (16)rem array0D0A0000+0 +1 +2 +3

Pass 3: 11 ÷ 16 = 0, remainder 11 → B.

dividing with DIV BX (base 16)2989 ÷ 16 = 186rem 13D186 ÷ 16 = 11rem 10A11 ÷ 16 = 0rem 11BAX0000hDX000BhSI0002hBX = 0010h (16)rem array0D0A0B00+0 +1 +2 +3

rem[2] ← 0Bh.

dividing with DIV BX (base 16)2989 ÷ 16 = 186rem 13D186 ÷ 16 = 11rem 10A11 ÷ 16 = 0rem 11BAX0000hDX000BhSI0003hBX = 0010h (16)rem array0D0A0B00+0 +1 +2 +3

SI = 3. Quotient 0 → ZF = 1, JNE does not jump: the loop ends with 3 digits.

dividing with DIV BX (base 16)2989 ÷ 16 = 186rem 13D186 ÷ 16 = 11rem 10A11 ÷ 16 = 0rem 11Bread last to firstAX0000hDX000BhSI0003hBX = 0010h (16)rem array0D0A0B00+0 +1 +2 +3

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= 22remainder1

45 ÷ 2 = 22, remainder 1.

45 ÷ 2= 22remainder122 ÷ 2= 11remainder0

22 ÷ 2 = 11, remainder 0.

45 ÷ 2= 22remainder122 ÷ 2= 11remainder011 ÷ 2= 5remainder1

11 ÷ 2 = 5, remainder 1.

45 ÷ 2= 22remainder122 ÷ 2= 11remainder011 ÷ 2= 5remainder15 ÷ 2= 2remainder1

5 ÷ 2 = 2, remainder 1.

45 ÷ 2= 22remainder122 ÷ 2= 11remainder011 ÷ 2= 5remainder15 ÷ 2= 2remainder12 ÷ 2= 1remainder0

2 ÷ 2 = 1, remainder 0.

45 ÷ 2= 22remainder122 ÷ 2= 11remainder011 ÷ 2= 5remainder15 ÷ 2= 2remainder12 ÷ 2= 1remainder01 ÷ 2= 0remainder1

1 ÷ 2 = 0, remainder 1.

45 ÷ 2= 22remainder122 ÷ 2= 11remainder011 ÷ 2= 5remainder15 ÷ 2= 2remainder12 ÷ 2= 1remainder01 ÷ 2= 0remainder1read bottom up101101b

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
00101101AL= 45CF00000000BL= 00hCX = 0division

AL = 45 = 0010 1101b. Shifting right one bit divides by 2; the bit that falls out is the remainder.

00101101AL= 45CF00000000BL= 00hCX = 8division

CX = 8: one pass for each of the 8 bits.

00010110AL= 22CF=100000000BL= 00hCX = 8division45 ÷ 2 = 22k 1

SHR AL, 1: 45 ÷ 2 = 22 (AL = 16h); the bit that drops out, the remainder 1 → CF = 1.

00010110AL= 22CF=010000000BL= 80hCX = 8division45 ÷ 2 = 22k 1

RCR BL, 1: CF enters BL from the left (bit 7); the remainder is saved.

00010110AL= 22CF=010000000BL= 80hCX = 7division45 ÷ 2 = 22k 1

LOOP: CX = 7 ≠ 0 → back to the bit label.

00001011AL= 11CF=001000000BL= 40hCX = 6division45 ÷ 2 = 22k 122 ÷ 2 = 11k 0

Pass 2 (summary): 22 ÷ 2 = 11, remainder 0. The earlier remainders shift one place right in BL; CX = 6.

00000101AL= 5CF=010100000BL= A0hCX = 5division45 ÷ 2 = 22k 122 ÷ 2 = 11k 011 ÷ 2 = 5k 1

Pass 3 (summary): 11 ÷ 2 = 5, remainder 1. The earlier remainders shift one place right in BL; CX = 5.

00000010AL= 2CF=011010000BL= D0hCX = 4division45 ÷ 2 = 22k 122 ÷ 2 = 11k 011 ÷ 2 = 5k 15 ÷ 2 = 2k 1

Pass 4 (summary): 5 ÷ 2 = 2, remainder 1. The earlier remainders shift one place right in BL; CX = 4.

00000001AL= 1CF=001101000BL= 68hCX = 3division45 ÷ 2 = 22k 122 ÷ 2 = 11k 011 ÷ 2 = 5k 15 ÷ 2 = 2k 12 ÷ 2 = 1k 0

Pass 5 (summary): 2 ÷ 2 = 1, remainder 0. The earlier remainders shift one place right in BL; CX = 3.

00000000AL= 0CF=010110100BL= B4hCX = 2division45 ÷ 2 = 22k 122 ÷ 2 = 11k 011 ÷ 2 = 5k 15 ÷ 2 = 2k 12 ÷ 2 = 1k 01 ÷ 2 = 0k 1

Pass 6 (summary): 1 ÷ 2 = 0, remainder 1. The earlier remainders shift one place right in BL; CX = 2.

00000000AL= 0CF=001011010BL= 5AhCX = 1division45 ÷ 2 = 22k 122 ÷ 2 = 11k 011 ÷ 2 = 5k 15 ÷ 2 = 2k 12 ÷ 2 = 1k 01 ÷ 2 = 0k 10 ÷ 2 = 0k 0

Pass 7 (summary): 0 ÷ 2 = 0, remainder 0. The earlier remainders shift one place right in BL; CX = 1. Leading zeros.

00000000AL= 0CF=000101101BL= 2DhCX = 0division45 ÷ 2 = 22k 122 ÷ 2 = 11k 011 ÷ 2 = 5k 15 ÷ 2 = 2k 12 ÷ 2 = 1k 01 ÷ 2 = 0k 10 ÷ 2 = 0k 00 ÷ 2 = 0k 0

Pass 8 (summary): 0 ÷ 2 = 0, remainder 0. The earlier remainders shift one place right in BL; CX = 0. Leading zeros.

00000000AL= 0CF=000101101BL= 2DhCX = 0division45 ÷ 2 = 22k 122 ÷ 2 = 11k 011 ÷ 2 = 5k 15 ÷ 2 = 2k 12 ÷ 2 = 1k 01 ÷ 2 = 0k 10 ÷ 2 = 0k 00 ÷ 2 = 0k 0

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.25bit1fraction left 0.25

0.625 × 2 = 1.25: integer part 1 → the first fraction bit (2−1). Continue with 0.25.

0.625 × 2= 1.25bit1fraction left 0.250.25 × 2= 0.5bit0fraction left 0.5

0.25 × 2 = 0.5: integer part 0 → the 2−2 bit.

0.625 × 2= 1.25bit1fraction left 0.250.25 × 2= 0.5bit0fraction left 0.50.5 × 2= 1.0bit1fraction left 0.0

0.5 × 2 = 1.0: integer part 1, fraction left 0 → stop.

0.625 × 2= 1.25bit1fraction left 0.250.25 × 2= 0.5bit0fraction left 0.50.5 × 2= 1.0bit1fraction left 0.0read top down0.101b

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.2bit0fraction left 0.2

0.1 × 2 = 0.2 → 0.

0.1 × 2= 0.2bit0fraction left 0.20.2 × 2= 0.4bit0fraction left 0.4

0.2 × 2 = 0.4 → 0.

0.1 × 2= 0.2bit0fraction left 0.20.2 × 2= 0.4bit0fraction left 0.40.4 × 2= 0.8bit0fraction left 0.8

0.4 × 2 = 0.8 → 0.

0.1 × 2= 0.2bit0fraction left 0.20.2 × 2= 0.4bit0fraction left 0.40.4 × 2= 0.8bit0fraction left 0.80.8 × 2= 1.6bit1fraction left 0.6

0.8 × 2 = 1.6 → 1, fraction 0.6.

0.1 × 2= 0.2bit0fraction left 0.20.2 × 2= 0.4bit0fraction left 0.40.4 × 2= 0.8bit0fraction left 0.80.8 × 2= 1.6bit1fraction left 0.60.6 × 2= 1.2bit1fraction left 0.2

0.6 × 2 = 1.2 → 1, fraction 0.2: we have seen this fraction before (row 2)!

0.1 × 2= 0.2bit0fraction left 0.20.2 × 2= 0.4bit0fraction left 0.40.4 × 2= 0.8bit0fraction left 0.80.8 × 2= 1.6bit1fraction left 0.60.6 × 2= 1.2bit1fraction left 0.20.2 × 2= 0.4bit0fraction left 0.4

0.2 is multiplied again: rows 2–5 repeat forever.

0.1 × 2= 0.2bit0fraction left 0.20.2 × 2= 0.4bit0fraction left 0.40.4 × 2= 0.8bit0fraction left 0.80.8 × 2= 1.6bit1fraction left 0.60.6 × 2= 1.2bit1fraction left 0.20.2 × 2= 0.4bit0fraction left 0.4repeating00110.0 0011 0011 …b

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 0FFh positive 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.
00h … 7Fh80h … FFh (MSB = 1)unsigned: 0128255signed: 0+127−128−1FBh = 251 = 256 − 5 → −5
The largest 8-bit combination is 255 (0 … 255 = 256 values). In 16 bits the negative range is likewise 32768 … 65535.

Signed Numbers

The Number Circle

000000110001220010330011440100550101660110770111−881000−791001−6101010−5111011−4121100−3131101−2141110−11511114 bitsinner: unsignedcircle: signed
  • 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

BinaryUn-
signed
Sign-
mag.
ones'
compl.
two's
compl.
00000000
00011111
00102222
00113333
01004444
01015555
01106666
01117777
BinaryUn-
signed
Sign-
mag.
ones'
compl.
two's
compl.
10008−0−7−8
10019−1−6−7
101010−2−5−6
101111−3−4−5
110012−4−3−4
110113−5−2−3
111014−6−1−2
111115−7−0−1
The middle column of the table is sign-magnitude (1000 = −0, 1111 = −7). The 8086 and all modern processors use two's complement: there is a single zero and the range is −8 … +7.

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 NOT instruction produces the ones' complement.

Start.

+45 =001011012Dh

+45 = 0010 1101b.

+45 =001011012DhNOT →11010010D2h (−45, ones' c.)

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 NEG instruction 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 =001011012Dh

+45 = 0010 1101b.

+45 =001011012DhNOT →11010010D2h

All bits are flipped (NOT): 1101 0010b.

+45 =001011012DhNOT →11010010D2h+ 1 →11010011D3h = −45

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
00101101AL2Dhunsigned 45 · signed +4500000000BL00hunsigned 0 · signed +0CF0ZF0SF0OF0

AL = +45 = 0010 1101b (2Dh).

00101101AL2Dhunsigned 45 · signed +4500101101BL2Dhunsigned 45 · signed +45CF0ZF0SF0OF0

BL = AL: a copy of the same value; the complement will be taken on BL in two steps.

00101101AL2Dhunsigned 45 · signed +4511010010BLD2hunsigned 210 · signed −46CF0ZF0SF0OF0NOT: 0 ↔ 1

NOT BL: every bit flips → 1101 0010b = D2h (ones' complement). NOT does not change the flags.

00101101AL2Dhunsigned 45 · signed +4511010011BLD3hunsigned 211 · signed −45CF0ZF0SF1OF0NOT + 1

INC BL: + 1 → 1101 0011b = D3h. Unsigned 211 = 256 − 45; signed −45.

11010011ALD3hunsigned 211 · signed −4511010011BLD3hunsigned 211 · signed −45CF1ZF0SF1OF0NEG = 0 − x

NEG AL (0 − AL) gives the same result in one instruction: AL = D3h. The operand ≠ 0 → CF = 1.

11010011ALD3hunsigned 211 · signed −4511010011BLD3hunsigned 211 · signed −45CF0ZF1SF0OF0difference 0 → equal

CMP AL, BL: D3h − D3h = 0 → ZF = 1. Both routes gave the same bit pattern.

00000000AL00hunsigned 0 · signed +011010011BLD3hunsigned 211 · signed −45CF1ZF1SF0OF0100h: the carry falls out

ADD AL, 45: D3h + 2Dh = 100h. Bit 9 falls out (CF = 1), AL = 00h, ZF = 1: −45 + 45 = 0 ✓

0000000000101101AX= 002Dh · unsigned 45 · signed +4511010011BLD3hunsigned 211 · signed −45CF1ZF1SF0OF0

AX = 45 = 002Dh: the same number in 16 bits.

1111111111010011AX= FFD3h · unsigned 65491 · signed −4511010011BLD3hunsigned 211 · signed −45CF1ZF0SF1OF016 bit NEG

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.
11111011−5 or 251+0000010151000000000this bit falls out (does not fit) → CF = 18-bit result = 0

Signed Numbers

Signed Ranges

WidthCombinationsUnsignedSigned (two's complement)Negatives
8 bits2560 … 255−128 … +12780h … FFh
16 bits655360 … 65535−32768 … +327678000h … FFFFh
32 bits4,294,967,2960 … 4,294,967,295−2,147,483,648 … +2,147,483,64780000000h …
  • 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.

−168421½¼⅛ ,

Digit weights: the leftmost bit is −16 (the sign bit); after the point ½, ¼, ⅛. Range −16 … +15.875, step 1/8 = 0.125.

−168421½¼⅛+6.7500110110,36h

+6.75 = 4 + 2 + ½ + ¼ → 00110.110.

−168421½¼⅛+6.7500110110,36h−6.7511001010,CAh

Two's complement (flip + 1; ignore the point): 00110110 → 11001001 + 1 = 11001.010.

−168421½¼⅛+6.7500110110,36h−6.7511001010,CAh−16 + 8 + 1 + ¼ = −6.75 ✓

Check: the sign bit is worth −16: −16 + 8 + 1 + 0.25 = −6.75 ✓

−168421½¼⅛+6.7500110110,36h−6.7511001010,CAh−6.75 × 2³ = −54 → 256 − 54 = 202 = CAh

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.

To the processor CAh is an ordinary byte; where the point sits is the programmer's convention. Addition works unchanged: −6.75 + 2.5 → CAh + 14h = DEh = 11011.110b = −16 + 11.75 = −4.25 ✓

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.
abcarryinsumcarryout
00000
01010
10010
11001
11111
0 + 0= 0carry 0write 00 + 1= 1carry 0write 11 + 0= 1carry 0write 11 + 1= 10carry 1write 0

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

carry397+ 267110110110000001011

110001101b (397) + 100001011b (267). Add column by column, right to left.

carry397+ 2671100110110000001011

Digit 0: 1 + 1 = 2 → write 0, carry 1.

carry397+ 267110011010110000001011

Digit 1: 0 + 1 + 1 (carry) = 2 → write 0, carry 1.

carry397+ 26711001101010110000001011

Digit 2: 1 + 0 + 1 (carry) = 2 → write 0, carry 1.

carry397+ 2671100110101011110000001011

Digit 3: 1 + 1 + 1 (carry) = 3 → write 1, carry 1.

carry397+ 267110011010101111001100001011

Digit 4: 0 + 0 + 1 (carry) = 1 → write 1.

carry397+ 2671100110101011110011000001011

Digit 5: 0 + 0 = 0 → write 0.

carry397+ 26711001101010111100110000001011

Digit 6: 0 + 0 = 0 → write 0.

carry397+ 267110011010101111001100000010111

Digit 7: 1 + 0 = 1 → write 1.

carry397+ 2671100110101011110011000000101110

Digit 8: 1 + 1 = 2 → write 0, carry 1.

carry397+ 267110011010101111001100000010111011= 664

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 =101111

A = 1011.

A =101111B =01106

B = 0110.

A =101111B =01106NOT B =1001ones' complement

Flip the bits of B: 1001.

A =101111B =01106NOT B =1001ones' complement+1 →1010−B (two's c.)

Add 1: 1010 = −6 (4 bits).

A =101111B =01106NOT B =1001ones' complement+1 →1010−B (two's c.)A + (−B) =101015 bits: the leftmost carry is dropped

1011 + 1010 = 10101 (5 bits).

A =101111B =01106NOT B =1001ones' complement+1 →1010−B (two's c.)A + (−B) =101015 bits: the leftmost carry is droppedresult =01015 ✓

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

carry+C679AB959A7Ch = 395485B96h = 23446

Hex addition: digit by digit, right to left. If a digit sum exceeds 15, subtract 16 and carry 1 to the left.

carry+C679AB9512C + 6 = 12 + 6 = 1818 − 16 = 2, carry 1

C + 6 = 18 ≥ 16 → write 2, carry 1.

carry+C679AB9512117 + 9 + 1 = 1717 − 16 = 1, carry 1

7 + 9 + 1 = 17 → write 1, carry 1.

carry+C679AB95121116A + B + 1 = 10 + 11 + 1 = 2222 − 16 = 6, carry 1

A + B + 1 = 22 → write 6, carry 1.

carry+C679AB95121116F9 + 5 + 1 = 15 = Fbelow 16, no carry

9 + 5 + 1 = 15 → write F, no carry.

carry+C679AB95121116FCheck: 39548 + 23446= 62994 ✓= F612h

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

borrow−C679AB95If the top digit is smaller,borrow 16 from the left

Hex subtraction: if the top digit is too small, borrow 1 from the digit to its left; that adds 16 to this digit.

borrow−C679AB956C − 6 = 12 − 6 = 6no borrow

C − 6 = 6.

borrow−C679AB956−1E7 − 9 fails → borrow 167 + 16 − 9 = 14 = E

7 < 9 → borrow from the left: 7 + 16 − 9 = 14 → E; the A on the left becomes 9.

borrow−C679AB956−1E−1EA − 1 = 9, 9 − B fails9 + 16 − 11 = 14 = E

9 < B → borrow again: 9 + 16 − 11 = 14 → E; the 9 on the left becomes 8.

borrow−C679AB956−1E−1E39 − 1 = 88 − 5 = 3

8 − 5 = 3.

borrow−C679AB956−1E−1E3Check: 39548 − 23446= 16102 ✓= 3EE6h

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

15's complementFFFFh − 5B96h= A469hsubtract each digit from F

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.

15's complementFFFFh − 5B96h= A469hsubtract each digit from F16's complementA469h + 1= A46Ah= 10000h − 5B96h

16's complement = 15's complement + 1 = A46Ah. This is −5B96h in 4 digits (NEG).

15's complementFFFFh − 5B96h= A469hsubtract each digit from F16's complementA469h + 1= A46Ah= 10000h − 5B96h9A7C − 5B969A7Ch + A46Ah= 1 3EE6hthe overflowing 1 is dropped → 3EE6h ✓

9A7Ch − 5B96h = 9A7Ch + A46Ah = 13EE6h. The 1 beyond 4 digits is dropped: 3EE6h, the same as direct subtraction.

15's complementFFFFh − 5B96h= A469hsubtract each digit from F16's complementA469h + 1= A46Ah= 10000h − 5B96h9A7C − 5B969A7Ch + A46Ah= 1 3EE6hthe overflowing 1 is dropped → 3EE6h ✓5B96 − 9A7C5B96h + 6584h= C11Ahno overflow → the result is negative

The other way round: 5B96h − 9A7Ch = 5B96h + 6584h (the 16's complement of 9A7Ch) = C11Ah. There is no overflowing digit → the result is negative.

15's complementFFFFh − 5B96h= A469hsubtract each digit from F16's complementA469h + 1= A46Ah= 10000h − 5B96h9A7C − 5B969A7Ch + A46Ah= 1 3EE6hthe overflowing 1 is dropped → 3EE6h ✓5B96 − 9A7C5B96h + 6584h= C11Ahno overflow → the result is negative−(16's c. of C11Ah)= −3EE6h10000h − C11Ah = 3EE6h

To get the magnitude, take the 16's complement again: C11Ah → 3EE6h, so the result is −3EE6h (−16102).

These are the hex counterparts of the binary ones' and two's complement. On the 8086, 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

OperationSignsExample (8 bits)Signed readingOF
A + B+ with +50h + 40h = 90h80 + 64 → −112 ✗1
A + B− with −9Ch + CEh = 6Ah−100 + (−50) → +106 ✗1
A + Bdifferent64h + CEh = 32h100 + (−50) = 50 ✓0 (never)
A − B+ minus −64h − CEh = 96h100 − (−50) → −106 ✗1
A − B− minus +9Ch − 32h = 6Ah−100 − 50 → +106 ✗1
A − Bsame9Ch − 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.MeaningExample → value
~not (flips all bits)~0Fh → F0h (8 bit)
*multiply3*4 → 12
/divide (integer)17/5 → 3
%mod (remainder)17%5 → 2
+add40h+2 → 42h
-subtract (unary −)-5 → FBh
<<shift left1<<4 → 16
>>shift right80h>>3 → 10h
&bitwise AND0F0h&3Ch → 30h
^bitwise XOR0Fh^05h → 0Ah
|bitwise OR0F0h|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, XOR and OR instructions.
  • Constants only: MOV AX, BX*2 is 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.
101100105 V0 Vhigh = 1low = 0

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.
RSClkQQ′ANDANDNORNOR

Encodings and Circuits

Flip-Flop Types

TypeInputsBehavior (on the clock pulse)Characteristic
SR (Set-Reset)S, RS = 1 → Q = 1 (set); R = 1 → Q = 0 (reset); S = R = 0 → no changeS = R = 1 forbidden
D (Data)DLoads its single data input to the output on the clock: Q ← Dregister/memory cell
JKJ, KJ = 1 → set, K = 1 → reset, J = K = 0 → no change, J = K = 1 → toggleno forbidden state, unlike SR
T (Toggle)TWith T = 1 the output flips on every pulse; T = 0 → no changecounters, frequency dividers
Besides set and reset, the JK toggles when J = K = 1; that toggle is what makes it flexible.

Encodings and Circuits

D Flip-Flop Timing

ClkDQQ takes D's current value only on the rising clock edge (dashed lines)
  • 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.
00111110001000000000000000000000signexponent (8 bits)fraction / mantissa (23 bits)313023220

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

00111110001000000000000000000000signexponent (8 bits)fraction / mantissa (23 bits)313023220

Example: 0 01111100 01000000000000000000000 = 3E200000h.

00111110001000000000000000000000signexponent (8 bits)fraction / mantissa (23 bits)313023220

Sign bit 0 → the number is positive: (+1).

00111110001000000000000000000000signexponent (8 bits)fraction / mantissa (23 bits)313023220

Exponent 01111100b = 124 → true exponent 124 − 127 = −3 → 2−3.

00111110001000000000000000000000signexponent (8 bits)fraction / mantissa (23 bits)313023220

Mantissa 0100…0 = 0×2⁻¹ + 1×2⁻² = 0.25 → with the hidden bit 1.25.

00111110001000000000000000000000signexponent (8 bits)fraction / mantissa (23 bits)313023220

Result: (+1) × 2−3 × 1.25 = 1.25 / 8 = 0.15625.

Result (+1) × (2−3) × 1.25; its decimal value is 0.15625.

Encodings and Circuits · Step by step

Encoding IEEE 754: −6.5

00000000000000000000000000000000signexponent (8 bits)fraction / mantissa (23 bits)313023220

Let us convert −6.5 to 32-bit IEEE 754.

10000000000000000000000000000000signexponent (8 bits)fraction / mantissa (23 bits)313023220

Negative → sign bit 1.

10000000000000000000000000000000signexponent (8 bits)fraction / mantissa (23 bits)313023220

6.5 = 110.1b (4 + 2 + 0.5). Normalize: 1.101b × 2².

11000000100000000000000000000000signexponent (8 bits)fraction / mantissa (23 bits)313023220

Exponent: 2 + 127 = 129 = 10000001b.

11000000110100000000000000000000signexponent (8 bits)fraction / mantissa (23 bits)313023220

Mantissa: drop the hidden 1 and pad the remaining 101 with 0s on the right.

11000000110100000000000000000000signexponent (8 bits)fraction / mantissa (23 bits)313023220

Result: 1100 0000 1101 0000 0…0b = C0D00000h.

PatternMeaning
e = 0, f = 0±0
e = 255, f = 0±∞ (e.g. 1/0)
e = 255, f ≠ 0NaN (not a number, e.g. 0/0)
e = 0, f ≠ 0denormalized (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 is 0001 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.
BCD 250010010125binary 2500011001= 19h (16 + 8 + 1)
DecimalBCD
00000
10001
20010
30011
40100
50101
60110
70111
81000
91001

Encodings and Circuits · Step by step

BCD Addition and Adjustment

Let us add BCD 38 and BCD 45.

380011 100038h

38 → 0011 1000.

380011 100038h+ 450100 010145h

45 → 0100 0101.

380011 100038h+ 450100 010145h= ?0111 11017Dh1101 = 13 > 9 → invalid digit

Add in binary: 0111 1101 = 7Dh. The low digit 1101 (13) is invalid in BCD.

380011 100038h+ 450100 010145h= ?0111 11017Dh1101 = 13 > 9 → invalid digit+ 60000 0110adjustment

Add 6 to an invalid digit (or when AF = 1): this skips the 16 − 10 = 6 unused patterns.

380011 100038h+ 450100 010145h= ?0111 11017Dh1101 = 13 > 9 → invalid digit+ 60000 0110adjustment= 831000 001183h1000 0011BCD 83 ✓

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

The 32-bit value 12345678h, starting at address 0100h:Little-endian (Intel x86)78h0100h56h0101h34h0102h12h0103hLSB (78h) at the lowest addressBig-endian (network, PowerPC)12h0100h34h0101h56h0102h78h0103hMSB (12h) at the lowest address
  • 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.
For 01020304h the little-endian memory layout is 04 03 02 01; the big-endian layout is 01 02 03 04.

Encodings and Circuits

Gray Code

DecimalBinarychanged bitGray
0000–000
10011001
20102011
30111010
41003110
51011111
61102101
71111100
  • 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).

B0110binary (6)

B = 0110 (6).

B0110binary (6)B >> 10011shift right one

B is shifted right one bit: 0011.

B0110binary (6)B >> 10011shift right oneB XOR (B >> 1)0101Gray

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
B (AL)1011= 11B >> 1 (BL)G (BL)

AL = 1011b = 11. The Gray code formula: G = B xor (B >> 1).

B (AL)1011= 11B >> 1 (BL)G (BL)

BL = AL: the shift is done on a copy, so B (AL) is kept.

B (AL)1011= 11B >> 1 (BL)0101= 5G (BL)

SHR BL, 1: 0101b. The rightmost bit falls out (CF = 1) and a 0 enters on the left.

B (AL)1011= 11B >> 1 (BL)0101= 5G (BL)1110= 14xorGray(11) = 1110

XOR BL, AL: 1011 xor 0101 = 1110b: the Gray code of 11. The top bit stayed the same as in binary.

B (AL)1100= 12B >> 1 (BH)G (BH)Gray(11) = 1110

AL = 1100b = 12, the next number.

B (AL)1100= 12B >> 1 (BH)G (BH)Gray(11) = 1110

BH = AL.

B (AL)1100= 12B >> 1 (BH)0110= 6G (BH)Gray(11) = 1110

SHR BH, 1: 0110b.

B (AL)1100= 12B >> 1 (BH)0110= 6G (BH)1010= 10xorGray(11) = 1110Gray(12) = 1010only bit 2 differs

XOR → 1010b. From 11 to 12, 3 bits change in binary (1011 → 1100); in Gray code only 1 bit (1110 → 1010).

Summary

Summary

Positional systemsValue = Σ digit × baseposition. Binary (b), hex (h), decimal; 1 hex digit = 4 bits.
ConversionFrom decimal: divide by the base, read the remainders bottom up. Binary ↔ hex: groups of 4.
Signed numbersTwo's complement: flip + 1 (NEG). The MSB is the sign bit; 8 bits −128…127, 16 bits −32768…32767.
EncodingsIEEE 754: sign/exponent(127)/mantissa. BCD: 4 bits per digit, +6 adjustment. x86 is little-endian. Gray: one bit changes.

Summary

Exercises

  1. Convert MCMXCIV and MMXXVI to decimal.
  2. Convert 11001010b to decimal and hex.
  3. Write 2024 in hex and binary.
  4. Write −100 as an 8-bit two's complement number. What does it look like in 16 bits?
  5. What are the 8-bit result, CF and signed reading of 0F0h + 20h?
  6. Do the BCD addition 47 + 36 with adjustment.
  7. What is the value of the IEEE 754 number 0 10000010 01100000000000000000000?
  8. What is the 3-bit Gray code of 5? If 0ABCDh is written as a word at address 0200h, what is [0200h]?
Answers
  1. 1000 + 900 + 90 + 4 = 1994; 2026.
  2. 128 + 64 + 8 + 2 = 202 = CAh.
  3. 7E8h = 0111 1110 1000b.
  4. 256 − 100 = 156 = 9Ch = 1001 1100b; FF9Ch in 16 bits.
  5. 110h → 10h, CF = 1; signed −16 + 32 = +16 is correct (OF = 0).
  6. 0100 0111 + 0011 0110 = 0111 1101 (7Dh) → +6 → 83h = BCD 83.
  7. +1.011b × 2³ = 1011b = 11.0.
  8. 101 ⊕ 010 = 111; [0200h] = CDh, [0201h] = ABh.

Summary

Exercises (2)

  1. Convert 0.375 and 0.2 to binary (8 fraction bits for 0.2).
  2. Write −3.625 in 8-bit Q5.3 format (binary and hex).
  3. Compute 7C5Eh + 3BA9h; compute 3BA9h − 7C5Eh with the 16's complement.
  4. In 8 bits, what are the results and OF for 70h + 30h, 90h − 20h and 90h − F0h?
Answers
  1. 0.375 = 0.011b (0.75 → 0; 1.5 → 1; 1.0 → 1). 0.2 = 0.0011 0011…b → 0.00110011b.
  2. 3.625 × 8 = 29 → −29 = 256 − 29 = E3h = 11100.011b. Check: −16 + 8 + 4 + 0.25 + 0.125 = −3.625.
  3. B807h. The 16's complement of 7C5Eh is 83A2h; 3BA9h + 83A2h = BF4Bh, no overflow → −(10000h − BF4Bh) = −40B5h.
  4. 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).

The End

Chapter 1.2: Number Systems
Previous: Chapter 1.1: Introduction · Next: Chapter 2.1: 8086 Architecture
1 / 1 Sercan KÜLCÜ. All rights reserved.