You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ML64中printf函数与proc指令异常问题求助

问题1:替换CRT库后程序崩溃

原本正常运行的独立汇编程序,将msvcrt.lib替换为libcmt.lib后崩溃。根据微软文档,两者均可静态链接CRT启动代码,仅msvcrt.lib搭配DLL使用。程序无依赖DLL,推测崩溃由printf函数导致,原因不明。

相关代码与命令

汇编文件test1.asm:

;this file -> test1.asm

includelib msvcrt.lib
includelib legacy_stdio_definitions.lib

extern _CRT_INIT: proc     ;custom entrypoint needs c-runtime to be initialised by the programmer.
extern printf: proc
extern ExitProcess: proc

.data
    msg db "hello world!", 13, 10, 0

.code
fun proc 
    sub rsp, 32          ;for shadow space
    call _CRT_INIT       ;called _CRT_INIT because I have my own entry point named "fun"
    lea rcx,msg
    call printf
    add rsp, 32
    call ExitProcess
fun endp
end

汇编命令:ml64 /nologo /c test1.asm
链接命令:link /entry:fun /subsystem:console test1.obj


问题2:proc指令后缀影响C调用汇编函数

修改汇编代码后从C文件调用fun函数,proc指令后必须添加任意字符(如"c"、"abed"等)程序才正常,否则崩溃。此前独立程序中proc后无需后缀即可运行,且任意无效字符也能生效,原因不明。

相关代码与命令

汇编文件test1.asm:

;this file -> test1.asm

;not including any libraries. linking the .c file make the libraries link too because that is in my libpath environment variable

;extern _CRT_INIT: proc    ;no need of _CRT_INIT because now mainCRTStartup() will be the entrypoint and .c file will take care of initializing the c-runtime
extern printf: proc
;extern ExitProcess: proc  ;no need of exitprocess. Instead i'm using ret instruction because I'm calling fun() from .c file.

.data
    msg db "hello world!", 13, 10, 0

.code
fun proc c               ;even if I replace 'c' with something like "abed" the program works. but if I dont give anything after proc then the program crashes.
    sub rsp, 32          ;for shadow space
    lea rcx,msg
    call printf
    add rsp, 32
    ret
fun endp
end

C文件test2.c:

//this file -> test2.c
#include <stdio.h>
#include <conio.h>
void fun();    //in C no need of extern keyword. extern is needed in c++
int main()
{
   fun();
   getch();
}

汇编命令:ml64 /nologo /c test1.asm
编译命令:cl /nologo /c test2.c
链接命令:link test1.obj test2.obj

内容的提问来源于stack exchange,提问作者Aniket Bose

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 07:35:11