<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Marius Bancila's Blog &#187; assembly</title>
	<atom:link href="http://mariusbancila.ro/blog/tag/assembly/feed/" rel="self" type="application/rss+xml" />
	<link>http://mariusbancila.ro/blog</link>
	<description>Sharing my opinions and ideas!</description>
	<lastBuildDate>Mon, 14 Nov 2011 07:19:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>No more inline ASM in VC++ on x64</title>
		<link>http://mariusbancila.ro/blog/2010/10/17/no-more-inline-asm-in-vc-on-x64/</link>
		<comments>http://mariusbancila.ro/blog/2010/10/17/no-more-inline-asm-in-vc-on-x64/#comments</comments>
		<pubDate>Sun, 17 Oct 2010 09:02:21 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[64bit]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Windows Programming]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[porting]]></category>
		<category><![CDATA[VC++]]></category>
		<category><![CDATA[x64]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=712</guid>
		<description><![CDATA[I&#8217;m working on a project to port a 32-bit application for the x64 platform. The first errors that came up when building for x64 were related to inline ASM code, which is no longer supported in VC++ for x64. VC++ has an inline assembler built within the compiler, so you could write assembly code directly [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a project to port a 32-bit application for the x64 platform. The first errors that came up when building for x64 were related to <a href="http://msdn.microsoft.com/en-us/library/4ks26t93.aspx">inline ASM code</a>, which is no longer supported in VC++ for x64. VC++ has an inline assembler built within the compiler, so you could write assembly code directly in C++ without the need of an external assembler (such as MASM). However, this works only on x86. For x64 and IA64 this is no longer possible. There are several workaround for this:</p>
<ul>
<li>put the assembly code into a separate file and use <a href="http://msdn.microsoft.com/en-us/library/hb5z4sxd.aspx">MASM for x64</a></li>
<li>use compiler intrinsics, which are functions that basically wrap assembly instructions or sequence of intructions</li>
<li>rewrite in C++, use Windows APIs, etc.</li>
</ul>
<p>One error that I had was related to this assembly code:</p>
<pre name="code" class="cpp">
   __asm {
      int 3
   }
</pre>
<p>This was used to generate a breakpoint. This can be easily replaced with compiler intrinsic <a href="http://msdn.microsoft.com/en-us/library/f408b4et.aspx">__debugbreak()</a>, which has an identical effect, except that is available on all platforms.</p>
<p>Other errors I had were due to assembly code used to retrieve the value of the EIP and EBP registers. They were used for walking the stack.</p>
<pre name="code" class="cpp">
__declspec(naked) DWORD_PTR* GetEip()
{
   __asm {
      pop   eax
      push  eax
      ret
   }
}

__declspec(naked) DWORD_PTR* GetEbp()
{
   __asm {
      mov   eax, ebp
      ret
   }
}
</pre>
<p>The naked specifier is another thing that is not supported on x64. One way to retrieve the value of these registers that works both with x86 and x64 is using RtlCaptureContext, except that this won&#8217;t work on operating system previous to Windows XP. In you don&#8217;t care about those operating systems, you could write something like this:</p>
<pre name="code" class="cpp">
      CONTEXT context;
      RtlCaptureContext(&#038;context);

#ifdef _WIN64
      DWORD_PTR* ebp = (DWORD_PTR*)context.Rsp;
      DWORD_PTR* eip = (DWORD_PTR*)context.Rip;
#else
      DWORD_PTR* ebp = (DWORD_PTR*)context.Ebp;
      DWORD_PTR* eip = (DWORD_PTR*)context.Eip;
#endif
</pre>
<p>Attention, on x64, register EBP (actually RBP) is no longer used. You should use RSP for getting the stack frame. </p>
<p>However, if you want to build a portable stack walking, use <a href="http://msdn.microsoft.com/en-us/library/ms680650%28VS.85%29.aspx">StackWalk64</a>. Despite the 64 suffix, this function works on all platforms (x86, x64, IA64). <a href="http://www.codeproject.com/KB/threads/StackWalker.aspx">Here</a> is an article that shows how to walk the stack using StackWalk64.</p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2010/10/17/no-more-inline-asm-in-vc-on-x64/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reference a XAML From Another Project</title>
		<link>http://mariusbancila.ro/blog/2009/08/26/reference-a-xaml-from-another-project/</link>
		<comments>http://mariusbancila.ro/blog/2009/08/26/reference-a-xaml-from-another-project/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 08:20:21 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[URI]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=363</guid>
		<description><![CDATA[When you create a WPF application, the start-up window is by default one from the same project (by default called Window1.xaml). < Application x:Class="WpfApplication1.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="Window1.xaml" > < Application.Resources > < /Application.Resources > < /Application > But what if you want to use a window from another project (class library)? The pack URI scheme, [...]]]></description>
			<content:encoded><![CDATA[<p>When you create a WPF application, the start-up window is by default one from the same project (by default called Window1.xaml). </p>
<pre class="prettyprint">
< Application x:Class="WpfApplication1.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml" >
    < Application.Resources >

    < /Application.Resources >
< /Application >
</pre>
<p>But what if you want to use a window from another project (class library)? The pack URI scheme, used by WPF, allows you to identify and load files from:</p>
<ul>
<li>the current assembly</li>
<li>a referenced assembly</li>
<li>a location relative to an assembly</li>
<li>the site of origin for the application</li>
</ul>
<p>The format of the pack URI is <strong><em>pack://authority/path</em></strong>. The authority identifies the type of package and the path the location of a part inside a package. There are two authorities supported by WPF:</p>
<ul>
<li>application:/// identifies application data files (known at compile time)</li>
<li>siteoforigin:/// identifies site of origin files</li>
</ul>
<p>To use resource files from a referenced assembly you need to use the application:/// authority, and the path must have the form <strong><em>AssemblyShortName[;Version][;PublicKey];</em>component/<em>Path</em></strong>. Version and PublicKey are optional.</p>
<p>Let&#8217;s say you want to use a XAML called SampleWindow.xaml from a referenced assembly called WpfDemoLib. The App.xaml file should look like this:</p>
<pre class="prettyprint">
< Application x:Class="WpfApplication1.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="pack://application:,,,/WpfDemoLib;component/SampleWindow.xaml" >
    < Application.Resources >

    < /Application.Resources >
< /Application >
</pre>
<p>You can learn more about pack URIs in WPF from <a href="http://msdn.microsoft.com/en-us/library/aa970069.aspx" target="_blank">MSDN</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2009/08/26/reference-a-xaml-from-another-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

