Labels

Monday 7 November 2016

Write 8087 ALP to obtain: i) Mean ii) Variance iii) Standard Deviation For a given set of data elements defined in data segment. Also display result.

section .data
arr dd 140.60,222.40,313.40,422.30,521.02
mean_msg db "The Mean is: "
len_mean equ $-mean_msg

varmsg db "The Variance is: "
lenvarmsg equ $-varmsg

sdmsg db "The Standard Deviation is: "
lensdmsg equ $-sdmsg

NL db 10
divv dw 5
num dw 10000
zero dd 0.0

section .bss
mean resd 1
buff rest 1
disbuff resb 21
var resw 1


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


section .text
global _start
_start:

;initializing FPU
finit

;****************MEAN**********************;

;Loading zero in stack top
fldz

mov ch,05
xor rsi,rsi
mov rdx,arr
up:
fadd dword [rdx+rsi]
add rsi,04
dec ch
jnz up

fidiv word [divv] ;fdiv given
fst dword [mean] ;why dword taken??It could be ten bytes long

disp mean_msg,len_mean
call display
disp NL,1

;***********VARIANCE************************;
fldz
;fst st1

mov ch,05
xor rsi,rsi
mov rdx,arr
up1:
fld dword [rdx+rsi]
fsub dword [mean]

fmul st0,st0
fadd st0,st1 ;fadd st1,st0------->THIS WAS SO TRUE

fst st1
add rsi,04
dec ch
jnz up1

fld st1

fidiv word [divv]
fst dword [var]

disp varmsg,lenvarmsg
call display
disp NL,1

;*************SD****************************;

fld dword [var]
fsqrt

disp sdmsg,lensdmsg
call display
disp NL,1

;*********Termination***********************;

mov rax,3Ch
mov rdi,00
syscall

display:
fimul word [num] ;caused the main issue
fbstp tword [buff]

mov rax,[buff+2]
mov rsi,disbuff
mov ch,16
mov cl,04
agn:
rol rax,cl
mov bl,al
and bl,0Fh
add bl,30h
mov [rsi],bl
inc rsi
dec ch
jnz agn

mov byte [rsi],'.'
inc rsi
mov ax,[buff]
mov ch,04
agn1:
rol ax,cl
mov bl,al
and bl,0FH
add bl,30H
mov [rsi],bl
inc rsi
dec ch
jnz agn1

mov rax,01
mov rdi,01
mov rsi,disbuff
mov rdx,21
syscall
ret



output:




[gaurav@localhost ~]$ nasm -f elf64 8087.asm -o 8087.o
[gaurav@localhost ~]$ ld -o 8087 8087.o
[gaurav@localhost ~]$ ./8087
The Mean is: 0000000000000323.9440
The Variance is: 0000000000018510.0478
The Standard Deviation is: 0000000000000136.0516
[gaurav@localhost ~]$

Write X86/64 ALP to perform multiplication of two 8-bit hexadecimal numbers. Use successive addition and add and shift method. Accept input from the user. (use of 64-bit registers is expected)

Program for multiplication of two 8 bit numbers by successive addition

section .data
msg1 db "enter first 8 bit hex no:",10
len1: equ $-msg1
msg2 db "enter second 8 bit hex no:",10
len2: equ $-msg2
msg3 db "Multiplication of two hex no is:",10
len3: equ $-msg3

section .bss

arr1 resb 3
arr2 resb 3
arr3 resb 4

%macro disp 2  
mov rax,01h
mov rdi,01h
mov rsi,%1
mov rdx,%2
syscall
%endmacro

%macro inn 2
mov rax,00h
mov rdi,00h
mov rsi,%1
mov rdx,%2
syscall
%endmacro

section .text
global _start
_start:

disp msg1,len1
inn arr1,03

disp msg2,len2
inn arr2,03

mov rsi,arr1
mov cl,04
xor bx,bx
mov ch,02
up:
cmp byte[rsi],39h
jng sk
sub byte[rsi],07h
sk:
sub byte[rsi],30h
shl bx,cl
add  bl,[rsi]
inc rsi
dec ch
jnz up

