Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,722 members, 7,816,973 topics. Date: Friday, 03 May 2024 at 09:47 PM

Assembly Tutorial : Part 1 (The Basics) - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Assembly Tutorial : Part 1 (The Basics) (2883 Views)

Learn Programming/web Development/basics Of Networking All By Yourself..... / Can Someone Who Have Mastered The Basics Of Php And Can Apply It Be Called A Dev / How Do I Become A Programmer After Learning The Basics (2) (3) (4)

(1) (Reply) (Go Down)

Assembly Tutorial : Part 1 (The Basics) by Nobody: 11:22am On Aug 19, 2015
On board this long trip, got bored and other than talking to this pretty girl beside me, I decided to make a tutorial as I promised. Assembly Programming, as I am typing from my mobile. It's gonna be assembly on Nasm.

The Netwide Assembler (NASM) is an assembler and disassembler for the Intel x86 architecture. It can be used to write 16-bit, 32-bit (IA-32) and 64-bit (x86-64) programs. NASM is considered to be one of the most popular assemblers for Linux

NASM was originally written by Simon Tatham with assistance from Julian Hall and is currently maintained by a small team led by H. Peter Anvin It is available as free software under the terms of the simplified (2-clause) BSD license.

Features

NASM can output several binary formats including COFF, Portable Executable, a.out, ELF, Mach-O and .bin (binary disk image, used for compiling operating systems), though position-independent code is only supported for ELF object files. NASM also has its own binary format called RDOFF.

The variety of output formats allows programs to be retargeted to virtually any x86 operating system. In addition, NASM can create flat binary files, usable in writing boot loaders, ROM images, and in various facets of OS development.NASM can run on non-x86 platforms, such as SPARC and PowerPC, though it cannot generate programs usable by those machines.

NASM uses a variation of Intel assembly syntax instead of AT&T syntax. It also avoids features such as automatic generation of segment overrides (and the related ASSUME directive) used by MASM and compatible assemblers.

Since I would be talking about Windows, I would say we get some of these handy as we would not wonna joke this time, shall we...

Requirement for Coding NASM.
NASM compiler
GCC compiler ( if you have code::blocks, no need to worry)
Text editor ( notepad, Sublime, notepad++)
And a Brain.
Now let's begin.
A simple hello world in C/C++ would look like this

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
printf("Hello World\n" );
Messagebox(NULL,"Welcome to C++", "",MB_OK);
return 0;
}


Noted. This should pop out a message box, after the other hello world. The messagebox because I called a windows function, while the other one will be a dos command. DOS command is what we are gonna begin with, cos that should be the starting point first before we proceed to more complex programs like using WinApi functions like CreateWindowsEx() and the rest

Simple Code Examples
A simple NASM code in windows should look like this

section .text
org 0x100
mov ah, 0x9
mov dx, hello
int 0x21

mov ax, 0x4c00
int 0x21

section .data
hello: db 'Hello, world!', 13, 10, '$'


Now don't be scared. Apparently it seems like total gibberish, it's cool. section .data is used to hold input variables which we would call into section .text

Here I want to compose a very simple hello world program with NASM, but am gonna be using some C calling libraries/ commands for your viewing, to some people C is easy to some, it's not easy so hence we have some basic understanding of C, am gonna be using commands like printf() and scanf() here in NASM. Don't be surprised. It's just something that probably most of us hasn't worked on.
Take a look at this

section .data
Msg:
Msg1: db " welcome to nasm ",10,0;
Msg2: db " Nice to meet you" 10,0
section .text
global _main
extern _printf

_main:
push ebp
mov ebp,esp

push msg1
call _printf
pop eax

push msg2
call _printf
pop eax

pop epb
ret


Now we proceed to compiling the code hence we have saved the program as helloworld.asm, we compile it as


gcc -fwin32 helloworld.asm - helloworld.o


Then we have the object file
Then we type

a

The program should automatically show hello world on your screen.
Now let's try a more complex example. Let's try to code something to add 2 numbers together and print the results. We should be looking at a code like this

