View Full Version : VC 2005 64-bit Assembly
Park_7677
2 Jul 2006, 5:12pm
In my adventures to compile PHP for Win64 I have come across some inline assembly code. Well VC 2005 does not support inline assembly with 64-bit compiling so it must be rewritten. I have no idea where to even begin with this part :confused2
It looks like this is it:
_asm mov lpPage, esp;
I've been looking online and it suggests moving the assembly code to a separate *.asm file and compile it along with the project. They might as well be speaking a foreign language :o
It's inside the IIS API module (php5isapi.dll). A person has emailed me about a POST issue with the PHP x64 and IIS which I think is caused by the CGI version (PHP-CGI.exe). If I can get the module to compile I think it would work out for him.
lightnin
3 Jul 2006, 2:21pm
what compiler are you using? inline assembly in that syntax is MS specific. if you are using gcc, at least in *nix, you can use the asm() funtion. example: asm("nop ; nop ; nop ; nop") executes four nop instructions. i'd try that before moving it into another file.
Park_7677
3 Jul 2006, 3:43pm
I'm using Visual C++ 2005. The code is specifically for Windows x64 (it's part of PHP for Windows x64; specifically the IIS module)
If 64-bit assembly code is desired, it cannot be written inline, but rather must be organized as a separate MASM file.
Above is where I was referring to about the separate file.
I'll try your suggestion later because I've got class in a few.
Thanks lightnin!
lightnin
3 Jul 2006, 4:57pm
ok you arent going to be able to use inline assembly *AT ALL* for amd64/windows. it just aint gonna happen. if you are using the visual basic c++ ide to do this stuff in, check out this link:
http://msdn.microsoft.com/msdntv/transcripts/20040708VisualCTMTranscript.aspx
there's a little blurb about adding assembly files to a project for use. i'd highly suggest looking into the documentation for the ide to see exactly how to structure the project and add asm files to it, if it's anything like other MS IDEs it'll generate code for you, if you like that sort of thing. i have the feeling the project will only build properly in the vc++ environment if you play by it's rules.
on the other hand, have you tried cross-compiling on a different platform? maybe gcc on linux? you still can't use inline assembly, but the compiler itself might be a little more flexible (but i could be wrong, never used vc++ for comparison).
in any event, here's a decent doc on win64 assembly language:
http://www.codegurus.be/codegurus/Programming/assembler&win64_en.htm
and here's how to build assembly/c++ projects in visual c++ ide:
http://www.daniweb.com/techtalkforums/thread8072.html
one thing i learned about building projects for amd64 is that the code needs to build with relatively fewer errors than an x86 project. basically make the code compile as cleanly as possible and you'll see fewer glitches later on down the road, if at all possible.
--edit
that last link will likely be the most helpful for you
Park_7677
3 Jul 2006, 11:12pm
Thanks for the links. I tried out the example in the 3rd link but I get some syntax string error when I use 'extern "C" ...'. In the project I'm working on I took "extern "C" ..." out and it will compile but I know I don't have the assembly code right.
Since it's one line I know it can't be too hard but I'm just not seeing it. Is what I have below close to what it should be?
_asm mov lpPage, esp;
I assume I would create a function like below in the ASM file
public movit
.CODE
movit PROC
mov ???, esp
movit ENDP
END
I don't know how to pass the lpPage variable to the assembly code (note the ??? in mov). And I assume "esp" is something in assembly because it's not defined in the C code at all.
Thanks for tying to help me. I really appreciate it. I'm just going to have to keep looking and trying.
lightnin
4 Jul 2006, 4:08am
hmmm yeah your prob might be in the assembly file. could you post the entire c file so i can see exactly what you're trying to do? i don't have a windows machine, thus i dont have visual c++, but I do have an amd64 machine with gcc so i could try to help...
btw where did you get this code you are trying to compile?
Park_7677
4 Jul 2006, 5:34am
The C source is from PHP.net so it should be correct. The problem is building the assembly file from scratch; I don't know how to code it.
File: php5isapi.c
Line: 883
What I am doing is trying to compile PHP for Windows x64 (here (http://www.short-media.com/forum/showthread.php?t=46956)). No one has a distribution for it so that's what I'm trying to put together. Of the many projects inside PHP, this one I'm working on is ISAPI (the API module for Internet Information Services [IIS], Microsoft's Web server).
Again, thank you! :)
lightnin
5 Jul 2006, 2:32pm
.DATA?
lpPage LPBYTE ?
esp LPBYTE ?
.CODE
main PROC
mov lpPage, esp ;
main ENDP
END
i can't find where the var esp is declared in this code, so i'm assuming that it's also a LPBYTE data type. you can give this a try.....
lightnin
5 Jul 2006, 6:18pm
a quick btw... i just read that IIS6 64 bit can run the 32 bit php5 isapi module natively.... any truth to that? it could happen...
Park_7677
6 Jul 2006, 12:01am
I tried your code but it didn't work. Defining lpPage as LPBYTE did not work. "1>.\movit_.asm(2) : error A2008: syntax error : LPBYTE" . Also I found that esp is a register inside Assembly so defining it is not necessary. On top of that esp becomes rsp when moving 32-bit to 64-bit (here (http://www.x86-64.org/documentation/assembly)). I changed that accordingly. I'm trying QWORD instead and it compiles the ASM file. Now I'm getting an unresolved external symbol movit referenced in function HttpExtensionProc
Release_TS\php5isapi.dll : fatal error LNK1120: 1 unresolved externals
a quick btw... i just read that IIS6 64 bit can run the 32 bit php5 isapi module natively.... any truth to that? it could happen...
Yes, but with an unfortunate side effect:
I am running widows server 2003 IIS 6.0(running 64 bit web applications). I installed PHP 5.0 on this machine and had it working. Then I realized that by enabling the 32 bit binaries to work in IIS I had disabled my 64 bit web apps from running.
I would like to know where I can get a 64 bit version of PHP that will run on windows server 2003 using IIS 6.0 in 64 bit mode. Any help or clues would be very appreciated.
That's why an IIS 64-bit and PHP 64-bit combination would be highly preferable to most people. It's unfortunate that by trying to get the most out of a 64-bit server (IIS HTTP in this case) they will hurt themselves in the long run if they want to use PHP (previously only 32-bit).
primesuspect
6 Jul 2006, 1:38am
on a totally unrelated note: Lighnin - MAD PROPS on the Lo Pan avatar! ;D
lightnin
6 Jul 2006, 1:57pm
on a totally unrelated note: Lighnin - MAD PROPS on the Lo Pan avatar! ;D
lmao thank you, thank you....
ok i feel retarded now, after you jogged my memory now i remember that esp is a register... duuhhh.... great job on finding that rsp reference btw. i wouldnt have thought they'd change that, but i guess it makes sense 'r' standing for register perhaps?
anyways, could you post the asm file as well as the c code that you have now? i'd like to see what you've got so far. i'm guessing moveit is what you named the asm process?
Park_7677
7 Jul 2006, 2:31am
I finally got a DLL to result but it doesn't actually work. :grumble: IIS will load HTML pages but when requesting PHP the server will try until it times out.
I didn't know one line of code was going to be this troublesome. :sad2: At least I know that the CGI/FastCGI interface works with IIS so Win64 with IIS users are not left totally out in the cold on this one.
public movit
.DATA?
lpPage QWORD ?
.CODE
movit PROC
mov lpPage, rsp
movit ENDP
END
C source attached.
Don't feel obligated lightnin. You've helped me out a lot and I'm further than I would have been. IIS people still can use my PHPx64 so this isn't vital. Thank you :)
lightnin
7 Jul 2006, 2:21pm
actually all is not lost just yet.
http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx
http://www.microsoft.com/whdc/devtools/debugging/install64bit.mspx
time to debug that sucker and see where your code is hanging up. now that you can get it to compile and begin some execution you should be able to see where it's hanging up at. there may be a problem somewhere else. but at least now you can see what to fix.
Park_7677
7 Jul 2006, 11:00pm
:eek: I got a working DLL finally! :bigggrin:
I was changing the Assembly code around trying to get it to work... and it did :ninja: I copied the DLL and am trying to rebuild it to see if I get the same result.
Either way I'll have an ISAPI DLL for my next release. Now I just need to fix all the bugs in PHP :eek3:
Thanks lightnin :)
EDIT:// I've tried rebuilding it several times and none of them work. I didn't change that much Assembly code between builds so I can't figure out why it built correctly once. I still have the working DLL so maybe I can pick it apart.
Park_7677
8 Jul 2006, 5:05am
Instead of editing my above post again I'll just post what I found out..
I rewrote the ASM file several times and I think my last time makes the most since. The original C code is just trying to get the location of the stack (esp/rsp) and store it in lpPage. So lpPage doesn't really need to be passed to the Assembly code, lpPage should instead equal what the Assembly code returns. I found out that eax/rax is the register used to return the value to C so I need to copy esp/rsp to eax/rax for returning to C.
movit_.asm
public movit
.CODE
movit PROC
mov rax, rsp
ret
movit ENDP
END
php5isapi.c
lpPage = movit();
Does it work? I don't know really, but logically it makes since. I've been having trouble getting that DLL into the debugger you linked since it's not itself executable. I've tried to attach the debugger to IIS (the webserver which loads the DLL) but I still don't see my DLL listed.:scratch:
Now on to the weird part.:wtf: PHP is built with a makefile. This makefile builds everything: the core php dll, php executables, apache filters, mysql extensions, etc. It is also supposed to make the module I'm working on now (isapi). It does but the resulting DLL does not work. However, if you open the Visual C project for isapi in the IDE and build it from there after I ran the makefile to build everything else it builds correctly. I found this out by luck, but it works. Very odd.
Thanks lightnin! Looks like I got it working :thumbup
lightnin
10 Jul 2006, 3:42pm
glad to hear you got it working. i linked to that debugger thinking you were using the vc++ ide to build, not the makefile. if you wanna use make to build it, you'll have to tweak the makefile i'm sure to both build and include the asm and resulting dll file.
but i'm glad to hear you got it compiled, good job on the assembly.
vBulletin® v3.8.1, Copyright ©2000-2009, Jelsoft Enterprises Ltd.