The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: frktons on February 15, 2010, 03:22:29 AM

Title: Learning Assembly for fun
Post by: frktons on February 15, 2010, 03:22:29 AM
Hi everybody.

I just arrived at MASM forum.
Once upon a time I used to program in PL/1 on an IBM mainframe.
Now I'm almost a retired programmer, and "just for fun" I decided to learn a bit of MASM.
Could you suggest a path to follow, I mean a Step By Step introduction to the
language and the tools?
I've already installed the MASM32 package on my pc, and now I'm trying to start.

Thanks for your suggestions.

Frank
Title: Re: Learning Assembly for fun
Post by: hutch-- on February 15, 2010, 03:29:28 AM
Hi Frank,

Welcome on board. The stuff you need to get is the manuals from Intel and decent access at the Microsoft win API function set. The architecture of later Intel hardware is worth learning as it gives you a good idea of howe current operating systems use it. RE masm32, have a poke around the tutorials and the example code as well as the help files on the Help menu of the editor, that should get you started and if you have specifics don't be afraid to ask in here, its a script kiddie free zone with a lot of experienced programmers who help out as their time allows.
Title: Re: Learning Assembly for fun
Post by: dedndave on February 15, 2010, 04:13:56 AM
welcome to the forum, Frank   :U
PL1, eh - i feel younger already

in the upper right corner of the forum is a link for Forum Links and Website
besides the intel references that Hutch mentioned, there is a link for Iczelion's tutorials - a good starter kit
also, in the masm32 folder, browse around in the Examples, Help, and Tutorials folders
of course, another good place to poke around is the forum, itself - the forum search tool will get you a lot of info

oh - here is a link i use quite often, also

http://www1.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_6/CH06-1.html
Title: Re: Learning Assembly for fun
Post by: frktons on February 15, 2010, 10:08:58 AM
Thanks friends, I'll try poking around the c:\masm32 folder for examples and help files,
as time permits.

If I find something hard to digest, or I get lost  :'(, or I discover some interesting stuff  :8)
I'll be right back.

Enjoy   
Title: Re: Learning Assembly for fun
Post by: Bill Cravener on February 15, 2010, 05:57:52 PM
Just never lose that word fun when approaching asm and the Windows API's and you'll do fine. :wink
Title: Re: Learning Assembly for fun
Post by: frktons on February 15, 2010, 08:45:26 PM
Quote from: dedndave on February 15, 2010, 04:13:56 AM
welcome to the forum, Frank   :U
PL1, eh - i feel younger already

in the upper right corner of the forum is a link for Forum Links and Website
besides the intel references that Hutch mentioned, there is a link for Iczelion's tutorials - a good starter kit
also, in the masm32 folder, browse around in the Examples, Help, and Tutorials folders
of course, another good place to poke around is the forum, itself - the forum search tool will get you a lot of info

oh - here is a link i use quite often, also

http://www1.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_6/CH06-1.html

ah! Ah! It'll take months for me, not a native english speaker, to manage something different than
printing "Hello" on the screen, and that is very FUN-ny indeed  :lol

By the way I printed that infamous chapter 6  :U and I'm happy to rejuvenate you with a PL1 subroutine  :bg

Quote from: Bill Cravener on February 15, 2010, 05:57:52 PM
Just never lose that word fun when approaching asm and the Windows API's and you'll do fine. :wink

Yes Bill, you are right, and without FUN I can't do anything  :U

Just for FUN, I was thinking about the first project:

*) Reading a file with 10,000 records, sorting it and writing it back to the disk with a different name.
and to make even more FUN, I was dreaming about 3 different versions of the program, PBCC, C, and MASM.
This should give me enough stuff to work, learning some respected HIGH level languages for the Windows platform.
Once the programs are ready, I'll post them, one by one, with the elapsed time each version takes to do the job, hoping somebody can
help me optimize them and I can learn some interesting concepts. Well, Not all three versions here I mean.  :bg

See you with the first version in short time[?]

Frank
Title: Re: Learning Assembly for fun
Post by: jj2007 on February 16, 2010, 02:27:17 AM
Quote from: frktons on February 15, 2010, 08:45:26 PM
ah! Ah! It'll take months for me, not a native english speaker, to manage something different than
printing "Hello" on the screen, and that is very FUN-ny indeed  :lol

