0

My Code;

section .data
    array DB 97,114,114,97,121 ; "a" "r" "r" "a" "y"

section .text
    global _start

_start:
    ;print "a"
    mov eax,4
    mov ebx,1
    mov ecx,[array]
    mov edx,1
    int 0x80

    ;print "r"
    mov eax,4
    mov ebx,1
    mov ecx,[array + 1]
    mov edx,1
    int 0x80

    ;print "r"
    mov eax,4
    mov ebx,1
    mov ecx,[array + 2]
    mov edx,1
    int 0x80

    ;print "a"
    mov eax,4
    mov ebx,1
    mov ecx,[array + 3]
    mov edx,1
    int 0x80

    ;print "y"
    mov eax,4
    mov ebx,1
    mov ecx,[array + 4]
    mov edx,1
    int 0x80

    ;exit
    mov eax,1
    int 0x80

this code is not working as i want. What i want is to print the elements inside an array. I'm not getting any error. Nothing print on the screen.

!!! I know there is a code like below.What I want is nothing like the below.

section .data
    array DB 97,114,114,97,121 ; "a" "r" "r" "a" "y"

section .text
    global _start

_start:
    mov eax,4
    mov ebx,1
    mov ecx,array
    mov edx,5
    int 0x80

    mov eax,1
    int 0x80

Thanks ... .

Jester
  • 52,795
  • 4
  • 67
  • 108
  • 3
    The write system call expects an address. Instead of `mov ecx,[array + 1]` and the like, you want a `lea ecx,[array + 1]`. Or you could also drop the brackets and have `mov ecx, array+1`. – Jester May 28 '21 at 00:34
  • Is there any difference in performance between `lea ecx,[array +1]` and `mov ecx,array+1`? @Jester – yulaf_ve_abant May 28 '21 at 00:38
  • Yes; mov-immediate is smaller code size, and can run on more execution ports on most CPUs. Never use `lea` to put a 32-bit constant into a register. (You will see a lot of example of code using `lea reg, [symbol]` though, from people who either don't know or care that it's worse. LEA is [only a good idea in 64-bit mode](https://stackoverflow.com/questions/57212012/how-to-load-address-of-function-or-label-into-register) where you can use RIP-relative LEA, but mov can only use absolute immediates.) – Peter Cordes May 28 '21 at 00:43
  • Oh, turns out I already wrote a whole answer about MOV vs. LEA efficiency for code like this: [What is the difference between MOV and LEA in terms of retrieving an address](https://stackoverflow.com/q/35475396) – Peter Cordes May 28 '21 at 00:47

0 Answers0