The MASM Forum

Microsoft 64 bit MASM => Examples => Topic started by: zedd151 on September 25, 2024, 05:32:30 AM

Title: 64 bit examples for beginners
Post by: zedd151 on September 25, 2024, 05:32:30 AM
During one of my infrequent ventures into 64 bit programming, I had converted a few examples from the Masm32 SDK to 64 bit. I am sharing them here to help beginners to masm 64 bit assembly  'get their feet wet' using the Masm64 SDK.  :biggrin:

Use the included batch files to build the programs (recommended).

I am assuming that the user has the Masm64 SDK installed (in the root of the drive where the examples also reside somewhere on the same drive where the Masm64 SDK is installed), and has the necessary Microsoft binaries there in the bin64 folder (ml64.exe, link.exe and its associated required dll's).
Visual Studio is not necessary to create or build assembly (*.asm) files, but the binaries may be copied from an existing VS installation. (Easiest way to obtain them)

1. minimum64 - a simple message box example.

2. generic64 - simple generic window example.

3. smalled64 - a small text editor example.

All three beginner level examples are included in the zip file attached below.

Note: I will be revising some of these examples, with commenting where it is lacking, as time permits... stay tuned! (Or at least check here from time to time  :tongue: )

Title: Re: 64 bit examples for beginners
Post by: sinsi on September 29, 2024, 04:18:31 PM
Good examples, at least they build without VS  :thumbsup:

One thing that offends my frugal soul :biggrin:
xor rcx, rcxThis does the same thing, but one (!) byte less
xor ecx, ecx
I know it's not a lot, but the number of times we zero a register it can add up.
Hutch and I had an argument about it, he basically said that it's 64-bit, so use 64-bit everywhere :badgrin:
He certainly wasn't a byte counter.

Title: Re: 64 bit examples for beginners
Post by: zedd151 on September 29, 2024, 11:36:34 PM
Quote from: sinsi on September 29, 2024, 04:18:31 PMOne thing that offends my frugal soul :biggrin:
xor rcx, rcx
Sorry to hear that.  :tongue:  As these are merely 'simple' conversions from Masm32 examples and not a rewrite, I will leave it be. The purpose was to show how easy 64 bit assembly can be using the Masm64 SDK. If you or any other member would like to contribute 64 bit examples here (for beginners) - in your own coding style - you are welcome to do so.  :thumbsup:


Title: Re: 64 bit examples for beginners
Post by: zedd151 on October 06, 2024, 05:07:16 AM
Speaking of "simple conversions from 32 -> 64 bit", I am working on a converter that will (in theory, anyway) convert simple 32 bit programs to 64 bit. I had done this a few years ago, but that source has been discarded since there were a few fatal flaws in that rendition. So, I am proceeding to write a new converter. This should be a great help to my endeavor to make a few more 64 bit examples.   :smiley:  Quicker than writing them from scratch, and there are a ton of such examples in 32 bit already - ripe for conversion. :wink2:

I will be creating a new topic for it, when it is nearly completed.  :biggrin:
Title: Re: 64 bit examples for beginners
Post by: NoCforMe on October 06, 2024, 05:36:23 AM
Interesting. How exactly would one convert a 32-bit program to 64-bit? Rename 32-bit registers (EAX--> RAX)? Add 64-bit prologues and stack-alignment stuff?
Title: Re: 64 bit examples for beginners
Post by: zedd151 on October 06, 2024, 05:53:51 AM
Quote from: NoCforMe on October 06, 2024, 05:36:23 AMInteresting. How exactly would one convert a 32-bit program to 64-bit? Rename 32-bit registers (EAX--> RAX)? Add 64-bit prologues and stack-alignment stuff?
something like that. Also adjust indirect addressing for 64 bit among other things.
Ex. [reg32+reg32*4] -> [reg64+reg64*8]
Ex. shl eax, 2 -> shl rax, 3
I have a qe plugin that does some of the work, and began expanding on what the plugin does. It was apparent that a stand alone program might be more useful for others, that brings me to mentioning it here.  :biggrin:
I won't comment much more here, you will have to wait until I post the initial code for more details...  :tongue:

But I will say this much about the beginnings.
When I first delved into 64 bit a few years back, I made a qe plugin that used two lists, old.txt and new.txt. It scans the source until a string on the indexed old list is found, and simply replaces it with the indexed string from the new list. Easy peasy. But then there will be other things that cannot be done via a simple replcement... that need to be changed for masm64. More details when I post the conversion project. I will be working on the project concurrently with converting one of my older programs to 64 bit.
Title: Re: 64 bit examples for beginners
Post by: NoCforMe on October 06, 2024, 06:16:29 AM
Quote from: zedd151 on October 06, 2024, 05:53:51 AMEx. shl eax, 2 -> shl rax, 3

I don't think so. What if the programmer just wanted to multiply the value in EAX by 4?
Title: Re: 64 bit examples for beginners
Post by: zedd151 on October 06, 2024, 06:29:04 AM
Quote from: NoCforMe on October 06, 2024, 06:16:29 AMI don't think so. What if the programmer just wanted to multiply the value in EAX by 4?
It is all in the context of where it is used. (In that example, multiplying eax by the size of register prior to allocating buffer memory) one of the things I am currently working on. If I cannot do it programmatically (figure out the context) I might use a yes/no message box, to let the user to decide to convert it or not. That is one of the tricky parts.  :azn:  There are a few others that are a bit tricky to do programmatically. Big Phun.  :tongue:
Title: Re: 64 bit examples for beginners
Post by: NoCforMe on October 06, 2024, 06:59:52 AM
Quote from: zedd151 on October 06, 2024, 06:29:04 AMIf I cannot do it programmatically (figure out the context)

That, my friend, would be really, really complicated. Far more complex than you're probably imagining. For one, you'd have to be able to parse all the code, not just do string comparisons.
Title: Re: 64 bit examples for beginners
Post by: zedd151 on October 06, 2024, 07:33:49 AM
You might be right, but so far no huge issues.  :cool:  I'll just have to wait until I reach a major hurdle.  :biggrin:

Worst that could happen, is that the output source code will have to be checked for errors/discrepancies  visually, and adjusted accordingly. Mind you, I won't be trying to convert complex algorithms or twiddle with the stack at all. The default prologue and epilogue seem to work okay. Remember this is for converting 'simple' examples.  :smiley:
Title: Re: 64 bit examples for beginners
Post by: jj2007 on October 07, 2024, 03:19:36 AM
Quote from: zedd151 on October 06, 2024, 05:07:16 AMI am working on a converter that will (in theory, anyway) convert simple 32 bit programs to 64 bit.

Congrats, ambitious but useful.

Quotethere are a ton of such examples in 32 bit already - ripe for conversion.

Stick to \Masm32\Examples\*.asm
Title: Re: 64 bit examples for beginners
Post by: zedd151 on October 07, 2024, 03:27:00 AM
Quote from: jj2007 on October 07, 2024, 03:19:36 AMStick to \Masm32\Examples\*.asm
There are some others that I will be looking at as well. But yes, that is where I started. I will revise some of the commenting as necessary as I go along, including the examples already posted here in the first post.

Most likely, I will start posting each example in its own thread rather than posting new ones here scattered among the comments, as the thread would get cluttered - and I don't want to cram the first post with a hundred examples either.  :smiley:
Title: Re: 64 bit examples for beginners
Post by: ognil on October 07, 2024, 05:46:58 AM
Quoteby zed151: I will revise some of the commenting as necessary as

Great idea, but pls, stop using the macros in MASM64.
Otherwise you'll have to write comments about them too. :badgrin:
Title: Re: 64 bit examples for beginners
Post by: zedd151 on October 07, 2024, 06:03:13 AM
Quote from: ognil on October 07, 2024, 05:46:58 AMGreat idea, but pls, stop using the macros in MASM64.
No can do. These are Masm64 examples. In case you have forgotten, or didn't know, the first "M" is for "macro".  :tongue:

The macros are there to make the programmers job easier. You do as you wish to do,  and I will do as I wish to do.  :badgrin:

:biggrin:
This project will continue at some time in the near future.
Title: Re: 64 bit examples for beginners
Post by: stoo23 on October 07, 2024, 05:32:55 PM
This topic was tidied up, with Removed 'posts' Merged with the existing Removed post Topic, here: Re: Macro Definition & usage discussion + supplementary (https://masm32.com/board/index.php?topic=12291.0)
Title: Re: 64 bit examples for beginners
Post by: zedd151 on October 20, 2024, 02:57:10 AM
As far as the 32->64 bit conversion program I had mentioned above (https://masm32.com/board/index.php?msg=133918) it is a "no go".  :sad:  While converting some of my own code, I found too many things that would still have to be manually adjusted. Things that are not easily done programatically. For now all the program can do is simple comparisons/conversions for registers and "dword"->"qword"; "dd"->"dq" and similar. Still saves a little bit of time, but nowhere near a full source code converter. Maybe next year.  :tongue:
Title: Re: 64 bit examples for beginners
Post by: sinsi on October 20, 2024, 03:09:50 AM
The problem is that you would still need to proofread it to make sure nothing was missed, and even then you will miss things :biggrin:
The other thing is that 32-bit code doesn't even know about the extra registers.
How many times have we written 32-bit code and compromised because we don't have enough registers?
If you use SEH, that is totally different in 64-bit land.

Too many differences imho.
Title: Re: 64 bit examples for beginners
Post by: zedd151 on October 20, 2024, 03:16:37 AM
Quote from: sinsi on October 20, 2024, 03:09:50 AMIf you use SEH, that is totally different in 64-bit land.
Hell no.  :eusa_naughty:  Nope, not me.   :biggrin:
QuoteToo many differences imho.
Thats for sure.  :smiley: