{"id":470,"date":"2010-01-12T04:41:54","date_gmt":"2010-01-12T09:41:54","guid":{"rendered":"http:\/\/www.briancbecker.com\/blog\/?p=470"},"modified":"2020-04-12T19:45:35","modified_gmt":"2020-04-12T23:45:35","slug":"porting-32-bit-nasm-code-to-64-bit","status":"publish","type":"post","link":"http:\/\/www.briancbecker.com\/blog\/2010\/porting-32-bit-nasm-code-to-64-bit\/","title":{"rendered":"Porting 32-bit NASM code to 64-bit"},"content":{"rendered":"<p>It&#8217;s terrible&#8230;yeah you heard me. Assembly language is hard even at the best of times and it is all so hardware dependent that porting is a super pain in the neck. Even on the same processor, the change from 32-bit code to 64-bit code is annoying.<\/p>\n<p>First rule of business, change BITS 32 to BITS 64. It seems obvious but when you have a bunch of *.asm files and you are doing them one by one, forgetting one can cause &#8220;error: impossible combination of address sizes&#8221; which will proceed to befuddle you for the next 10 minutes. Or not as it seems I&#8217;m the only one on Google has actually gotten this error.<\/p>\n<p>I also found my first use for Wolfram Alpha: taking the modulus of 16-byte hex numbers to determine if they are 16-bit aligned. Yeah, I couldn&#8217;t even find a quick way to do that in Matlab, which is surprising. But then I realized I was being really braindead because it is really simple to see if a pointer address is 16-byte aligned: the last digit should be zero! Oops&#8230;I&#8217;m being silly again.<\/p>\n<p>The errror &#8220;error LNK2017: &#8216;ADDR32&#8217; relocation to &#8216;.rdata&#8217; invalid without \/LARGEADDRESSAWARE:NO&#8221; means you forgot to add &#8220;default rel&#8221; to the top of your assembly language file.<\/p>\n<p>Apparently you also have to preserve some of the new registers across function calls. None of the NASM\/YASM manuals or anything I read mentioned this! Code after I ran one of my functions was crashing and through turning off bits of the code, I was able to narrow it down to mov r12,ecx to store the first parameter. Of course then the thought struck me: maybe I need to preserve r12 so I finally had to Google &#8220;preserve r12 assembly.&#8221; I found <a href=\"http:\/\/oradim.blogspot.com\/2009\/03\/jmp-around-win64-with-ml64exe-and.html\">some 64-bit sample assembly code from Mark William&#8217;s blog<\/a> which had some comments about preserving r12-r15 and that seemed to fix the problem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s terrible&#8230;yeah you heard me. Assembly language is hard even at the best of times and it is all so&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-470","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.briancbecker.com\/blog\/wp-json\/wp\/v2\/posts\/470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.briancbecker.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.briancbecker.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.briancbecker.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.briancbecker.com\/blog\/wp-json\/wp\/v2\/comments?post=470"}],"version-history":[{"count":4,"href":"http:\/\/www.briancbecker.com\/blog\/wp-json\/wp\/v2\/posts\/470\/revisions"}],"predecessor-version":[{"id":474,"href":"http:\/\/www.briancbecker.com\/blog\/wp-json\/wp\/v2\/posts\/470\/revisions\/474"}],"wp:attachment":[{"href":"http:\/\/www.briancbecker.com\/blog\/wp-json\/wp\/v2\/media?parent=470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.briancbecker.com\/blog\/wp-json\/wp\/v2\/categories?post=470"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.briancbecker.com\/blog\/wp-json\/wp\/v2\/tags?post=470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}