Hope all is well.
I am trying to loop over each element of an array and printing each element but cannot find the right way. I have been searching for multiple hours now.
So far I have this code:
.section .data
array:
.quad 1,2,3,4,5
.section .text
.globl main
main:
leaq array(%rip), %rcx
movq $0, %rsi
jmp test
body:
movq (%rcx, %rsi, 8), %rdi
callq print_int
incq %rsi
jmp test
test:
cmpq $5, %rsi
jne body
je exit
exit:
movq $3, %rdi
callq print_int
But I get a segmentation fault. The closest I have gotten is printing 13
:
.section .data
array:
.quad 1,2,3,4,5
.section .text
.globl main
main:
leaq array(%rip), %rcx
movq $0, %rsi
loop:
movq (%rcx, %rsi, 8), %rax
movq %rax, %rdi
callq print_int
addq $1, %rsi
cmpq $5, %rsi
je loop
jmp exit
exit:
movq $3, %rdi
callq print_int
print_int
is a function I am using from a compiler runtime.
I am compiling with gcc
: gcc -g runtime.o program.s
.
Any help will be appeaciated. thanks
The duplicate question doesn’t answer my question because I’m not asking about functional calls. I want to print the elements of an array