I'm working on a 64-bit assembly program using NASM and MinGW on Windows 10. My goal is to read the content of a file and print it to the console using C library functions (fopen, fread, fclose).
While the file is successfully opened, and the program prints the "File content:" prompt, the content of the file is not being displayed as expected. The file content either does not print at all.
Here's my current code:
; read file and print content
section .data
filename db "test.txt", 0
read_mode db "r", 0
prompt db "File content:", 10, 0
buffer_size equ 1024
bytes_read dq 0
hFile dq 0
buffer db buffer_size dup(0)
error_msg db "Failed to open the file.", 10, 0
section .text
extern printf, fopen, fread, fclose
global main
main:
push rbp
mov rbp, rsp
; Open the file
lea rcx, [rel filename] ; Filename
lea rdx, [rel read_mode] ; Read mode
call fopen
mov [rel hFile], rax ; Store file handle
; Check if the file was opened successfully
cmp rax, 0
je file_error
; Read file content
lea rcx, [rel buffer] ; Buffer
mov rdx, buffer_size ; Buffer size
lea r8, [rel bytes_read] ; Bytes read
mov r9, [rel hFile] ; File handle
call fread
; Print the content
lea rcx, [rel prompt] ; Print the prompt
call printf
lea rcx, [rel buffer] ; Print the buffer content
call printf
; Close the file
mov rcx, [rel hFile]
call fclose
; Clean up and exit
jmp program_end
file_error:
lea rcx, [rel error_msg]
call printf
program_end:
mov rsp, rbp
pop rbp
ret
Issues:
- File Reading and Buffer Handling: The fread function is used to read the file content into a buffer, but the content is not being displayed correctly.
- Printing the Buffer: The content is either not printed at all. The buffer might not be null-terminated correctly.
What I've Tried:
- Added null-termination to the buffer after reading with
fread. - Verified that the file path is correct and the file is accessible.
- Ensured that the file handle is correctly passed to
freadandfclose.
Questions summary:
- Is there something wrong with how fread is used in this context?
- Are there any known issues with using printf to print the buffer when dealing with C library functions in assembly?
- How can I ensure that the buffer is properly handled and printed?
Additional Information:
- The file is in the same directory as the executable.
- The fopen, fread, and fclose functions are correctly declared as extern.
- Using windows 10 x64 OS, nasm and mingw-w-64 as environments.
I'm a newbie to assembly, and a self learner. Thank you in advance for your help!
freadwrong. Probably confused it withReadFile. It does not take a pointer for bytes read. It needs a record count input. Usemov r8d, 1instead. You also have to allocate shadow space, put asub rsp, 32after themov rbp, rspin the prologue. Finally it's a bad idea to useprintfwithout a format string. You probably want to usefwritetostdoutanyway since you have a buffer with a length (use whatfreadreturned).freadby passing the correct item size and count. I also allocated shadow space in the prologue usingsub rsp, 32. Instead of usingfwrite, I switched to usingprintfwith a format string, ensuring the buffer is null-terminated before printing. This approach resolved the issue, and the file content is now printed correctly.