Thursday, May 27, 2021

Write an X86/64 ALP to accept five 64 bit Hexadecimal numbers from user and store them in an array and display the accepted numbers.

 section .data

msg1 db 10,13,"Enter 5 64 bit numbers"

len1 equ $-msg1

msg2 db 10,13,"Entered 5 64 bit numbers"

len2 equ $-msg2


section .bss

array resd 200

counter resb 1


section .text

global _start

_start:


;display

mov Rax,1

mov Rdi,1

mov Rsi,msg1

mov Rdx,len1

syscall


;accept


mov byte[counter],05

mov rbx,00


loop1:

mov rax,0                  ; 0 for read

mov rdi,0                  ; 0 for keyboard   

mov rsi, array             ;move pointer to start of array

add rsi,rbx               

mov rdx,17                  

syscall

          add rbx,17                    ;to move counter

dec byte[counter]

JNZ loop1


;display

mov Rax,1

mov Rdi,1

mov Rsi,msg2

mov Rdx,len2

syscall


;display

mov byte[counter],05

mov rbx,00

loop2: 

mov rax,1                    ;1 for write 

mov rdi, 1                    ;1 for monitor

mov rsi, array                

add rsi,rbx

mov rdx,17                   ;16 bit +1 for enter 

syscall

add rbx,17

dec byte[counter]

JNZ loop2


;exit system call

mov rax ,60

mov rdi,0

syscall


;output

;vacoea@vacoea-Pegatron:~$ cd ~/Desktop

;vacoea@vacoea-Pegatron:~/Desktop$ nasm -f elf64 ass1.asm

;vacoea@vacoea-Pegatron:~/Desktop$ ld -o ass1 ass1.o

;vacoea@vacoea-Pegatron:~/Desktop$ ./ass1


;Enter 5 64 bit numbers12

;23

;34

;45

;56


;Entered 5 64 bit numbers12

;23

;34

;45

;56


Monday, March 1, 2021

Write an X86/64 ALP to accept a string and to display its length.

 ;String length in Assembly Lanuguage


;macros begin

%macro write 2    ;will be used to write on the screen

mov rax,01h

mov rdi,0h

mov rsi,%1

mov rdx,%2

syscall

%endmacro


%macro read 2    ;will be used to read from the screen

mov rax,0h

mov rdi,1h

mov rsi,%1

mov rdx,%2

syscall

%endmacro


;macros end


section .data



msg2: db "Enter the string ",10

len2: equ $-msg2

msg3 : db "The length of the string is "

len3: equ $-msg3


section .bss


orgstr resb 50  ;original string

wordlen resb 1  ;length of the string

ascii resb 2  ;len in ascii

count resb 1


section .text

global _start:

_start:

write msg2,len2   ;display message



length:   ;the length option


mov al,0  ;initialize al with zero

read orgstr,50

mov byte[wordlen],al

dec byte[wordlen] ;now len has the length of the string without the null character

mov esi,wordlen

mov edi,ascii

mov byte[count],2

mov al,byte[esi]

loop:

rol al,04

mov bl,al

and bl,0Fh

cmp bl,09h

jbe nocorrection

add bl,7h

nocorrection:

add bl,30h

mov byte[edi],bl

inc edi

dec byte[count]

jnz loop


write ascii,2





exit:   ;to exit from the program

mov rax,60

mov rdx,0h

syscall






Write an X86/64 ALP to accept five 64 bit Hexadecimal numbers from user and store them in an array and display the accepted numbers.

 msg1 db "Enter 5 64-bit numbers ",10,13

len1 equ $-msg1


msg2 db "Entered 5 64-bit numbers are ",10,13

len2 equ $-msg2


section .bss

array resb 120

count resb 1


section .text

global _start

_start:


;logic to read 5 64-bit numbers


mov bh,05        ; count is in bh

mov rbx,00      ; array index


up:  mov rax,00

        mov rdi,00

        mov rsi,array

        add rsi,rbx

        mov rdx,17

        syscall

        add rbx,17

        dec byte[count]  ; dec count

        jnz up                  ; check count is 0 or not using zero flag i;



mov byte[count],05       ; count is in bh

mov rbx,00                     ;array index


up1: mov rax,01

        mov rdi,01

        mov rsi,array

        add rsi,rbx

        mov rdx,17

        syscall

        add rbx,17

        dec byte[count]      ; dec count

        jnz up1                    ; check count is 0 or not using zero flag i;



;exit syscall

mov rax, 60

mov rdi,00

syscall

Sunday, February 9, 2020

Write X86/64 ALP to switch from real mode to protected mode and display the values of GDTR, LDTR, IDTR, TR and MSW Registers.


Protected Mode System registers
GDT: Global Descriptor Table
IDT: Interrupt Descriptor Table
LDT: Local Descriptor Table
TR: Task Register



MSW: Machine Status Word
PE: Protection enabled bit. When it is set it is in Protected mode otherwise it is in real mode


Program: registers.asm

%macro scall 4
mov rax,%1
mov rdi,%2
mov rsi,%3
mov rdx,%4
syscall
%endmacro

Section .data
title: db 0x0A,"----Assignment 6-----", 0x0A
title_len: equ $-title
regmsg: db 0x0A,"***** REGISTER CONTENTS *****"
regmsg_len: equ $-regmsg
gmsg: db 0x0A,"Contents of GDTR : "
gmsg_len: equ $-gmsg
lmsg: db 0x0A,"Contents of LDTR : "
lmsg_len: equ $-lmsg
imsg: db 0x0A,"Contents of IDTR : "
imsg_len: equ $-imsg
tmsg: db 0x0A,"Contents of TR : "
tmsg_len: equ $-tmsg
mmsg: db 0x0A,"Contents of MSW : "
mmsg_len: equ $-mmsg
realmsg: db "---- In Real mode. ----"
realmsg_len: equ $-realmsg
protmsg: db "---- In Protected Mode. ----"
protmsg_len: equ $-protmsg
cnt2:db 04H
newline: db 0x0A

Section .bss
g: resd 1
resw 1
l: resw 1
idtr: resd 1
resw 1
msw: resd 1

tr: resw 1
value :resb 4

Section .text
global _start
_start:
scall 1,1,title,title_len
smsw [msw]
mov eax,dword[msw]
bt eax,0
jc next
scall 1,1,realmsg,realmsg_len
jmp EXIT
next:
scall 1,1,protmsg,protmsg_len

 scall 1,1, regmsg,regmsg_len
;printing register contents
scall 1,1,gmsg,gmsg_len
SGDT [g]
mov bx, word[g+4]
call HtoA
mov bx,word[g+2]
call HtoA
mov bx, word[g]
call HtoA

;--- LDTR CONTENTS----t find valid values for all labels after 1001 passes, giving up.

scall 1,1, lmsg,lmsg_len
SLDT [l]
mov bx,word[l]
call HtoA

;--- IDTR Contents -------
scall 1,1,imsg,imsg_len
SIDT [idtr]
mov bx, word[idtr+4]
call HtoA
mov bx,word[idtr+2]
call HtoA
mov bx, word[idtr]
call HtoA

;---- Task Register Contents -0-----
scall 1,1, tmsg,tmsg_len
mov bx,word[tr]
call HtoA

;------- Content of MSW ---------
scall 1,1,mmsg,mmsg_len
mov bx, word[msw+2]
call HtoA
mov bx, word[msw]
call HtoA
scall 1,1,newline,1
EXIT:
mov rax,60
mov rdi,0
syscall

;------HEX TO ASCII CONVERSION METHOD ----------------
HtoA: ;hex_no to be converted is in bx //result is stored in rdi/user defined variable
mov rdi,value
mov byte[cnt2],4H
aup:
rol bx,04
mov cl,bl
and cl,0FH
cmp cl,09H
jbe ANEXT
ADD cl,07H
ANEXT:
add cl, 30H
mov byte[rdi],cl
INC rdi
dec byte[cnt2]
JNZ aup
scall 1,1,value,4
ret

OUTPUT


Wednesday, March 20, 2019

Write 80387 ALP to obtain: i) Mean ii) Variance iii) Standard Deviation

section .data

meanmsg db 10,"CALCULATED MEAN IS:-"
meanmsg_len equ $-meanmsg
sdmsg db 10,"CALCULATED STANDARD DEVIATION IS:-"
sdmsg_len equ $-sdmsg
varmsg db 10,"CALCULATED VARIANCE IS:-"
varmsg_len equ $-varmsg
array dd 102.56,198.21,100.67,230.78,67.93
arraycnt dw 05
dpoint db '.'
hdec dq 100  ;Write 64-bit floating-point constant

