Online Compiler Nasm

extern fopen, fclose, fscanf, fprintf, malloc, free section .data input_name db "input.txt", 0 output_name db "output.txt", 0 mode_read db "r", 0 mode_write db "w", 0 fmt_in db "%d", 0 fmt_first db "%d", 0 fmt_next db " %d", 0 ; ebx - file* ; ecx - текущий узел при поиске места вставки ; edx - адрес нового созданного узла ; esi - голова списка ; edi - next при поиске и free, текущий узел при печати section .text global main main: push ebp mov ebp, esp push ebx push esi push edi and esp, -16 sub esp, 32 xor esi, esi mov dword [esp], input_name mov dword [esp+4], mode_read call fopen mov ebx, eax read_loop: mov dword [esp], ebx mov dword [esp+4], fmt_in lea eax, [ebp-16] mov dword [esp+8], eax call fscanf cmp eax, 1 jne read_end mov dword [esp], 8 call malloc mov edx, eax mov eax, [ebp-16] mov [edx], eax mov dword [edx+4], 0 cmp esi, 0 je insert_head cmp eax, [esi] jl insert_head mov ecx, esi find_place: mov edi, [ecx+4] cmp edi, 0 je insert_after cmp eax, [edi] jl insert_after mov ecx, edi jmp find_place insert_head: mov [edx+4], esi mov esi, edx jmp read_loop insert_after: mov edi, [ecx+4] mov [edx+4], edi mov [ecx+4], edx jmp read_loop read_end: mov dword [esp], ebx call fclose mov dword [esp], output_name mov dword [esp+4], mode_write call fopen mov ebx, eax mov edi, esi mov dword [ebp-20], 1 print_loop: cmp edi, 0 je print_end cmp dword [ebp-20], 1 jne print_next mov dword [esp], ebx mov dword [esp+4], fmt_first mov eax, [edi] mov dword [esp+8], eax call fprintf mov dword [ebp-20], 0 jmp print_advance print_next: mov dword [esp], ebx mov dword [esp+4], fmt_next mov eax, [edi] mov dword [esp+8], eax call fprintf print_advance: mov edi, [edi+4] jmp print_loop print_end: mov dword [esp], ebx call fclose free_loop: cmp esi, 0 je finish mov edi, [esi+4] mov dword [esp], esi call free mov esi, edi jmp free_loop finish: xor eax, eax lea esp, [ebp-12] pop edi pop esi pop ebx mov esp, ebp pop ebp ret
This program reads integers from `input.txt`, inserts them into a singly linked list in sorted order, writes the sorted list to `output.txt`, then frees the memory. The list nodes contain an integer and a pointer to the next node.

- The stack alignment (`and esp, -16`) is done after pushing registers, which may cause misalignment when calling C functions. The stack must be 16-byte aligned at the point of a `call` instruction. Check where the stack pointer is relative to that requirement after the pushes and alignment.  
- The `lea esp, [ebp-12]` before popping registers assumes a specific stack layout, but the earlier `sub esp, 32` and the pushes may not match this offset. Verify that the stack pointer is correctly restored to the saved register values.