Closed ammarbinfaisal closed 1 year ago
fun main() { let sum = 0 for i in 1..10000000 { sum = sum + i print(i) } print(sum) }
this should sum up from 1 to 10000000 and print also in that order. But it prints
1 0 -1 ...
this is the generated assembly
section .data string0: db "%d", 10, 0 section .text extern print_int extern print_char extern print_bool global main _jZ8uVbhV3vC: push rbp mov rbp, rsp sub rsp, 40 mov qword [rbp-8], 0 mov qword [rbp-16], 1 mov qword [rbp-24], 10000000 mov rax, qword [rbp-16] mov qword [rbp-32], rax mov rbx, qword [rbp-16] mov rcx, qword [rbp-24] cmp rcx, rbx jle _5AWX39IVUWS mov rbx, 0 jmp _P2NcHciWvqZ _5AWX39IVUWS: mov rbx, 1 _P2NcHciWvqZ: mov qword [rbp-40], rbx mov rcx, qword [rbp-32] mov rdx, qword [rbp-24] _9h2fhfUVuS9: mov r8, qword [rbp-32] mov r9, qword [rbp-24] cmp r9, r8 jne _Ta2N95RxRTZ mov r8, 0 jmp _HWUsaD6HEdz _Ta2N95RxRTZ: mov r8, 1 _HWUsaD6HEdz: cmp r8, 0 je _BpLnfgDsc2W mov r8, qword [rbp-8] mov r9, qword [rbp-32] add r9, r8 mov qword [rbp-8], r9 mov r8, qword [rbp-32] mov rdi, r8 call print_int mov r10, qword [rbp-40] cmp r10, 0 je _D8F2qNfHK5a mov r10, qword [rbp-32] add r10, 1 mov qword [rbp-32], r10 jmp _9h2fhfUVuS9 _D8F2qNfHK5a: mov r11, qword [rbp-32] sub r11, 1 mov qword [rbp-32], r11 jmp _9h2fhfUVuS9 _BpLnfgDsc2W: mov r12, qword [rbp-8] mov rdi, r12 call print_int mov rax, r12 jmp _jZ8uVbhV3vC_ret _jZ8uVbhV3vC_ret: pop rbp add rsp, 40 ret main: call _jZ8uVbhV3vC mov rax, 60 mov rdi, 0 syscall
this should sum up from 1 to 10000000 and print also in that order. But it prints
this is the generated assembly