section .bss
dispbuff resb 1
resbuff rest 1 ;Reserve space for 80-bit floating-point constants

mean resd 1   ;Reserve double words (32 bits)
variance resd 1

%macro linuxsyscall 4
mov rax,%1
mov rdi,%2
mov rsi,%3
mov rdx,%4
syscall
%endmacro 


section .text
global _start
_start:

finit
fldz
mov rbx,array
mov rsi,00
xor rcx,rcx
mov cx,[arraycnt]

up:
fadd dword[RBX+RSI*4]
inc rsi
loop up

fidiv word[arraycnt]
fst dword[mean]
linuxsyscall 01,01,meanmsg,meanmsg_len
call dispres

mov rcx,00
mov cx,[arraycnt]
mov rbx,array
mov rsi,00
FLDZ

up1:

fld dword[array+rsi*4]              ;load the number
fsub dword[mean]                    ;st0(1st number)minus mean
fmul st0                            ;square it
fadd                                ;add st1=st0+st1
inc rsi   
loop up1

FIDIV word[arraycnt]
FST dword[variance]
FSQRT
linuxsyscall 01,01,sdmsg,sdmsg_len
CALL dispres

FLD dword[variance]
linuxsyscall 01,01,varmsg,varmsg_len
CALL dispres

exit:
mov rax,60
mov rdi,0
syscall


disp8_proc:
mov rdi,dispbuff
mov rcx,02

back:
rol bl,04
mov dl,bl
and dl,0FH
cmp dl,09
jbe next1
add dl,07H

next1:
add dl,30H
mov [rdi],dl
inc rdi
loop back
ret

dispres:
fimul dword[hdec]
fbstp tword[resbuff]
xor rcx,rcx
mov rcx,09H
mov rsi,resbuff+9

up2:
push rcx
push rsi
mov bl,[rsi]
call disp8_proc



linuxsyscall 01,01,dispbuff,2

pop rsi
dec rsi
pop rcx
loop up2

linuxsyscall 01,01,dpoint,1
mov bl,[resbuff]
call disp8_proc
linuxsyscall 01,01,dispbuff,2
ret

Write x86 ALP to find the factorial of a given integer number on a command line

%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 .data
msg1 db "Enter the 8 bit number:",0ah,0dh
len1 equ $  -msg1

msg2 db "The factorial of given 8 bit number is:",0ah,0dh
len2 equ $  -msg2

msg3 db "The factorial for 0 or 1 is:",0ah,0dh
len3 equ $  -msg3

zeroonefact db "0001"
zeroonefactlen equ $-zeroonefact

section .bss
num resb 3
res resb 16

section .text
global _start
_start:

disp msg1, len1
inn num, 3
call accept

xor rax, rax
mov ax,bx
cmp ax,01h
jbe onezero

call factorial
call display
mov rax,60
mov rdi,0
syscall



onezero:
disp msg3,  len3
disp zeroonefact,  zeroonefactlen

;exit:


accept:
       mov rsi,num
       mov cl,04
       xor rbx,rbx
       mov ch,02
       up:
       cmp byte[rsi],39h
       jng sk
       sub byte[rsi],07h
       sk:
       sub byte[rsi],30h
       rol bl,cl
       add bl,[rsi]
       inc rsi
       dec ch
       jnz up
ret

factorial:
xor rbx, rbx
mov rbx,rax
up1:sub rbx ,01
mul rbx
cmp rbx,01
jne up1
ret

display:
mov rsi,res
mov ch,16
mov cl,04

again1:
rol rax,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 msg2, len2
disp res, 16
ret


Write X86 menu driven Assembly Language Program (ALP) to implement OS (DOS) commands TYPE, COPY and DELETE using file operations.

;macro.asm


%macro read 2
mov rax,0 ;read
mov rdi,0 ;stdin/keyboard
mov rsi,%1 ;buf
mov rdx,%2 ;buf_len
syscall
%endmacro

%macro print 2
mov rax,1 ;print
mov rdi,1 ;stdout/screen
mov rsi,%1 ;msg
mov rdx,%2 ;msg_len
syscall
%endmacro

%macro fopen 1
mov rax,2 ;open
mov rdi,%1 ;filename
mov rsi,2 ;mode RW
mov rdx,0777o ;File permissions
syscall
%endmacro

%macro fread 3
mov rax,0 ;read
mov rdi,%1 ;filehandle
mov rsi,%2 ;buf
mov rdx,%3 ;buf_len
syscall
%endmacro

%macro fwrite 3
mov rax,1 ;write/print
mov rdi,%1 ;filehandle
mov rsi,%2 ;buf
mov rdx,%3 ;buf_len
syscall
%endmacro

%macro fclose 1
mov rax,3 ;close
mov rdi,%1 ;file handle
syscall
%endmacro

%macro exit 0
print nline,nline_len
mov rax,60 ;exit
mov rdi,0
syscall
%endmacro

;file.asm

%include "macro.asm"
Section .data
title: db 0ah,"----Commands -----", 0ah
 db "1. Copy ",0ah
 db "2. Type ",0ah
 db "3. Delete ",0ah
 db "4. Exit ",0ah
 db "Enter Your choice",0ah
title_len: equ $-title

openmsg: db "File Opened Successfully",0ah,0dh
openmsg_len: equ $-openmsg
closemsg: db "File Closed Successfully",0ah,0dh
closemsg_len: equ $-closemsg
errormsg: db "Failed to open file", 0ah,0dh
errormsg_len: equ $-errormsg
delmsg: db "Deleted File", 0ah,0dh
delmsg_len: equ $-delmsg
typemsg: db "=-----File Contents ----=",0ah,0dh
typemsg_len: equ $-typemsg

f1name: db 'file1.txt',0
f2name: db 'file2.txt',0
f3name: db 'file3.txt',0

Section .bss
buffer: resb 200
bufferlen:resb 8
cnt1:resb 8
;fdis:resb 8
fhandle: resq 1
choice: resb 2

Section .text
global main
main:
write title,title_len
read choice,2

;------------- CHOOSE OPTION --------------------------
;compare choice here

cmp byte[choice],'1' ;if choice is to display content
je COPY
cmp byte[choice],'2'
je TYPE
cmp byte[choice],'3'
je DELETE
cmp byte[choice],'4'
je EXIT

COPY:
 fopen f1name  ;Opening file
 cmp rax, -1h
 je next
 mov [fhandle],rax
 ;mov qword[fdis],rax  ;RAX contains file descriptor value
 ;bt rax,63 ;63rd bit is +ve(0) if file is successfull opened else it is -ve (1)
 ;jc next

 write openmsg,openmsg_len
 jmp next1

next:
 write errormsg,errormsg_len
 jmp EXIT

next1:
 fread [fhandle],buffer,200    ;reading contents of file in buffer
  ;rax contains actual number of bytes read
 mov qword[bufferlen],rax
 mov qword[cnt1],rax

 ;Closing file1
  fclose f1name

 write closemsg,closemsg_len




;-------------------FILE 2 ------------------
 fopen f2name

 cmp rax, -1h
 je next3
 mov [fhandle],rax
 ;mov qword[fhandle],rax  ;RAX contains file descriptor value
 ;bt rax,63 ;63rd bit is +ve(0) if file is successfull opened else it is -ve (1)
 ;jc next3

 write openmsg,openmsg_len
 jmp next21

next3:
 write errormsg,errormsg_len
 jmp EXIT

next21:
 fwrite qword[fhandle],buffer,qword[bufferlen]            ;writing to file2.txt

 fclose f2name

 write closemsg,closemsg_len
 jmp main

;-----------------------------------------------------------------------

TYPE:
 fopen f2name ;Opening file
 cmp rax, -1h
 je tnext
 mov [fhandle],rax
 ;mov qword[fdis],rax     ;RAX contains file descriptor value
 ;bt rax,63 ;63rd bit is +ve(0) if file is successfull opened else it is -ve (1)
 ;jc tnext

 write openmsg,openmsg_len
 jmp tnext1

tnext:
 write errormsg,errormsg_len
 jmp EXIT

tnext1:
 fread [fhandle],buffer,200 ;reading contents of file in buffer
 mov qword[bufferlen],rax
 write typemsg,typemsg_len
 write buffer,qword[bufferlen]

 ;Closing file2
 fclose f2name

 write closemsg,closemsg_len
JMP main

;----------------------------------------------------------

DELETE:
 mov rax,87 ;System CALL FOR UNLINK
 mov rdi,f3name
 syscall
 write delmsg,delmsg_len
jmp main

EXIT:
 mov rax,3ch
 mov rdi,00
 syscall