1

I have a question regarding how to use/call the c function fprintf in NASM assembly. I have looked up the documentation of fprintf and pushed the parameters to the stack in the correct order. When I run my code I am getting and output file that is empty and the name is "Project4_int.outw". It is clearly just appending the write string to the end and not opening the file correctly. What am I doing wrong?

section .data
write_char: db 'w', 0
intFilename: db "Project4_int.txt"
intfdes: dd 0
format: db "%d", 10, 0

section .text
push dword write_char
push dword intFilename
call fopen
add esp, 8
mov [intfdes], eax

push dword [intArray + 4]
push dword format
push dword [intfdes]
call fprintf
add esp, 12

Nick7902
  • 11
  • 1
  • 1
    Did you close the file? Did you correctly terminate the program (i.e. not with a syscall)? What OS are you using? You are missing a zero terminator after your file name (although the `intfdes` being initialized to zero happens to double as a terminator) – Jester Nov 08 '21 at 21:59
  • 1
    This is only a snippet of my code, I terminate the program at the end. I realized that I have to push everything to the stack in 4 bytes so storing [intArray + 4] in eax and then pushing eax to the stack fixed my problem. I also was trying to close the file with system calls but changed that to fclose. Found this solution here: https://stackoverflow.com/questions/26951493/are-there-any-examples-of-programs-that-generate-text-text-files-as-output-in-na – Nick7902 Nov 08 '21 at 22:17
  • `push dword [intArray + 4]` is pushing 4 bytes so doing that via `eax` should make no difference. – Jester Nov 08 '21 at 22:44

0 Answers0