Hi Frank,
To get you started, here a Hello World skeleton.
Benvenuto al Forum :thumbu

include \masm32\include\masm32rt.inc

.data ; initialised variables
MyString db "Ciao", 13, 10, 0
MyReal4 REAL4 123.456
MyReal10 REAL10 123.456e-99

.data? ; non-initialised (i.e. zeroed) variables
NonInitDword dd ?

.code
hw db "Hello World at 1024 bytes", 13, 10, 0 ; with polink.exe
start:
MsgBox 0, offset hw, "Masm32 is easy!", MB_OK
exit ; short form of invoke ExitProcess, 0

end start
Title: Re: Learning Assembly for fun
Post by: BlackVortex on February 16, 2010, 04:16:32 AM
Oh my god, what is that little msvcrt+macro infestation. That's not what you wanna show to a MASM newcomer ... use assembly ppl.
Title: Re: Learning Assembly for fun
Post by: dedndave on February 16, 2010, 04:46:36 AM
QuoteOh my god, what is that little msvcrt+macro infestation.

lol @ "infestation"
you know Jochen - he loves his macros (and indentation)
i am starting to get the hang of reading macros
the indentation - i am mentally blocking (pun?) that, still   :P
Title: Re: Learning Assembly for fun
Post by: jj2007 on February 16, 2010, 07:55:37 AM
Quote from: BlackVortex on February 16, 2010, 04:16:32 AM
Oh my god, what is that little msvcrt+macro infestation. That's not what you wanna show to a MASM newcomer ... use assembly ppl.

Depends on whether you want to scare away the newcomer with "real" assembler, or whether you want to give him a chance to see his first mov eax, 123 in action. What I provide above is just a skeleton for adding TheRealThing(TM)
Title: Re: Learning Assembly for fun
Post by: frktons on February 16, 2010, 12:48:03 PM
Quote from: jj2007 on February 16, 2010, 07:55:37 AM
Quote from: BlackVortex on February 16, 2010, 04:16:32 AM
Oh my god, what is that little msvcrt+macro infestation. That's not what you wanna show to a MASM newcomer ... use assembly ppl.

Depends on whether you want to scare away the newcomer with "real" assembler, or whether you want to give him a chance to see his first mov eax, 123 in action. What I provide above is just a skeleton for adding TheRealThing(TM)

Thanks jj, a skeleton is always welcomed in my wardrobe  :lol
I'll make use of it as I get to the Asm job.

By the way I managed to realize the first part of the project [see my previous post] in PBCC
and the outcome is:

                             Start Prog Time =>13:41:19

                             End Prog time ==>13:41:21

                             Number of Records =>245,204

                             Max Lenght of Records => 28

                             Number of CPU cycles 4,029,689,037

Not too bad for a compiled program that took me about 2 hours to write and test.  :8)
This is because PL1 is much alike Basic Language, and I had some previous knowledge of the Basic as well.

Now I'm going for the C version, which could take a little longer, as I've almost no previous knowledge of
the language, libraries, tools, and so fort.  Well let's get Pelles-C stuff.

See you for the second version [not too soon I suppose]  :eek
Title: Re: Learning Assembly for fun
Post by: BlackVortex on February 21, 2010, 03:39:45 AM
Quote from: jj2007 on February 16, 2010, 07:55:37 AM
Quote from: BlackVortex on February 16, 2010, 04:16:32 AM
Oh my god, what is that little msvcrt+macro infestation. That's not what you wanna show to a MASM newcomer ... use assembly ppl.

Depends on whether you want to scare away the newcomer with "real" assembler, or whether you want to give him a chance to see his first mov eax, 123 in action. What I provide above is just a skeleton for adding TheRealThing(TM)
Yeah, check out the newcomers getting their registers trashed by what appears to be a simple "print" macro    :bdg

wsprintf is easy !
Title: Re: Learning Assembly for fun
Post by: jj2007 on February 21, 2010, 08:22:53 AM
Quote from: BlackVortex on February 21, 2010, 03:39:45 AM
Yeah, check out the newcomers getting their registers trashed by what appears to be a simple "print" macro    :bdg

I am always willing to learn: Please explain why newcomers learn better about the trashing of eax from "real" rather than Macro Assember. Here are the examples (I apologise for having used C runtime calls in the "real" assembler example).

