2014-12-07 69 views
0

我想知道如何可以優化這個代碼更多一點。現在他有467k和59條線。我如何優化此代碼? [大會8086字母金字塔]

數據段:

code_char db 'A' 
counter_space db 39 
counter_char dw 1 
counter_rows dw 25 

程序段:

rows: 

mov cl, counter_space ;here I write space 
mov ah,02h 
mov dl,'' 
space: 
int 21h 
loop space 

mov cx, counter_char ;here I write letters 
mov ah,02h 
mov dl,code_char 
letters: 
int 21h 
loop letters 

mov ah,02h ;here I go to another line(enter) 
mov dl,0ah 
int 21h 

INC code_char ;here I change the value of variable's 
DEC counter_space 
ADD counter_char,2 
DEC counter_rows 

mov cx,counter_rows ;here I count the rows to 25 
loop rows 

mov ah,01h ;here I w8 to any key 
int 21h 

      mov  ah,4ch 
      mov  al,0 
      int  21h 

如果您有任何建議,請發表評論。 我剛開始學習Assembly。

+0

你的目標是優化速度或大小?你在用什麼彙編語言?以及你用於彙編程序的命令行參數是什麼?代碼看起來像MS-DOS環境的16位代碼('int 21h'等等),所以它已經過時了。如果使用16位代碼不是老師給出的要求,而且您現在也不打算寫自己的引導加載程序,我建議您學習現代的32位或64位x86/x86-64程序集,而不是過時的16位x86彙編。 – nrz 2014-12-07 15:06:37

+0

我必須對大小(行數,內存)進行優化。您對其MS-DOS環境的16位是正確的)。我必須使用16位。 – Lukas 2014-12-07 15:11:44

+0

你正在使用什麼彙編程序和什麼命令行參數?你用'(線,內存)'來表示什麼?在我看來,對代碼行進行優化對於彙編代碼來說沒有多大意義。如果你真的需要,只需使用'db'編寫所有的代碼,你只能得到1行代碼......你的意思是你的EXE文件的大小是467千字節?如果您確實需要優化可執行文件大小,請將您的可執行文件作爲COM文件。之後,檢查代碼中所有指令的編碼大小,並查找縮短可執行文件的位置。 – nrz 2014-12-07 15:38:41

回答

0

您可以使用所有其他變量可以從counter_rows變量來計算的事實,所以你真的只需要一個變量:

code_char  = 'A' + 25 - counter_rows 
counter_space = counter_rows + 14 
counter_char = 51 - counter_rows * 2 

由於counter_rows是您的外環計數器,你可以只保留它一直在寄存器中,而不是爲它分配內存。這使得可以在沒有任何內存引用的情況下運行該程序。

還有一些小的優化可以完成。除第一次呼叫外,您不需要將ah寄存器設置爲02h。當爲按鍵呼叫設置ah01h時,您可以減少寄存器,因爲您知道它之前爲02h。您可以設置ax而不是分別設置ahal

如果我計算正確,應該採取實際的代碼,並從59到41的數據字節下來:

mov bx, 25 ;counter_rows 
rows: 

    ;here I write space 
    mov cx, bx ; counter_space = counter_rows + 14 
    add cl, 14 
    mov ah, 02h 
    mov dl, 32 ;space 
    space: 
    int 21h 
    loop space 

    ;here I write letters 
    mov cl, 51 ;counter_char = 51 - counter_rows * 2 
    sub cl, bl 
    sub cl, bl 
    ;mov ah, 02h - already set 
    mov dl, 65 + 25 ;code_char = 'A' + 25 - counter_rows 
    sub dl, bl 
    letters: 
    int 21h 
    loop letters 

    ;here I go to another line(enter) 
    ;mov ah, 02h - already set 
    mov dl, 0ah 
    int 21h 

    dec bx 
jnz rows 

;here I wait for any key 
dec ah ;02h - 1 = 01h 
int 21h 

mov ax,4c00h ;set ah and al in one go 
int 21h