指向数组的 ASM 语言指针 [英] ASM language pointer to an array

查看:25
本文介绍了指向数组的 ASM 语言指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目我应该创建一个程序,用 x 个斐波那契数填充一个数组.在这种情况下,它是 47.我有编程设置来获取数字等,但我似乎无法将它们放入我的数组中.任何帮助都会很棒,因为我确定我刚刚开始使用我的语法.我正在 Visual Studio 中调试它,所以我猜那是 masm?提前致谢.

my project I'm supposed to create a program that fills an array with an x number of fibbonacci numbers. In this case it's 47. I've got the programming set to get it the numbers etc, I just can't seem to get them into my array. Any help would be great as I'm sure i'm just off on my syntax. I'm debugging it in visual studio, so I guess that's masm? Thanks in advance.

.386
.model flat,stdcall
.stack 4096
ExitProcess proto,dwExitCode:dword

.data
fibba DWORD 47 DUP(?)

.code
main proc       

mov eax,1                               ;current number
mov ebx,0                               ;last number in array
mov edi,0                               ;array location
mov ecx,LENGTHOF fibba                  ;how many loops

    L1:
    mov [fibba+edi] ,eax                ;move current into array
    push eax                            ;move current into stack
    add eax, ebx                        ;adding previous to current
    pop ebx                             ;pulling last number used off stack
    inc edi                             ;incrementing array location
    loop L1


    invoke ExitProcess,0
main endp
end main

推荐答案

使用 [fibba+edi*4] 或将 edi 增加 4(dword 的宽度)).您目前正在经营 4B 商店,而这些商店与 3B 重叠.

Use [fibba+edi*4] or increment edi by 4 (the width of a dword). You're currently doing 4B stores that overlap by 3B.

相关:我对汇编语言 (x86):如何创建循环来计算斐波那契数列.

使用 push eax/pop ebx 是一种新的解决问题的方法.这不是最有效的,但它应该有效.好主意.此外,loop 指令很慢,请改用 dec ecx/jnz(或不同的循环条件,如 cmp edi, fibba+47*4).

Using push eax/pop ebx is a novel way to solve the problem. It's not the most efficient, but it should work. Neat idea. Also, the loop instruction is slow, use dec ecx/jnz (or a different loop condition, like cmp edi, fibba+47*4) instead.

(在评论中)

我在技术上将 fibba 定义为一个词还是一个双词?我应该使用双字.但那时 push pop 将不起作用,因为 push/pop 限制为 4 个字节..是吗?

Am I technically defining fibba as a word or as a double word? I should be using a doubleword. But at that point push pop wont work will it because push/pop is limited to 4 bytes.. Yeah?

您保留了 47 * 4 个字节,fibba 标签标记了它的开始.DWORD 是 32 位(4 个字节).在 x86 asm 术语中,一个词"是指由于其 16 位传统,因此只有 16 位.

You're reserving 47 * 4 bytes, with the fibba label marking the start of it. A DWORD is 32 bits (4 bytes). In x86 asm terminology, a "word" is only 16 bits, because of its 16-bit heritage.

在大多数 CPU 上,自然"是整数的大小是一个字(例如 MIPS、ARM 等上的 32 位),但在 x86 上不是.自然"x86 上的整数大小为 4 字节/32 位,但它被称为 dword(双字).无论如何,您不是在 fibba 中推送/弹出,而是使用它来保存/恢复寄存器的全宽(也是 4 个字节).

On most CPUs, the "natural" size of an integer is a word (e.g. 32 bits on MIPS, ARM, etc.), but not on x86. The "natural" size of an integer on x86 is 4 bytes / 32-bits, but it's called a dword (double-word). Anyway, you're not pushing/popping in fibba, you're using it to save/restore the full width of a register (which is also 4 bytes).

微软的汇编器有一些魔法"它查看您在标签后使用哪些指令来暗示 mov [fibba], 0 之类的操作数大小,但其他汇编程序需要显式大小,因为这两个操作数都不是寄存器.IMO 这是一种更好的思考方式:内存本身没有任何类型,它只是一袋字节,作为 asm 程序员,您可以自行解决.这就是为什么您在执行重叠 4 字节存储仅偏移 1 个字节时没有收到任何警告的原因,您只是在内存中获得了虚假数据.

Microsoft's assembler has some "magic" where it looks at what directives you use after a label to imply an operand size for something like mov [fibba], 0, but other assemblers require an explicit size because neither operand is a register. IMO this is a better way to think about it: memory doesn't inherently have any types, it's just a bag of bytes, and it's up to you as the asm programmer to get it right. That's why you didn't get any warnings for doing overlapping 4-byte stores offset by only 1 byte, you just got bogus data in memory.

这篇关于指向数组的 ASM 语言指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