News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Introduction and question

Started by A_B, December 13, 2009, 12:53:37 AM

Previous topic - Next topic

A_B

Hi there!,

I joined the forum for obvious reasons, I want to learn assembly.
The reasons I want to learn it are primarily having an understanding of what's going on "under the hood" I believe this would make me a better programmer in high-level languages. Secondly, I am also interested in writing small mathematical apps in assembly. I am currently going through the discrete math topic in math at school an i think assembly could be a useful way to consolidate some of the knowledge.
Lastly assembly could be usefull to me for actually optimizing my compiled C++ code, although the main thing I hope to get out of assembly is to understand the C++ code better and therefore being able to write more optimized C++ code without having to tinker with the assembly.

My main interests in programming are games (for which I probably mostly won't use assembly), mathematical programming, and cellular automata. It is in the math and CA that I expect assembly to be of use.


Now, for the actual questions;
Where do I begin?
I have visited many websited linked in this forum and all of the seem to preach a "different way" of writing assembly. I have seen people say MASM32 is great, but others say it's old and obsolete. I've seen HLA, but I don't like it because it's almost just like programming C++ again, and I won't really get what's going on anyways. What I'm looking for is assembly at the lowest level that is still writable. (I'm not going to try and write 0's and 1's either).
So how to I start learning? What software do I use to assemble my assembly code and to disassemble .exe's?
Where do I find good resources to learn assembly at the level I'm interested in?

If this information is of any use:
I have an intel pentium D processor
x86   32-bit      but I think that is quite to be expected.


Thanks
Alex

dedndave

welcome to the forum
well - to get started with 32-bit assembler, it seems many use Iczelion's Tutorials
http://website.masm32.com/iczelion/iczelion.zip

but, there are a vast number and variety of resources
intel has the instruction set manuals, of course
http://developer.intel.com/design/Pentium4/documentation.htm

then, there is the msdn website that provides an API reference, among other things
this thing is huge
http://msdn.microsoft.com/en-us/library/default.aspx
they also have individual functions under the different headings, for example
http://msdn.microsoft.com/en-us/library/aa366887(VS.85).aspx

if you install the masm32 package, there are many good example programs and tutorials included
one of them is Raymond's FPU tutorial that you will like for math
let us know when you have all that memorized, and we will give you some more   :bg

redskull

There's a good book (by the author of HLA, no less) entitled "Write Great Code" that goes into details just like that; understanding what's happening under the hood when you write high-level, so as to make you a better high level programmer.  That being said, you might be dismayed to find that under a protected mode O/S like Windows, your ability to poke and prod with the guts is very limited; all I/O is performed through mandatory Windows services, unlike DOS where you had full access to the "lowest level".  However, your math intensive work probably has little interaction with the peripherals, and might be a better fit under a Windows environment.  Something like a game, however, will end up being a long list of Windows functions calls, which while interesting, is not much more exciting than a long list of C++ function calls.  If you want a program with an interactive GUI, Iczelions tutorials are the seminal work; if you are fine with text-mode, search on MSDN for "Console functions".  There's lots of code floating around here, so the best way is to give it a shot and ask a question if it doesn't work.  And, for the record, MASM is all three: old, obsolete, and totally great  :U

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

A_B

ok... I'm lost.
I think I found how to write and assemble .asm in VC++ 2008 express but I can't find any tutorials and very little sample code. The sample code I do find always gives errors. I also found a way to write DOS programs at the command prompt by going "debug" "-a 0100"...
Which does produce a working program but I have no clue how those DOS files are different from a .exe, and how it relates to compiled C++ code. The only MASM compiler i've got is therefore the one with VC++ 2008. The DOS stuff also seems a lot different structure wise. The MASM stuff I saw all require ".MODEL, .STACK" etc  while in the dos you can simply write the commands.

I probably had the wrong idea that assembly on windows would be very similar to Knuth's MIX, but it seems to be a whole lot more complicated and abstract then that...

