我对汇编很新,我想知道如何将输出写入stderr.我知道您可以访问C标准库函数,如printf,以打印到控制台.但我无法弄清楚如何打印到stderr.我试图使用fprintf,但我只是猜测参数,我不知道如何指定stderr作为文件指针.谢谢.
编辑:根据sehe的建议,我尝试了这个:
.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
Run Code Online (Sandbox Code Playgroud)
但它只是导致我的程序崩溃.还有其他建议吗?
你在使用MSVCRT DLL的fprintf吗?
第一个参数是指向流的指针.以下是如何在汇编中使用fprintf.此外,从Assembly调用C函数时,需要在每次调用参数后调整堆栈.
另外,BIGGIE ......你的字符串是NOT NULL终止的!您必须NULL终止字符串,这是函数查找字符串长度的方式.不确定你正在使用什么汇编程序,但这是你在MASM中可以做到的:
include masm32rt.inc
_iobuf STRUCT
_ptr DWORD ?
_cnt DWORD ?
_base DWORD ?
_flag DWORD ?
_file DWORD ?
_charbuf DWORD ?
_bufsiz DWORD ?
_tmpfname DWORD ?
_iobuf ENDS
FILE TYPEDEF _iobuf
.data
msg db 'test', 0Ah, 0
.data?
stdin dd ?
stdout dd ?
stderr dd ?
.code
start:
call crt___p__iob
mov stdin,eax
add eax,SIZEOF(FILE)
mov stdout,eax
add eax,SIZEOF(FILE)
mov stderr,eax
push offset msg
push eax
call crt_fprintf
add esp, 4 * 2
push 0
call crt_exit
end start
Run Code Online (Sandbox Code Playgroud)