Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,200,117 members, 7,973,753 topics. Date: Sunday, 13 October 2024 at 05:21 AM |
Nairaland Forum / Science/Technology / Programming / Assembly Tutorial : Part 1 (The Basics) (2939 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)
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
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
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
Now we proceed to compiling the code hence we have saved the program as helloworld.asm, we compile it as
Then we have the object file Then we type
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
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
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
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
Sub - Subtract two numbers
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 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 |
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: 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)
What Do You Hate Most In Programming / Please Help. How To Create A Database For A Car Rental / Need Help With MEAN Stack Technology? Get In Here
(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. 27 |