【发布时间】:2015-12-07 09:58:18
【问题描述】:
当使用带有 MASM(x86 架构)的汇编语言时,可以通过包含库来使用标准 C 函数。例如:printf 和 getchar。
在Visual Studio 中使用Asembly With Source Code/FAs 进行编译并检查生成的程序集文件时,我偶然发现了以下内容:
PUBLIC _printf
EXTRN __imp__getchar : PROC
_printf 声明为 PUBLIC 并在 本地 定义(inline 在同一文件中,因此不在库文件中外部定义),而 _imp_getchar 是外部定义
这是编译器在debug 中编译时生成的_printf 定义:
_TEXT SEGMENT
__ArgList$ = -20 ; size = 4
__Result$ = -8 ; size = 4
__Format$ = 8 ; size = 4
_printf PROC ; COMDAT
; 950 : {
push ebp
mov ebp, esp
sub esp, 216 ; 000000d8H
push ebx
push esi
push edi
lea edi, DWORD PTR [ebp-216]
mov ecx, 54 ; 00000036H
mov eax, -858993460 ; ccccccccH
rep stosd
; 951 : int _Result;
; 952 : va_list _ArgList;
; 953 : __crt_va_start(_ArgList, _Format);
call ??$__vcrt_va_start_verify_argument_type@QBD@@YAXXZ ; __vcrt_va_start_verify_argument_type<char const * const>
lea eax, DWORD PTR __Format$[ebp+4]
mov DWORD PTR __ArgList$[ebp], eax
; 954 : _Result = _vfprintf_l(stdout, _Format, NULL, _ArgList);
mov eax, DWORD PTR __ArgList$[ebp]
push eax
push 0
mov ecx, DWORD PTR __Format$[ebp]
push ecx
mov esi, esp
push 1
call DWORD PTR __imp____acrt_iob_func
add esp, 4
cmp esi, esp
call __RTC_CheckEsp
push eax
call __vfprintf_l
add esp, 16 ; 00000010H
mov DWORD PTR __Result$[ebp], eax
; 955 : __crt_va_end(_ArgList);
mov DWORD PTR __ArgList$[ebp], 0
; 956 : return _Result;
mov eax, DWORD PTR __Result$[ebp]
; 957 : }
pop edi
pop esi
pop ebx
add esp, 216 ; 000000d8H
cmp ebp, esp
call __RTC_CheckEsp
mov esp, ebp
pop ebp
ret 0
_printf ENDP
_TEXT ENDS
我的问题
为什么_printf 是在本地定义的,而getchar 是在外部定义的?
【问题讨论】:
-
因为这是
printf本身的来源。 -
@Jester 这如何以任何方式回答我的问题?这如何解释
getchar的来源未被复制?
标签: visual-studio assembly x86 masm