Program to READ and DISPLAY ASCII CONTENTS OF FILE already created

;GROUP B ASSIGNMENT
;15.Write a Program to READ and DISPLAY ASCII CONTENTS OF FILE
;I have already created 'MAL_LAB.txt' file and written some text in it.
;0777 is the PERMISSION MODE

section .data
 file_name db 'MAL_LAB.txt',0  ;'MAL_LAB.txt' file to read which is to be there in the current directory

 fnotmsg db 'FIE NOT FOUND...',10
 fnmsg_len equ $-fnotmsg

 openmsg db 'FILE OPENED SUCESSFULLY!',10
 omsg_len equ $-openmsg

 filemsg db 'FILE ASCII CONTENTS ARE:-',10
 fmsg_len equ $-filemsg 

 spacechar db 20h
 nwline db 10

section .bss
 fd_in resd 1  ;file decriptor
 fbuff resb 20  ;buffer 
 fb_len equ $-fbuff
 act_len resd 1
 dispbuff resb 2

%macro print 2
  mov eax,4
  mov ebx,1
  mov ecx,%1
  mov edx,%2
  int 80h
%endmacro

section .text
 global _start
_start:

;open the file for reading
  mov eax,5  ;system call number (sys_open)
  mov ebx,file_name ;file name
  mov ecx,0  ;file access mode
  mov edx,0777  ;read,write and execute by all
  int 80h   ;call kernel

  mov [fd_in],eax  ;file descriptor
  bt eax,31  ;negative value in eax indicates error
  jnc conti1

  print fnotmsg,fnmsg_len
  jmp exit
conti1:
  print openmsg,omsg_len
  print filemsg,fmsg_len  

;read from file
readfile:
  mov eax,3  ;system call number (sys_read)
  mov ebx,[fd_in]  ;file descriptor 
  mov ecx,fbuff  ;pointer to the input buffer
  mov edx,fb_len  ;buffer size i.e the number of bytes to read
  int 80h   ;call kernel

  mov [act_len],eax ;Store the count of char read in memory
  cmp eax,0  ;Check the count is zero
  je nxt1   ;If zero jump to close file else continue

  mov esi,fbuff  ;Point esi to buffer in which file contents are stored
rdisp:
  mov bl,[esi]  ;Read ASCII value char by char
  call disp8_proc  ;& Display 
  inc esi   ;Point to next char
  print spacechar,1 ;Display space
  dec dword [act_len] ;Decrement count
  jnz rdisp  ;Repeat display process till actual count becomes zero

  jmp readfile  ;Jump to read next part of file

nxt1:
;close the file
  mov eax,6  ;system call number (sys_close)
  mov ebx,[fd_in]  ;file decriptor
  int 80h

exit:

  print nwline,01

  mov eax,1 ;system call number (sys_exit)
  mov ebx,0
  int 80h  ;call kernel
;---------------------------------------------
disp8_proc:
 mov ecx,02
 mov edi,dispbuff
dup1:
 rol bl,4
 mov al,bl
 and al,0fh
 cmp al,09
 jbe dskip
 add al,07h
dskip: add al,30h
 mov [edi],al
 inc edi
 loop dup1

 print dispbuff,02
 ret

;-------------OUTPUT----------------------
;[mahendra@(none) alp]$ nasm -f elf64 msmalb15.asm
;[mahendra@(none) alp]$ ld -o msmalb15 msmalb15.o

;Case 1: If file exist

;[mahendra@(none) alp]$ ./msmalb15
;FILE OPENED SUCESSFULLY!
;FILE ASCII CONTENTS ARE:-
;54 68 69 73 20 69 73 20 52 65 61 64 20 61 6E 64 20 44 69 73 70 6C 61 79 20 66 69 6C 65 20 63 6F 6E 74 65 6E 74 73 20 61 73 73 69 67 6E 6D 65 6E 74 20 6F 66 20 4D 41 20 4C 61 62 2E 0A 0A 53 45 20 43 6F 6D 70 28 32 30 31 32 20 43 6F 75 72 73 65 29 0A 
;[mahendra@(none) alp]$ 

;Case 2: If file does not exist
;[mahendra@(none) alp]$ ./msmalb15
;FIE NOT FOUND...
;[mahendra@(none) alp]$
SHARE
    Blogger Comment
    Facebook Comment