-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.asm
154 lines (118 loc) · 3.12 KB
/
setup.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
; ***************************************************************************************
; ***************************************************************************************
; ** Basic setup and functions for asm programs
.DATA
ERRORMSG_MEMRESIZE db "Could not resize memory", 13, 10, "$"
GOODBYE_MSG db "Merci d'avoir joue. Vos progres ont ete sauvegardes!", 13, 10, "$"
.CODE
SETUP:
; Set up some basic stuff for the code at start
; i.e. freeing unnecessary memory
; Should be called at start (otherwise stack size may not be estimated properly)
; Registers are changed
; Resize the memory allocated to our program
; Subtract SS and ES (do not modify either!)
mov bx, ss
mov ax, es
sub bx, ax
; Find the end of the stack and shift 4bits to the rights
; Also add 1 to be safe
mov ax, sp
add ax, 0fh ; that's to make sure we can round up
shr ax, 4
inc ax
; now add both and adjust
add bx, ax
mov ah, 4Ah
int 21h
jc CantResizeMemory
; also place DS on the data segment by default
mov ax, @DATA
mov ds, ax
ret
RESET_SCREEN:
; go back to text mode - this function modifies AX
mov ax, 0003h
int 10h
ret
ENDPROG:
; This is simply the end of program
; reset the screen - display a nice message and exit back to DOS
; have we won ??!?
mov al, [IS_WINNER]
or al, al
jz @@nope_didnt_win
call WINNING_MESSAGE
jmp @@quit_prog
@@nope_didnt_win:
mov dx, offset GOODBYE_MSG
mov ah, 09h
int 21h
@@quit_prog:
; free memory
mov ax, [BUFFER_PTR]
mov es, ax
mov ah, 49h
int 21h
call MemoryStillAvail
mov ah, 4ch
int 21h
; ********************************************************************************************
; ********************************************************************************************
; ** Various functions
CantResizeMemory:
call RESET_SCREEN
; This routine is called if DOS can't resize the program memory
mov ax, @DATA
mov ds, ax
mov dx, offset ERRORMSG_MEMRESIZE
mov ah, 9
int 21h
jmp ENDPROG
; Function to call to check how much memory is still available
MemoryStillAvail:
pusha
push ds
mov ax, @DATA
mov ds, ax
; clear the string first before writing down the size
mov di, offset memory_size
mov cx, 5
call clear_ascii
mov bx, 0ffffh
mov ah, 48h
int 21h
mov ax, bx
shr ax, 6
; convert it to ASCII
mov di, offset memory_size
call convert_ax_ascii
; Then print it out
mov dx, offset msg_memsize
mov ah, 9
int 21h
mov si, offset memory_size
mov cx, 5
call print_ascii
mov dx, offset msg_memsize2
mov ah, 9
int 21h
pop ds
popa
ret
clear_ascii:
; CX = size of string
; DI = address of the string to clear
push di
push cx
push ax
push es
push ds
pop es
xor ax, ax
rep stosb
pop es
pop ax
pop cx
pop di
ret