crt_printf and ExitProcess in MASM32 with lib

2019-05-30 07:28发布

问题:

crt_printf, crt_scanf, ExitProcess

Windows 7 with masm32v11r for Environment Path

in .asm file, I'd like to call crt_printf to print (or call ExitProcess to end main procedure). However my code goes with:

.386  
.model flat,stdcall  
option casemap:none  
includelib D:\masm32\lib\msvcrt.lib  
printf proto C:dword,:vararg  
scanf proto C:dword,:vararg

.DATA
print_int    DB "%d",0
print_char   DB "%c",0

and my call procedure goes with:

PUSH    offset __temp13@_cal@main
PUSH    offset print_string
CALL    crt_printf
ADD     ESP,    8
PUSH    _realCock@main
PUSH    offset print_int
CALL    crt_printf
ADD     ESP,    8
PUSH    offset __temp14@_cal@main

When I Click the button of build All, messages come with:

Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

Assembling: D:\masm32\bin\object_code.asm
D:\masm32\bin\object_code.asm(105) : error A2006: undefined symbol : crt_printf
D:\masm32\bin\object_code.asm(109) : error A2006: undefined symbol : crt_printf
D:\masm32\bin\object_code.asm(179) : error A2006: undefined symbol : crt_scanf
D:\masm32\bin\object_code.asm(249) : error A2006: undefined symbol : ExitProcess
Assembly Error

I've struggled with such error for 24 hours, Thx!

回答1:

crt_printf is a special construct of the MASM32 developers to distiguish it from their macro printf. If you don't include \masm32\macros\macros.asm you don't need this special feature:

.386
.model flat,stdcall

includelib \masm32\lib\msvcrt.lib
includelib \masm32\lib\kernel32.lib

printf proto C :dword, :vararg          ; msvcrt
ExitProcess proto STDCALL :DWORD        ; kernel32

.DATA
    fmt db "%s",10,0
    hello db "Hello world!",0

.CODE
main PROC
    push OFFSET hello
    push OFFSET fmt
    call printf
    add esp, (2 * 4)

    push 0
    call ExitProcess
main ENDP

END main

The crt_... aliasses are declared in the msvcrt.inc:

.386
.model flat,stdcall

include \masm32\include\msvcrt.inc

includelib \masm32\lib\msvcrt.lib
includelib \masm32\lib\kernel32.lib

printf proto C :dword, :vararg          ; msvcrt
ExitProcess proto STDCALL :DWORD        ; kernel32

.DATA
    fmt db "%s",10,0
    hello db "Hello world!",0

.CODE
main PROC
    push OFFSET hello
    push OFFSET fmt
    call crt_printf
    add esp, (2 * 4)

    push 0
    call ExitProcess
main ENDP

END main

If you want the whole bunch with all declarations and macros then include masm32rt.inc:

include \masm32\include\masm32rt.inc

.DATA
    fmt db "%s",10,0
    hello db "Hello world!",0

.CODE
main PROC
    push OFFSET hello
    push OFFSET fmt
    call crt_printf
    add esp, (2 * 4)

    printf ("Hello again: %s\n",OFFSET hello);

    push 0
    call ExitProcess
main ENDP

END main