最開始寫的有很多迴圈的(容易理解,但複雜性高) assume cs:code,ss:stack data segment db '1975','1976','1977','1978','1979','1980','1981','1982','1983' db '1984','1985','1986' ...
最開始寫的有很多迴圈的(容易理解,但複雜性高)
assume cs:code,ss:stack data segment db '1975','1976','1977','1978','1979','1980','1981','1982','1983' db '1984','1985','1986','1987','1988','1989','1990','1991','1992' db '1993','1994','1995' dd 16,22,382,1356,2390,8000,16000,24486,50065,97479,140417,197514 dd 345980,590827,803530,1183000,1843000,2759000,3753000,4649000,5937000 dw 3,7,9,13,28,38,130,220,476,778,1001,1442,2258,2793,4037,5635,8226 dw 11542,14430,15257,17800 data ends table segment db 21 dup('year summ ne ?? ') table ends stack segment dw 0,0,0,0,0,0,0,0 stack ends code segment start: mov ax,data mov ds,ax mov ax,table mov es,ax mov ax,stack mov ss,ax mov sp,10h mov bx,0 mov bp,0 mov cx,21 s:push cx mov si,0 mov cx,4 s0:mov al,[bx+si] mov es:[bp+si],al inc si loop s0 mov al,' ' mov es:[bp+si+1],al add bx,4 add bp,16 pop cx loop s mov bx,84 mov bp,5 mov cx,21 s1:mov ax,ds:[bx] mov dx,ds:[bx+2] mov es:[bp],ax mov es:[bp+2],dx mov al,' ' mov es:[bp+4],al add bx,4 add bp,16 loop s1 mov bx,168 mov bp,10 mov cx,21 s2:mov ax,ds:[bx] mov es:[bp],ax mov al,' ' mov es:[bp+2],ax add bx,2 add bp,16 loop s2 mov bx,84 mov si,168 mov bp,13 mov cx,21 s3:mov ax,ds:[bx] mov dx,ds:[bx+2] div word ptr ds:[si] mov es:[bp],ax mov al,' ' mov es:[bp+2],al add si,2 add bx,4 add bp,16 loop s3 mov ax,4c00h int 21h code ends end start
運行結果如下
修改後,只剩下一個迴圈,代碼複製性減少了很多
assume cs:code,ss:stack data segment db '1975','1976','1977','1978','1979','1980','1981','1982','1983' db '1984','1985','1986','1987','1988','1989','1990','1991','1992' db '1993','1994','1995' dd 16,22,382,1356,2390,8000,16000,24486,50065,97479,140417,197514 dd 345980,590827,803530,1183000,1843000,2759000,3753000,4649000,5937000 dw 3,7,9,13,28,38,130,220,476,778,1001,1442,2258,2793,4037,5635,8226 dw 11542,14430,15257,17800 data ends table segment db 21 dup('year summ ne ?? ') table ends stack segment dw 0,0,0,0,0,0,0,0 stack ends code segment start: mov ax,data mov ds,ax mov ax,table mov es,ax mov ax,stack mov ss,ax mov sp,10h mov bx,0 mov bp,0 mov si,0 mov cx,21 s:mov ax,ds:[bx] mov es:[bp],ax mov ax,ds:[bx+2] mov es:[bp+2],ax mov al,' ' mov es:[bp+4],al mov ax,ds:[bx+84] mov dx,ds:[bx+84+2] mov es:[bp+5],ax mov es:[bp+7],dx mov al,' ' mov es:[bp+9],al mov ax,ds:[si+168] mov es:[bp+10],ax mov al,' ' mov es:[bp+12],ax mov ax,ds:[bx+84] mov dx,ds:[bx+84+2] div word ptr ds:[si+168] mov es:[bp+13],ax mov al,' ' mov es:[bp+15],al add bp,16 //->用來表示table表內的記憶體偏移地址(用來控制table表的行數,以及間接地表示記憶體偏移地址) add bx,4 //->用來表示dd性與字元串型數據的偏移地址(每讀取一段數據後,需移動四個位元組) add si,2 //->>用來表示dw性數據的偏移地址(每讀取一段數據後,只需移動兩個位元組) loop s mov ax,4c00h int 21h code ends end start
運行結果如下: