News:

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

Where do I start learning Assembly?

Started by Walkingtheunwalked, August 18, 2009, 01:11:02 PM

Previous topic - Next topic

Walkingtheunwalked

I expected to find such a topic in the beginners' section, but I didn't so I'm opening one. If the moderator agrees with my opinion that such a topic is important, I suggest him to make it sticky.

So, I want to learn programming in Assembly and I'm looking for a point to start from. I have briefly had a look in this forum section and confess that things like "DX:AX", "SI\DI crashes with ESI\EDI" and "16-bytes alignment" look puzzling to me.

Would anyone suggest me a basics tutorial that is clearly written and structured? I find the technical documents on Intel's (I have a Core 2 duo processor) website excessively large and wordy.

To make the information offered on this topic even more complete for me and other beginners who may read, I am asking below a few particular questions related to Assembly programming. Please note that as assembler I'm using sol_ASM, so please target your replies at general Assembly instructions and syntax and not at MASM specifics.

* What are the required parts of any ASM program?

*  How should a program in ASM be structured? I mean, is there a declaration section and only then followed by the actual program instructions? Is it obligatory to structure it like this? What is the "good practice" way to structure an ASM program?

I will come back with more questions, after I get your answers for the above. I hope I will get a variety of replies and opinions, so that the answer be more complete.

Thank you everyone for your time to read an help a fellow wanting to learn!

dedndave

in the upper right-hand corner of the forum page is a link "Forum Links and Website"
on that page, you will find a link to Iczelion's tutorials
also, there are many example programs in the masm32\examples folder (assuming you have installed masm32)
welcome to the forum   :U

hutch--

Walk,

Unless you are an experienced programmer you have little chance of successfully learning assembler programming, it is an advanced subject that requires a lot of knowledge about how a computer works, how Windows operates and how you write low level software. Unless you have this type of exp0erience you would be far better to learn a simpler more friendly environment like a basic or C compiler. If you need to write assembler code, you can always come back when you know enough.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Jimg

If you google for assembly language tutorial, you will find many links to sites.  Unfortunately, these are almost all for 16-bit assembly.  You have to make the decision early on whether you are going to start with 16-bit, or go directly to 32-bit.   (Notice, I didn't say waste your time with 16-bit, but it would save a lot of time to just start with 32-bit, as that is what you will eventually be using most all of the time.)

Tedd

Ignore Hutch, he's a grump.

It sounds like you already have some experience in a more structured language, so quick answers..

* There are no 'required' parts, it's pretty much a free-for-all. The power comes from that, but obviously it's also easy to mess up. So there are certain things it's advisable to do, but you can also get away with not doing most of them. You'll pick them up as you learn anyway.
* Free-for-all, again. There are a few required bits to tell the assembler what your intentions are, but other that anything can go anywhere. Usually it turns out something like: assembler directives, data, global variables, code. And code is split into functions (with no real constraint on where you put your entry-point/main/start.) Once you learn the basics you'll develop your own style (usually some variation of whichever tutorials you learned from.)

I would suggest trying out the first 5 or so of Iczelion's tutorials, just to get a feel -- even use masm just to make life easier, then you can adapt what you know to sol_asm.
And come back here to bug us with questions :wink But at least try to figure things out for yourself first.
No snowflake in an avalanche feels responsible.

FORTRANS

Hi,

   You could try an assembly language course book.  Google popped
up with a bunch of links.  For example.

http://www.techbooksforfree.com/assembly.shtml

   "The Art of Assembly Language Programming" has its own site
you could look at.

Steve N.

Astro

Hi,

I suggest just reading up on the architecture of the system first of all (registers, flags, memory organization), then start looking at the basic instruction set. MASM help file "opcodes.hlp" is useful for starting out. Not sure what sol ASM has in the way of documentation.

I second, third, forth and fifth Iczelions tutorials.  :cheekygreen:  I spent the first few days after I decided to learn assembly programming just reading through them.

How well do you know the Win32 API? This is something else you must be comfortable using if you're going to be programming on Windows to any extent. You also need to understand how Windows does things.

As Ted said above - research first, then ask here if you get stuck.  :U

Look for threads started by me - I've been 4 weeks or so at this now, and I may have asked questions you have, too. Got some great replies which I've still yet to study myself.  :thumbu

Best regards,
Astro.

Walkingtheunwalked

Thank you everyone who replyed! At the moment I'm reading and researching documentation. Will get back with comments, more details about what I've been through and questions shortly, when I'll feel ready.

Walkingtheunwalked

I've had a look at Iczelion's tutorials and my objection is that they're very dependent on Windows' API and dll-s etc... My interest is to use as less dependencies as I can, and do not intend to stick to programming for Window, or for any other OS in particular. The same for the ASM books and pages on the internet: they're usually either targeted at programming for Windows, or for Linux, and are heavily dependent on the libraries of the OS.

However, I do not know how else to do at the moment, so I'll accept for now a minimal part of Windows dependence in order to see my programs working. This relates to my next 2 questions below:

1.  The below is a "Hello world" program I found as a sample in the sol_asm's zip download. I want to display, not a text, but the value contained by a register, let's say the EAX register, and can't figure out how to do it. Any ideas how to do it?

I have highlighted in red the portions of the program where I believe the modifications should be made in order to get the desired result:


;------------------------------------------------------
; SOLAR assembler
; Copyright (c) 2004-2008, Bogdan Valentin Ontanu.
; All rights reserved.
;------------------------------------------------------

;------------------------------------------------------------------
; A simple Hello World Win32 console application
;------------------------------------------------------------------

import_dll    kernel32.dll
   import_func   ExitProcess
   import_func   GetStdHandle
   import_func   WriteFile

;--------------
; sections
;--------------
section "code"       class_code
section "data"      class_data
section "idata"      class_imports


STD_INPUT_HANDLE   EQU   -10
STD_OUTPUT_HANDLE   EQU   -11



.data

   out_handle   dd   0
   bytes_wr   dd   0

   msg_txt      db   13,10," Hello World...",13,10,0
   msg_len      equ   $-msg_txt

.code
.entry Start

Start:
   invoke   GetStdHandle,STD_OUTPUT_HANDLE
   mov   [out_handle],eax

   invoke   WriteFile,[out_handle],msg_txt,msg_len,bytes_wr,0

   invoke   ExitProcess,0
   ret




2. How can I display the "Hello world!" text without importing functions from Windows. Otherwise said, without depending on this particular OS.

2-Bit Chip

#9
Quote from: MSDNBOOL WINAPI WriteFile(
  __in         HANDLE hFile,
  __in         LPCVOID lpBuffer,
  __in         DWORD nNumberOfBytesToWrite,
  __out_opt    LPDWORD lpNumberOfBytesWritten,
  __inout_opt  LPOVERLAPPED lpOverlapped
);

invoke WriteFile, out_handle, offset msg_txt, msg_len, offset bytes_wr, 0

dedndave

Quoteinvoke WriteFile, out_handle, msg_txt, msg_len, offset bytes_wr, 0
close

        INVOKE  WriteFile,
                out_handle,
                ADDR msg_txt,
                SIZEOF msg_txt,
                ADDR bytes_wr,
                NULL

when using WriteFile to display text, the string need not be 0 terminated, as the length is specified
notice that the ms variable names starting with "lp" are long pointers, i.e. the address or offset is needed, not the variable value
this link will help you read the documents...
http://msdn.microsoft.com/en-us/library/aa260976(VS.60).aspx

BogdanOntanu

Hi Walkingtheunwalked,


There are diferences in between MASM syntax and Sol_Asm syntax and you and the people that are wanting to help you will also have problems.

For example there is no need for "offset" keyword in Sol_Asm and:

mov [out_handle],eax

IS the correct syntax in Sol_asm

Again the invoke WriteFIle,... in your example is correct syntax for Sol_Asm but it is incorrect for MASM ...

Hence if you intend to ask questions here then it is much better to learn and use MASM syntax. For Sol_asm syntax questions I do recommend Sol_Asm's forums.

However while you learn you have a better chance here because I am known to be harsh and slow while people here are kind and fast to answer.

To answer your questions:

Question 1:
=========
In order to  output a number value instead of a string you will have to:
a) convert the value/number to a string with a procedure you write or by using a library function. Among many other such functions MASM32 has a dwtoa procedure in it's includes ... if I recall correctly.
b) Output the resulting string.

Hence you should study such procedures for converting a number to a string. You could also use windows's wsprintfA function but beware that this function is the only one that uses a C calling convention in Win32 API.

Question 2:
=========
Normally you can not. One way or another you always depend on the target OS.

This is the purpose of an OS existence: to provide a set of basic functions that applications can use in a way that is independent of the specific hardware architecture.

Hence Windows/Linus/Unix/etc OS do not allow you to write directly to the hardware. Windows does emulate a limited set of commands for compatibility with very old DOS applications.

In order to access hardware directly you can eventually:
a) attempt to write your own OS
b) use another OS that will allow you to access hardware directly (DOS, etc)
c) learn to write kernel mode drivers (complicated and anyway dependent of the target OS)
d) use an emulator that simulates some raw hardware configuration under Windows in order to learn the basics (limited)


If you write your own OS or use DOS/emulator you will anyway end up creating and using a set of common functions. 
Such a set is already provided by most operating systems API.

Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

BogdanOntanu

Quote from: 2-Bit Chip on August 19, 2009, 04:49:43 PM
mov [out_handle],eax

what the code above will do is: mov eax to where out_handle points to.


what should be there instead would be:
mov out_handle, eax
so now out_handles holds the handle to the output

No... you are wrong 2-Chip....


mov [out_handle],eax
and
mov out_handle,eax


Perform the exact same action in MASM. Both forms write the content of eax register to out_handle variable. Basically in MASM you can avoid using [] arround variables. This is IMHO confusing but it is a MASM / Intel syntax choice.

There is No need to debate if this choiche is good or bad ... because it can generate a flamewar very easy.

Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

ecube

brackets are useful in manipulating variables via registers take for example
.data?
myvar dd ?
.code
start:
invoke myfunc,addr myvar

myfunc proc retval:DWORD

;incorrect
mov retval,1

;correct
mov ecx,retval
mov [ecx],1

ret
myfunc endp



mitchi

If you are serious about becoming good at assembly programming, then we'll see you around I guess !
Learning all of this takes a lot of time, but good things come to those who work hard!