section .data
Msg:
msg1: db " Addition app in nasm", 10,0;
msg2: db "Result is =%d ",10,0
section .text
global _main
extern _printf

_main:
push ebp
mov ebp,esp

push msg1
call _printf

mov eax,5
mov ebx, 6
add eax,ebx

push msg2
call _printf

pop ecx
ret


smiley doesn't seem hard does it? The code just added two numbers together and out puts the results for viewing. Just compile the same way and you achieve your results.

ScanF() in nasm : IO in NASM
Having considered this, we proceed to _scanf in NASM. Scanf() as we know is used to get input and then use for a simple purpose, manipulation and the rest
Hey now let's consider using scanf() to get data and display on our screen.
Code should look like this

section .data
Msg:
msg1: db " What's your name?",10,0
msg2: db" Hello %s, Welcome to NASM",10,0
formin : db '%s', 0
formout : db '%s,10,0
name times 4 db 0;

section .text
global _main
extern _scanf
extern _printf

_main:
push ebp
mov ebp,esp

push msg1
call _printf

mov eax, name
push name
push formin
call _scanf

push msg2
call _printf

pop ecx
ret


Then you compile and it displays the results likewise.

I won't call this a conclusion, having seen this, let me go further to explain our syntaxes very well for basic understanding, in assembly we see things like ac,bx,cx,dx. Those are 16 bit registers used, but since we wouldn't be using 16 bit, we use something different here in nasm we should be using the 32 bit registers hence the difference, other than the ax,bx,cx,dx we should be using something EAX,EBX,ECX,EDX those are Registers for 32 bit assembly, Nasm uses 32 bit assembly, so when you see things like

mov eax,9

What I succeeded in doing is moving 9 into the eax register. Nothing to be scared about
Syntaxes for nasm
Mov - move data into a register, you move from a source to a destination.
Mov eax, 9
Means you move 9 into the register as suggested earlier
Add - Used to Add two numbers together

Add eax,ebx

Sub - Subtract two numbers

Sub eax,ecx

imul - multiplication, for multiplying two numbers together
idiv - division used in dividing two numbers in arithmetic.
Jmp - jump to a particular function call. I see some examples in c++ with inline assembly, where a lot of people do use call and jmp functions, hence I decided to show the meaning to most of us here.
Jnz - jump if not zero.
Hence I typed all from my blackberry wink but when I get home I should be typing again and explaining further on nasm with more complicated examples, but just to get attention of some reverse engineers and the rest on here. Hope I made a little tutorial. Would post more about the assembly tutorial series in part 2.

Good morning smiley
Re: Assembly Tutorial : Part 1 (The Basics) by engrrichie92(m): 1:11pm On Aug 26, 2015
*runs out of thread *
Re: Assembly Tutorial : Part 1 (The Basics) by Stconvict(m): 1:41pm On Aug 26, 2015
Wow! Nice Tutorial.
I tried assembly once, but left it because it wasn't meant for cross-platform deployment.
Different assembly langs for different architectures (x86, ARM, Power) due to different instruction sets. That's so not cool!
It's not all bad though. I doesn't hurt to learn. I'd love to read more of your tuts.
Re: Assembly Tutorial : Part 1 (The Basics) by Nobody: 7:01pm On Aug 26, 2015
Stconvict:
Wow! Nice Tutorial.
I tried assembly once, but left it because it wasn't meant for cross-platform deployment.
Different assembly langs for different architectures (x86, ARM, Power) due to different instruction sets. That's so not cool!
It's not all bad though. I doesn't hurt to learn. I'd love to read more of your tuts.

smiley thank you chief. Am working in the second one now. I paste in some days time.
Cross-platform? Nasm runs on linux and windows.

(1) (Reply)

Data Science Enthusiasts, Let's Meet Here, Talk And Share Ideas! / Indians Are Not Joking In Tech See Photo � / Software Developers, Most of Your Jobs are Going To India!

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 43
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.