интересная реализация рекурсии(на Бейсике нет контроля ошибок выхода из координат):

Код:
10 REM Sierpinski Carpet using recursion
20 rem HOME : HGR2 : HCOLOR=3
30 DIM S(7): DIM R(7): DIM L(7): DIM T(7)
40 LET S(1) = 255 : LET R(1) = 176 : LET L(1)=0 : LET T(1)=0
50 LET C = 1 : GOSUB 100
60 STOP

100 LET XD = S(C) : LET YD = R(C) : LET X = L(C) : LET Y = T(C) 
110 IF (C > 7) OR (XD < 1) OR (YD < 1) THEN RETURN
120 REM 
130 PLOT X + XD/3, Y + YD/3: DRAW XD/3, 0: REM HPLOT X + XD/3, Y + YD/3 TO X + 2*XD/3, Y + YD/3
140 PLOT X + 2*XD/3, Y + YD/3: DRAW 0, YD/3: REM HPLOT X + 2*XD/3, Y + YD/3 TO X + 2*XD/3, Y + YD*2/3
150 PLOT X + 2*XD/3, (Y + 2*YD/3): DRAW -XD/3, 0 : REM HPLOT X + 2*XD/3, (Y + 2*YD/3) TO X + XD/3, Y + 2*YD/3
160 PLOT (X + XD/3), (Y + YD/3): DRAW 0, YD/3 : REM HPLOT (X + XD/3), (Y + YD/3) TO X + XD/3, Y + 2*YD/3

200 REM drawing TOP LEFT
210 LET S(C+1) = S(C)/3
220 LET R(C+1) = R(C)/3
230 LET L(C+1) = L(C) : LET T(C+1) = T(C) : LET C = C+1 :LET  HCOLOR=6 : GOSUB 100 

240 REM drawing TOP RIGHT
250 LET C = C - 1
260 LET S(C+1) = S(C)/3
270 LET R(C+1) = R(C)/3
280 LET L(C+1) = L(C) + S(C+1)*2 : LET T(C+1) = T(C) : LET C = C+1 : LET HCOLOR=1 : GOSUB 100

290 REM drawing BOTTOM LEFT
300 LET C = C - 1
310 LET S(C+1) = S(C)/3
320 LET R(C+1) = R(C)/3
330 LET L(C+1) = L(C) : LET T(C+1) = T(C) + R(C+1)*2 : LET C = C+1 : LET HCOLOR=5 : GOSUB 100

340 LET C = C - 1 : REM BOTTOM RIGHT
350 LET S(C+1) = S(C)/3
360 LET R(C+1) = R(C)/3
370 LET L(C+1) = L(C) + S(C+1)*2 : LET T(C+1) = T(C) + R(C+1)*2 : LET C = C+1 : LET HCOLOR=2 : GOSUB  100

380 LET C = C - 1 : REM top
390 LET S(C+1) = S(C)/3
400 LET R(C+1) = R(C)/3
410 LET L(C+1) = L(C) + S(C+1) : LET T(C+1) = T(C) : LET C = C+1 : LET HCOLOR=3 : GOSUB 100

420 LET C = C - 1 : REM left
430 LET S(C+1) = S(C)/3
440 LET R(C+1) = R(C)/3
450 LET L(C+1) = L(C) : LET T(C+1) = T(C) + R(C+1) : LET C = C+1 : LET HCOLOR=3 : GOSUB 100

460 LET C = C - 1 : REM right
470 LET S(C+1) = S(C)/3
480 LET R(C+1) = R(C)/3
490 LET L(C+1) = L(C) + S(C+1)*2 : LET T(C+1) = T(C) + R(C+1) : LET C = C+1 : LET HCOLOR=3 : GOSUB 100

500 LET C = C - 1 : REM bottom
510 LET S(C+1) = S(C)/3
520 LET R(C+1) = R(C)/3
530 LET L(C+1) = L(C) + S(C+1) : LET T(C+1) = T(C) + R(C+1)*2 : LET C = C+1 : LET HCOLOR=3 : GOSUB 100

540 LET C = C - 1

600 RETURN


На ассемблере:

Код:
	device zxspectrum128
        ORG #6000
begin
;10 REM Sierpinski Carpet using recursion
;20 HOME : HGR2 : HCOLOR=3
;30 DIM XS(7), YS(7), XL(7), YT(7)
xs equ $B000
ys equ $B100
axl equ $B200
yt equ $B300
div3=$B400

;generate div table
	ld hl,div3
	xor a
	ld c,a
