-
Notifications
You must be signed in to change notification settings - Fork 0
/
7_question.asm
199 lines (150 loc) · 4.63 KB
/
7_question.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
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
; Program description
StSeg Segment STACK 'STACK'
DB 100H DUP (?)
StSeg ENDS
DtSeg Segment
N_fac dw 0
r_fac dw 0
n_r_fac dw 0
; place data here
DtSeg ENDS
CDSeg Segment
ASSUME CS: CDSeg, DS: DtSeg, SS: StSeg
Start:
MOV AX, DtSeg
MOV DS, AX
; my code starts here
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; input n , r
call input_routine
push dx ; dx = n
mov ax, dx ; ax = dx
call input_routine
push dx ; dx = r
sub ax, dx ; ax = n - r
push ax
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
mov ax, 1 ; ax = 1
pop cx
call factorial ; call function to calc fact(cx)
mov n_r_fac , ax ; n_r_fac = ax
mov ax, 1 ; ax = 1
pop cx
call factorial ; call function to calc fact(cx)
mov r_fac , ax ; r_fac = ax
mov ax, 1 ; ax = 1
pop cx
call factorial ; call fucntion to calc fact(cx)
mov N_fac , ax ; N_fac = ax
mov ax, N_fac
mov dx, 0
div n_r_fac ; ax = n! / (n-r)!
mov dx, 0
div r_fac ; ax /= r!
call print
; my code ends here
MOV AH, 4CH
MOV AL, 0
INT 21H
factorial PROC ; ax should be 1 at first, pop first value from stack save it in cx then calculate factorial(cx) store return in AX
add cx, 0 ; cx += 0
jz is_zero_f ; if cx == 0 jump to is_zero_f
mul cx ; ax *= cx
sub cx, 1 ; cx --
jz is_zero_f ; goto is_zero_f
call factorial ; recursive call
is_zero_f:
ret
factorial ENDP
print PROC ; print value in ax
add ax,0 ; sign bit = 1 iff ax is neg
jz is_zero ; ax == 0 then jump
js is_neg_print ; if is neg make it pos and print '-'
continue:
mov cx,0 ; cx = dx = 0
mov dx,0
division:
; if ax is zero
cmp ax,0
je print_each_digit ; time to print all elements in stack
mov bx,0AH ; bx = 10
div bx ; ax //= bx
add dx,48 ; remainder:dx += 48 [ascii representation]
push dx ; store in stack
inc cx ; how much digits does the number have?
xor dx,dx ; dx = 0
jmp division ; continue division
print_each_digit:
cmp cx,0 ; if cx == 0 code is ended
je end ; if is zero go to end
pop dx ; pop digit
; interrupt to print
mov ah,02h
int 21h
dec cx ; cx --
jmp print_each_digit
is_neg_print:
push ax ; save ax
mov ah,02h ; print -
mov dx, 45
int 21h
pop ax ; load ax
neg ax ; negate ax
jmp continue ; begin division
is_zero:
mov dx, 48
; interrupt to print
mov ah,02h
int 21h
jmp end
end:
ret
print ENDP
input_routine PROC NEAR ; input int and store it in DX
pushf
push AX
push BX
push CX
push 0000H ; flag is 0
xor CX, CX ; CX = 0
xor DX, DX ; DX = 0
input_number:
mov AH, 07H ; read char
INT 21H
CMP AL, 2DH ; AL = '-'
jz is_neg_ir
sub AL, 0DH ; AL -= 13
jz end_ir ; if AL = '\n' go to end
sub AL, 23H ; AL -= 35 (in fact AL -= 48 to make binary from ascii)
Xor AH, AH ; AH = 0
xchg AX, CX ; prepare for mul
xchg DX, AX ; store valuable values in CX to retrieve after mul
MOV BX, 000AH ; BX = 10
MUL BX ; DX, AX = AX * 10
xchg AX, DX ; retrieve values of DX and AX
xchg AX, CX ; retrieve values of DX and AX
ADD DX, AX ; DX += AX
JMP input_number ; continue reading chars and convert them to int
is_neg_ir:
push 01H ; flag for negative number is 1
JMP input_number ; continue reading chars and convert them to int
end_ir: ; final
POP AX ; AX is the flag
CMP AX, 0001H ; if AX == 1 then zf = 1
jz negate_num ; the number is negative
POP CX
POP BX
POP AX
POPF
RET
negate_num:
POP AX
not DX ; 1's comp
ADD DX, 01H ; 2's comp
POP CX
POP BX
POP AX
POPF
RET
input_routine ENDP
CDSeg ENDS
END Start