RosettaCodeData/Task/Loops-Increment-loop-index-within-loop-body/360-Assembly/loops-increment-loop-index-within-loop-body.360
2023-07-01 13:44:08 -04:00

73 lines
3.1 KiB
Text

* Loops/Increment loop index within loop body - 16/07/2018
LOOPILWB PROLOG
SR R6,R6 i=0
ZAP N,=P'42' n=42
DO WHILE=(C,R6,LT,IMAX) do while(i<imax)
BAL R14,ISPRIME call isprime(n)
IF C,R0,EQ,=F'1' THEN if n is prime then
LA R6,1(R6) i=i+1
XDECO R6,XDEC edit i
MVC PG+2(2),XDEC+10 output i
MVC ZN,EM load edit mask
ED ZN,N edit n
MVC PG+7(L'ZN),ZN output n
XPRNT PG,L'PG print buffer
ZAP WP,N n
AP WP,N +n
SP WP,=P'1' +1
ZAP N,WP n=n+n-1
ENDIF , endif
ZAP WP,N n
AP WP,=P'1' +1
ZAP N,WP n=n+1
ENDDO , enddo
EPILOG
ISPRIME EQU * isprime(n) -----------------------
CP N,=P'2' if n=2
BE RETURN1 then return(1)
CP N,=P'3' if n=3
BE RETURN1 then return(1)
ZAP WDP,N n
DP WDP,=PL8'2' /2
CP WDP+8(8),=P'0' if mod(n,2)=0
BE RETURN0 then return(0)
ZAP WDP,N n
DP WDP,=PL8'3' /3
CP WDP+8(8),=P'0' if mod(n,3)=0
BE RETURN0 then return(0)
ZAP J,=P'5' j=5
LWHILE ZAP WP,J j
MP WP,J *j
CP WP,N while(j*j<=n)
BH EWHILE ~
ZAP WDP,N n
DP WDP,J /j
CP WDP+8(8),=P'0' if mod(n,j)=0
BE RETURN0 then return(0)
ZAP WP,J j
AP WP,=P'2' +2
ZAP WDP,N n
DP WDP,WP n/(j+2)
CP WDP+8(8),=P'0' if mod(n,j+2)=0
BE RETURN0 then return(0)
ZAP WP,J j
AP WP,=P'6' +6
ZAP J,WP j=j+6
B LWHILE loopwhile
EWHILE B RETURN1 return(1)
RETURN0 LA R0,0 rc=0
B RETURNX
RETURN1 LA R0,1 rc=1
RETURNX BR R14 return to caller -----------------
IMAX DC F'42' limit
EM DC XL20'402020206B2020206B2020206B2020206B202120' mask
N DS PL8 n
J DS PL8 j
PG DC CL80'i=00 : 000,000,000,000,000' buffer
XDEC DS CL12 temp for XDECO
WP DS PL8 temp for AP,SP,MP
WDP DS PL16 temp for DP
CW DS CL16 temp for UNPK
ZN DS CL20
REGEQU
END LOOPILWB