include \masm32\include\masm32rt.inc   ; Version A: "real" assembler

.data
hw db "Hello World", 13, 10, 0
errmsg db " is in eax, we expected 123", 13, 10, 0
pak db "Press any key", 13, 10, 0

.data?
NumBuffer db 20 dup (?)

.code
start:
mov eax, 123
invoke StdOut, addr hw
invoke dwtoa, eax, addr NumBuffer
invoke StdOut, addr NumBuffer
invoke StdOut, addr errmsg
invoke StdOut, addr pak
invoke GetStdHandle,STD_INPUT_HANDLE
invoke FlushConsoleInputBuffer, eax
@@: invoke Sleep, 1
call crt__kbhit
test eax, eax
jz @B
call crt__getch
invoke ExitProcess, 0

end start


include \masm32\include\masm32rt.inc  ; Version B: Macro assembler

.code
start:
mov eax, 123
print "Hello World", 13, 10
print str$(eax), " is in eax, we expected 123", 13, 10
inkey "Press any key", 13, 10
exit

end start
Title: Re: Learning Assembly for fun
Post by: BlackVortex on February 21, 2010, 09:05:28 AM
You may be a better programmer than me, but seriously ... like I said in my previous post : wsprintf FTW !

Anyway, here is my approach, no macros, no runtimes, just pure winAPI asm. (goasm syntax, doesn't matter of course)

#dynamiclinkfile kernel32.dll user32.dll
#include windows.h

DATA SECTION

sz_1               db "Hello World, %d (decimal) is in eax",13,10,0
h_console_out      dd 0
buffer             db 128 dup 0
junk               dd 0

CODE SECTION
START:

invoke GetStdHandle, STD_OUTPUT_HANDLE
mov [h_console_out], eax

mov eax, 123
invoke wsprintf , addr buffer, addr sz_1, eax
invoke WriteConsole, [h_console_out], addr buffer , eax , addr junk, NULL

invoke ExitProcess, NULL

Noone would expect the eax to retain its value.

EDIT: nevermind,corrected it
Title: Re: Learning Assembly for fun
Post by: hutch-- on February 21, 2010, 09:14:39 AM
Frank,

Just a short history on what style of assembler you are "supposed" to write. The old brigade with crude mnemonic crunching as some notion of purity left assembler in a shambles, near abandoned and ridiculed in the programming community. The pseudo high level stuff in MASM resurrected it from the dead and made it a living language again. Most would not write C like they did in the middle 80s yet they expect you to write assembler in the way that people cobbled together a few DOS interrupts 25 years ago.

A macro assembler is in fact a very smart tool, it allows the assembler programmer the capacity to design and build their own language while losing nothjing to fast low level code. The other factor is modern assembler programming must use operating system functions to access memory, hardware and so on so these clapped out notions of purity cannot in fact be put in place.

Its as usual, pick what works for you be it high level emulation or bare mnemonics and over time you will set up whatever format does the job for you. Good assembler programming never went out of its way to do things the hard way, it went for efficiency and performance and using your own choice of available capacity.
Title: Re: Learning Assembly for fun
Post by: jcfuller on February 21, 2010, 10:35:48 AM
Hutch,
  I hear you and concur. I started assembly programming on an Atari 800XL using MAC65, which was, as the name suggests, a macro assembler for the 6502 processor.  If I remember correctly I think there was even  a pretty  complete "basic" composed of macro's at the time.

James
Title: Re: Learning Assembly for fun
Post by: jj2007 on February 21, 2010, 11:11:08 AM
Quote from: jcfuller on February 21, 2010, 10:35:48 AM
If I remember correctly I think there was even  a pretty  complete "basic" composed of macro's at the time.

When I wrote MasmBasic (http://www.masm32.com/board/index.php?topic=12460), I got a suspicion that the early BASIC's that required the Let keyword were done that way - just macros. Masm is incredibly versatile in this respect.
Title: Re: Learning Assembly for fun
Post by: hutch-- on February 21, 2010, 11:44:09 AM
It tends to be an issue of priority stacking, I have always treated assembler AS assembler, not an appendage to another language and while I happily write code in compilers, I use an assembler for what it is good for, complete architecture freedom, massively powerful facilities, as many library module as you want to write and the choice of how high or low level you want to write.

When I hear people talking about writing pure assembler my mind hearkens back to the old brigade that left assembler as a shambles that no-one used and most laughed at. They don't seem to laugh any more.  :P

What was even funnier was the old fellas cobbling together a few DOS interrupts were simply using the TSR runtime library that was part of the OS, there was nothing pure about it and while anyone who has written this type of code can write it like an assembler dump, if it is ever going to be seen by anyone else or maintained or fixed then it gets written very differently. Badly written code of any flavour suffers all of these problems and assembler is no different from the rest when its badly documented and poorly laid out.
Title: Re: Learning Assembly for fun
Post by: frktons on February 21, 2010, 10:35:31 PM
Hi everybody!

Very good point of view you are expressing, and in many ways I agree with most of them,
even if they apparently look different or opposite.  :dazzled:

I've looked around, searched the internet, and collected some infos and tools for starting my holidays on "ASM-beach" as I prefer to imagine it.

From what I have got, till now, it looks like:

-MASM is well and healthy, still upgraded by Microsoft, but with lesser infos as time passes by.
After MASM 6.1 the docs tended to disappear, and it's hard to know if there are new opcodes or functionalities
as ML stepped from 6 to 9 in the last 12 years or more. I guess there are many.  :8)

-NASM is a growing tool, with lots of free e-books and tutorials, also Video Tutorials. :eek

- many other good assemblers growing up [FASM,GOASM and so on].

From the point of view of a novice assembler student, "me", NASM looks more attractive for the
quantity of infos you can freely find around.

Probably from the point of view of experienced assembly programmers, MASM is a more powerful tool, and even if there is a partial lack of infos, it doesn't matter for them, they can manage to use it anyway.

Macros are wonderful extras, but they "appear" more like "advanced stuff" for any programming language than starting points to learn  from zero. It is very nice to know they are available, anyway. :bdg

Of course I lack assembly experience and expertise, so what I've found could be a very small and partial
view of the whole.

By the way, I'd like to express my opinion about the samples I've found around for the Hello.asm gem.

It's always simpler to write a piece of code than to explain it, as I've learned in my 30 years of coding.

From dos to linux to windows console and gui examples, I've noticed very few explanatory comments.

One of the best documented one goes like:


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

;                 Build this with the "Project" menu using
;                       "Console Assemble and Link"

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    .486                                    ; create 32 bit code
    .model flat, stdcall                    ; 32 bit memory model
    option casemap :none                    ; case sensitive

    include \masm32\include\windows.inc     ; always first
    include \masm32\macros\macros.asm       ; MASM support macros

  ; -----------------------------------------------------------------
  ; include files that have MASM format prototypes for function calls
  ; -----------------------------------------------------------------
    include \masm32\include\masm32.inc
    include \masm32\include\gdi32.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc

  ; ------------------------------------------------
  ; Library files that have definitions for function
  ; exports and tested reliable prebuilt code.
  ; ------------------------------------------------
    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\gdi32.lib
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib

    .code                       ; Tell MASM where the code starts

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

start:                          ; The CODE entry point to the program

    print chr$("Hey, this actually works.",13,10)
    exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start                       ; Tell MASM where the program ends


When the first line of code is written, in my opinion, it should be a line of comment, explaining itself, something like:

; This line is a comment. Every line starting with a ";" is a line of comment in this Assembly sintax....

If there is a line of code like:

mov eax, 123

what I need to know is what is "mov" [OK I guess what it is], why I am using this
"opcode" and what an opcode is, and why I use 123 instead of 234 or anything else.

So 1 line of code could need 10 lines or more of comments to explain all the stuff that instruction means or, at least, the important ones.

This is what I really need when I look for a tutorial.  

I don't know if a tutorial like this exist, but it would cover almost all my needs to start learning assembly.

Some more days to analize the stuff I collected, and I'll know if there is something that suits my needs.  :bg

Bye and enjoy

Frank





Title: Re: Learning Assembly for fun
Post by: BlackVortex on February 21, 2010, 10:47:40 PM
Just because MASM documentation is outdated, it doesn't mean it's useless. All ml.exe versions are compatible, so don't worry about it. The best tutorials are Iczelion's (google it, it's a whole series)

Also, when you see unknown API names after invokes, they're usually windows APIs and you can searh MSDN for them. If they aren't APIs, then they're functions, maybe in one of the include files. Those are documented in the MASM32 docs. Opcodes are there, too.

For starters, keep close to Iczelion's programming style.

QuoteSome more days to analize the stuff I collected, and I'll know if there is something that suits my needs. 
I hope you meant "analyze"   :cheekygreen:
Title: Re: Learning Assembly for fun
Post by: frktons on February 21, 2010, 11:13:06 PM
Quote
I hope you meant "analyze"   :cheekygreen:

I did, indeed, my dude  :lol

but "analize" is more funny and this topic is "just for fun".  :cheekygreen:
Title: Re: Learning Assembly for fun
Post by: hutch-- on February 22, 2010, 12:18:49 AM
Frank,

With MASM, the documentation from version 6.0 is still basically current with notation and the macros. Microsoft have slightly updated the documentation that is on MSDN but very little has changed over a long time. What they do is add extra instructions as they become available and in the case of 64 bit, the have a 64 bit version ML64 but it is not fully developed yet as it does the job they need.

NASM is a good choice if you are aiming at multiple platforms but it is different from historical Intel notation so you will have to get used to it. It will certainly fill the category of recreational programming.
Title: Re: Learning Assembly for fun
Post by: dedndave on February 22, 2010, 12:42:54 AM
and, btw, Frank....
Next Generation was my fave   :P

(http://www.jeremyreimer.com/postman/files/images/StarTrekTNG.jpg)

sorry - no Franktons, there
but, how about that red-head ?
oh - and seven of nine, too
Title: Re: Learning Assembly for fun
Post by: frktons on February 22, 2010, 12:55:34 AM
Thanks for the feeds-back  :thumbu

There are enough ebooks and docs around for MASM-6, I think, so merging
the infos step by step could give me a good idea of the whole matter.

regards

the Borg-girl from Voyager serie was gorgeous  :P
Title: Re: Learning Assembly for fun
Post by: qWord on February 22, 2010, 01:00:39 AM
Quote from: dedndave on February 22, 2010, 12:42:54 AM
Next Generation was my fave :P
Data was the best of all  :toothy ... unfortunately he died in the one of the last movies.
Title: Re: Learning Assembly for fun
Post by: dedndave on February 22, 2010, 01:28:46 AM
yah - that borg girl was Seven of Nine - she was introduced in "Next Generation" (played by Jeri Ryan)
also not shown in that pic is Lt Torres - half klingon + half human = total babe   :bg
(played by Roxann Dawson)

Data was fun, but i think he was programmed in C   :bg
he was unable to use contractions
i sometimes suffer from that problem when i type - lol
Title: Re: Learning Assembly for fun
Post by: hutch-- on February 22, 2010, 01:42:12 AM
I certainly like the second series the best, its production techniques are a bit old by today's standards but it often had a plot and I liked both of the ladies in it, Marina Sirtis and the lady standing next to Picard who was not only good looking but could dance and do many things.
Title: Re: Learning Assembly for fun
Post by: dedndave on February 22, 2010, 02:52:56 AM
and - - she was a Doctor, to boot !!!
ahhhh - to be a "cared for" man   :bg
Gates McFadden is her name
i got the first couple seasons from torrent, and a few later episodes
then, my isp cracked down on torrents - oh well   :(
Title: Re: Learning Assembly for fun
Post by: GregL on February 22, 2010, 04:04:29 AM
I have never had a good experience with torrents, straight downloads work much better for me.

Re. Star Trek, personally I prefer the original series, I just got done watching all three seasons, took me a couple of months. They're available on DVD and Blu-Ray.

Title: Re: Learning Assembly for fun
Post by: hutch-- on February 22, 2010, 04:10:44 AM
 :bg

Yes, Gates McFadden, always liked her, genuine talent there and looks like a nice lady as well.

I did like marina Sirtis though, I saw her years later in a movie playing a Russian military officer and she still had the talent. Nice Greek girl.
Title: Re: Learning Assembly for fun
Post by: dedndave on February 22, 2010, 05:35:16 AM
i was sweet on her, as well   :P
another one i liked was Ezri Dax from the Deep Space 9 series - played by Nicole DeBoer - what a cutie pie