VC ++内联汇编代码-异常 [英] VC++ inline assembly code - exception
问题描述
想要简单地尝试在我的MS VC ++项目中使用内联汇编代码.
获取
jih_c ++.exe中0x004182bc的未处理异常:0xC0000005:访问冲突写入位置0x00006687.
在下面的第一行ebx行中.
(在MS Vista上使用Microsoft Visual C ++ 2005)
MS Code链接:
http://msdn.microsoft.com/zh-CN/library/y8b57x4b.aspx
VC ++项目代码:
//InlineAssembler_Calling_C_Functions_in_Inline_Assembly.cpp
//处理器:x86
#include< stdio.h>
char format [] =%s%s \ n";
char hello [] ="Hello";
char world [] ="world";
int main(void)
{
__asm
{
mov eax,偏移世界
推eax
mov eax,偏移量是您好
推eax
mov eax,偏移格式
推eax
致电printf
//清理堆栈,以便main可以干净地退出
//使用未使用的寄存器ebx进行清理
流行ebx
流行ebx
流行ebx
}
}
Hi,
Wanted to simply try using inline assembly code in my MS VC++ Project.
Getting
Unhandled exception at 0x004182bc in jih_c++.exe: 0xC0000005: Access violation writing location 0x00006687.
at the first pop ebx line below.
(Using Microsoft Visual C++ 2005 on MS Vista)
MS Code link:
http://msdn.microsoft.com/en-us/library/y8b57x4b.aspx
VC++ Project Code:
// InlineAssembler_Calling_C_Functions_in_Inline_Assembly.cpp
// processor: x86
#include <stdio.h>
char format[] = "%s %s\n";
char hello[] = "Hello";
char world[] = "world";
int main( void )
{
__asm
{
mov eax, offset world
push eax
mov eax, offset hello
push eax
mov eax, offset format
push eax
call printf
//clean up the stack so that main can exit cleanly
//use the unused register ebx to do the cleanup
pop ebx
pop ebx
pop ebx
}
}
推荐答案
printf行对我来说也很麻烦.该示例必须被破坏.
The printf line blows up for me, too. The sample must be broken.
请参见
http://www.codeproject.com/Messages/3168938/Calling-C-Functions-in-Inline-Assembly-MSDN-exampl.aspx
现在就可以使用-上面的答案汇总-参见下面的代码和注释
//InlineAssembler_Calling_C_Functions_in_Inline_Assembly.cpp
//处理器:x86
#include< stdio.h>
char format [] =%s%s \ n";
char hello [] ="Hello";
char world [] ="world";
int main(void)
{
__asm
{
mov eax,偏移世界
推eax
mov eax,偏移量是您好
推eax
mov eax,偏移格式
推eax
//调用printf-不起作用
//调用指令要直接使用"__imp__printf"
//作为printf函数所在的地址.
//
//下面的解决方案1起作用-将函数的地址放入esi
//并使用esi中的地址执行对该函数的间接调用.
//mov esi,printf
//致电esi
//
//或下面的解决方案2起作用
致电DWORD PTR printf
//或
//调用dword ptr [esi + printf]
//调用指令使用__imp__printf作为指向该地址的指针.
//使用此命令的原因:运行前,printf函数的地址未知
//因为该库是动态链接的.
//另一种解决方法是将C运行时链接更改为/MT或/MTd
//
//堆栈清理
//清理堆栈,以便main可以干净地退出
//使用未使用的寄存器ebx进行清理
流行ebx
流行ebx
流行ebx
}
}
See
http://www.codeproject.com/Messages/3168938/Calling-C-Functions-in-Inline-Assembly-MSDN-exampl.aspx
Works now - Summarized answers from above - See code and comments below
// InlineAssembler_Calling_C_Functions_in_Inline_Assembly.cpp
// processor: x86
#include <stdio.h>
char format[] = "%s %s\n";
char hello[] = "Hello";
char world[] = "world";
int main( void )
{
__asm
{
mov eax, offset world
push eax
mov eax, offset hello
push eax
mov eax, offset format
push eax
//call printf - does not work
//The call instruction wants to use the "__imp__printf" directly
//as an address where the printf function is.
//
//solution 1 below works - it puts the address of the function into esi
//and performs an indirect call to that function using the address in esi.
//mov esi,printf
//call esi
//
//or solution 2 below works
call DWORD PTR printf
//or
//call dword ptr [esi+printf]
//The call instruction uses __imp__printf as a pointer to this address.
//reason to use this: the address of the printf function is unknown before runtime
//because the library is dynamically linked.
//Another way to fix it is to change the C runtime linking to /MT or /MTd
//
//Stack clean-up
//clean up the stack so that main can exit cleanly
//use the unused register ebx to do the cleanup
pop ebx
pop ebx
pop ebx
}
}
您好,我找到了一个逐步解释此代码的地方,它非常酷而简单:)希望您喜欢它.
http://rodrigosavage.blogspot.com/2010/07/有效率的vector-library-with-asm-and-c.html
它甚至有图表!!每个指令中的堆栈,内存和寄存器的方式.
Hello, i find a place where they explained step by step this code, its very cool and simple :) hope you like it.
http://rodrigosavage.blogspot.com/2010/07/efficient-vector-library-with-asm-and-c.html
it even have diagrams!! of how is the stack, memory and register in each instruction.
这篇关于VC ++内联汇编代码-异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!