xor dx,dx
mov rsi,arr2
mov cl,04
mov ch,02
up1:
cmp byte[rsi],39h
jng sk1
sub byte[rsi],07h
sk1:
sub byte[rsi],30h
shl dx,cl
add dl,[rsi]

inc rsi
dec ch
jnz up1

xor ax,ax
xor cl,cl
cmp dl,bl
jng sph
mov cl,bl
mov bl,dl
jmp outt
sph:
mov cl,dl
outt:
add ax,bx
dec cl
jnz outt


mov rsi,arr3
mov ch,04
mov cl,04
again1:
rol ax,cl
mov bl,al
and bl,0fh
cmp bl,09h
jng skip2
add bl,07h
skip2:
add bl,30h
mov [rsi],bl
inc rsi
dec ch
jnz again1

disp arr3,04

mov rax,3ch
mov rdi,00
syscall

Program for multiplication of two 8 bit numbers using add and shift method

section .data
msg1 db "enter first 8 bit hex no:",10
len1: equ $-msg1
msg2 db "enter second 8 bit hex no:",10
len2: equ $-msg2
msg3 db "Multiplication of two hex no is:",10
len3: equ $-msg3

section .bss
arr1 resb 3
arr2 resb 3
arr3 resb 4

%macro disp 2
mov rax,01h
mov rdi,01h
mov rsi,%1
mov rdx,%2
syscall
%endmacro

%macro inn 2
mov rax,00h
mov rdi,00h
mov rsi,%1
mov rdx,%2
syscall
%endmacro

section .text
global _start
_start:

disp msg1,len1
inn arr1,03

disp msg2,len2
inn arr2,03

mov rsi,arr1
mov cl,04
xor bx,bx
mov ch,02
up:
cmp byte[rsi],39h
jng sk
sub byte[rsi],07h
sk:
sub byte[rsi],30h
shl bx,cl
add  bl,[rsi]
inc rsi
dec ch
jnz up

xor dx,dx
mov rsi,arr2
mov cl,04
mov ch,02
up1:
cmp byte[rsi],39h
jng sk1
sub byte[rsi],07h
sk1:
sub byte[rsi],30h
shl dx,cl
add dl,[rsi]

inc rsi
dec ch
jnz up1




xor ax,ax
mov cl,00h
mov ch,08h
again:
shr dl,01
jnc xx
shl bx,cl
add ax,bx
shr bx,cl
xx:
inc cl
dec ch
jnz again


mov rsi,arr3
mov ch,04
mov cl,04
again1:
rol ax,cl
mov bl,al
and bl,0fh
cmp bl,09h
jng skip2
add bl,07h
skip2:
add bl,30h
mov [rsi],bl
inc rsi
dec ch
jnz again1

disp arr3,04

mov rax,3ch
mov rdi,00
syscall


Write 8086 ALP to perform string manipulation. The strings to be accepted from the user is to be stored in data segment of program_l and write FAR PROCEDURES in code segment program_2 for following operations on the string.

Write 8086 ALP to perform string manipulation. The strings to be accepted from the user is to be stored in
data segment of program_l and write FAR PROCEDURES in code segment program_2 for following
operations on the string:
(a) Concatenation of two strings (b) Number of occurrences of a sub-string in the given string Use PUBLIC
and EXTERN directive. Create .OBJ files of both the modules and link them to create an EXE file.


  • str1.asm


.model medium
.data
acc1 db 10,13,'enter string1:  $'
acc2 db 10,13,'enter string2:  $'
acc3 db 10,13,'enter substring:  $'
con db 10,13,'concatenated string:  $'
same db 10,13,'strings are same $'
diff db 10,13,'strings are different $'
sbstr db 10,13,'No. of SubString Occurances : $'
str1 db 15,?,15 dup('$')
str2 db 15,?,15 dup('$')
str3 db 31 dup('$')
str4 db 15,00,15 dup('$')
num db 00h

