Wednesday, January 24, 2018

5. Write X86 ALP to find, a) Number of Blank spaces b) Number of lines c) Occurrence of a particular character. Accept the data from the text file. The text file has to be accessed during Program_1 execution and write FAR PROCEDURES in Program_2 for the rest of the processing. Use of PUBLIC and EXTERN directives is mandatory.

f1.asm
%macro print 2
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro

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

Section .data

title db 10,13,"****ASSIGNMENT: 5****"
title_len equ $-title
openmsg db 10,13,"File Opened Successfully"
openmsg_len equ $-openmsg
errormsg db 10,13,"Failed to open file"
errormsg_len equ $-errormsg

fname db "abc.txt"

Section .bss
global cnt1,cnt2,cnt3,buffer,bufferlen,fdis
cnt1 resb 8
cnt2 resb 8
cnt3 resb 8
bufferlen resb 8
buffer resb 200
fdis resq 1                     ;for storing file descriptor value

Section .text
global _start
extern CSPACE,CNEWLINE,CCHAR
start:
print title,title_len

;Opening file
mov rax,2
mov rdi,fname
mov rsi,2
mov rdx,777
syscall

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

print openmsg,openmsg_len
jmp next1

next:
 print errormsg,errormsg_len
 jmp EXIT

next1:
;reading contents of file in buffer
mov rax,0
mov rdi,[fdis]
mov rsi,buffer
mov rdx,200
syscall
                                ;rax contains actual number of bytes read
 mov qword[bufferlen],rax
 mov qword[cnt1],rax             ;cnt1 for using in cspace procedure
 mov qword[cnt2],rax             ;cnt2 for using in cnewline procedure
 mov qword[cnt3],rax             ;cnt3 for using in cchar procedure

call CCHAR
call CSPACE
call CNEWLINE

mov rax,3
mov rdi,fname
syscall

EXIT:
mov rax,60
mov rdi,0
syscall

f2.asm
%macro print 2
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro

%macro accept 2
mov rax,0
mov rdi,0
mov rsi,%1
mov rdx,%2
syscall
%endmacro
section .data
spacemsg db 10,13,"Spaces = "
spacemsg_len equ $-spacemsg

newlinemsg db  10,13, "Return(Enter) chars = "
newlinemsg_len equ $-newlinemsg

chms db 10,13,"Character count: "
chms_len equ $-chms

chinput db 10,13,"Character Input: ",
chinput_len equ $-chinput

scnt db 00H
ncnt db 00H
chcnt db 00H
hexcnt2 db 00H

section .bss
extern cnt1,cnt2,cnt3,buffer,bufferlen
spacecount resb 2
ch1 resb 2
newlinecount resb 2
chcount resb 2

section .text
global _start1
global CSPACE, CNEWLINE,CCHAR
_start1:

CSPACE:
 mov rsi,buffer
up:
 mov al,byte[rsi]
 cmp al,20H
 je next2
 inc rsi
 dec qword[cnt1]
 jnz up
 jmp next3

next2:
 inc byte[scnt]
 inc rsi
 dec qword[cnt1]
 jmp up

next3:
 mov bl, byte[scnt]
 mov rdi, spacecount
 call HtoA

 print spacemsg,spacemsg_len
 print spacecount,2

ret

CNEWLINE:
 mov rsi,buffer
up2:
 mov al,byte[rsi]
 cmp al,0x0A
 je next4
 inc rsi
 dec qword[cnt2]
 jnz up2
 jmp next5

next4:
 inc byte[ncnt]
 inc rsi;
 dec qword[cnt2]
 jnz up2

next5:
 mov bl, byte[ncnt]
 mov rdi, newlinecount
 call HtoA

 print newlinemsg,newlinemsg_len
 print newlinecount,2

ret

CCHAR:
 print chinput,chinput_len
 accept ch1,2
 mov rsi,buffer
up3:
 mov al,byte[rsi]
 cmp al,byte[ch1]
 je nextl
 inc rsi
 dec qword[cnt3]
 jnz up3
 jmp next7

nextl:
 inc byte[chcnt]
 inc rsi;
 dec qword[cnt3]
 jnz up3

next7:
 mov bl, byte[chcnt]
 mov rdi, chcount
 call HtoA
 print chms,chms_len
 print chcount,2

ret
;------HEX TO ASCII CONVERSION METHOD FOR VALUE(2 DIGIT) ----------------

HtoA:             ;hex_no to be converted is in ebx //result is stored in rdi/user defined variable
mov byte[hexcnt2],02H
h2aup:
rol bl,04
mov cl,bl
and cl,0FH
CMP CL,09H
jbe add30
ADD cl,07H

add30:
add cl, 30H
mov byte[rdi],cl
INC rdi
dec byte[hexcnt2]
JNZ h2aup
ret

exit:
mov rax,60
mov rdi,0
syscall
abc.txt

Welcome SE computer student in SVIT College, Chincholi, Sinner, Dist: Nashik
How are you

OUTPUT:
ntlab@ntlab-Veriton-M200-H81:~/file$ nasm -f elf64 f1.asm
ntlab@ntlab-Veriton-M200-H81:~/file$ nasm -f elf64 f2.asm
ntlab@ntlab-Veriton-M200-H81:~/file$ ld -o prog f1.o f2.o
ld: warning: cannot find entry symbol _start; defaulting to 00000000004000b0
ntlab@ntlab-Veriton-M200-H81:~/file$ ./prog

****ASSIGNMENT: 5****
File Opened Successfully
Character Input: c

Character count: 03
Spaces = 0C
Return(Enter) chars = 03


ntlab@ntlab-Veriton-M200-H81:~/file$ 
 

No comments:

Post a Comment

Write an Assembly Language Program (ALP) to find the largest of given byte / Word / Double-word / 64-bit numbers.

  %macro scall 4 ;macro declaration with 4 parameters          mov eax , %1 ;1st p...