首页 > 解决方案 > 我不明白如何遍历数组

问题描述

我想使用si寄存器遍历一组持续时间数字。但它不适合我。如果我只传递一个数字dur,那么它工作正常,如果我想迭代,那么它会冻结而没有别的。解释错误。谢谢!

sseg    segment stack
        db      256 dup(?)
sseg    ends
dseg    segment
    music       dw  4063, 2559, 3835
    duration    dd  270000, 360000, 180000
    dur         dd 270000
    len         dw  3
dseg    ends
cseg    segment
        assume ss:sseg,cs:cseg,ds:dseg
start:  jmp main
main:   push    ds
        xor     ax,ax
        push    ax
        mov     ax,dseg
        mov     ds,ax
        cli
        ;------------------------------
        xor     di,di
        xor     si,si
cycle:
        mov     al,10110110b
        out     43h,al
        
        in      al,61h
        or      al,3
        out     61h,al
        ;------------
        mov     ax,music[si]
        out     42h,al
        mov     al,ah
        out     42h,al

        les     dx,dur      ;duratin[si]
        mov     cx,es
        mov     ah,86h
        int     15h
        ;------------
        in      al,61h
        and     al,11111100b
        out     61h,al
        ;------------
        inc     si
        inc     di
        cmp     si,len
        jnae    cycle
        ;------------------------------
exit:
        sti
        mov     ax,4c00h
        int     21h     
cseg    ends
        end start

标签: assemblytimerx86x86-16masm

解决方案


mov     ax,music[si]

在接下来的讨论中, MASM 是否允许music[si][music+si]不重要。

重要的一点是SI寄存器不是我们从高级语言中知道的数组索引,而是它是从数组开头的偏移量,并且总是以字节为单位
因此,在您的程序中,您需要将 2 添加到寻址音乐数组(单词)的寄存器中,并且您需要将 4 添加到寻址持续时间数组(dwords)的寄存器中。

    music       dw  4063, 2559, 3835
    duration    dd  270000, 360000, 180000
    len = $-offset duration

    ...

    xor     di, di           ; Offset in duration array
    xor     si, si           ; Offset in music array
cycle:
    ...
    mov     ax, music[si]
    ...
    les     dx, duration[di] ; This is DI, beware of your typo
    mov     cx, es
    mov     ah, 86h
    int     15h
    ...
    add     si, 2            ; To next music item (word=2)
    add     di, 4            ; To next duration item (dword=4)
    cmp     di, len
    jb      cycle

推荐阅读