-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_string_to_packed_bcd.asm
96 lines (59 loc) · 1.72 KB
/
convert_string_to_packed_bcd.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
name "convert"
; this program uses a subroutine written in 8086 assembly language
; that can be used for converting a string of number
; (max of 4 ascii digit) to equivalent packed bcd digits.
; bcd is binary coded decimal.
; this program does no screen output.
; to see results click "vars".
org 100h
jmp start
string db '1234' ; 4 ascii digits.
packed_bcd dw ? ; two bytes (word) to store 4 digits.
start:
lea bx, string
lea di, packed_bcd
call pack_to_bcd_and_binary
ret ; return to operating system.
; subroutine written in 8086 assembly language
; that can be used for converting a string of number
; (max of 4 ascii digit) to equivalent packed
; bcd digits.
; input parameters:
; bx - address of source string (4 ascii digits).
; output:
; di - must be set to address for packed bcd (2 bytes).
pack_to_bcd_and_binary proc near
pusha
; point to 2 upper digits of packed bcd:
; (assumed that we have 4 digits)
add di, 1
; loop only for 2 because every time we
; read 2 digits (2 x 2 = 4 digits)
mov cx, 2
; reset packed bcd:
mov word ptr [di], 0
; to convert a char (0..9) to digit we need
; to subtract 48 (30h) from its ascii code,
; or just clear the upper nibble of a byte.
; mask: 00001111b (0fh)
next_digit:
mov ax, [bx] ; read 2 digits.
and ah, 00001111b
and al, 00001111b
; 8086 and all other Intel's microprocessors store less
; significant byte at lower address.
xchg al, ah
; move first digit to upper nibble:
shl ah, 4
; pack bcd:
or ah, al
; store 2 digits:
mov [di], ah
; next packed bcd:
sub di, 1
; next word (2 digits):
add bx, 2
loop next_digit
popa
ret
pack_to_bcd_and_binary endp