17 Replies - 8200 Views - Last Post: 11 August 2012 - 10:03 AM

How to print a number in x64 windows Assembly

Posted 10 August 2012 - 02:57 PM

Hello everyone! I recently began trying to learn assembly for my x64 windows 7 laptop using FASM. I wrote a program to solve the first Project Euler challenge, but I realized I don't know how to display my answer! I figured an easy way would be to make a message box with the message text as my number, so I did the 'divide by 10 and push to stack' algorithm, but it doesn't work. How do I convert a bunch of ascii numbers into outputted text? Thanks

Re: How to print a number in x64 windows Assembly

Posted 10 August 2012 - 03:23 PM

I have no idea what that push [ten] is doing for you.

You need to create a buffer to hold your string.
I think you do it like this in FASM
MyMessge resb SIZEOFBUFFER.

You convert your number to a character, move that value into index zero of your buffer, increase the pointer add another character until complete. Once done, zero terminate the buffer and pass that to MessageBox

Re: How to print a number in x64 windows Assembly

You need to create a buffer to hold your string.
I think you do it like this in FASM
MyMessge resb SIZEOFBUFFER.

You convert your number to a character, move that value into index zero of your buffer, increase the pointer add another character until complete. Once done, zero terminate the buffer and pass that to MessageBox

Sorry, I forgot to mention that ten is just a quadword defined as a 10, so that it would divide properly. I heard that you can only push a qword or a byte in x64, and pushing bytes leaves the stack mis-aligned or something, so I just decided to push a qword. I pushed it so that when I was popping the characters back off I knew when to stop popping.

As far as the resb command, I believe it's just rb in FASM. So I've reserved a byte of data now.. what did you mean when you said to put the character into index zero of the byte?

Re: How to print a number in x64 windows Assembly

Posted 10 August 2012 - 03:42 PM

reserving 1 byte of data will only hold, well 1 byte!!! Which means it will only hold 1 character. Try to put more into the buffer and you will get a seg fault. For a 64 bit number, I would reserve 24 bytes. With a negative symbol, a 64 byte number will be 20 bytes plus the null will be 21. Its good to create "aligned" buffers so we make it 24.

You don't need all these push/pops, loose track of one and your program can/will go POOF! Your writing for 64bit, use all those extra registers!

Re: How to print a number in x64 windows Assembly

Posted 10 August 2012 - 04:33 PM

Are you in school? Even if you are, I urge you read a few tutorials on memory in Assembly.
EDI contains the pointer to our string/buffer. You convert a character, move that character into our buffer at the pointer in edi. Increase/decrease the pointer to add other characters.

Re: How to print a number in x64 windows Assembly

Posted 10 August 2012 - 06:17 PM

Yes I'm in school (though not taking an ASM class) and read many tutorials on FASM and have searched (on Google) specifically on how to use the EDI/ESI registers and about string operations but I haven't found anything that explains it, which is why I'm asking on here. Is it correct to say that I should point the di register to my buffer's address and then move my character to si and perform 'movs'? I really don't understand why this works or how it works.

Re: How to print a number in x64 windows Assembly

Posted 10 August 2012 - 07:17 PM

Okay so I took a slightly different approach. I know that the answer has 6 digits and I know my algorithm for computing the answer works as I did it in another language and got the right answer. Here's part of my new code:

Re: How to print a number in x64 windows Assembly

Posted 11 August 2012 - 07:08 AM

Okay thanks a lot that all makes sense. My program still crashes however, so I'm assuming it has something to do with the way I call the MessageBox? I've done the same thing as my Hello World message box, but this time I'm trying to load 'Jer' as my text.

Re: How to print a number in x64 windows Assembly

Posted 11 August 2012 - 08:58 AM

How is Message defined? How is Jer defined? You can't just move things around like you would in a high level language. One line of instruction in C or another language, is usually a few instructions in Assembly.

You don't need to do lea rdx,[Message] you could simply do mov rdx, Message but that won't fix your problem, need to now how Jer is defined, what is it?

I adivse you to spend the weekend reading The Art of Assembly you can disregard the stuff about HLA, it will give you a solid understanding of Assembly