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

来源:互联网 发布:北京淘宝科技有限公司 编辑:程序博客网 时间:2024/05/16 07:10

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 ProceduresINCLUDE Irvine32.inc.datasymbol= 'x'delay_time = 10space = ' 'iterative byte "ITERATIVE",0recursive byte "RECURSIVE",0.codemain PROC; displays "ITERATIVE"mov dh, 3mov dl, 12call gotoxymov edx, offset iterativecall writestring; prints the square using iterationmov bl, 10; top-left xmov bh, 6; top-left ymov al, 25; bottom-right xmov ah, 21; bottom-right ycall spiral_print; displays "RECURSIVE"mov dh, 3mov dl, 32call gotoxymov edx, offset recursivecall writestringmov dh,3;find the size of xmov dl,29;find the size of ycall gotoxy;go to the start location for recurive; prints the square using recursionmov bl, 30; top-left xmov bh, 6; top-left ymov al, 45; bottom-right xmov ah, 21; bottom-right ycall spiral_print_recurs; moves the cursor for "Press any key to continue"mov dh, 22mov dl, 10call gotoxycall waitmsgexitmain 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: recursionspiral_print_recurs proc USES EAX EBX EDX ECX ESIcmp bl, alja end_proc ; jumps to the end when bl is greater than blinc dl                  ;for next location on one rowinc dh;for next loaction on one columnmov cl,alsub cl,blmovzx esi,clcall horizontal_printcall vertical_printcall horizontal_print_revcall vertical_print_revsub al,1add bl,1call spiral_print_recurs; implement the rest of the procedureend_proc:retspiral_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: iterationspiral_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 locationmov cl, alsub cl, blmovzx esi, cl        ;confirm the times of first loopadd cl, 1shr cl, 1; divides cl by 2L5:mov ecx,esi             ;the number of loopL1:mov eax,delay_time      ;every time print with  delay timecall gotoxy             ;located call delaymov al,symbol           ;print *call writecharinc dl                  ;cursor point next locationloop L1mov ecx,esi;the number of loopL2:mov eax,delay_time      ;every time print with  delay timecall gotoxy call delaymov al,symbolcall writecharinc dh                  ;cursor point next locationloop L2mov ecx,esiL3:                         ; it is the same with the before loopmov eax,delay_time      ;every time print with  delay time                    call gotoxycall delaymov al,symbolcall writechardec dlloop L3 mov ecx,esiL4:    ;mov eax,delay_time      ;every time print with  delay timecall gotoxy call delaymov al,symbolcall writechardec dhloop L4sub 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 rowinc dh;for next loaction on one columnloop L5endloop:ret; implement the rest of the procedurespiral_print endp; Receives: DH (row), DL (col), ESI (# of prints); Returns: nothing; Description: prints symbol for ESI times from (DL, DH);              each print increments DHvertical_print proc USES  ECX EAX ESImov ecx,esiL1:mov eax,delay_timecall gotoxy call delaymov al,symbol           ;print *call writecharinc dh                  ;cursor point next locationloop L1; implement this procedureretvertical_print endp; Receives: DH (row), DL (col), ESI (# of prints); Returns: nothing; Description: prints symbol for ESI times from (DL, DH);              each print increments DLhorizontal_print proc USES  ECX EAX ESImov ecx,esi;the number of loopL2:mov eax,delay_timecall gotoxy             ;located call delaymov al,symbol           ;print *call writecharinc dl                  ;cursor point next locationloop L2ret; implement this procedurehorizontal_print endp; Receives: DH (row), DL (col), ESI (# of prints); Returns: nothing; Description: prints symbol for ESI times from (DL, DH);              each print decrements DHvertical_print_rev proc USES  ECX EAX ESImov ecx,esiL3:mov eax,delay_timecall gotoxy call delaymov al,symbol  call writechardec dh                  ;cursor point next locationloop L3ret; implement this procedurevertical_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 DLhorizontal_print_rev proc USES  ECX EAX ESImov ecx,esi;the number of loopL4:mov eax,delay_timecall gotoxy             ;located call delaymov al,symbol  call writechardec dl                  ;cursor point next locationloop L4ret; implement this procedurehorizontal_print_rev endpEND main