【问题标题】:String into an array/structure in assembly在汇编中将字符串放入数组/结构中
【发布时间】:2012-09-11 00:53:41
【问题描述】:

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

无效的有效地址

这是什么意思?

代码

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 是。您必须单独进行乘法运算。

标签: arrays string assembly x86 nasm


【解决方案1】:

编辑:添加代码并编辑答案以匹配相关更改。

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相乘,有效的比例因子是1248

编辑:正如 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 位寻址模式进行了有用的总结。

【讨论】:

  • 那里,我应该是个人和姓氏。
  • 为什么我不能乘以 25?但这是一个结构的大小。那我怎样才能在正确的地方输入字符串呢?我迷路了。
  • @DoctorWhom SIB 字节无法对其进行编码。只需手动进行乘法运算。
  • @DoctorWhom 检查已编辑的答案。在 x86 汇编编程中,您需要管理许多任意规则,例如,在 32 位和 64 位寻址中,唯一合法的缩放因子是 124 和 @ 987654335@,或者对于mul,另一个(隐式)操作数始终为alaxeaxrax,结果始终存储在axdx:ax、@ 987654343@ 或 rdx:rax 并且对于 shlshrrolrorrclrcr,另一个操作数必须是 cl 或立即值,其他寄存器对此无效。就是这样设计的。
  • 那么,为什么是双倍宽度乘法? imul eax, esi 也可以正常工作,而不会杀死 edx,你可以这样做 imul eax, esi, 25
猜你喜欢
  • 1970-01-01
  • 2021-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多