arrays - 在汇编中将字符串转换为数组/结构

标签 arrays string assembly x86 nasm

我需要做的就是获取用户输入的字符串并将它们放入数组或结构中,但我不断收到错误

Invalid effective address

这是什么意思?

代码

section .data
  fName db 'Enter your first name: '
  fNameLen equ $-fName
  lName db 'Enter your last name: '
  lNameLen equ $-lName

  numberOfStruct equ 50
  structSize equ 25
  firstName equ 0
  lastName equ 10


section .bss
  person resb numberOfStruct*structSize

section .text
  global _start

_start:
  mov esi, 0
  input_start:
    mov eax, 4
    mov ebx, 1
    mov ecx, fName
    mov edx, fNameLen
    int 80h

    mov eax, 3
    mov ebx, 0
    lea ecx, [person+structSize*esi+firstName] ;This is where the error appears
    mov edx, 15
    int 80h

    mov eax, 4
    mov ebx, 1
    mov ecx, lName
    mov edx, lNameLen
    int 80h

    mov eax, 3
    mov ebx, 0
    lea ecx, [person+structSize*esi+lastName] ;This is where the error appears
    mov edx, 10
    int 80h

    inc esi

    cmp esi,10
    jl input_start

    exit:
    mov eax, 1
    mov ebx, 0
    int 80h

我做错了吗?

最佳答案

编辑:添加了代码并编辑了答案以匹配问题中的更改。

lea ecx, [person+structSize*esi+firstName] ; this is where the error appears

lea ecx, [person+structSize*esi+lastName]   ; this is where the error appears

两者都有相同的错误:不能与 25 相乘,有效的缩放因子为 124 8

编辑:正如 Harold 指出的,imul 是计算地址的最简单方法:

 imul ecx,esi,25                    ; ecx == 25*esi
 lea ecx,[ecx+person+firstName]     ; ecx == 25*esi + person + firstName

您还可以使用 3 个 lea 来计算地址:

 lea ecx,[8*esi]                    ; ecx == 8*esi
 lea ecx,[ecx+2*ecx]                ; ecx == 24*esi
 lea ecx,[ecx+esi+person+firstName] ; ecx == 25*esi + person + firstName

Wikipedia对所有 64 位、32 位和 16 位寻址模式进行了有用的总结。

关于arrays - 在汇编中将字符串转换为数组/结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12482434/

相关文章:

c++ - C vector 错误 'char(*)[]: unknown size' - vector 删除迭代器超出范围

javascript - 不使用键访问 json 对象中的数组

c - 反转 32 位整数数组的元素顺序

javascript - 如何为我的文本框使用此 trim 功能

c# - 在任何第一个数字之前拆分字符串

python - 一次加法需要多少 CPU 周期?

javascript - 如何从 math.floor 中获取重复的数字?

c++ - 如何将用户输入与字符串进行比较?

c - 如何反编译这个 x87 汇编计算?

c - NASM 数组指针操作