-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathblock_copy.asm
More file actions
70 lines (60 loc) · 2.81 KB
/
block_copy.asm
File metadata and controls
70 lines (60 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
; =============================================================================
; TITLE: Memory Block Copy
; DESCRIPTION: Demonstrate efficient memory-to-memory data transfer using
; the 8086 specialized string instruction MOVSB.
; AUTHOR: Amey Thakur (https://github.com/Amey-Thakur)
; REPOSITORY: https://github.com/Amey-Thakur/8086-ASSEMBLY-LANGUAGE-PROGRAMS
; LICENSE: MIT License
; =============================================================================
.MODEL SMALL
.STACK 100H
; -----------------------------------------------------------------------------
; DATA SEGMENT
; -----------------------------------------------------------------------------
.DATA
SOURCE DB 'Hello, World!', 0 ; Null-terminated source string
SOURCE_LEN EQU $ - SOURCE ; Automatically calculate length
DEST DB 20 DUP(0) ; Destination buffer (initialized to 0)
MSG DB 'Memory block transfer successfully completed!$'
; -----------------------------------------------------------------------------
; CODE SEGMENT
; -----------------------------------------------------------------------------
.CODE
MAIN PROC
; Initialize Segment Registers
MOV AX, @DATA
MOV DS, AX ; DS points to Source segment
MOV ES, AX ; ES points to Destination segment (same here)
; -------------------------------------------------------------------------
; STRING INSTRUCTION SETUP
; Source Pointer: DS:SI
; Destination Pointer: ES:DI
; Count: CX
; -------------------------------------------------------------------------
LEA SI, SOURCE ; Source offset
LEA DI, DEST ; Destination offset
MOV CX, SOURCE_LEN ; Number of bytes to copy
; CLD (Clear Direction Flag): Ensures SI and DI increment (forward)
CLD
; REP MOVSB: Repeat 'Move String Byte' while CX > 0
; Automatically handles: ES:[DI] = DS:[SI]; SI++; DI++; CX--;
REP MOVSB
; Verify by printing success message
LEA DX, MSG
MOV AH, 09H ; DOS: Print string
INT 21H
; Return to DOS
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
; =============================================================================
; TECHNICAL NOTES
; =============================================================================
; 1. PERFORMANCE:
; - 'REP MOVSB' is significantly faster than a manual loop and MOV instruction.
; - Always ensure ES is correctly initialized as many string instructions
; implicitly use ES for the destination.
; 2. FLAGS:
; - Direction Flag (DF): 0 (CLD) for forward, 1 (STD) for backward.
; = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =