彙編求最小公約數

 實現了遞歸的調用app

輸入和輸出
格式爲輸入一個數,回車
再輸入一個數,回車
打印答案ide

? View Code 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
DATAS SEGMENT
    ;此處輸入數據段代碼
    answer   db 'the answer is $'
    scanf db 'aaaaaaaaaaa'
    a	  dw  20 dup(?)
DATAS ENDS
 
STACKS SEGMENT
    ;此處輸入堆棧段代碼
    dw 100 dup(?)
 
STACKS ENDS
 
CODES SEGMENT
    ASSUME CS:CODES,DS:DATAS,SS:STACKS
START:
    MOV AX,DATAS
    MOV DS,AX
    ;此處輸入代碼段代碼
    lea bx,a
 	call readnum
	mov [bx],cx
	inc bx
	inc bx
    call readnum
    mov [bx],cx
    mov ax,[bx]
    push [bx]
    dec bx
    dec bx
    push [bx]
    mov cx,[bx]
    mov bx,cx
    call gcd
    mov ax,cx
    call PRINTAX
    MOV AH,4CH
    INT 21H
 
gcd proc far
	mov  bp,sp
	mov  ax,[bp+4]
	mov  bx,[bp+6]
	cmp bx,0
	jne gcd2
gcd1:
	mov cx,ax
	jmp outit
gcd2: cwd
	div bx
	mov ax,dx
	push ax
	push bx
	call gcd
	pop bx
	pop ax
outit:
ret
gcd endp
 
readnum proc      ;讀取一個數,以回車中斷,數應小於256
				  ;若是大於256小於65 536
				  ;在調用儲存時指針加2便可
				  ;返回的值儲存在cx中
	push ax
	push bx
	push dx
	mov dx, offset scanf
 	mov ah, 0ah
 	int 21h
 	lea bx,scanf
 	xor ax,ax
 	inc bx
 	inc bx
 	xor cx,cx
 	xor dx,dx
s1:
	mov dl,[bx]
	cmp dl,0dh
	je s2
	push ax
	push bx
	xor ax,ax
	xor bx,bx
	mov ax,10
	sub dx,30h
	mov bx,cx
	push dx
	mul bx
	mov cx,ax
	pop dx
	add cx,dx
	pop bx
	pop ax
	inc bx
s2:
	jne s1
	pop dx
	pop bx
	pop ax
	ret
readnum endp
 
PRINTAX PROC          ;以10進制輸出AX中的無符號整數.
push ax
	MOV AH,09H
	MOV DX,OFFSET answer
    INT 21H
    pop ax
      MOV  BX, 10     ;按10進制輸出.
      OR   AX, AX
      JZ   _0_
LOOP_P:
      XOR  DX, DX
      DIV  BX
      MOV  CX, AX     ;商.
      OR   CX, DX
      JZ   _E_        ;若商與餘數都爲0則結束遞歸.
      PUSH DX         ;保存DX中的餘數.
      CALL LOOP_P     ;遞歸.
      POP  DX         ;恢復餘數.
      ADD  DL, '0'    ;變成ASCII碼.
      JMP  _1_
_0_:  MOV  DL, '0'    ;是0則直接輸出.
_1_:  MOV  AH, 2
      INT  21H
_E_:  RET
PRINTAX ENDP
CODES ENDS
    END START
相關文章
相關標籤/搜索