Tuesday, January 24, 2012

Discovering assembly a tale of a new commer - first

So I recently decided to start learning assembly following major issues with using a llvm, gcc or open64 as a back end to a compiler.
Okay this is a big task I know writing a front end and multiple back ends e.g. Windows 32bit, Windows 64bit, Linux 32bit and Linux 64bit.
But I've decided that I've got no choice but to use it in some form.


The majority of my posts for this topic will be about learning assembly.


Environment:


I am using Windows 7 64bit as my operating system along side a cygwin environment where cygwin\bin is on my path variable.
Notable software I'm using is Console2, nasm via cygwin and gcc via cygwin. Things like bash should be installed be default by cygwin.
Recommended either notepad++ or nano via cygwin.
The only reason you should use notepad++ is if your not keen on cli.
If you do use notepad++ remember to use UNIX EOL for your files (edit->EOL Conversion->UNIX Format).

You need to configure Console2 to work with bash.
To do so edit->settings:
shell \bin\bash.exe
startup dir \home\user
If on linux it should be far simpler with setting up environment.
Using package manager just install gcc and nasm.
For Ubuntu or Debian based systems
sudo apt-get install gcc nasm




You can use gedit instead of Notepad++ if you wish.
That should cover everything but that's just in theory.

Now your probably thinking gimme the c0d3z not a lecture!
Okay then here is some code to get you started:

Assumed top level directory ~/projects/asm/blog_first
For the shell script which will build it
build.sh
Remember in linux to chmod +x build.sh before you run it!
#!/bin/bash

# Cleans up current directory and removes all old binaries

rm -rf objects

mkdir objects

cd source
# First we create an library file from start.asm using nasm for linux or cygwin (Windows) environments.
nasm -f elf start.asm

# Moves to newly compiled binaries to the object directory which in turns cleans up the source directory.

mv start.o ../objects/

cd ../objects

# Links the asm file to an executable

ld -s -o blog_first start.o

# Run this newly created binary!

./blog_first
Now on to the good stuff (te actual ASM)

source/start.asm
segment .text
global _start
_start:

File structure:

\build.sh
\sources\start.asm

Okay so it does nothing..
But hey if there is no output means the environment is setup!

I hope this helps a little bit for somebody starting out new like me.




No comments:

Post a Comment