In my code i'm trying to print an array in reverse. My two main ideas were to either use the stack and the LIFO property to do it or use a loop as index from 10 to 0 zero to access the elements in reverse. I went with the second one due to alignments problems with the stack method.
I'm quite new to assembly and would appreciate some help on that one, to know where my error is. Thanks in advance !
DEFAULT REL
; external functions for inputs/outputs printf and scanf/printf
extern printf
extern scanf
section .data
prompt db "Entrez un entier : ",0
longIntFormat db "%ld",0
section .bss
entier resb 10 ; array of 10 integers
section.text
push rbp
mov rcx, 0
mov rdx, 0 ; initialise counter
lea rcx, [entier] ; load array into register rcx
; fills the array with user input
_getLoop:
; call printf
lea rdi,[prompt]
mov rax,0
call printf wrt ..plt
; call scanf
lea rdi,[longIntFormat]
lea rsi, [rcx + rdx] ; array + Index
mov rax,0
call scanf wrt ..plt
inc rdx ; inc. Index/counter
cmp rdx, 10
jl _getLoop ; While counter is less than 10 (size of array)
mov rcx, 0 ; set rcx to 0
mov rdx, 10 ; counter set to 10
lea rcx, [entier] ; load array into rcx
; print the array in reverse using the counter as Index
_printLoop:
; call printf
lea rdi, [rcx + rdx] ; rdi = [array + Index]
mov rax,0
call printf wrt ..plt
dec rdx
cmp rdx, 0 ; compare counter with 0
jge _printLoop ; Once 0 is reached the loop has gone through all the array
;restores registers
pop rbp
; returns 0 to C program
mov rax, 0
ret