Hexadecimal (hex) and binary are fundamental number systems in low-level programming. Hex (base-16) is a compact way to represent binary (base-2) data, making it easier for programmers to read and write machine-level instructions.
In assembly language, working with hex and binary is essential because:
- Registers store binary data, but debugging often requires a hex representation.
- Memory addresses and opcodes are frequently displayed in hex.
- Embedded systems rely on binary manipulation for hardware control.
In this guide, we’ll break down how to convert hex to binary in assembly language with practical examples.
Table of Contents
Basics of Hexadecimal and Binary
Before diving into assembly, let’s recap hex and binary:
Hex (Base-16) | Binary (Base-2) |
---|---|
0 | 0000 |
1 | 0001 |
2 | 0010 |
3 | 0011 |
4 | 0100 |
5 | 0101 |
6 | 0110 |
7 | 0111 |
8 | 1000 |
9 | 1001 |
A (10) | 1010 |
B (11) | 1011 |
C (12) | 1100 |
D (13) | 1101 |
E (14) | 1110 |
F (15) | 1111 |
Key Rule:
- 1 hex digit = 4 binary bits (since 16 = 2⁴).
- To convert hex to binary, replace each hex digit with its 4-bit binary equivalent.
Why Convert Hex to Binary in Assembly?
In assembly language:
✔ Registers store binary values, but debugging tools display them in hex.
✔ Bitwise operations (AND, OR, SHIFT) require binary understanding.
✔ Hardware programming (e.g., microcontrollers) often uses binary configurations.
Step-by-Step Hex-to-Binary Conversion
Manual Conversion Example
Let’s convert 2F
(hex) to binary:
- Split
2F
into two digits:2
andF
. - Replace each with its 4-bit binary equivalent:
2
→0010
F
→1111
- Combine them:
2F
(hex) =00101111
(binary).
Hex-to-Binary Conversion in Assembly (x86/MASM)
Pseudo-Code Logic
- Read a hex digit (ASCII input).
- Convert ASCII to numeric value:
'0'-'9'
→ Subtract'0'
(0x30).'A'-'F'
→ Subtract'A'
(0x41) and add 10.
- Map to 4-bit binary and store in a register.
- Print binary digits by shifting and masking.
Assembly Code Example (x86 MASM)
.model small
.stack 100h
.data
hexInput db 'A' ; Sample hex digit
binaryOutput db 8 dup('0'), '$' ; 8-bit binary result
.code
main proc
mov ax, @data
mov ds, ax
; Convert hex digit to binary
mov al, hexInput
cmp al, '9'
jbe digit_0_to_9
sub al, 'A' - 10 ; A-F → 10-15
jmp convert
digit_0_to_9:
sub al, '0' ; 0-9 → 0-9
convert:
mov cx, 8 ; 8 bits to process
lea si, binaryOutput
convert_loop:
rol al, 1 ; Rotate left (MSB → CF)
mov bl, '0'
jnc store_bit
mov bl, '1'
store_bit:
mov [si], bl
inc si
loop convert_loop
; Print binary result
mov ah, 09h
lea dx, binaryOutput
int 21h
mov ah, 4Ch
int 21h
main endp
end main
Explanation:
- Takes a hex digit (
'A'
) and converts it to binary (1010
). - Uses bit rotation (
ROL
) to extract each bit. - Stores the result as an 8-bit ASCII string.
Use Cases for Hex-to-Binary Conversion
- Debugging: Inspecting register values in binary.
- Embedded Systems: Configuring hardware registers.
- Network Protocols: Parsing binary packet headers.
- Game Development: Optimising bit-level operations.
[ALSO READ: How to convert Hexadecimal to Binary?]
FAQs
Why is 1 hex digit equal to 4 binary bits?
Because 16 (hex base) = 2⁴ (binary), so each hex digit perfectly represents 4 bits.
Which assembly languages support hex-to-binary conversion?
All major ones (x86, ARM, MIPS) support bitwise operations needed for conversion.
What’s the easiest way to implement this in assembly?
Using lookup tables or bit shifting (as shown in the example).
Can I convert a full hex string (e.g., “1A3F”) to binary?
Yes! Process each hex digit sequentially and concatenate the 4-bit results.
Is hex or binary more efficient in assembly?
Hex is easier for humans, but binary is what the CPU actually processes.
Conclusion
Understanding hex-to-binary conversion is crucial for low-level programming. By mastering this in assembly, you’ll gain deeper control over hardware and debugging.
Try it yourself! Use our AiCalculator.in Hex to Binary Tool to experiment with conversions. Happy coding!