2015-11-17 24 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