lut:
	ld (hl),c
	inc a
	cp 3
	jr nz,no3
	inc c
	xor a
no3:
	inc l
	jr nz,lut
	
;40 LET XS(1) = 280 : YS(1) = 192 : XL(1)=0 : YT(1)=0
	ld a,255
	ld (xs+1),a

	ld a,191
	ld (ys+1),a

	xor a
	ld (xl+1),a
	ld (yt+1),a
;50 LET C = 1 : GOSUB 100
	ld ixl,1 ; C
	
;	jr $
	call l100
;60 END
	jr $
drwline: ; from BC(y.x) to DE (y.x)
	ld a,d
	sub b
	call sgn
	ld (deltay+1),a

	ld a,e
	sub c
	call sgn
	ld (deltax+1),a

drlp:
	ld a,c
	cp e
	jr nz,contp
	ld a,b
	cp d
	ret z
contp:
;plot
; Get screen address 
; B = Y pixel position 
; C = X pixel position 
; Returns address in HL
	push bc,de
Get_Pixel_Address:  LD A,B          ; Calculate Y2,Y1,Y0 
                     AND %00000111   ; Mask out unwanted bits 
                     OR %01000000    ; Set base address of screen 
                     LD H,A          ; Store in H 
                     LD A,B          ; Calculate Y7,Y6 
                     RRA             ; Shift to position 
                     RRA 
                     RRA 
                     AND %00011000   ; Mask out unwanted bits 
                     OR H            ; OR with Y2,Y1,Y0 
                     LD H,A          ; Store in H 
                     LD A,B          ; Calculate Y5,Y4,Y3 
                     RLA             ; Shift to position 
                     RLA 
                     AND %11100000   ; Mask out unwanted bits 
                     LD L,A          ; Store in L 
                     LD A,C          ; Calculate X4,X3,X2,X1,X0 
                     RRA             ; Shift into position 
                     RRA 
                     RRA 
                     AND %00011111   ; Mask out unwanted bits 
                     OR L            ; OR with Y5,Y4,Y3 
                     LD L,A          ; Store in L 
	ld a,c
	and 7
	inc a
	ld b,a
	xor a
	scf

mm:
	rra
	djnz mm

	or (hl)
	ld (hl),a

	pop de,bc
	ld a,c
deltax: add a,0
	ld c,a

	ld a,b
deltay: add a,0
	ld b,a
	jp drlp

sgn:
	ret z
	add a,a
	sbc a,a
	ret m
	ld a,1
	ret
l100:
;100 XD = XS(C) : YD = YS(C) : X = XL(C) : Y = YT(C)
	ld ixh,xs/256
	ld e,(ix+0) ; XD
	ld ixh,ys/256
	ld d,(ix+0) ;YD

	ld ixh,axl/256
	ld c,(ix+0) ; X
	ld ixh,yt/256
	ld b,(ix+0) ;Y

;110 IF (C > 7 OR XD < 1 OR YD < 1) THEN RETURN
	ld a,ixl
	cp 7
	ret nc
	
;	ld a,e
;	or a
;	ret z
;	;ret m
;
;	ld a,d
;	or a
;	ret z
	;ret m

;120 REM 
;130 HPLOT X + XD/3, Y + YD/3 TO X + 2*XD/3, Y + YD/3
	push bc
	push de
	
	ld h,div3/256
	ld l,e ; XD
	ld a,(hl)
	add a,c ; X
	ld c,a ; X + XD/3
	
	add a,(hl) ;X + 2*XD/3
	ld e,a
	
	ld l,d ;YD
	ld a,(hl)
	add a,b ;Y
	ld b,a
	ld d,a ;Y + YD/3

	call drwline
	pop de
	pop bc


;140 HPLOT X + 2*XD/3, Y + YD/3 TO X + 2*XD/3, Y + YD*2/3
	push bc
	push de

	ld h,div3/256
	ld l,e ; XD
	ld a,(hl)
	add a,c ; X
	add a,(hl)
	ld c,a ; X + 2*XD/3
	ld e,a
	
	ld l,d ;YD
	ld a,(hl)
	add a,b ;Y
	ld b,a ; Y + YD/3
	add a,(hl)
	ld d,a ;Y + YD*2/3
	
	call drwline
	pop de
	pop bc


;150 HPLOT X + 2*XD/3, (Y + 2*YD/3) TO X + XD/3, Y + 2*YD/3
	push bc
	push de
	
	ld h,div3/256
	ld l,e ; XD
	ld a,(hl)
	add a,c ; X
	ld e,a ;X + XD/3
	add a,(hl)
	ld c,a ; X + 2*XD/3

	ld l,d ;YD
	ld a,(hl)
	add a,b ;Y
	add a,(hl)
	ld b,a ; Y + YD*2/3
	ld d,a ; Y + YD*2/3
	
	call drwline
	pop de
	pop bc


;160 HPLOT (X + XD/3), (Y + YD/3) TO X + XD/3, Y + 2*YD/3
	push bc
	push de

	ld h,div3/256
	ld l,e ; XD
	ld a,(hl)
	add a,c ; X
	ld e,a ;X + XD/3
	ld c,a ; X + XD/3

	ld l,d ;YD
	ld a,(hl)
	add a,b ; Y
	ld b,a ; Y + YD/3
	add a,(hl)
	ld d,a ; Y + 2*YD/3

	call drwline
	pop de
	pop bc

;200 REM drawing TOP LEFT
;210 XS(C+1) = XS(C)/3
	ld ixh,xs/256
	ld h,div3/256
	ld l,(ix+0) ;XS(C)/3
	ld a,(hl)
	ld (ix+1),a ;XS(C+1)

;220 YS(C+1) = YS(C)/3
	ld ixh,ys/256
	ld h,div3/256
	ld l,(ix+0) ;YS(C)/3
	ld a,(hl)
	ld (ix+1),a ;YS(C+1)

;230 XL(C+1) = XL(C) : YT(C+1) = YT(C) : C = C+1 : HCOLOR=6 : GOSUB 100
	ld ixh,axl/256
	ld a,(ix+0)
	ld (ix+1),a ; XL(C+1) = XL(C)

	ld ixh,yt/256
	ld a,(ix+0)
	ld (ix+1),a;YT(C+1) = YT(C)

	inc ixl ; C = C+1
	call l100

;240 REM drawing TOP RIGHT
;250 C = C - 1
	dec ixl

;260 XS(C+1) = XS(C)/3
	ld ixh,xs/256
	ld h,div3/256
	ld l,(ix+0)
	ld a,(hl)
	ld (ix+1),a

;270 YS(C+1) = YS(C)/3
	ld ixh,ys/256
	ld h,div3/256
	ld l,(ix+0)
	ld a,(hl)
	ld (ix+1),a

;280 XL(C+1) = XL(C) + XS(C+1)*2 : YT(C+1) = YT(C) : C = C+1 : HCOLOR=1 : GOSUB 100
	ld ixh,xs/256
	ld a,(ix+1) ; XS(C+1)*2
	add a,a ; *2
	ld ixh,axl/256
	add a,(ix+0); XL(C)
	ld (ix+1),a ;XL(C+1)

;YT(C+1) = YT(C)
	ld ixh,yt/256
	ld a,(ix+0)
	ld (ix+1),a

	inc ixl ;C = C+1

	call l100

;290 REM drawing BOTTOM LEFT
;300 C = C - 1
	dec ixl
;310 XS(C+1) = XS(C)/3
	ld ixh,xs/256
	ld h,div3/256
	ld l,(ix+0)
	ld a,(hl)
	ld (ix+1),a

;320 YS(C+1) = YS(C)/3
	ld ixh,ys/256
	ld h,div3/256
	ld l,(ix+0)
	ld a,(hl)
	ld (ix+1),a

;330 XL(C+1) = XL(C) : YT(C+1) = YT(C) + YS(C+1)*2 : C = C+1 : HCOLOR=5 : GOSUB 100
	ld ixh,axl/256
	ld a,(ix+0)
	ld (ix+1),a
;YT(C+1) = YT(C) + YS(C+1)*2
	ld ixh,ys/256
	ld a,(ix+1)
	add a,a;*2
	ld ixh,yt/256
	add a,(ix+0)
	ld (ix+1),a
;C = C+1
	inc ixl
	call l100

;340 C = C - 1 : REM BOTTOM RIGHT
	dec ixl
;350 XS(C+1) = XS(C)/3
	ld ixh,xs/256
	ld h,div3/256
	ld l,(ix+0)
	ld a,(hl)
	ld (ix+1),a

;360 YS(C+1) = YS(C)/3
	ld ixh,ys/256
	ld h,div3/256
	ld l,(ix+0)
	ld a,(hl)
	ld (ix+1),a

;370 XL(C+1) = XL(C) + XS(C+1)*2 : YT(C+1) = YT(C) + YS(C+1)*2 : C = C+1 : HCOLOR=2 : GOSUB  100
	ld ixh,xs/256
	ld a,(ix+1)
	add a,a; *2
	ld ixh,axl/256
	add a,(ix+0)
	ld (ix+1),a
;YT(C+1) = YT(C) + YS(C+1)*2
	ld ixh,ys/256
	ld a,(ix+1)
	add a,a; *2
	ld ixh,yt/256
	add a,(ix+0)
	ld (ix+1),a
;C = C+1
	inc ixl
	call l100

;380 C = C - 1 : REM top
	dec ixl
;390 XS(C+1) = XS(C)/3
	ld ixh,xs/256
	ld h,div3/256
	ld l,(ix+0)
	ld a,(hl)
	ld (ix+1),a
;400 YS(C+1) = YS(C)/3
	ld ixh,ys/256
	ld h,div3/256
	ld l,(ix+0)
	ld a,(hl)
	ld (ix+1),a
;410 XL(C+1) = XL(C) + XS(C+1) : YT(C+1) = YT(C) : C = C+1 : HCOLOR=3 : GOSUB 100
	ld ixh,xs/256
	ld a,(ix+1)
	ld ixh,axl/256
	add a,(ix+0)
	ld (ix+1),a
	
	ld ixh,yt/256
	ld a,(ix+0)
	ld (ix+1),a
	
	inc ixl
	call l100

;420 C = C - 1 : REM left
	dec ixl
;430 XS(C+1) = XS(C)/3
	ld ixh,xs/256
	ld h,div3/256
	ld l,(ix+0)
	ld a,(hl)
	ld (ix+1),a
;440 YS(C+1) = YS(C)/3
	ld ixh,ys/256
	ld h,div3/256
	ld l,(ix+0)
	ld a,(hl)
	ld (ix+1),a

;450 XL(C+1) = XL(C) : YT(C+1) = YT(C) + YS(C+1) : C = C+1 : HCOLOR=3 : GOSUB 100
	ld ixh,axl/256
	ld a,(ix+0)
	ld (ix+1),a
;YT(C+1) = YT(C) + YS(C+1)
	ld ixh,ys/256
	ld a,(ix+1)
	ld ixh,yt/256
	add a,(ix+0)
	ld (ix+1),a

	inc ixl
	call l100

;460 C = C - 1 : REM right
	dec ixl
;470 XS(C+1) = XS(C)/3
	ld ixh,xs/256
	ld h,div3/256
	ld l,(ix+0)
	ld a,(hl)
	ld (ix+1),a

;480 YS(C+1) = YS(C)/3
	ld ixh,ys/256
	ld h,div3/256
	ld l,(ix+0)
	ld a,(hl)
	ld (ix+1),a

;490 XL(C+1) = XL(C) + XS(C+1)*2 : YT(C+1) = YT(C) + YS(C+1) : C = C+1 : HCOLOR=3 : GOSUB 100
	ld ixh,xs/256
	ld a,(ix+1)
	add a,a ;*2
	ld ixh,axl/256
	add a,(ix+0)
	ld (ix+1),a
;YT(C+1) = YT(C) + YS(C+1)
	ld ixh,ys/256
	ld a,(ix+1)
	ld ixh,yt/256
	add a,(ix+0)
	ld (ix+1),a

	inc ixl
	call l100
;500 C = C - 1 : REM bottom
	dec ixl
;510 XS(C+1) = XS(C)/3
	ld ixh,xs/256
	ld h,div3/256
	ld l,(ix+0)
	ld a,(hl)
	ld (ix+1),a
;520 YS(C+1) = YS(C)/3
	ld ixh,ys/256
	ld h,div3/256
	ld l,(ix+0)
	ld a,(hl)
	ld (ix+1),a
;530 XL(C+1) = XL(C) + XS(C+1) : YT(C+1) = YT(C) + YS(C+1)*2 : C = C+1 : HCOLOR=3 : GOSUB 100
	ld ixh,xs/256
	ld a,(ix+1)
	ld ixh,axl/256
	add a,(ix+0)
	ld (ix+1),a
;YT(C+1) = YT(C) + YS(C+1)*2 
	ld ixh,ys/256
	ld a,(ix+1)
	add a,a ;*2
	ld ixh,yt/256
	add a,(ix+0)
	ld (ix+1),a
	inc ixl
	call l100
;540 C = C - 1
	dec ixl
;600 RETURN
	ret
end
	display /d,end-begin
	savesna "!void.sna",begin