View Single Post
Old May 2nd, 2006, 11:47 AM   #3
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 3 Narue is on a distinguished road
>Can someone tell me about Assembly and/or Assembler?
Assembly is a language based on mnemonic names for machine instructions. It's as low as you can get without using a hex editor to manually write opcodes. An assembler is a program that translates the mnemonic names and other directives into machine code.

>What programs there can be used for it?
It depends on what you're doing. Some assemblers can output executable programs directly, so all you would need is the assembler and a debugger. A somewhat more flexible approach uses assemblers that output object files, in which case you need the assembler, a debugger, and a linker. OllyDbg is a good debugger, and I usually use the linker that comes with GCC.

>Some basic, for an example a "Hello World"-program?
Here's one in FASM that assembles right to an executable:
format PE console
entry start

include 'C:\fasmw\include\win32a.inc'

;======================================
section '.data' data readable writeable
;======================================

hello db 'Hello, world!',10,0

;=======================================
section '.code' code readable executable
;=======================================

start:
	cinvoke	printf,hello
	invoke	ExitProcess,0

;=====================================
section '.import' import data readable
;=====================================

library kernel,'kernel32.dll',\
	msvcrt,'msvcrt.dll'

import kernel,\
       ExitProcess,'ExitProcess'

import msvcrt,\
       printf,'printf'
There are no instructions in that, it's all directives and macros. So hello world only gives you a taste of how much framework an assembler requires, and how easy I/O is. Here's the same program in NASM, but it assembles to an object file and needs to be linked:
;==============
[section .data]
;==============

hello db 'Hello, world!',10,0

;==============
[section .text]
;==============

global _main
extern _printf

_main:
	push	hello
	call	_printf
	add	esp,4
	mov	eax,0
	ret
This is smaller because it relies on the linker to do the import stuff that FASM was doing. NASM also doesn't come with a convenient include file with macros, so I did manually what cinvoke was doing behind the scenes (the C calling convention: arguments are pushed in reverse order, and the stack is restored after the call). Also, instead of calling ExitProcess, this NASM program is a direct translation of the following C program (which is why eax is set to 0 before returning):
#include <stdio.h>

int main ( void )
{
  printf ( "Hello, world!\n" );
  return 0;
}
FASM can also output object files just like NASM:
format MS COFF

include 'C:\fasmw\include\win32a.inc'

;===================
section '.data' data
;===================

hello db 'Hello, world!',10,0

;===================
section '.code' code
;===================

public _main
extrn _printf

_main:
	cinvoke	_printf,hello
	mov	eax,0
	ret
>How I start?
Get an assembler, find a reference, and start writing programs. Just like any language. Of the assemblers out there, the most popular are MASM, NASM, and FASM. FASM is a good combination of flexibility and simplicity, but NASM is easier to learn in my opinion. MASM has too many inconsistencies to be a good first assembler, but it's powerful. You might also look into HLA, if you want to ease into assembly rather than jump right in.

>Is it a good thing to learn Assembly?
Yes.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote