Thursday, 16 April 2015

Multiplication of two numbers using Assembly Language

**************PROGRAM*********************
MULTIPLICATION OF TWO NUMBERS
*************************************************

section .data
m0 db 10,"MENU",10
   db "1)Multiply two numbers by successive addition",10
   db "2)Multiply two numbers by shift-add method",10
   db "3)Exit",10
   db "Enter your choice",10
l0 equ $-m0
m1 db "Enter first number:",10
l1 equ $-m1
m2 db "Enter second number:",10
l2 equ $-m2
m3 db "Answer is:",10
l3 equ $-m3

section .bss
choice resb 2
num resb 17
fnum1 resb 8
fnum2 resb 8
cnt resq 1
result resq 1
ctr resq 1
temp resb 8
A resb 8
Q resb 8
N resb 8
M resb 8

%macro write 2
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro
%macro read 2
mov rax,0
mov rdi,0
mov rsi,%1
mov rdx,%2
syscall
%endmacro
%macro exit 0
mov rax,60
mov rbx,0
syscall
%endmacro

section .text
global _start
_start:
write m0,l0
read choice,2
cmp byte[choice],31h
je mul1
cmp byte[choice],32h
je mul2
exit

mul1:
write m1,l1
read num,17
dec rax
mov qword[cnt],rax
call accept
mov qword[fnum1],rax

write m2,l2
read num,17
dec rax
mov qword[cnt],rax
call accept
mov qword[fnum2],rax

mov qword[result],00
again:
mov rax,qword[fnum1]
add qword[result],rax
dec qword[fnum2]
jnz again

write m3,l3
call display
exit

mul2:
write m1,l1
read num,17
dec rax
mov qword[cnt],rax
call accept
mov qword[fnum1],rax

write m2,l2
read num,17
dec rax
mov qword[cnt],rax
call accept
mov qword[fnum2],rax

mov qword[A],00
mov rax,qword[fnum1]
mov qword[Q],rax
mov rbx,qword[fnum2]
mov qword[M],rbx
mov qword[N],64

logic:
mov rax,qword[Q]
and rax,01h
cmp rax,01h
jne zero
mov rbx,00
mov rbx,qword[A]
add rbx,qword[M]
mov qword[A],rbx

zero:
mov rax,qword[A]
mov rbx,qword[Q]
shr rbx,01
and rax,01h
cmp rax,01h
jne label6
mov rdx,01
shl rdx,63
or rbx,rdx

label6:
mov rax,qword[A]
shr rax,01
mov qword[Q],rbx
mov qword[A],rax
dec qword[N]
cmp qword[N],00
jne logic

mov rax,qword[A]
mov qword[result],rax
call display

mov rax,qword[Q]
mov qword[result],rax
call display
exit

accept:
mov rax,00
mov rbx,00
mov rsi,num
oncemore:
shl rax,04
mov bl,byte[rsi]
cmp bl,39h
jle skip
sub bl,07h
skip:
sub bl,30h
add rax,rbx
inc rsi
dec qword[cnt]
jnz oncemore
ret

display:
mov rsi,result
add rsi,07h
mov qword[ctr],08

lab3:     mov al,byte[rsi]
        shr al,04
        cmp al,09h
        jle skip1
        add al,07h
skip1:  add al,30h
        mov byte[temp],al
        push rsi
        write temp,01
        pop rsi
        mov al,byte[rsi]
           and al,0Fh
        cmp al,09h
        jle skip2
        add al,07h
skip2:  add al,30h
        mov byte[temp],al
        push rsi
        write temp,01
        pop rsi
        dec rsi
        dec qword[ctr]
        jnz lab3
ret


******************OUTPUT********************
sourabh@ubuntu:~$ nasm -f elf64 mul.asm
sourabh@ubuntu:~$ ld -o mul mul.o
sourabh@ubuntu:~$ ./mul

MENU
1)Multiply two numbers by successive addition
2)Multiply two numbers by shift-add method
3)Exit
Enter your choice
1
Enter first number:
A
Enter second number:
A
Answer is:
0000000000000064

MENU
1)Multiply two numbers by successive addition
2)Multiply two numbers by shift-add method
3)Exit
Enter your choice
2
Enter first number:
A
Enter second number:
A
Answer is:
00000000000000000000000000000064

Wednesday, 15 April 2015

Digital to Analog Conversion using 8255 (Programmable Peripheral Interface)

**************PROGRAM*******************
 DIGITAL TO ANALOG CONVERSION
**********************************************
1) SQUARE WAVE-
mov al,89                                    ;configure 8255
out 67,al                                      ;command address for 8255 - 67H
mov al,01                                    ;configure DAC (start of conversion)
out 63,al                                      ;command address for DAC- 63H
mov al,00
l1: out 61,al
    call delay
    mov al,FF
    out 61,al
    call delay
    jmp l1

2) RAMP WAVE-
mov al,89
out 67,al
mov al,01
out 63,al
mov al,00
l1: out 61,al
     inc al
     cmp al,FF
     jmp l1

3) STAIRCASE WAVE-
mov al,89
out 67,al
mov al,01
out 63,al
mov al,00
l1: out 61,al
     call delay
     add al,51
     cmp al,FF
     jnz l1
l2: out 61,al
     call delay
     sub al,51
     cmp al,00
     jnz l2
     jmp l1

4) TRAPEZOIDAL WAVE-
mov al,89
out 67,al
mov al,01
out 63,al
mov al,00
l1: out 61,al
     inc al
     cmp al,FF
     jne l1
out 61,al
call delay
l2: out 61,al
     dec al
     cmp al,00
     jne l2
     call delay
     jmp l1




String Operations in Assembly Language

***********PROGRAM**********
STRING OPERATIONS
***********************************

section .data
m1 db 10,"MENU",10
   db "1)String Length",10
   db "2)String copy",10
   db "3)String Compare",10
   db "4)String reverse",10
   db "5)Palindrome",10
   db "6)exit",10
   db "Enter your choice:",10
l1 equ $-m1
m2 db "Enter string:",10
l2 equ $-m2
m3 db "Length of the string is:",10
l3 equ $-m3
m4 db "The copied String is:",10
l4 equ $-m4
m5 db "The Strings are equal",10
l5 equ $-m5
m6 db "The Strings are not equal",10
l6 equ $-m6
m7 db "The String is a palindrome",10
l7 equ $-m7
m8 db "The String is not a palindrome",10
l8 equ $-m8
m9 db "The Reversed String is:",10
l9 equ $-m9

section .bss
choice resb 3
s1 resb 40
s2 resb 40
cnt resq 1
ctr resq 1
temp resb 2

%macro write 2
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro
%macro read 2
mov rax,0
mov rdi,0
mov rsi,%1
mov rdx,%2
syscall
%endmacro
%macro exit 0
mov rax,60
mov rbx,0
syscall
%endmacro

section .text
global _start
_start:
loop:
write m1,l1
read choice,3
dec rax
cmp byte[choice],31h
je lenght
cmp byte[choice],32h
je copy
cmp byte[choice],33h
je compare
cmp byte[choice],34h
je reverse
cmp byte[choice],35h
je palin
exit

lenght:
write m2,l2
read s1,40
dec rax
mov qword[cnt],rax
mov rsi,cnt
mov qword[ctr],08h
add rsi,07h
push rsi
push rax
write m3,l3
pop rax
pop rsi
call display
call loop

copy:
write m2,l2
read s1,40
dec rax
mov qword[cnt],rax
mov qword[ctr],rax
mov rsi,s1
mov rdi,s2

again:
mov al,byte[rsi]
mov byte[rdi],al
inc rsi
inc rdi
dec qword[cnt]
cmp qword[cnt],00
jnz again
write m4,l4
write s2,qword[ctr]
call loop
exit

compare:
write m2,l2
read s1,40
dec rax
mov qword[cnt],rax
write m2,l2
read s2,40
mov qword[ctr],rax
mov rsi,s1
mov rdi,s2
mov rax,00
mov rbx,00
go:
add al,byte[rsi]
add bl,byte[rdi]
inc rsi
inc rdi
dec qword[ctr]
cmp qword[ctr],00
jnz go
cmp al,bl
je got
write m6,l6
call loop
got:
write m5,l5
call loop

reverse:
write m2,l2
read s1,40
dec rax
mov qword[cnt],rax
mov rsi,s1
mov rdi,s2
add rdi,qword[cnt]
dec rdi

rev:
mov al,byte[rsi]
mov byte[rdi],al
dec rdi
inc rsi
dec qword[cnt]
cmp qword[cnt],00
jne rev
write m9,l9
write s2,40
call loop

palin:
write m2,l2
read s1,40
dec rax
mov qword[cnt],rax
mov rsi,s1
mov rdi,s1
add rdi,qword[cnt]
dec rdi

pl:
mov al,byte[rsi]
cmp al,byte[rdi]
je same
jne diff
same:
inc rsi
dec rdi
cmp rsi,rdi
jge yep
jmp pl

yep:
write m7,l7
call loop

diff:
write m8,l8
call loop

display:
mov al,byte[rsi]
mov cl,04
shr al,cl
cmp al,09h
jle a1
add al,07h
a1: add al,30h
mov byte[temp],al
push rsi
write temp,01
pop rsi
mov al,byte[rsi]
mov cl,04
shl al,cl
shr al,cl
cmp al,09h
jle a2
add al,07h
a2: add al,30h
mov byte[temp],al
push rsi
write temp,01
pop rsi
dec rsi
dec byte[ctr]
jne display
ret

******************OUTPUT******************
sourabh@ubuntu~$ nasm -f elf64 str.asm
sourabh@ubuntu~$ ld -o str str.o
sourabh@ubuntu~$ ./str

MENU
1)String Length
2)String copy
3)String Compare
4)String reverse
5)Palindrome
6)exit
Enter your choice: 1
Enter string: string
Length of the string is: 0000000000000006

MENU
1)String Length
2)String copy
3)String Compare
4)String reverse
5)Palindrome
6)exit
Enter your choice: 2
Enter string: hello world
The copied String is: hello world

MENU
1)String Length
2)String copy
3)String Compare
4)String reverse
5)Palindrome
6)exit
Enter your choice: 3
Enter string: hello
Enter string: hello
The Strings are equal

MENU
1)String Length
2)String copy
3)String Compare
4)String reverse
5)Palindrome
6)exit
Enter your choice: 4
Enter string: hello
The Reversed String is: olleh

MENU
1)String Length
2)String copy
3)String Compare
4)String reverse
5)Palindrome
6)exit
Enter your choice: 5
Enter string: aabbaa
The String is a palindrome

Tuesday, 14 April 2015

String Concatenation & Substring in Assembly Language

*****************PROGRAM************************
STRING CONCATENATION & SUBSTRING
*******************************************************
PROGRAM 1:

global m2,l2,m3,l3,m4,l4,m5,l5,m6,l6,str1,str2,cnt1,cnt2
extern strcat,substr

%macro read 2
mov rax,0
mov rdi,0
mov rsi,%1
mov rdx,%2
syscall
%endmacro

%macro write 2
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro

%macro exit 0
mov rax,60
mov rdi,0
syscall
%endmacro

section .data
m1 db 10,"MENU",10
   db "1.String Concatenation",10
   db "2.Substring",10
   db "3.Exit",10
   db "Enter Your Choice: "
l1 equ $-m1
m2 db 10, "Enter First String: "
l2 equ $-m2
m3 db "Enter Second String: "
l3 equ $-m3
m4 db 10,"The New String is: "
l4 equ $-m4
m5 db 10,"It is a Substring"
l5 equ $-m5
m6 db 10,"It is not a Substring"
l6 equ $-m6

section .bss
choice resb 2
cnt1 resq 1
cnt2 resq 1
str1 resb 30
str2 resb 30

section .text
global _start
_start:
menu:    write m1,l1
    read choice,2
    cmp byte[choice],31h
    je ch1
    cmp byte[choice],32h
    je ch2
    cmp byte[choice],33h
    je ch3

ch1:call strcat
jmp menu

ch2: call substr
jmp menu

ch3:exit

PROGRAM 2:

global strcat,substr
extern m2,l2,m3,l3,m4,l4,m5,l5,m6,l6,str1,str2,cnt1,cnt2,ctr

%macro read 2
mov rax,0
mov rdi,0
mov rsi,%1
mov rdx,%2
syscall
%endmacro

%macro write 2
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro

section .data
section .bss
section .text
global _start:

strcat:    write m2,l2
    read str1,30
    dec rax
    mov qword[cnt1],rax
    write m3,l3
    read str2,30
    dec rax
    mov qword[cnt2],rax
    mov rcx,qword[cnt1]
    add qword[cnt2],rcx
    mov rsi,str1
    add rsi,qword[cnt1]
    mov rdi,str2
cpy1:    mov bl,byte[rdi]
    mov byte[rsi],bl
    inc rsi
    inc rdi
    dec rax
    jnz cpy1
    write m4,l4
    write str1,qword[cnt2]
        ret

substr:    write m2,l2
    read str1,30
    dec rax
    mov qword[cnt1],rax
    write m3,l3
    read str2,30
    dec rax
    mov qword[cnt2],rax
    mov rcx,rax
    mov rsi,str1
    mov rdi,str2
    mov rbx,00
logic:    mov al,byte[rsi]
    cmp al,byte[rdi]
    je same
    cmp rbx,02
    je up
    mov rdi,str2
    mov rcx,qword[cnt2]
    inc rbx
    jmp logic

up:    inc rsi
    dec qword[cnt1]
    jz nt
    jmp logic

same:    inc rsi
    inc rdi
    dec rcx
    jz st
    dec qword[cnt1]
    jz nt
    jmp logic

st:    write m5,l5
    ret

nt:    write m6,l6
    ret


*********************OUTPUT*********************

sourabh@ubuntu:~$ nasm -f elf64 prog1.asm
sourabh@ubuntu:~$ nasm -f elf64 prog2.asm
sourabh@ubuntu:~$ ld -o prog1.exe prog1.o prog2.o
sourabh@ubuntu:~$ ./prog1.exe

MENU
1.String Concatenation
2.Substring
3.Exit
Enter Your Choice: 1

Enter First String: GOOD
Enter Second String: MORNING

The New String is: GOODMORNING

MENU
1.String Concatenation
2.Substring
3.Exit
Enter Your Choice: 2

Enter First String: HELLO
Enter Second String: LOL

It is not a Substring

Floating point operations in Assembly Language

*************PROGRAM***************
FLOATING POINT OPERATIONS
******************************************

extern printf,scanf

section .data
m0 db 10,"FLOATING POINT OPERATIONS",0
m1 db "%s",0
m2 db 10,"Enter number:",0
m3 db "Number is:",0
m4 db "%lf",0
m5 db 10,"The Mean is:",0
m6 db 10,"The Variance is:",0
m7 db 10,"The Standard Deviation is:",0

section .bss
v1 resb 8
v2 resb 8
v3 resb 8
f1 resb 8
f2 resb 8
f3 resb 8
c resb 2
mean resb 8
var resb 8
dev resb 8

%macro print 2
mov rax,0
mov rdi,%1
mov rsi,%2
call printf
%endmacro

%macro printfloat 2
push rbp
mov rax,1
mov rdi,%1
movsd xmm0,%2
call printf
pop rbp
%endmacro

%macro scan 2
mov rax,0
mov rdi,%1
mov rsi,%2
call scanf
%endmacro

section .text
global main
main:
print m1,m0
print m1,m2
scan m4,v1
print m1,m3
printfloat m4,[v1]

print m1,m2
scan m4,v2
print m1,m3
printfloat m4,[v2]

print m1,m2
scan m4,v3
print m1,m3
printfloat m4,[v3]

finit
fld qword[v1]
fadd qword[v2]
fadd qword[v3]
mov byte[c],03

fidiv word[c]
fstp qword[mean]
print m1,m5
printfloat m4,[mean]

finit
fld qword[v1]
fld qword[v2]
fld qword[v3]
fsub qword[mean]
fmul st0
fstp qword[f1]
fsub qword[mean]
fmul st0
fstp qword[f2]
fsub qword[mean]
fmul st0
fstp qword[f3]
fld qword[f1]
fadd qword[f2]
fadd qword[f3]
fidiv word[c]
fstp qword[var]
print m1,m6
printfloat m4,[var]

finit
fld qword[var]
fsqrt
fstp qword[dev]
print m1,m7
printfloat m4,[dev]

**************OUTPUT****************
sourabh@Ubuntu:~ nasm -f elf64 float.asm
sourabh@Ubuntu:~ gcc float.o
sourabh@Ubuntu:~ ./a.out

FLOATING POINT OPERATIONS
Enter number:10
Number is:10.000000
Enter number:20
Number is:20.000000
Enter number:30
Number is:30.000000
The Mean is:20.000000
The Variance is:66.666667
The Standard Deviation is:8.164966 :D