2015-11-17 61 views
0

我現在有這樣的代碼:簡化組裝程序

.model small 
.stack 100h 
.data 
.code 
    CLRSCR: 
     mov ax,0003h 
     int 10h 
    ROWCOLINIT: 
     mov dh,0 
     mov dl,0 
    MYLOOP: 
     mov ax,dx 
     mov ah,0 
     mov bl,2 
     div bl 
     cmp ah,0 
     je EVENCOL 
    ODDCOL: 
     mov al,2 
    CURSORINIT: 
     mov ah,02h 
     mov bh,0 
     int 10h 
    ATTRIBINIT: 
     mov ah,09h 
     mov bl,30h 
    PRINTCHAR: 
     mov cx,1 
     int 10h 
     inc dl 
     cmp dl,5 
     je RESETCOLINCROW2 
    DONTRESETCOL: 
     cmp dh,5 
     je EXIT 
     jmp MYLOOP 
    LOOP2: 
     mov ax,dx 
     mov ah,0 
     mov bl,2 
     div bl 
     cmp ah,0 
     je EVENCOL2 
    ODDCOL2: 
     mov al,42 
    CURSORINIT2: 
     mov ah,02h 
     mov bh,0 
     int 10h 
    ATTRIBINIT2: 
     mov ah,09h 
     mov bl,30h 
    PRINTCHAR2: 
     mov cx,1 
     int 10h 
     inc dl 
     cmp dl,5 
     je RESETCOLINCROW 
    DONTRESETCOL2: 
     cmp dh,5 
     je EXIT 
     jmp LOOP2 
    EXIT: 
     mov ah,4ch 
     int 21h 
    RESETCOLINCROW: 
     mov dl,0 
     inc dh 
     jmp DONTRESETCOL 
    RESETCOLINCROW2: 
     mov dl,0 
     inc dh 
     jmp DONTRESETCOL2 
    EVENCOL: 
     mov al,42 
     jmp CURSORINIT 
    EVENCOL2: 
     mov al,2 
     jmp CURSORINIT2 

end 

我的程序輸出是:

*☻*☻* 
☻*☻*☻ 
*☻*☻* 
☻*☻*☻ 
*☻*☻* 

我試圖讓使用2個迴路這個代碼更簡單。我如何去做彙編器中的嵌套循環?

+1

確實的規格讓你改變成另一種語言? :) – lordkain

+4

也許你應該評論你的代碼並告訴我們這個程序應該做什麼? – zx485

+0

雖然你說要簡化爲兩個循環,但也可以用一個循環生成所需的效果(單循環也更簡單) –

回答

0

這是一種解決方案,僅使用8086/8088指令。它不依賴於386指令,所以應該在任何8086/8088 +仿真器中運行。

這個程序的想法是,我們打印*和笑臉(02h)之間交替的25個字符。我們每行打印5個字符(在每個字符後面向前移動光標),如果打印了5個字符,則跳至下一行的開頭。我們這樣做直到我們已經打印出所需字符的數量(25)。

要在*和笑臉之間交替使用我要使用的字符取決於當前正在處理的字符(在DI中)是奇數還是偶數。如果它甚至是我打印的*,如果它很奇怪,我會打印一張笑臉。

我在代碼中提供了一些意見,以便讓您瞭解每一步發生的情況。我使用這些寄存器作爲變量。

BP = Num of characters per row 
DI = Current character number we are processing 
BH = Current text page to print on 
BL = Attribute to use when printing to screen 
CX = Total number of characters to print with int 10h/ah=09h. Always = 1 
DH = Current cursor row 
DL = Current cursor column 

我使用的代碼是:

.model small 
.stack 100h 

.data 
outcharsarr db '*', 2 ; even = * odd = smiley face 

CHARSPERROW equ 5  ; Number of characters to print per row 
CHARSTOPRINT equ 25  ; Total number of characters to print 

.code 
    MAIN: 
    mov ax, @data  ; Use the proper data segment 
    mov ds, ax 

    mov ax,0003h   ; Clear screen 
    int 10h 

    xor dx, dx   ; Update cursor to 0,0 . DH=Row, DL=Column 
    mov ah, 2   
    int 10h 

    mov bp, CHARSPERROW; BP = Num of characters per row 
    xor di, di   ; DI = Current character number we are processing 
    mov bx, 0030h  ; BH = Current page to print on 
         ; BL = Attribute to use when printing 
    mov cx, 1   ; CX = Number of characters to print 

printloop: 
    xor si, si   ; Index to even/odd array 
    test di, 1   ; Is current character pos even/odd? 
    jz pl_evenpos  ; if even si=0 
    inc si    ; if odd si=1 
pl_evenpos:  
    mov al, outcharsarr[si] 
         ;Print the character out based on even/odd index 
    mov ah, 09h 
    int 10h 

    dec bp    ; dec number of chars still needed on current row 
    ja pl_nextcol  ; If we haven't printed enough for current row 
         ; goto next column 

    ; If we reach here we have printed the require number for this row 
    ; advance to beginning of next row 
    xor dl, dl   ; col=0 
    inc dh    ; row=row+1 
    mov ah, 2   ; Update cursor 
    int 10h 
    mov bp, CHARSPERROW 
         ; Reset the number of characters needed on current row 
    jmp pl_endloop 

pl_nextcol: 
    inc dl    ; col=col+1 
    mov ah, 2   ; Update cursor 
    int 10h 

pl_endloop: 
    inc di 
    cmp di, CHARSTOPRINT 
         ; Have we reached total number of characters to print? 
    jb printloop  ; If not got back and continue printing 

    mov ah,4ch   ; Exit program 
    int 21h 

end MAIN