Largest proper divisor of n: Difference between revisions

Content deleted Content added
Loren (talk | contribs)
Added XPL0 example.
Loren (talk | contribs)
Added X86 Assembly example.
Line 613:
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50
</pre>
 
=={{header|X86 Assembly}}==
<lang asm> 1 ;Assemble with: tasm, tlink /t
2 0000 .model tiny
3 0000 .code
4 org 100h ;.com program starts here
5
6 ;bl = D = divisor
7 ;bh = N
8
9 0100 B7 01 start: mov bh, 1 ;for N:= 1 to 100 do
10 0102 8A DF d10: mov bl, bh ;D:= if N=1 then 1 else N/2
11 0104 D0 EB shr bl, 1
12 0106 75 02 jne d15
13 0108 FE C3 inc bl
14 010A d15:
15 010A 8A C7 d20: mov al, bh ;while rem(N/D) # 0 do
16 010C 98 cbw ; ah:= 0 (extend sign of al into ah)
17 010D F6 FB idiv bl ; al:= ax/bl; ah:= remainder
18 010F 84 E4 test ah, ah
19 0111 74 04 je d30
20 0113 FE CB dec bl ; D--
21 0115 EB F3 jmp d20
22 0117 d30:
23 0117 8A C3 mov al, bl ;output number in D
24 0119 D4 0A aam 10 ;ah:= al/10; al:= remainder
25 011B 50 push ax ;save low digit in remainder
26 011C 8A C4 mov al, ah ;get high digit
27 011E 04 20 add al, 20h ;if zero make it a space char
28 0120 84 E4 test ah, ah
29 0122 74 02 je d50
30 0124 04 10 add al, 10h ;else make it into ASCII digit
31 0126 CD 29 d50: int 29h ;output high digit or space
32 0128 58 pop ax ;get low digit
33 0129 04 30 add al, 30h ;make it ASCII
34 012B CD 29 int 29h ;output low digit
35
36 012D B0 20 mov al, 20h ;output space char
37 012F CD 29 int 29h
38
39 0131 8A C7 mov al, bh ;if remainder(N/10) = 0 then CR LF
40 0133 D4 0A aam 10 ;ah:= al/10; al:= remainder
41 0135 3C 00 cmp al, 0
42 0137 75 08 jne next
43 0139 B0 0D mov al, 0Dh ;CR
44 013B CD 29 int 29h
45 013D B0 0A mov al, 0Ah ;LF
46 013F CD 29 int 29h
47
48 0141 FE C7 next: inc bh ;next N
49 0143 80 FF 64 cmp bh, 100
50 0146 7E BA jle d10
51 0148 C3 ret
52
53 end start
</lang>
 
{{out}}
<pre>
1 1 1 2 1 3 1 4 3 5
1 6 1 7 5 8 1 9 1 10
7 11 1 12 5 13 9 14 1 15
1 16 11 17 7 18 1 19 13 20
1 21 1 22 15 23 1 24 7 25
17 26 1 27 11 28 19 29 1 30
1 31 21 32 13 33 1 34 23 35
1 36 1 37 25 38 11 39 1 40
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50
</pre>