0

Im very new to assembly and nasm and im trying to print all the chars from a string, like this in python:

s = '1234'
for i in s:
    print(i)

I have read that only bx register can be used for indexing. But I have seen a lot of people using any other register. But it doesn't work for me in any way.

Im in MacOS x86_64.

SECTION .data

    t: db "1234", 10
    t.len: equ $-t

    e: db "0"
    e.len: equ 2

SECTION .text
global start

start:

    jmp top

top:

    mov al, [rel t + bx]
    mov [rel e], al

    mov rax, 0x2000004
    mov rdi, 1
    mov rsi, e
    mov rdx, t.len
    syscall

    inc bx

    cmp rsi, 0
    je exit
    jmp top

exit:

    mov rax, 0x2000001
    mov rdi, 0
    syscall

Im getting this two errors:

get_string_len.asm:20: error: impossible combination of address sizes
get_string_len.asm:20: error: invalid effective address
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
l4x3
  • 3
  • 3
  • 2
    *I have read that only bx register can be used for indexing* - that's for 16-bit mode, and even then it's not true. In 64-bit mode, any 64-bit register can be part of a `[reg + disp32]` addressing mode. But you can't use any registers other than RIP in a RIP-relative addressing mode like `[rel t]`. Normally you'd just do `lea rsi, [rel t]` and write all `t.len` bytes in one system call. – Peter Cordes Mar 23 '21 at 19:45
  • 1
    `bx` is one of the 16-bit registers that can be used for 16-bit addressing, among `bp`, `si`, and `di`. You need any 32-bit or 64-bit register instead. Also, `rel` is not valid for addresses that involve a register. – ecm Mar 23 '21 at 19:46
  • Your loop seems confused. Do you want to print each byte separately? If so, pass len=1 in RDX, not `t.len`. (And not `e.len`, which would include whatever garbage follows the 1 byte you reserved.) – Peter Cordes Mar 23 '21 at 19:48
  • Possible duplicate for the array indexing: [Relative Addressing errors - Mac 10.10](https://stackoverflow.com/a/26929101) and/or [Mach-O 64-bit format does not support 32-bit absolute addresses. NASM Accessing Array](https://stackoverflow.com/q/47300844) – Peter Cordes Mar 23 '21 at 19:51
  • Yes, i want to print each byte separately. I need to increase the index with a register. When I write the index directly in the code it works correctly, but when I do it with a register it does not. – l4x3 Mar 23 '21 at 20:02

0 Answers0