-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMemory.ASM
122 lines (107 loc) · 4.14 KB
/
Memory.ASM
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
;==============================================================================================
; FILE : Memory.ASM
; Memory routines for the tool MXEdit
; Author : yizhang82
; Start Date : 2002-9-2
; End Date : 2002-9-9
;==============================================================================================
.MODEL SMALL, C
INCLUDE MEMORY.INC
.CODE
InitMem PROC FAR C
; assume DOS 3.0 or higher
mov ah, 62h ; Query DOS for PSP
int 21h ; bx - PSP segment address
mov es, bx ; Point ES to block
mov bx, 500h ; New block size
mov ah, 4ah ; Function number
int 21h ; Resize Memory Block
ret
InitMem ENDP
MemResize PROC FAR C, Mem:WORD, Bytes:WORD, OldSize:WORD
mov es, Mem
mov bx, Bytes
mov ah, 4ah
int 21h
.IF carry?
; FAIL, now we allocate another block of memory instead
INVOKE MemAlloc, Bytes
.IF ax == NULL
; memory not enough
ret
.ENDIF
push ds
mov dx, es
mov ds, dx
mov es, ax
xor di, di
xor si, si
cld
mov cx, OldSize
REP movsb
pop ds
push ax
INVOKE MemFree, Mem
.IF ax == NULL
; error freeing memory
pop ax
INVOKE MemFree, ax
ret
.ENDIF
pop ax
.ELSE
mov ax, es
.ENDIF
ret
MemResize ENDP
MemAlloc PROC FAR C, Bytes:WORD
mov ax, Bytes
mov bx, Bytes
mov cx, 16
xor dx, dx
div cx
.IF DX != 0 ; Bytes is a multiple of 16, counted in para
mov bx, Bytes
add bx, 16
sub bx, dx ; bx is a bit longer than Bytes
.ENDIF
; whatever, bx is always bytes which to be allocated
xor dx, dx
mov ax, bx
mov cx, 16
div cx ; perform a 16-bit division
; ax - paras to allocate
mov bx, ax
;xor cx, cx
xor al, al
mov ah, 48h ; dos function 48h, allocate memory
int 21h
jc ErrorInAllocate ; CF = 1 Error
; CF = 0 Success, ax - segment of the memory
ret
ErrorInAllocate :
mov ax, NULL
ret
MemAlloc ENDP
MemFree PROC FAR C, MemSeg:WORD
mov ax, MemSeg
mov es, ax
mov ah, 49h
xor cx, cx
int 21h
jc ErrorInFree
mov ax, 1
ret
ErrorInFree:
mov ax, 0
ret
MemFree ENDP
;================================================================
; TEST CODE
;================================================================
;.STARTUP
;INVOKE InitMem
;INVOKE MemAlloc, 25 ; allocate 25 bytes
;INVOKE MemFree, ax
;.EXIT 0
END