I’m trying to write a simple x86-64 assembly program (AT&T syntax) to calculate the sum of a float array and print the result using printf. Here’s my code:
asm
.section .data
msg: .asciz "Result is: %fn"
n: .int 5
a: .float 1.1, 2.2, 3.3, 4.4, 5.5
res: .float 0
.section .text
.globl main
main:
xor %ecx, %ecx
movss res, %xmm0
mov n, %ebx
while:
cmp %ebx, %ecx
je end_while
addss a(,%ecx, 4) , %xmm0
inc %ecx
jmp while
end_while:
mov $msg, %rdi
mov $0, %eax
call printf
call exit
Problem: When I run this program, I get a Segmentation fault (core dumped) error. I suspect there’s something wrong with how I’m using the printf function, but I’m not sure where the issue is.
Am I using the printf function correctly in this context?
Is there something wrong with the way I access the float array (addss a(,%ecx,4), %xmm0)?
Any help or guidance would be greatly appreciated!
Anh Khang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2