Overview of Registers
Registers are high-speed storage units within the processor, used for temporarily storing data and instructions. Compared to memory access, register operations are extremely fast because data is processed directly within the processor.
Classification of Registers
1. General Purpose Registers
Data Registers
32-bit Data Registers:
- EAX, EBX, ECX, EDX
Hierarchy:
32-bit 16-bit 8-bit (high) 8-bit (low)
EAX = AX = AH AL
EBX = BX = BH BL
ECX = CX = CH CL
EDX = DX = DH DL
Special Purpose:
section .text
global _start
_start:
; EAX - Accumulator, used for arithmetic operations and system calls
mov eax, 10 ; Store immediate value
add eax, 5 ; Addition operation
mov eax, 4 ; System call number (sys_write)
; EBX - Base register, used for indexed addressing
mov ebx, 1 ; File descriptor (stdout)
lea ebx, [array] ; Load base address of array
; ECX - Counter register, used for loop counting
mov ecx, 10 ; Loop counter
mov ecx, message ; String pointer
; EDX - Data register, used for I/O and large number operations
mov edx, length ; Data length
mov edx, 0 ; High part result of multiplication
; Example of using 8-bit and 16-bit registers
mov al, 'A' ; Character operation
mov ax, 1000 ; 16-bit value
mov bx, cx ; Transfer between registers
Pointer Registers
32-bit Pointer Registers:
- EIP, ESP, EBP
section .text
global _start
_start:
; EIP - Instruction pointer (automatically managed)
call subroutine ; EIP changes automatically when calling subroutine
jmp label ; Modify EIP when jumping
; ESP - Stack pointer
push eax ; ESP decreases automatically
pop ebx ; ESP increases automatically
mov esp, ebp ; Restore stack pointer
; EBP - Base pointer
push ebp ; Save old base pointer
mov ebp, esp ; Set new stack frame
sub esp, 16 ; Allocate space for local variables
; Access parameters and local variables
mov eax, [ebp+8] ; First parameter
mov [ebp-4], eax ; Local variable
Index Registers
32-bit Index Registers:
- ESI, EDI
section .data
source db 'Hello World', 0
destination times 12 db 0
array dd 1, 2, 3, 4, 5
section .text
global _start
_start:
; ESI - Source index register
mov esi, source ; Address of source string
; EDI - Destination index register
mov edi, destination ; Address of destination buffer
; Example of string copy
mov ecx, 11 ; Length of string
cld ; Clear direction flag (move forward)
rep movsb ; Copy byte by byte
; Example of array traversal
mov esi, array ; Starting address of array
mov ecx, 5 ; Number of elements
mov eax, 0 ; Accumulated sum
sum_loop:
add eax, [esi] ; Accumulate array elements
add esi, 4 ; Move to the next element (4 bytes)
loop sum_loop ; Loop until ECX=0
2. Control Registers
Flags Register
EFLAGS Register Structure:
Bit Position: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Flags: OF DF IF TF SF ZF AF PF CF
section .text
global _start
_start:
; Arithmetic operations affect flags
mov eax, 10
add eax, 20 ; Affects OF, SF, ZF, AF, PF, CF
; Comparison operations affect flags
mov ebx, 15
cmp eax, ebx ; Affects SF, ZF, PF, CF, AF, OF
; Conditional jumps based on flags
jg greater ; Jump if greater (SF=OF and ZF=0)
jl less ; Jump if less (SF≠OF)
je equal ; Jump if equal (ZF=1)
; Direction flag controls string operation direction
std ; Set direction flag (DF=1, right to left)
cld ; Clear direction flag (DF=0, left to right)
; Interrupt flag controls interrupt response
cli ; Clear interrupt flag (IF=0, disable interrupts)
sti ; Set interrupt flag (IF=1, enable interrupts)
greater:
; Handle greater case
jmp end
less:
; Handle less case
jmp end
equal:
; Handle equal case
end:
Detailed Example of Flags
section .text
global _start
_start:
; Carry Flag (CF) Example
mov al, 0xFF
add al, 1 ; AL=0, CF=1 (carry)
mov al, 0x7F
add al, 1 ; AL=0x80, CF=0 (no carry)
; Zero Flag (ZF) Example
mov eax, 10
sub eax, 10 ; EAX=0, ZF=1 (result is zero)
mov eax, 5
dec eax ; EAX=4, ZF=0 (result is non-zero)
; Sign Flag (SF) Example
mov eax, 5
sub eax, 10 ; EAX=-5, SF=1 (result is negative)
mov eax, 10
sub eax, 5 ; EAX=5, SF=0 (result is positive)
; Overflow Flag (OF) Example
mov al, 0x7F ; +127
add al, 1 ; AL=0x80 (-128), OF=1 (signed overflow)
mov al, 0x80 ; -128
sub al, 1 ; AL=0x7F (+127), OF=1 (signed overflow)
; Parity Flag (PF) Example
mov al, 0x03 ; Binary: 00000011 (2 ones, even)
add al, 0 ; PF=1 (even number of ones)
mov al, 0x01 ; Binary: 00000001 (1 one, odd)
add al, 0 ; PF=0 (odd number of ones)
3. Segment Registers
16-bit Segment Registers:
- CS, DS, SS, ES, FS, GS
section .data
message db "Segment Register Demo", 0
data_value dd 1234
section .bss
stack_space resb 64
section .text
global _start
_start:
; CS - Code segment register (automatically managed)
; Current instruction address = CS:EIP
; DS - Data segment register
mov eax, [data_value] ; Default uses DS segment
; Explicitly specify segment register
mov ax, ds
mov es, ax ; Set ES to data segment
mov eax, [es:data_value] ; Access data using ES segment
; SS - Stack segment register
; Stack operations automatically use SS:ESP
; Set custom stack
mov ax, ss
mov ds, ax ; Temporarily use data segment to access stack
mov ebx, [esp+4] ; Access stack parameter
; FS and GS - Additional segment registers
; Used for operating system special purposes or additional data segments
; System call example
mov eax, 4 ; sys_write
mov ebx, 1 ; stdout
mov ecx, message ; String address (DS segment)
mov edx, 19 ; String length
int 0x80
mov eax, 1 ; sys_exit
mov ebx, 0 ; Exit code
int 0x80
Comprehensive Example Program
section .data
stars_msg db 'Displaying 9 stars', 0xa
stars_len equ $ - stars_msg
newline db 0xa
section .bss
; No uninitialized data
section .text
global _start
_start:
; Display message - Demonstrate multiple registers working together
mov edx, stars_len ; EDX = message length
mov ecx, stars_msg ; ECX = message address
mov ebx, 1 ; EBX = file descriptor (stdout)
mov eax, 4 ; EAX = system call number (sys_write)
int 0x80 ; Call kernel
; Display 9 stars - Demonstrate loop and counter register
mov ecx, 9 ; ECX = loop counter
mov ebx, 1 ; EBX = file descriptor
display_stars:
; Save counter
push ecx
push ebx
; Display one star
mov edx, 1 ; Length = 1
mov ecx, star_char ; Star character
mov eax, 4 ; sys_write
int 0x80
; Restore registers
pop ebx
pop ecx
; Loop control
loop display_stars ; ECX decrements, if not 0 then loop
; Display newline
mov edx, 1 ; Length
mov ecx, newline ; Newline character
mov ebx, 1 ; stdout
mov eax, 4 ; sys_write
int 0x80
; Program exit
mov eax, 1 ; sys_exit
mov ebx, 0 ; Exit code
int 0x80
section .data
star_char db '*'
Best Practices for Register Usage
1. Register Preservation
subroutine:
push ebp ; Save base pointer
push ebx ; Save callee-saved registers
push esi
push edi
; Subroutine code
pop edi ; Restore registers
pop esi
pop ebx
pop ebp
ret
2. Efficient Data Transfer
; Good practice - Use registers for transfer
mov eax, ebx
add eax, ecx
; Bad practice - Excessive memory access
mov [temp], ebx
mov eax, [temp]
add eax, ecx
3. Register Selection Strategy
; Choose appropriate registers based on purpose
mov eax, [value1] ; EAX for arithmetic operations
mov ebx, [base_addr] ; EBX for base address
mov ecx, counter ; ECX for counting
mov edx, data_size ; EDX for data size
Conclusion
Registers are the core of assembly programming, and proper use of registers can significantly improve program performance:
- Data Registers: Used for arithmetic operations and data storage
- Pointer Registers: Manage program flow and stack operations
- Index Registers: Handle array and string operations
- Control Registers: Control program flow and status checks
- Segment Registers: Manage memory segmentation and access permissions
Mastering the characteristics and uses of various registers is key to writing efficient assembly programs.