如何在 x86 程序集中正确索引数组 [英] How to index arrays properly in x86 assembly
问题描述
我正在努力确保我了解 SI 和 DI 寄存器.我在汇编语言方面的背景有些限制在 6502,所以请耐心等待.
I am trying to make sure that I understand the SI and DI registers. My background in assembly language is somewhat limited to 6502, so bear with me here.
我有一个关于如何使用 SI 作为简单计数器的快速示例.我有点担心我可能会滥用这个寄存器.
I have a quick example of how I would go about using SI as a simple counter. I am a bit concerned that I might be misusing this register though.
mov si, 0 ; set si to 0
mov cx, 5 ; set cx to 5 as we will count down to 1
do:
mov ah, 02h ; setup 02h DOS character output interrupt
mov dl, [table + si] ; grab our table with the si offset
add dl, '0' ; convert to ascii integer
int 21h ; call DOS service
inc si ; increment si
loop do ; repeat unto cx = 0
ret
table: db 1,2,3,4,5
---
OUTPUT:> 12345
这是使用 SI 的正确方法吗?我知道在 6502 程序集中,您可以使用 X 和 Y 寄存器来偏移数组/表.然而,在我对 x86 的研究中,我开始意识到还有很多东西可以使用.比如在'loop'指令中CX如何自动递减.
Is this the right way to use SI? I know in 6502 assembly, you can use the X and Y registers to offset arrays / tables. However, in my studies of x86, I am starting to realize how much more there is to work with. Such as how CX is automatically decremented in the 'loop' instruction.
我希望继续前进,我将能够通过编写高效的代码来节省资源.
I am hoping that moving forward, I will be able to save resources by writing efficient code.
预先感谢您的意见.
推荐答案
SI
的这种使用非常好.在大多数英特尔调用约定中,SI 的优点是作为保留寄存器.此外,从历史上看,SI 是少数几个可以在内存加载操作中用作索引的寄存器之一.在现代英特尔 CPU 中,任何寄存器都可以.
This use of SI
is perfectly fine. SI has the benefit of being a preserved register in most Intel calling conventions. Also, historically, SI was one of the few registers that you could use as an index in a memory load operation; in a modern Intel CPU, any register would do.
SI
仍然可以通过 lods
指令得到一些特殊处理.
SI
still gets some special treatment with the lods
instruction.
这篇关于如何在 x86 程序集中正确索引数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!