3

All I need to do is get the user-inputted strings and put them inside the array or structure, but I keep getting the error

Invalid effective address

What does this mean?

The code

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

Did I do it entirely wrong?

1
  • 25 is not a valid scale, only 1, 2, 4 and 8 are. You'll have to do the multiplication separately. Commented Sep 18, 2012 at 18:10

1 Answer 1

5

Edit: Added code and edited answer to match changes in question.

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

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

Both these have the same error: You cannot multiply with 25, the valid scaling factors are 1, 2, 4 and 8.

Edit: As Harold pointed out, imul is the easiest way to compute the address:

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

You can also compute the address by using 3 lea's:

 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 has a useful summary of all 64-bit, 32-bit and 16-bit addressing modes.

Sign up to request clarification or add additional context in comments.

8 Comments

there, i should be person and lastName.
why can't i multiply with 25? but that's the size of one structure. how can i input the string in the right place then? im lost.
@DoctorWhom the SIB byte can't encode it. Just do the multiplication manually.
@DoctorWhom Check edited answer. In x86 assembly programming there are a lot of arbitrary rules which you need to manage, such as the fact that in 32-bit and 64-bit addressing the only legal scaling factors are 1, 2, 4 and 8, or the fact that for mul the other (implicit) operand is always al, ax, eax or rax and the result is always stored in ax, dx:ax, edx:eax or rdx:rax and the fact that for shl, shr, rol, ror, rcl and rcr the other operand must be either cl or an immediate value, other registers are not valid for that. It's designed that way.
So, why a double-width multiplication? imul eax, esi would work just as well, without killing edx, and you could do imul eax, esi, 25.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.