-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrc16.asm
More file actions
280 lines (139 loc) · 5.24 KB
/
crc16.asm
File metadata and controls
280 lines (139 loc) · 5.24 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
.MODEL small
.STACK 4096
.DATA
message DB 10 DUP(0)
crc_l DB 0FFh
crc_h DB 0FFh
polynomial DW 0A001h
exit_notification DB 10, 13, 10, 'CRC16 = $'
message_2 DB 10, 13, 10, "Enter your text to calculate CRC16: $"
message_3 DB 10, 13, 10, "{ Continue -> C (Upper case) }---/\---{ Exit -> E (Upper case) }: $"
final_message DB 10, 13, 10, "The program has ended correctly! $"
auxiliary_string DB 10, 13, 10, 'Wrong character! Please try again!', 13, 10, '$'
.CODE
main PROC
mov ax, @data
mov ds, ax
mov crc_l, 0FFh
mov crc_h, 0FFh
mov dx,OFFSET message_2
mov ah,09h
int 21h
lea di, message
; This code reads characters from the keyboard one by one into the message buffer until the Enter key (code 13) is pressed, after which it terminates the input.
read_loop:
mov ah, 1
int 21h
cmp al, 13
je input_done
mov [di], al
inc di
jmp read_loop
; This code places a zero (0) byte at the end of the entered string (to mark the end of the text) and loads the start address of the message string into the SI register for further processing.
input_done:
mov byte ptr [di], 0
lea si, message
; Reads one character at a time from the line, checks the end, and starts processing for CRC16.
next_byte:
lodsb
cmp al, 0
je done
xor al, crc_l
mov crc_l, al
mov cx, 8
; Shifts the CRC one bit to the right and, if the shift causes a carry (overflow), proceeds to the XOR operation with the polynomial to update the CRC.
bit_loop:
mov ax, 0
mov al, crc_l
mov ah, crc_h
shr ax, 1
jc xor_polynomial
jmp store_crc
; Performs XOR between register AX (current CRC) and the specified polynomial 0A001h if there was a carry during the shift.
xor_polynomial:
xor ax, polynomial
; Stores the updated CRC value in crc_l and crc_h, repeats the cycle for the next bit, and after 8 bits moves on to processing the next character.
store_crc:
mov crc_l, al
mov crc_h, ah
loop bit_loop
jmp next_byte
; Displays the message CRC16 =, then sequentially displays the most significant (crc_h) and least significant (crc_l) bytes of the calculated CRC in hexadecimal format, after which it proceeds to select an action (continue or exit).
done:
mov dx, OFFSET exit_notification
mov ah,09h
int 21h
mov al, crc_h
call print_hex_byte
mov al, crc_l
call print_hex_byte
call next
; The print_hex_byte procedure displays the contents of the AL register in hexadecimal format (2 characters) on the screen.
print_hex_byte PROC
push ax
push bx
mov bl, al
shr al, 4
call print_hex_digit
mov al, bl
and al, 0Fh
call print_hex_digit
pop bx
pop ax
ret
print_hex_byte ENDP
; The print_hex_digit procedure displays a single hexadecimal character (0–9, A–F) passed in the AL register on the screen.
print_hex_digit PROC
cmp al, 9
jbe outer_print_num
add al, 7
outer_print_num:
add al, '0'
mov dl, al
mov ah, 02h
int 21h
ret
print_hex_digit ENDP
; Processes user input for repeat or exit, clears the screen, displays a message, and proceeds according to the selection (C - repeat, E - exit, other error).
next PROC
call clear_away
mov dx,OFFSET message_3
mov ah,09h
int 21h
mov ah,01h
int 21h
cmp al,'C'
jz EnteredC
cmp al,'E'
jz EnteredE
jnz ErrorPressF
; If the user enters ‘C’, clears the input buffer and restarts the program from the beginning.
EnteredC:
call clear_away
jmp main
; If the user enters ‘E’, displays a farewell message and terminates the program via interrupt int 21h with function 4Ch.
EnteredE:
mov dx, OFFSET final_message
mov ah,09h
int 21h
mov ah,04ch
int 21h
; If an incorrect character is entered, displays an error message and restarts the action selection via next.
ErrorPressF:
mov dx,OFFSET auxiliary_string
mov ah,09h
int 21h
call next
next ENDP
; Clears the message array of 10 bytes, writing a zero (0) to each one.
clear_away PROC
mov cx, 10
lea di, message
; Cyclically writes zero (0) to each byte of the message buffer, clearing it completely.
clear_loop:
mov byte ptr [di], 0
inc di
loop clear_loop
ret
clear_away ENDP
END main