.code
        disp macro msg
        lea dx,msg
        mov ah,09h
        int 21h
        endm

        gets macro str
        lea dx,str
        mov ah,0ah
        int 21h
        endm

start:  public str1
        public str2
        public str3
        public str4
        public num
        public same
        public diff
        extrn concat:far
        extrn compare:far
        extrn substring:far

        mov ax,@data
        mov ds,ax
        mov es,ax
        xor ax,ax

             
        disp acc1
        gets str1

        disp acc2
        gets str2
        disp con
        call far ptr concat

        call far ptr compare

        disp acc3
        gets str4

        disp sbstr
        call far ptr substring


        mov ah,4ch
        int 21h
        end start
        ends
        end



  • str2.asm

.model tiny
.code
        disp macro msg1
        lea dx,msg1
        mov ah,09h
        int 21h
        endm
start:  public concat
        public substring
        public compare
        extrn str1:byte
        extrn str2:byte
        extrn str3:byte
        extrn str4:byte
        extrn num:byte
        extrn same:byte
        extrn diff:byte

concat proc far
        mov cl,str1+1
        mov ch,00h
        lea si,str1+2
        lea di,str3
        cld
        rep movsb
        mov cl,str2+1
        mov ch,00h
        lea si,str2+2
        cld
        rep movsb

        disp str3
        ret
concat endp

compare proc far
        lea si,str1+2
        lea di,str2+2
        mov cl,str1+1
        cmp cl,str2+1
        jne notsame
        mov ch,00h
        cld
        rep cmpsb
        jnz notsame
        lea dx,same
        mov ah,09h
        int 21h
        jmp exit_cmp

notsame:disp diff
exit_cmp:ret
compare endp

substring proc far
        mov num,00h
        lea si,str3
        mov dl,str1+1
        add dl,str2+1
        mov dh,00h
reinit: mov cl,str4+1
        mov ch,00h
        lea di,str4+2
        cld
nxtchar:cmpsb
        jne mismatch
        cmp cx,01h
        jne skip
        inc num

skip:   dec dx
        jz exitsbstr
        dec cx
        jnz nxtchar
        jmp reinit

mismatch:
        dec si
        lea di,str4+2
        mov cl,str4+1
        mov ch,00h
        dec dx
        jz exitsbstr
        cmpsb
        jne reinit
        dec cx
        jmp nxtchar

exitsbstr:
        mov bl,num
        call print

        ret
substring endp

print proc near
        mov ch,02h
        mov cl,04h
again:  rol bl,cl
        mov dl,bl
        and dl,0fh
        cmp dl,09h
        jbe skip1
        add dl,07h
skip1:  add dl,30h
        mov ah,02h
        int 21h
        dec ch
        jnz again
        ret
print endp


end

Write X86/64 ALP for the following operations on the string entered by the user. (use of 64-bit registers is expected).

Write X86/64 ALP for the following operations on the string entered by the user. (use of 64-bit registers is
expected)
a) Calculate Length of the string b) Reverse the string
c) Check whether the string is palindrome
OR
Make your program user friendly by providing MENU like:
(a) Enter the string b) Calculate length of string c) Reverse string d) Check palindrome e) Exit
Display appropriate messages to prompt the user while accepting the input and displaying the result.

section .data
msg1 db "welcome to string operations",10
len1: equ $-msg1

msg2 db "Enter your choice",10
len2: equ $-msg2

msg3 db "1.Input a string",10,"2.calculate length of string",10,"3.Reverse input string",10,"4.to check string is palindrom ",10,"5.exit",10
len3: equ $-msg3

def db 10,"=================================================",10
ledef: equ $-def

msgask db 10,"Do you want to continue?(press 1 for yes)",10
lenask: equ $-msgask

inp db "Enter the string",10
inplen: equ $-inp

msg4 db "Length of string is:",10
len4: equ $-msg4

msg5 db "Reverce string is:",10
len5: equ $-msg5

