I'm working on a compiler for a custom programming language, and I'm currently working on implementing arrays. I have integer arrays fully working and float arrays mostly working, but with these I'm getting a weird error.
When I try to access an index via the value of another variable, it returns the correct element except when I access index 0. This is the code I'm compiling: (the float datatype is equivalent to float in C- 32-bit single-precision floating point type):
func main
float[4] numbers = 1.1, 2.2, 3.3, 4.4
int index = 0
float no = numbers[index]
printf("%f\n", no)
end
Here is the Assembly it generates (I use GAS with Intel syntax as my Assembler):
.intel_syntax noprefix
.data
FLT_0: .long 1066192077
FLT_1: .long 1074580685
FLT_2: .long 1079194419
FLT_3: .long 1082969293
STR_0: .string "%f\n"
.text
.global main
main:
push rbp
mov rbp, rsp
sub rsp, 32
movss xmm0, DWORD PTR FLT_0[rip]
movss DWORD PTR [rbp-16], xmm0
movss xmm0, DWORD PTR FLT_1[rip]
movss DWORD PTR [rbp-12], xmm0
movss xmm0, DWORD PTR FLT_2[rip]
movss DWORD PTR [rbp-8], xmm0
movss xmm0, DWORD PTR FLT_3[rip]
movss DWORD PTR [rbp-4], xmm0
mov DWORD PTR [rbp-20], 0
mov eax, DWORD PTR [rbp-20]
cdqe
movss xmm1, DWORD PTR [rbp-16+rax*4]
movss DWORD PTR [rbp-24], xmm1
mov rdi, OFFSET FLAT:STR_0
cvtss2sd xmm0, DWORD PTR [rbp-24]
call printf
leave
ret
The compiled program should print 1.100; however, it prints 0.0000. But, if I change the index variable in my language to any other value, it works. Any ideas?
Thanks!