assembly - 我不明白如何遍历数组
问题描述
我想使用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
解决方案
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
推荐阅读
- mysql - 查找sql中每个不同行的最大计数
- wordpress - 根据在 WooCommerce 中选择的数量和选项显示可变产品的总价格和总重量
- postgresql - postgres 优化范围搜索查询
- javascript - 如何使热图图表上的颜色以比例值显示?
- css - 为什么 CSS 中的“direction: rtl”在基于 Chromium 的浏览器上不起作用?
- javascript - 渐变文本颜色,使用 Material-UI
- javascript - TSC 重新编译未更改的 TypeScript 文件
- amazon-web-services - AWS - 在 Terraform 中运行带有 ALB 和超过 5 个端口的 ECS EC2 集群运行容器
- python - ipykernel_launcher.py:错误:需要以下参数:
- c - 无法阻止数组在凯撒算法中溢出