pdfasg9
pdfasg9
section .data
global msg6, len6, scount, ncount, chacount, new, new_len ; make these labels available to other
programs
new: db 0x0A
new_len: equ $-new
section .bss
global cnt, cnt2, cnt3, buffer ; make these labels available to other programs
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
;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
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
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 :-