I’m pretty new to assembly, and I was wondering how one would write output to stderr. I know you can access C Standard Library functions, like printf, to print to the console. But I can’t figure out how to print to stderr. I was trying to use fprintf, but I’m just sort of guessing at arguments, and I have no idea how to specify stderr as the file pointer. Thanks.
Edit: As per sehe’s suggestion, I tried this:
.586
.model small,c
.stack 100h
.data
msg db 'test', 0Ah
.code
includelib MSVCRT
extrn fprintf:near
extrn exit:near
public main
main proc
push offset msg
push 2 ;specify stderr
call fprintf ;print to stderr
push 0
call exit ;exit status code 0
main endp
end main
But it just caused my program to crash. Any other advice?
Are you using fprintf from the MSVCRT dll?
The first parameter is a pointer to a stream. Here is how you could use fprintf in assembly.
Also, when calling C functions from Assembly, you need to adjust the stack after each call that you push parameters to.
Also, a BIGGIE… your string is NOT NULL terminated! You must NULL terminate your strings, that is how functions find the length of the string.
Not sure what Assembler you are using, but this is how you can do it in MASM: