Sunday, April 3, 2016

POST 1 : Beginning with ALP

Hello, Everyone!
My Name is Akshay Patel and in this blog I am going to tell you about Assembly Language Programming.
I am going to use NASM assembler in Linux(Ubuntu) Operating System.
I will be using 64-bit configuration.

So let's begin with Assembly Language Programming

So to create first ALP(Assembly Language Program):
1. open terminal
2. Type :
gedit <program_name>.asm
e.g. my_ALP.asm
Gedit will open the file where we will type our Code.
3.To compile the file type  :
nasm -f elf64  <program_name>.asm
eg. nasm -f elf 64 my_ALP.asm
4.Now we need to link our assembly files to create an executeable:
ld -o <executable_name> <objectfile_name>.o
e.g. ld -o my_executable my_ALP.o
5.To run our code type:
./<executable_name>
e.g. ./my_executable
================================================
In gedit <program_name>.asm
To begin with,"gedit" is the text editor to type the code. The extension for assembly language programs is .asm
================================================
In nasm -f elf64  <program_name>.asm

"nasm" is the name of the assembler which we are using.
There are other assemblers like TASM,YASM,MASM
The "-f elf64" option selects a 64-bit output format which is compatible
with Linux and GCC.
NASM genrates object file names <program_name>.o . It contains instructions and data in a form ready to link with
other code from other object files or libraries.
================================================
In ld -o <executable_name> <objectfile_name>.o
ld is used to link all the object files together and generate a single executable.

The -o <executable_name> option gives a name to the executable file produced by
ld.
Without that option, ld produces a file named a.out .
================================================



So, this is how we create an ALP file and create an executable from it.

Saturday, April 2, 2016

POST 2: HEXADECIMAL to ASCII conversion


Hello everyone!
In this blog, I will be discussing HEXADICMAL to ASCII conversion method.
To begin with, I will first explain method for conversion of 1 single byte
Then moving further, to convert a word.
Then you can try on your own for double word,Quad word.
There are two implementation methods for HEXADICMAL to ASCII

Question 1:
Why do we need HEXADICMAL to ASCII conversion?

For printing purposes, ASCII values are for each character. So, to display any character on screen we need to pass its ASCII equivalent to standard output file. This is the simplest way to explain the need for conversion from HEXADICMAL to ASCII.

Code for conversion of byte:
For Byte conversion click here


Code for conversion of word:

For Word conversion click here



For a quick refrence:
ASCII TABLE
INTEL REFERENCE MANUAL
For Working of XLAT instruction (only for method 2) click here or read 4-591 of above link.


Here, given flow chart assumes only a sinlge digit Hexadecimal value to be converted.
The same can be used for any length of number to be converted.





METHOD 1:
Algorithm:
1.Take the MSB of byte
2.Using XLAT get equivalent ASCII value for MSB (MSB will act as offset for reference table)
3.Store converted value
4.Take the LSB of byte
5.Using XLAT get equivalent ASCII value for LSB (LSB will act as offset for reference table)
6.Store converted value



By using XLAT, refernce table actullay stores the ASCII equivalent of each digit (its respective index) because refernce table stores string values i.e ASCII


If there is any wrong information posted in this blog, please infrom me.
Your feedbacks will be very helpful for me.

Friday, April 1, 2016

POST 3:ASCII to HEXADECIMAL CONVERSION

Hello everyone!
In this blog I will be discussing about conversion of ASCII values to HEXADECIMAL value.

Why do we need this conversion?
The characters that we give as input from the keyboard are read in the format of ASCII system.
For performing various operations like addition,multiplication,division and subtraction we need to use HEXADECIMAL equivalent of those numbers.

Consider  addition of  2 and 3.
ASCII value of 2 is 32h and that of 3 is 33h.
So, if we add these two numbers without conversion, we get 32h+ 33h =65h.
But ASCII equivalent of our expected answer 2+3=5 is 35h.
As you can see this gives us wrong answer.

Now if we convert these numbers before any operation we will get correct answer.
e.g.
2+ 3
HEXADECIMAL equivalent of 2 is 02h and that of 3 is 03h.
After addition, 02h + 03h = 05h we get correct answer.
And to print the answer we convert i back to ASCII value i.e. 35h.

Here is the routine for BYTE conversion of ASCII to HEX:


ASCII_HEX:
xor bl,bl ;bl will store the result. So,initializing rbx with zero
mov byte[counter],2 ;counter = number of characters to convert
loop: ;loop label
mov al,byte[rsi] ;Take the byte of value to be converted
cmp al,39h ;Refer ASCII table. This is needed for deciding value of character between (0 to 9) or (A to F)
jbe sub_30 ;If character<39 it is between 0 to 9. So,subtract only 30 from it
sub al,0x07 ;If character>39 it is between A to F. So,subtract only 7 + 30 from it
sub_30:
sub al,0x30 ;Now al contains converted value read from rsi
rol bl,4 ;Rotation is needed for storing new converted value which is of 4 bits
add bl,al ;Store in bl
inc rsi ;Point to next character
dec byte[counter] ;Decrement Counter
jnz loop ;If Counter!=0 jump to loop label
mov byte[rdi],bl ;Store result in destination
RET ;Return


Here, each character occupies one byte in its ASCII format. After conversion,it takes 4 bits to represent it in HEX format (0 to F)


The main logic that might be difficult to understand is comparison with 39 and the subtraction of 30 and 07. For this refer to ASCII table.
Here comparison with 39 decides whether number is between 0 to 9 or A to F.


CLICK HERE FOR BYTE CONVERSION

CLICK HERE FOR WORD CONVERSION