ASM Program to find number of Spaces, Newline and Occurrence of Character
Problem Statement:
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.Code:-
Support us by clicking on ads.
p1.asm :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | %macro scall 4 mov rax,%1 mov rdi,%2 mov rsi,%3 mov rdx,%4 syscall %endmacro Section .data title: db 0x0A,"----ASSIGNMENT 5 -----", 0x0A title_len: equ $-title openmsg: db "File Opened Successfully",0x0A openmsg_len: equ $-openmsg errormsg: db "Failed to open file", 0x0A errormsg_len: equ $-errormsg fname: db 'abc.txt', 0 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: resb 8 ;for storing file descriptor value Section .text global main extern CSPACE,CNEWLINE,CCHAR main: scall 1,1,title,title_len scall 2,fname,2,777 ;Opening file 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 scall 1,1,openmsg,openmsg_len jmp next1 next: scall 1,1,errormsg,errormsg_len jmp EXIT next1: scall 0,[fdis],buffer,200 ;reading contents of file in buffer ;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 ;scall 1,1,title,title_len call CNEWLINE ;scall 1,1, title,title_len mov rax,3 mov rdi,fname syscall EXIT: mov rax,60 mov rdi,0 syscall |
p2.asm :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | %macro scall 4 mov rax,%1 mov rdi,%2 mov rsi,%3 mov rdx,%4 syscall %endmacro Section .data spacemsg:db 0x0A,"Spaces = " spacemsg_len: equ $-spacemsg newlinemsg:db 0x0A, "Return(Enter) chars = " newlinemsg_len: equ $-newlinemsg chms:db 0x0A,"Character count: " chms_len: equ $-chms chinput: db 0x0A,"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 main2 global CSPACE, CNEWLINE,CCHAR main2: mov rax,60 mov rdi,0 syscall 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 scall 1,1, spacemsg,spacemsg_len scall 1,1,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 scall 1,1, newlinemsg,newlinemsg_len scall 1,1,newlinecount,2 ret CCHAR: scall 1,1,chinput,chinput_len scall 0,0,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 scall 1,1,chms,chms_len scall 1,1,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 aup1: rol bl,04 mov cl,bl and cl,0FH CMP CL,09H jbe ANEXT1 ADD cl,07H ANEXT1: add cl, 30H mov byte[rdi],cl INC rdi dec byte[hexcnt2] JNZ aup1 ret |
Commands for execution:
Note: Both Programs and text files need to be in same directory.
$ nasm -f elf64 p1.asm
$ nasm -f elf64 p2.asm
$ ld -o prog p1.o p2.o
$ ./prog
No comments: