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

3

我只需要获取用户输入的字符串并将它们放入数组或结构中,但我一直得到错误:

无效的有效地址

这是什么意思?

代码

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

我完全做错了吗?


25 不是有效的比例尺,只有 1、2、4 和 8 是有效的。您需要单独进行乘法运算。 - harold
1个回答

5

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

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”进行乘法运算,有效的缩放因子是“1”、“2”、“4”和“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

维基百科提供了一个有用的总结,涵盖了所有64位、32位和16位寻址模式。


那里,我应该有名字和姓氏。 - Doctor Whom
为什么我不能用25相乘?但那是一个结构的大小。那么我应该如何在正确的位置输入字符串呢?我感到很迷惑。 - Doctor Whom
@DoctorWhom,SIB字节无法编码。请手动进行乘法运算。 - harold
1
那么,为什么要进行双宽度乘法?imul eax, esi同样可以正常工作,而不会破坏edx,你也可以使用imul eax, esi, 25 - harold
@harold 你说得对,有符号乘法imul在这里可以很好地工作。回答已相应地进行了编辑。imul eax,esi,25会覆盖此时已经设置的eax寄存器(但是该指令可以在imul之后移动而不会出现问题)。使用imul ecx,esi,25不需要移动任何其他行。 - nrz
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接