Google Community Chat
 
Win32 Assembly tutorial(Only for x86 processors)

You're visiting Google Community as a guest.
In order to post, you'll need to register and log in.


(If you were registered and logged in, these advertisements wouldn't be here)
Post new topic   Reply to topic    Google Community Forum Index // Web Design, Coding & Programming Forum
   
Author Message
Eric
Better than you
Google Guru


Joined: 12 Aug 2004
Location: Toronto, Ontario, Canada.
2491.35 GC$

Items

PostPosted: Thu Sep 30, 2004 11:39 pm    Post subject: Win32 Assembly tutorial(Only for x86 processors) Reply with quote
Before beginning this tutorial, it is essential that you download the MASM32 Assembler for Windows. You can download it from www.masm32.com. You might be able to use RadASM for this tutorial but I'm not sure if it will work as I haven't used it...yet that is Wink..Also I don't think the code will be compatible/effective for Windows running on Hewlett Packard Alpha processors.

Observe the following code:
Code:

.386

.model flat, stdcall

option casemap:none

include windows.inc
include kernel32.inc
include user32.inc
inlcudelib kernel32.lib
includelib user32.lib

.data
lpText db "Hello, World!", 0;string to display
lpCaption db "Hello", 0;our name in titlebar

.code
start:
invoke MessageBox, NULL, addr lpText, addr lpCaption, MB_OK
invoke ExitProcess, 0
end start


So what does all this code mean? Well we will start with line by line. I will be explaining most of the things.

.386
This tells our assembler what type of intel compatible type processor we will be working with. .386 will run on any 32bit processor, it uses 80386(x86) instruction set, you can also specify .486 which is Pentium I and .586 which is Pentium II and .686 which is Pentium III but for now we will use .386 for compatibility reasons. I belive the instruction set for Pentium IV is the same as Pentium I, so .486 might work. But since I'm not really sure, I suggest specifying .386.

.model flat, stdcall
.model tells our assembler what memory model our program is. Under Win32 there is only one model that we can use and that is flat.

stdcall tells MASM how we would like to pass our parameters. It will pass parameters from right to left, but it's our duty to keep for stack balancing after the call is made. Win32 uses stdcall except in one instance and that's with wsprintf()

option casemap:none
This tells MASM to make our labels case-sensitive, e.g. invoke MessageBox() is different to invoke messagebox().

include and inclubelib
These files and libraries are found within MASM's include directory, the files we are including help us with Win32 API calls we invoke in our program, functions that we use from the associated .dlls like kernel32.dll and user32.dll

.data
This is the section where we put our constants, which are set variables that can't be altered. A lot of this is explained in Linux tutorials for Assembly using NASM and would be worth checking out for better understanding.

Then we have our constant names, lpCaption and lpText which are just names that I created that help me with the program, you could call them anything you want that could help you like MsgTitle and Msg. We then use db, which is define byte. Next we set the strings we want to be displayed in our Title and in our Message and then we NULL terminate it (zero on the end) this is how C identifies it's strings. NULL is equivalent to 0 but we can use either for this example.

.code
This section is where you start writing our code, it contains our program's main entry point which is start:

[b]invoke[b]
We use this when a function we've included is needed to be called, this will grab what we want from our included files and we will just need to make sure we pass the correct parameters in order.

Now that we have learnt what the different parts in the code mean, we are ready to assemble our first program Smile.
I'll explain how to compile the file in steps:
1. Open notepad.
2. Copy the code into notepad.
3. Save it as a .asm file, for example say, helloworld.asm
4. Open helloworld.asm in MASM32.
5. Click on the 'Projects' drop-down menu and click on 'Assemble and Link'
6. Your helloworld.asm should successfully be linked and assembled into the file helloworld.exe Very Happy.

Now run, the file, it should bring up a window which displays 'Hello World!'.
If it runs successfully, congratulations, you've made your first "program"(Razz sorta) in ASM.
_________________
"A lie gets halfway around the world before the truth has a chance to get it's pants on." - Winston Churchill
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number
Sponsored Links
Posted: 21 Nov 2008 4:50 am    Post subject: Advertisements
Back to top
Post new topic   Reply to topic    Google Community Forum Index // Web Design, Coding & Programming Forum All times are GMT - 8 Hours
Page 1 of 1


 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Host your free forums with Invision Plus.net forum web hosting with your own subdomain.

alexisBlue v1.2 // Theme Created By: Andrew Charron // Icons in Part By: Travis Carden

© 2005-2006 Google Community

Powered by phpBB

Privacy Policy | Contact Us

Powered by Google Search blog

This website is not affiliated in any way with Google, Inc.
Google™ is a registered trademark of Google, Inc.