0

This is my nasm code:

extern fprintf
extern printf
extern stdout

section .data
    mas dw 1,1,1,1,1, 2,2,2,2,2, 3,3,3,3,3, 4,4,4,4,4, 5,5,5,5,5, 6,6,6,6,6, 7,7,7,7,7
    fmtstr db "%d", 10, 0

section .text
    global main
main:
    push rbp
    mov rbp,rsp

    mov esi, 2
    mov eax, [mas + esi * 20]

    mov rdi, fmtstr
    mov rsi, rdx
    mov rax, 0
    call printf

leave
ret

all I want is to get in eax mas[2], where each element of array consists of 5 int, so it is 20 bytes But nasm complains about [mas + esi * 20]: error: invalid effective address

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    [x86 asm: How to set a reg to a non-SIB supported offset?](https://stackoverflow.com/q/70186421) shows how to do manual address math if your scale factor isn't 1, 2, 4, or 8. But how do you expect EAX (a 4-byte register) to hold 20 bytes of data? You'll get the first 2 words of the struct. Also, prefer 64-bit addressing modes like `[mas + rsi]` not `esi`. (e.g. `mov esi,2` / `lea eax, [rsi + rsi*4]` / `mov eax, [mas + rax * 2]` like a compiler would use, look at https://godbolt.org/). – Peter Cordes Dec 02 '21 at 11:17
  • The rest of your code doesn't make much sense either; you copy RDX (which still holds `envp`) to the 2nd arg of `printf("%d\n", envp)`, not using your EAX load result. – Peter Cordes Dec 02 '21 at 11:20
  • @PeterCordes I mean that struct in all consists 20 bytes and I just need 4 bytes for each int to change it – Саша Волотко Dec 02 '21 at 11:24
  • 1
    Ok, if that's what you're trying to do, it's an exact duplicate, not also or instead some other design bug. Since this is x86-64, you could use RAX to load 4x `short` word-sized integers. (`int` is 4 bytes in standard x86-64 ABIs, like System V which you're using. You used `dw` which emits in units of 2-byte words, rather than `dd` for dwords.) – Peter Cordes Dec 02 '21 at 11:29
  • @PeterCordes it was my experiments, in main prog I use `dd`. Thank u for your link to answer, coz English isn't my native, so idk how will my question be in eng. – Саша Волотко Dec 02 '21 at 11:44

0 Answers0