0% found this document useful (0 votes)
11 views5 pages

pdfasg9

Uploaded by

hetavimodi2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

pdfasg9

Uploaded by

hetavimodi2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment 9 (a)

; 9a ALP for counting spaces, lines & a given character in a file

section .data
global msg6, len6, scount, ncount, chacount, new, new_len ; make these labels available to other
programs

fname: db 'abc.txt', 0 ; file to be processed is abc.txt

msg: db "File opened successfully", 0x0A


len: equ $-msg

msg1: db "File closed successfully", 0x0A


len1: equ $-msg1

msg2: db "Error in opening file", 0x0A


len2: equ $-msg2

msg3: db "Spaces:", 0x0A


len3: equ $-msg3

msg4: db "NewLines:", 0x0A


len4: equ $-msg4

msg5: db "Enter character", 0x0A


len5: equ $-msg5

msg6: db "No of occurrences:", 0x0A


len6: equ $-msg6

new: db 0x0A
new_len: equ $-new

scount: db 0 ; initialize all counters for spaces, lines & character to 0


ncount: db 0
ccount: db 0
chacount: db 0

section .bss
global cnt, cnt2, cnt3, buffer ; make these labels available to other programs

fd: resb 17 ; file handle of opened file abc.txt is stored here


buffer: resb 200 ; characters read from file are stored here (max 200)
buf_len: resb 17

cnt: resb 2 ; cnt, cnt2, cnt3 hold the count of total characters in file abc.txt
cnt2: resb 2
cnt3: resb 2
cha: resb 2 ; character whose count is to be found in file is stored here

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

section .text
global _start

extern spaces, enters, occ ; these labels are declared in some other program

_start:
mov rax, 2 ; open file abc.txt
mov rdi, fname
mov rsi, 2
mov rdx, 0777 ; with rwx (read write execute) permissions
syscall

mov qword [fd], rax ; after opening, the file handle comes in rax, save it in memory at fd
BT rax, 63 ; copy 64th bit of rax in carry flag
jc next ; if carry means "Error in opening file"

scall 1,1, msg, len ; if no carry, display message "File opened successfully"
jmp next2

next:
scall 1,1, msg2, len2 ; if carry, display message "Error in opening file"

next2:
scall 0, [fd], buffer, 200 ; read characters from file whose handle is in fd, in buffer (max 200 bytes)
mov qword [buf_len], rax ; it also returns actual count of characters in rax
mov qword [cnt], rax ; copy the count in cnt, cnt2, cnt3
mov qword [cnt2], rax
mov qword [cnt3], rax

scall 1,1, msg3, len3 ; display message "Spaces:"


call spaces ; call procedure spaces (in another program 9b.asm)

scall 1,1, msg4, len4 ; display message "Lines:"


call enters ; call procedure enters (in another program 9b.asm)

scall 1,1, msg5, len5 ; display message "Enter character"


scall 0, 1, cha, 2 ; accept the character
mov bl, byte [cha] ; copy it from cha location to bl
call occ ; call procedure occ (in another program 9b.asm)

mov rax, 60 ; exit the program


mov rdi, 0
syscall
Assignment 9 (b)

;9b.asm ALP which contains three procedures which count spaces, lines & given character from a given file

section .data
extern msg6,len6,scount,ncount,chacount,new,new_len ; these labels are declared in another program
(9a.asm)

section .bss
extern cnt,cnt2,cnt3,scall,buffer ; these labels are declared in another program (9a.asm)

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

section .text
global main2

main2:
global spaces,enters,occ ; these declarations of labels are available to other programs

spaces: ; procedure to count space characters


mov rsi,buffer ; point rsi to buffer where file contents are stored after reading

up:
mov al, byte[rsi] ; take a character from buffer
cmp al,20H ; compare it with ASCII of space i.e. 20H
je next3 ; if equal, go to next3, where it increments scount
inc rsi ; else only increment pointer
dec byte[cnt] ; decrement character count
jnz up ; if not 0, take next character
jmp next4 ; skip following 4 instructions

next3:
inc rsi ; increment pointer to point to next character
inc byte[scount] ; increment scount (count of spaces)
dec byte[cnt] ; decrement character count
jnz up ; if not 0, take next character

next4:
add byte[scount], 30h ; after all characters are checked, convert final count of space to ASCII by adding
30H
scall 1,1,scount,2 ; display count of spaces
scall 1,1,new,new_len ; take cursor to next line to display count of lines
ret ; return to calling program

enters: ; procedure to count enter characters i.e. to count lines


mov rsi,buffer ; point rsi to buffer where file contents are stored after reading

up2:
mov al, byte[rsi] ; take a character from buffer
cmp al,0AH ; compare it with ASCII of new line i.e. 0AH
je next5 ; if equal, go to next5, where it increments ncount
inc rsi ; else only increment pointer
dec byte[cnt2] ; decrement character count
jnz up2 ; if not 0, take next character
jmp next6 ; skip following 4 instructions

next5:
inc rsi ; increment pointer to point to next character
inc byte[ncount] ; increment ncount (count of lines)
dec byte[cnt2] ; decrement character count
jnz up2 ; if not 0, take next character

next6:
add byte[ncount], 30h ; after all characters are checked, convert final count of lines to ASCII by adding
30H
scall 1,1,ncount,2 ; display count of lines
scall 1,1,new,new_len ; take cursor to next line to display count of lines
ret ; return to calling program

occ:
mov rsi,buffer ; point rsi to buffer where file contents are stored after reading

up3:
mov al, byte[rsi] ; take a character from buffer
cmp al,bl ; compare it with given character which is bl (copied in bl by calling program)
je next7 ; if equal, go to next7, where it increments chacount
inc rsi ; else only increment pointer
dec byte[cnt3] ; decrement character count
jnz up3 ; if not 0, take next character
jmp next8 ; skip following 4 instructions

next7:
inc rsi ; increment pointer to point to next character
inc byte[chacount] ; increment chacount (count of given character)
dec byte[cnt3] ; decrement character count
jnz up3 ; if not 0, take next character

next8:
add byte[chacount], 30h ; after all characters are checked, convert final count of char to ASCII by adding
30H
scall 1,1,msg6,len6 ; display message "character = "
scall 1,1,chacount,1 ; display count of character
scall 1,1,new,new_len ; take cursor to next line to display count of lines
ret ; return to calling program
OUTPUT :-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy