欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

编译语言-运用迭代和递归的方式按照要求输出

更新时间: 2014-01-05 03:16:41 责任编辑: Author_N1

 

汇编语言--运用迭代和递归的方式按照要求输出

write a program to print square matrices of ‘x’. The printing job starts with the left-most corner and proceeds in a spiral way. The program should be paused for 10 milliseconds between printing each two symbols. Below are three snapshots of an execution of the program. (Note the edge length of square is an even number.)

 

TITLE Example of Procedures
INCLUDE Irvine32.inc

.data
symbol= 'x'
delay_time = 10
space = ' '

iterative byte "ITERATIVE",0
recursive byte "RECURSIVE",0

.code

main PROC
	; displays "ITERATIVE"
	mov dh, 3
	mov dl, 12
	call gotoxy
	mov edx, offset iterative
	call writestring

	; prints the square using iteration
	mov bl, 10		; top-left x
	mov bh, 6		; top-left y
	mov al, 25		; bottom-right x
	mov ah, 21		; bottom-right y
	

	call spiral_print

	; displays "RECURSIVE"
	mov dh, 3
	mov dl, 32
	call gotoxy
	mov edx, offset recursive
	call writestring
	
	mov dh,3					;find the size of x
	mov dl,29					;find the size of y
	call gotoxy				;go to the start location for recurive

	; prints the square using recursion
	mov bl, 30		; top-left x
	mov bh, 6		; top-left y
	mov al, 45		; bottom-right x
	mov ah, 21		; bottom-right y
	call spiral_print_recurs

	; moves the cursor for "Press any key to continue"
	mov dh, 22
	mov dl, 10
	call gotoxy
	
	call waitmsg
	exit
main ENDP



; Receives BL (top-left x), BH (top-left y)
;	   AL (bottom-right x), AH (bottom-right y)
; Precondition: (BL-AL)==(AH-BH), (BL-AL+1) is even
; Returns: nothing
; Description: Prints a square of constant symbol. The top-left corner of 
;		the square is (BL, BH). The bottom-right corner of the square
;		is (AL, AH). The program pauses for constant delay_time milliseconds.
;		Algorithm: recursion
spiral_print_recurs proc USES EAX EBX EDX ECX ESI
	cmp bl, al
	ja end_proc ; jumps to the end when bl is greater than bl
	
	inc dl                  ;for next location on one row
	inc dh					;for next loaction on one column
	
	mov cl,al
	sub cl,bl
	movzx esi,cl
	
	
	
	call horizontal_print
	call vertical_print
	call horizontal_print_rev
	call vertical_print_rev
	
	sub al,1
	add bl,1
	call spiral_print_recurs
	
	; implement the rest of the procedure

end_proc:
	ret
spiral_print_recurs endp


; Receives BL (top-left x), BH (top-left y)
;	   AL (bottom-right x), AH (bottom-right y)
; Precondition: (BL-AL)==(AH-BH), (BL-AL+1) is even
; Returns: nothing
; Description: Prints a square of constant symbol. The top-left corner of 
;		the square is (BL, BH). The bottom-right corner of the square
;		is (AL, AH). The program pauses for constant delay_time milliseconds.
;		Algorithm: iteration
spiral_print proc USES EAX EBX EDX ECX ESI

 mov dh,4					;find the size of x
 mov dl,10					;find the size of y
 call gotoxy				;go to the first location

	mov cl, al
	sub cl, bl
	movzx esi, cl	        ;confirm the times of first loop
	add cl, 1
	shr cl, 1				; divides cl by 2
	
	
L5:		
	
	mov ecx,esi             ;the number of loop
	
L1:
	mov eax,delay_time      ;every time print with  delay time
	call gotoxy             ;located 
	call delay
	mov al,symbol           ;print *
	call writechar
	inc dl                  ;cursor point next location
	
	loop L1
		
	mov ecx,esi				;the number of loop
		
L2:
	mov eax,delay_time      ;every time print with  delay time
	call gotoxy	 
	call delay
	mov al,symbol
	call writechar	
	inc dh                  ;cursor point next location
	loop L2	
	
	mov ecx,esi
L3:    
	                     ; it is the same with the before loop
	mov eax,delay_time      ;every time print with  delay time                    
	call gotoxy
	call delay
	mov al,symbol
	call writechar
	dec dl
	
	loop L3
 
	mov ecx,esi

L4:						    ;
	mov eax,delay_time      ;every time print with  delay time
	call gotoxy 
	call delay
	mov al,symbol
	call writechar
	dec dh
	
	loop L4
		
	sub esi,2               ;every time we print a square we also need to subtract
							;two 
	js endloop              ;judge if print over it will jump 
	
	inc dl                  ;for next location on one row
	inc dh					;for next loaction on one column
	loop L5
endloop:
		ret
	
	

	; implement the rest of the procedure


spiral_print endp






; Receives: DH (row), DL (col), ESI (# of prints)
; Returns: nothing
; Description: prints symbol for ESI times from (DL, DH)
;              each print increments DH		
vertical_print proc USES  ECX EAX ESI
	
	mov ecx,esi
	
	L1:
	mov eax,delay_time
	call gotoxy	 
	call delay
	mov al,symbol           ;print *
	call writechar	
	inc dh                  ;cursor point next location
	loop L1	
	; implement this procedure
	ret
vertical_print endp

; Receives: DH (row), DL (col), ESI (# of prints)
; Returns: nothing
; Description: prints symbol for ESI times from (DL, DH)			
;              each print increments DL
horizontal_print proc USES  ECX EAX ESI
	
	
	mov ecx,esi				;the number of loop	
L2:	
	mov eax,delay_time
	call gotoxy             ;located 
	call delay
	mov al,symbol           ;print *
	call writechar
	inc dl                  ;cursor point next location
	loop L2
	ret
	; implement this procedure
	
horizontal_print endp

; Receives: DH (row), DL (col), ESI (# of prints)
; Returns: nothing
; Description: prints symbol for ESI times from (DL, DH)
;              each print decrements DH		
vertical_print_rev proc USES  ECX EAX ESI
	
	mov ecx,esi
	
	L3:
	mov eax,delay_time
	call gotoxy	 
	call delay
	mov al,symbol  
	call writechar	
	dec dh                  ;cursor point next location
	loop L3	
	
	
	ret
	
	
	
	; implement this procedure
	
vertical_print_rev endp

; Receives: DH (row), DL (col), ESI (# of prints)
; Returns: nothing
; Description: prints symbol for ESI times from (DL, DH)			
;              each print decrements DL

horizontal_print_rev proc USES  ECX EAX ESI

	mov ecx,esi				;the number of loop	
L4:	
	mov eax,delay_time
	call gotoxy             ;located 
	call delay	
	mov al,symbol  
	call writechar
	dec dl                  ;cursor point next location
	loop L4
	ret
	
	
	; implement this procedure
	
horizontal_print_rev endp
END main


 

 

 

 

 

上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2014-01-05 03:16:41
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/20317/
本文WWW.DOC100.NET DOC100.NET版权所有。