-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGCD.asm
121 lines (106 loc) · 2.59 KB
/
GCD.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
.MODEL SMALL
.STACK 100H
.DATA
X DW ? ; Variable for the first input number
Y DW ? ; Variable for the second input number
Z DW 10 ; Constant for base 10 conversion
CR EQU 0DH ; Carriage Return
LF EQU 0AH ; Line Feed
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
; Input the first number into BX
XOR BX, BX
CALL INPUT_NUMBER
MOV X, BX ; Store the first number in X
; Print new line
CALL PRINT_NEWLINE
; Input the second number into BX
XOR BX, BX
CALL INPUT_NUMBER
MOV Y, BX ; Store the second number in Y
; Print new line
CALL PRINT_NEWLINE
; Calculate GCD
MOV AX, X
MOV BX, Y
CALL GCD
; Output the GCD
MOV CX, AX ; GCD is now in AX, move it to CX for printing
CALL OUTPUT_NUMBER
; Exit program
MOV AH, 4CH
INT 21H
MAIN ENDP
INPUT_NUMBER PROC
; Read a number from the keyboard and store it in BX
LOCAL INPUT_LOOP, END_INPUT_LOOP
INPUT_LOOP:
MOV AH, 1
INT 21H
CMP AL, CR ; Check for Carriage Return
JE END_INPUT_LOOP
CMP AL, LF ; Check for Line Feed
JE END_INPUT_LOOP
SUB AL, '0' ; Convert ASCII to digit
MOV AH, 0
MOV CX, AX
MOV AX, 10
MUL BX ; BX = BX * 10
ADD BX, CX ; BX = BX + digit
JMP INPUT_LOOP
END_INPUT_LOOP:
RET
INPUT_NUMBER ENDP
PRINT_NEWLINE PROC
MOV AH, 2
MOV DL, CR
INT 21H
MOV DL, LF
INT 21H
RET
PRINT_NEWLINE ENDP
GCD PROC
; Calculate GCD of AX and BX, result in AX
LOCAL LOOP_START, END_GCD
LOOP_START:
CMP AX, BX
JE END_GCD
JG GREATER
JL LOWER
GREATER:
SUB AX, BX
JMP LOOP_START
LOWER:
SUB BX, AX
JMP LOOP_START
END_GCD:
RET
GCD ENDP
OUTPUT_NUMBER PROC
; Output the number in CX
LOCAL OUTPUT_LOOP, END_OUTPUT, DIVIDE_STEP
MOV DX, 0 ; Clear DX
MOV BX, Z ; Set divisor (10)
OUTPUT_LOOP:
CMP CX, 0
JE END_OUTPUT
DIVIDE_STEP:
MOV AX, CX
XOR DX, DX ; Clear DX for division
DIV BX ; AX / 10, quotient in AX, remainder in DX
PUSH DX ; Save the remainder (digit)
MOV CX, AX
JMP OUTPUT_LOOP
END_OUTPUT:
MOV AH, 2
PRINT_DIGIT_LOOP:
POP DX
ADD DL, '0' ; Convert to ASCII
INT 21H ; Print digit
CMP SP, BP ; Check if all digits are printed
JNE PRINT_DIGIT_LOOP
RET
OUTPUT_NUMBER ENDP
END MAIN