I stll have no clue how to even write a simple program that prints the number 1 to the screen, nor can I fin any example of such a program that I can actually compile (most examples I find are more complex then printing something to the screen anyways and they're usually 50 lines or more with barely any explanation).

Any "guides" I found never mention what assembler is used, or what architecture for that matter. Many of them (I think) require MASM32 without mentioning it. They don't work to a program that can be compiled, instead they show little bits and pieces of code that by themselves don't do anything.


I'm really confused with all this, I thought assembly was a language composed of commands that went straight to the CPU, then how can you have 50 different ways to write assembly for the same processor?


Thanks
Alex

A_B

I decided to try assembly in Linux and I got it to work in under 5 minutes, why is it such a hassle in windows?

TNick

Hello!

It's not. You just did not searched enough in this forum.
here are some links:
http://www.masm32.com/board/index.php?topic=7051.0
http://www.masm32.com/board/index.php?topic=12126.0
http://www.masm32.com/board/index.php?topic=10371.0
...


Basically, you need to: download the masm32  package, write a file that has your code and pass it to ml.exe in some form.
Nick

A_B

Yes, there is plenty of information to get things to work, but ALL of them require MASM32.
Since I want to learn assembly mainly for educational purposes (and math), I don't like the idea of half my code being written for me, If I did, I would just be writing C++.

What I'm looking for is the basics of coding my own assembly, not the basics of coding a working program largely based on someone else's work.

I hope that makes sense (and if I've got it all wrong about MASM32, please do correct me)

Thanks
Alex

TNick

MASM32 is a complex package. It has:
- constants used by the operating system (you need this whatever you do)
- definitions of functions that belong to OS (you will need at least kernel32, wich has ExitProcess, wich is the proper way to end a program) as inc and lib
- utility functions (converting string to dword, reverse, string to float, reverse, etc) these you may not use.
- executables that you must use in order to build an exe (ml, link, rc, ...)
-...
Most basic program under windows is this:



; tell wich instruction set to use
.686
; memory model (just flat under windows) and calling convention (STDCALL)
.MODEL FLAT,STDCALL
; SomeSymbol is diffrent from SOMESYMBOL
OPTION CASEMAP:NONE

; include at this point a great deal of constants that we use to communicate with OS
; in this case, we need it for NULL, in ExitProcess
; if you just write INVOKE  ExitProcess,    0, then you don't need this line
INCLUDE         windows.inc

; definitions for functions found in kernel32.dll (among wich is ExitProcess)
INCLUDE         kernel32.inc
INCLUDELIB      kernel32.lib

; start the code section
.CODE
; label that becames the entry point
APP_Entry_Point:


; hard core assembly here
; vvvvvvvvvvvvvvvvvvvvvvv

        or      ecx,            -1
        xor     eax,            eax
        inc     eax
        dec     eax
        xor     eax,            ecx
        not     eax
        and     eax,            ecx

; ^^^^^^^^^^^^^^^^^^^^^^^
; hard core assembly here




; standard way to end a program
INVOKE  ExitProcess,    NULL

; mark the label as entry point
END APP_Entry_Point


So, you are safe :). No one writes the code for you.
Nick

sinsi

Put it this way Alex, there are some of us who
- don't use stack frames
- don't use macros we didn't write
- don't use invoke
so we only use ml.exe and link.exe

You can use as much or as little as you want.
I use the masm32 package because it has the include files, the libraries for linking and an easy to use editor.
I usually use my own include files (handcrafted) and libraries (from the latest sdk) but still use hutch's qeditor.exe

>I decided to try assembly in Linux and I got it to work in under 5 minutes, why is it such a hassle in windows?
Which assembler did you use? I tried linux and failed miserably... :bdg
Light travels faster than sound, that's why some people seem bright until you hear them.

jj2007

Quote from: TNick on December 13, 2009, 10:40:03 AM
Most basic program under windows is this:
...
INCLUDE         kernel32.inc
INCLUDELIB      kernel32.lib


Nick,
Your version works but requires that those here in the forum who test your code have set the environment variables properly. It is a real nuisance to adjust such code for testing. Not by accident Masm32 has hard-coded paths.

The line
include \masm32\include\masm32rt.inc
does what is really needed, and works perfectly with 99% of all coding needs under Masm. Note do not use C:\masm... as some members use other drives.

This is a complete Windows application under Masm32:

include \masm32\include\masm32rt.inc

.code
start:
invoke MessageBox, 0, chr$("Masm32 is easy"), chr$("Welcome"), MB_OK
invoke ExitProcess, 0
end start

A_B

Thanks guys! Seems I was wrong about MASM32 after all :P
I presume then that "absolute hardcore pure assembly" is only for programming that is completely independant of the OS?

@sinsi.
I don't know which linux you use, I've got Ubuntu which make make thing a little easy for me.
The things to do in ubuntu are:
1: install NASM -> sudo apt-get install nasm
2: create source file -> test.asm
3: cd to correct dir and -> as test.asm -o test.o
3: link the object file -> ld test.o -o test

The format of a NASM assembly source file is:

.section .data
#and data you intend to use
.section .text
#I don't know what this is for yet :p

.globl _start

_start:
   #the program

TNick

:)

@jj2007:
Except if you don't have masm32 on same drive as your projects folder.
:bdg

dedndave

as i understand it, they tried the environment variable method earlier in the history of masm32
it seems they had problems with it, as some users could not get it to work correctly
i would think that, if you set the variables correctly, using the correct method, that would work fine
at any rate, the files are all hard-coded to \masm32\include
you can still set your environment variables
just know that, if you post code in the forum, almost everyone in here uses the same drive for development as installation
99% (if not all) of the code posted  in here is set up for \masm32\include and \masm32\lib
most of the more recent code uses the \masm32\include\masm32rt.inc file to simplify things
you may see one of the following (or a combination)

        include \masm32\include\masm32rt.inc
        .586
        include \masm32\macros\timers.asm

or

        include \masm32\include\masm32rt.inc
        include \masm32\include\advapi32.inc
        includelib \masm32\lib\advapi32.lib

in the first example, they want to use MichaelW's timing macros, which require .586 or higher
the masm32rt.inc uses .486 (i think), and it must be "adjusted" prior to declaring the timers macro file

in the second example, they want to use functions from advapi32, which was omitted from masm32rt.inc for some reason
i am sure there are other less-common libraries that are also omitted - winsock may be one
i have added advapi to my own masm32rt.inc file, but if i post that code, i have to add those lines for everyone else

TNick

I found it funny to talk about paths in files ( :bdg ) but ... I guess it may be a problem to those that are just getting started.

ACK

Thanks for heads-up.  :wink
Nick

dedndave

i agree - lol
if it were up to me, i would not reference any paths
i am too old and lazy to modify all the masm32 files

just a note - the environment variables provide masm with the "default" search paths
if it is specified in the include / includelib directives, the default path is not used