msg6 db "Given string is palindrom",10
len6: equ $-msg6

msg7 db "Given string is not palindrom",10
len7: equ $-msg7

msglast db"Thank You for using string operation",10
lenlast: equ $-msglast


section .bss

n resb 2
ask resb 2
len resb 3
leng resb 1
arr1 resb 20
arr2 resb 20
arr3 resb 20

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

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


section .text
global _start
_start:

disp msg1,len1
again:
disp def,ledef
disp msg2,len2
disp msg3,len3
inn n,02

cmp byte[n],31h
jne op2
call procinp
disp msgask,lenask
inn ask,02
mov rsi,ask
cmp byte[rsi],31H
jz again

op2:
cmp byte[n],32h
jne op3
call procal
disp msgask,lenask
inn ask,02
mov rsi,ask
cmp byte[rsi],31H
jz again

op3:
cmp byte[n],33h
jne op4
call procrev
disp msg5,len5
mov rax,01
mov rdi,01
mov rsi,arr2
xor rdx,rdx
mov dl,[leng]
syscall
disp msgask,lenask
inn ask,02
mov rsi,ask
cmp byte[rsi],31H
jz again

op4:
cmp byte[n],34h
jne op5
call procrev
call procpalin
disp msgask,lenask
inn ask,02
mov rsi,ask
cmp byte[rsi],31H
jz again

op5:
disp msglast,lenlast
mov rax,3ch
mov rdi,00
syscall


procinp:
disp inp,inplen
inn arr1,20
mov rsi,len
mov cl,04
mov ch,02
dec al
mov [leng],al
s1:
rol al,cl
mov bl,al
and bl,0fH
cmp bl,09h
jng skip1
add bl,07h
skip1:
add bl,30h
mov [rsi],bl
inc rsi
dec ch
jnz s1
mov byte[rsi],'H'
ret

procal:
disp msg4,len4
disp len,03
ret

procrev:
mov ch,[leng]
mov rsi,arr1
mov rdi,arr2-1
xor rbx,rbx
mov bl,[leng]
add rdi,rbx

up3:
mov al,[rsi]
mov [rdi],al
inc rsi
dec rdi
dec ch
jnz up3
ret


procpalin:

xor rcx,rcx

mov rsi,arr1
mov rdi,arr2
cld
mov cl,[leng]
repe cmpsb
je y
disp msg6,len6
jmp x
y:
disp msg7,len7

x:
ret



Write 64 bit ALP to convert 4-digit Hex number into its equivalent BCD number and 5-digit BCD number into its equivalent HEX number. Make your program user friendly to accept the choice from user for: (a) HEX to BCD b) BCD to HEX (c) EXIT. Display proper strings to prompt the user while accepting the input and displaying the result. (use of 64-bit registers is expected)

section .data
msg db "welcome to code conversion",10
len: equ $-msg

note db 10,"enter your choice",10,"1.hexadecimal to bcd",10,"2.bcd to hexadecimal",10,"3.exit",10
notlen: equ $-note

hexmsg db "enter 16 bit hexadecimal number",10
hexlen: equ $-hexmsg

msg1 db "BCD no for given hexadecimal no is:",10
len1: equ $-msg1

msg2 db "Enter BCD no to convert in hexadecimal no(less than 65535)",10
len2: equ $-msg2

msg3 db "hexadecimal no for given BCD no is:",10
len3: equ $-msg3

msg4 db 13,"Thank you for using code conversion.",10
len4: equ $-msg4

section .bss

n resb 2
hex resb 5
bcd resb 6


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

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


section .text
global _start
_start:

disp msg,len

again:
disp note,notlen
inn n,02

cmp byte[n],31h
jne BCD

disp hexmsg,hexlen
inn hex,05

xor rax,rax
mov rsi,hex
mov cl,04
mov ch,04
up:
cmp byte[rsi],39h
jng skip1
sub byte[rsi],07h
skip1:
sub byte[rsi],30h
shl rax,cl
add al,[rsi]
inc rsi
dec ch
jnz up

xor ebx,ebx
mov bl,0Ah
xor edx,edx
mov rsi,bcd
add rsi,04

up1:

div ebx
mov [rsi],dl
xor edx,edx
dec rsi
cmp eax,00
jne up1

mov rsi,bcd
mov cl,05

up2:
add byte[rsi],30h
inc rsi
dec cl
jnz up2

disp msg1,len1
disp bcd,05
jmp again

BCD:
cmp byte[n],32h
jne exit

disp msg2,len2
inn bcd,06

xor rax,rax

mov rsi,bcd

mov ch,05

uphex:
sub byte[rsi],30h
inc rsi
dec ch
jnz uphex

mov cl,05h
mov rsi,bcd

j1:


add dx,0Ah
mul dx
xor bx,bx
mov bl,[rsi]
add al,bl

inc rsi
dec cl
jnz j1

mov rsi,hex
add rsi,03h
mov ch,04
mov cl,04
as:
mov bl,al
and bl,0fh

cmp bl,09h
jng skip6
add bl,07h
skip6:
add bl,30h
mov [rsi],bl
shr rax,cl
dec esi
dec ch
jnz as

disp msg3,len3
disp hex,04
jmp again

exit:
cmp byte[n],33h
je sn
sn:
disp msg4,len4
mov rax,3ch
mov rdi,00
syscall


Write X86/64 ALP to perform non-overlapped and overlapped block transfer (with and without string specific instructions). Block containing data can be defined in the data segment.

Without Using String Specific Instructions
section .data
msg db "enter an offset",10
len: equ $-msg
arr1 db "se computer",10,0,0,0,0
len1: equ $-arr1

section  .bss

n resb 2

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

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

section .text

global  _start
_start:

disp msg,len
inn n,2

cmp byte[n],39h
jng skip
sub byte[n],07h
skip:
sub byte[n],30h

mov rsi,arr1+len1-1   ;seting pointer rsi & rdi to respective locations
mov rdi,rsi
mov cl,len1
xor rax,rax
mov al,[n]
add rdi,rax

up:
mov al,[rsi]
mov [rdi],al
dec rdi
dec rsi
dec cl
jnz up

disp arr1,len1+len1-1

mov rax,3ch
mov rdi,00
syscall

Using String Specific Instructions

section .data
msg db "enter an offset",10
len: equ $-msg
arr1 db "se computer",10,0,0,0,0
len1: equ $-arr1


section  .bss
n resb 2

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

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

section .text

global  _start
_start:

disp msg,len
inn n,2

cmp byte[n],39h
jng skip
sub byte[n],07h
skip:
sub byte[n],30h

mov rsi,arr1+len1-1   ;seting pointer rsi & rdi to respective locations
mov rdi,rsi
mov rcx,len1
xor rax,rax
mov al,[n]
add rdi,rax

std
rep movsb

disp arr1,len1+len1-1

mov rax,3ch
mov rdi,00
syscall

Write X86/64 Assembly language program (ALP) to add array of N hexadecimal numbers stored in the memory. Accept input from the user.

section .data
msg db "Enter how many numbers you want to enter",10
len: equ $-msg
msg1 db "Enter the 64-bit number",10
len1: equ $-msg1

section .bss
var1 resb 2
var2 resb 17
var3 resq 1
mem resb 1
section .text
global _start
_start:
mov rax,01h
mov rdi,01h
mov rsi,msg
mov rdx,len
syscall

mov rax,00h
mov rdi,00h
mov rsi,var1
mov rdx,2
syscall

cmp byte [var1],39h
jng z
sub byte [var1],07h
z:
sub byte [var1],30h

up:
mov rax,01h
mov rdi,01h
mov rsi,msg1
mov rdx,len1
syscall

mov rax,00h
mov rdi,00h
mov rsi,var2
mov rdx,17
syscall

dec al

mov rsi,var2
mov ch,al
mov cl,04

xor rbx,rbx
Y:
shl rbx,cl
mov al,[rsi]
cmp al,39h
jng X
sub al,07h
X:
sub al,30h

add bl,al
inc rsi
dec ch
jnz Y

add [var3],rbx
dec byte [var1]
jnz up

xor rbx,rbx
mov rbx,[var3]
mov rsi,var2
mov ch,16
mov cl,04h
up1:
rol rbx,cl
mov al,bl
and al,0fh
cmp al,09h
jng sk
add al,07h
sk:add al,30h
mov [rsi],al
inc rsi
dec ch
jnz up1

mov rax,01h
mov rdi,01h
mov rsi,var2
mov rdx,16
uppp:
cmp byte[rsi],30h
jne xxx
inc rsi
dec rdx
jnz uppp



xxx:
syscall

mov rax,3Ch
mov rdi,00
syscall

Sunday 6 November 2016

Compiler Design Lecture 23

Compiler Design Lecture 23

Compiler Design Lecture 22


                                                         Compiler Design Lecture 22

Compiler Design Lecture 21


Compiler Design Lecture 21

Compiler Design Lecture 20 Prof. Ravindrababu Ravula


Compiler Design Lecture 20

Prof. Ravindrababu Ravula

Compiler Design Lecture 19 Prof. Ravindrababu Ravula


Compiler Design Lecture 19

Prof. Ravindrababu Ravula

Compiler Design Lecture 18 Prof. Ravindrababu Ravula


Compiler Design Lecture 18

Prof. Ravindrababu Ravula

Compiler Design Lecture 17 Prof. Ravindrababu Ravula


Compiler Design Lecture 17

Prof. Ravindrababu Ravula

Compiler Design Lecture 16 Prof. Ravindrababu Ravula


Compiler Design Lecture 16

Prof. Ravindrababu Ravula

Compiler Design Lecture 15 Prof. Ravindrababu Ravula


Compiler Design Lecture 15

Prof. Ravindrababu Ravula

Compiler Design Lecture 14 Prof. Ravindrababu Ravula


Compiler Design Lecture 14

Prof. Ravindrababu Ravula

Compiler Design Lecture 13 Prof. Ravindrababu Ravula


Compiler Design Lecture 13

Prof. Ravindrababu Ravula

Compiler Design Lecture 12 Prof. Ravindrababu Ravula


Compiler Design Lecture 12

Prof. Ravindrababu Ravula

Compiler Design Lecture 11 Prof. Ravindrababu Ravula


Compiler Design Lecture 11

Prof. Ravindrababu Ravula

Compiler Design Lecture 10 Prof. Ravindrababu Ravula


Compiler Design Lecture 10

Prof. Ravindrababu Ravula

Compiler Design Lecture 9 Prof. Ravindrababu Ravula


Compiler Design Lecture 9

Prof. Ravindrababu Ravula

Compiler Design Lecture 8 Prof. Ravindrababu Ravula


Compiler Design Lecture 8

Prof. Ravindrababu Ravula

Compiler Design Lecture 7 Prof. Ravindrababu Ravula


Compiler Design Lecture 7

Prof. Ravindrababu Ravula

Compiler Design Lecture 6 Prof. Ravindrababu Ravula



Compiler Design Lecture 6

Prof. Ravindrababu Ravula

Compiler Design Lecture 5 Prof. Ravindrababu Ravula


Compiler Design Lecture 5

Prof. Ravindrababu Ravula

Compiler Design Lecture 4 Prof. Ravindrababu Ravula


Compiler Design Lecture 4

Prof. Ravindrababu Ravula

Compiler Design Lecture 3 Prof. Ravindrababu Ravula


Compiler Design Lecture 3

Prof. Ravindrababu Ravula

Compiler Design Lecture 2 Prof. Ravindrababu Ravula


Compiler Design Lecture 2

Prof. Ravindrababu Ravula

Compiler Design Lecture 1 Prof. Ravindrababu Ravula


Compiler Design Lecture 1

Prof. Ravindrababu Ravula