NASM programs

download NASM programs

of 22

description

This is a set of nasm programs. Linux Netwide Assembeler. Assembly language programming in Linux

Transcript of NASM programs

Program:section .data ;1.1msg: db "Enter the character:" msglen: equ $-msg spcl: db "Special Character", 10 length1: equ $-spcl alphan: db "Alpha Numeric", 10 length2: equ $-alphan alphab: db "Alphabet", 0Ah length3: equ $-alphab %macro printstr 2 mov eax , 4 mov ebx , 1 mov ecx , %1 mov edx , %2 int 80h %endmacro section .bss key_stroke: resb 1 section .text global _start: _start: printstr msg, msglen mov eax, 3 mov ebx, 0 mov ecx, key_stroke mov edx, 2 int 80h cmp byte[key_stroke], 30H jnb check jb special_key check: cmp byte[key_stroke], 3AH jb alphanumeric cmp byte[key_stroke], 41H jnb check1 jb special_key check1:cmp byte[key_stroke], 5BH jb alphabetic cmp byte[key_stroke], 61H jnb check2 jb special_key jmp stop check2:cmp byte[key_stroke], 7BH jb alphabetic jb special_key special_key:printstr spcl, length1 jmp stop alphanumeric: printstr alphan, length2 jmp stop alphabetic: printstr alphab, length3 stop: mov eax, 1 mov ebx, 0 int 80h ;A=65 or 41h Z=91 or 5Bh ;a=97 or 61h z=123 or 7Bh ;0=48 or 30h 9=57 or 39h ;special character 58 or 3Ah TO 64 or 40h

Output:

Enter the character: a Alphabet Enter the character: A Alphabet Enter the character: 1 Alpha Numeric Enter the character: . Special CharacterProgram:section .data;1.2msg1 : db 10,"Enter 2-digit number "len : equ $-msg1e : db 10,"The number is even",10le : equ $-eo : db 10,"The number is odd",10lo : equ $-o%macro display 2mov eax , 4mov ebx , 1mov ecx , %1mov edx , %2int 80h%endmacrosection .bssn1 : resb 2section .txtglobal _start_start: display msg1,lenmov eax , 3mov ebx , 0mov ecx , n1mov edx , 2int 80hdisplay n1,2mov al , byte[n1+1]sub ax , 30hmov bl , 2div bl ;Ah=remainder , al=quotientcmp ah,0jne odd;if zf=0even:display e , lejmp exitodd:display o , loexit:mov eax , 1mov ebx , 0int 80h

Output:Enter 2-digit number 05 05 The number is odd Enter 2-digit number 12 12 The number is even Program:section .data ;1.3msg : db "Enter the no : " len : equ $-msg msg2 : db "Square is : " len2 : equ $-msg2 newline : db 10 nlen : equ $-newline %macro printstr 2 mov eax , 4 mov ebx , 1 mov ecx , %1 mov edx , %2 int 80h %endmacro section .bss num : resb 1 squ : resb 2 section .txt global _start _start:printstr msg, len mov eax , 3 mov ebx , 0 mov ecx , num mov edx , 1 int 80h mov al , byte[num] sub al , 30h mov cl , al mov ch , 00 mov ah , 00 mul cx ;ax and cx is multiplied mov cl , 10 div cl ;split into 2 seperate digits add ax , 3030h mov word[squ] , ax printstr msg2, len2 printstr squ, 2 printstr newline, nlen mov eax , 1 mov ebx , 0 int 80hOutput:Enter the no : 7 Square is : 49

Program:section .data;1.4msg : db "Enter a number "msg2 : db 10len : equ $-msglen2 : equ $-msg2num db '0'%macro printstr 2mov eax , 4mov ebx , 1mov ecx , %1mov edx , %2int 80h%endmacrosection .bssn:resb 1section .textglobal _start_start:printstr msg, lenmov eax , 3mov ebx , 0mov ecx , nmov edx , 1int 80hloop:printstr num, 2 ; print the ASCII valprintstr msg2, len2sub byte[num] , 30h ;convert to hexsub byte[n] , 30h ;convert to hex for cmpmov ah , byte[n]cmp ah , byte[num]je exitinc byte[num]add byte[num] , 30hadd byte[n] , 30hjmp loopexit:mov eax , 1mov ebx , 0int 80hOutput:Enter a number 401234Program:section .data;1.5msg : db "Enter a 16 bit number : "len : equ $-msgmsg1 : db "The sum is : "len1 : equ $-msg1newline : db 10lenn : equ $-newline%macro printstr 2mov eax , 4mov ebx , 1mov ecx , %1mov edx , %2int 80h%endmacro%macro readstr 2mov eax , 3mov ebx , 0mov ecx , %1mov edx , %2int 80h%endmacro%macro convertab 1 ; binary will be stored in alpush cx;mov cl , byte[%1] ;cl will have the tens place digitmov ch , byte[%1+1] ;ch will have the ones place digitsub cx , 3030h;convert from ASCII to hexmov al , 10mul cladd al , chpop cx;%endmacrosection .bssnum1 : resb 2num2 : resb 2temp1 : resb 1temp2 : resb 1section .textglobal _start_start:printstr msg, len ;macro call for printingreadstr num1, 3 ;macro call for readingprintstr msg, lenreadstr num2, 3printstr msg1, len1 convertab num1mov dl , alconvertab num2add al , dlmov cl , 10div cl ;split into 2 seperate digits add ax , 3030h ;convert back to ASCIImov byte[temp1] , almov byte[temp2] , ahprintstr temp1,1printstr temp2,1printstr newline,1mov eax , 1mov ebx , 0int 80h

Output:Enter a 16 bit number : 42Enter a 16 bit number : 51The sum is : 93

Enter a 16 bit number : 12Enter a 16 bit number : 15The sum is : 27Program:section .data;1.6msg: db "Enter the number(4 digits)",10dmsg: equ $-msgnewline: db 10inp1: dw 30hinp2: dw 30hsum: dw 30hcount: db 30htemp: db 30h%macro printstr 2mov eax , 4mov ebx , 1mov ecx , %1mov edx , %2int 80h%endmacro%macro readstr 2mov eax , 3mov ebx , 0mov ecx , %1mov edx , %2int 80h%endmacrosection .textconvert: sub bl, 30hmov eax, 0mov ecx, 0mov al, blmov byte[count], 3acon: mov edx,0mov ecx, 10mul cxsub bh, 30hshr ebx, 8mov edx,0mov dl, bladd ax, dxsub byte[count],1cmp byte[count],0jne aconret

global _start_start: printstr msg, dmsgreadstr inp1, 5mov ebx, 0mov ebx, [inp1]call convertmov word[inp1], axreadstr inp2, 4mov ebx, 0mov ebx, [inp2]call convertmov word[inp2], axmov eax, 0mov edx, 0mov ax, [inp1]mov dx,[inp2]add ax, dxpush 29hmov byte[count], 5break: mov ebx, 10mov edx, 0div bxadd dl, 30hpush dxsub byte[count], 1cmp byte[count],0jne breakmov eax, 0pop axprint: mov byte[temp], alprintstr temp, 1mov eax, 0pop axcmp al, 29hjne printprintstr newline, 1mov eax, 1mov ebx, 0int 80h

Output:Enter the number(4 digits)1453821209665

Enter the number(4 digits)9564212511689Program:section .data;1.7big : db "Big-Endian ",10,13lit : db "Little-Endian",10,13blen : equ $-bigllen : equ $-lit%macro printstr 2mov eax , 4mov ebx , 1mov ecx , %1mov edx , %2int 80h%endmacrosection .textglobal _start_start:mov eax , 0xffff0000push eaxpop axpop bxcmp bx , 0xffffje littlebigend:printstr big, blenjmp endlittle:printstr lit, llenint 80hend:mov eax , 1mov ebx , 0int 80h

Output:

Little-EndianProgram:section .data;1.8msg: db 'Enter the 1st no:'len: equ $-msgmsg1: db 'Enter 2nd no:'len1: equ $-msg1msg2: db 'GCD:'len2: equ $-msg2nw: db 10lennw: equ $-nw%macro printstr 2mov eax , 4mov ebx , 1mov ecx , %1mov edx , %2int 80h%endmacro%macro readstr 2mov eax , 3mov ebx , 0mov ecx , %1mov edx , %2int 80h%endmacrosection .bssnum1: resb 2num2: resb 2gcdno: resb 2gcdno1:resb 2section .txtglobal _start_start:printstr msg,lenreadstr num1,4printstr msg1,len1readstr num2,4printstr msg2,len2mov cl,byte[num1]mov ch,byte[num1+1]sub cx,3030hmov al,10mul cladd al,chmov bl,almov cl,byte[num2]mov ch,byte[num2+1]sub cx,3030hmov al,10mul cladd al,chmov dl,almov dh,00mov bh,00fndgcd:cmp dl,00je resultpush dxmov al,blmov ah,00div dlmov dl,ahpop bxjmp fndgcdresult:mov ah,00mov al,blmov cl,10div cladd ax,3030hmov byte[gcdno],almov byte[gcdno1],ahprintstr gcdno,2printstr gcdno1,2mov eax,1mov ebx,0int 80h

Output:

Enter the 1st no:15Enter 2nd no:12GCD:03

Enter the 1st no:17Enter 2nd no:12GCD:01Program:section .data;2.1msg: db "Enter the number(4 digits)",10dmsg: equ $-msgnewline: db 10inp1: dw 30hinp2: dw 30hsub: dw 30hcount: db 30htemp: db 30h%macro printstr 2mov eax , 4mov ebx , 1mov ecx , %1mov edx , %2int 80h%endmacro%macro readstr 2mov eax , 3mov ebx , 0mov ecx , %1mov edx , %2int 80h%endmacrosection .textconvert: sub bl, 30hmov eax, 0mov ecx, 0mov al, blmov byte[count], 3acon: mov edx,0mov ecx, 10mul cxsub bh, 30hshr ebx, 8mov edx,0mov dl, bladd ax, dxsub byte[count],1cmp byte[count],0jne aconret

global _start_start: printstr msg, dmsgreadstr inp1, 4readstr temp, 1mov ebx, 0mov ebx, [inp1]call convertmov word[inp1], axreadstr inp2, 5mov ebx, [inp2]call convertmov word[inp2], axmov eax, 0mov edx, 0mov ax, [inp1]mov dx,[inp2]sub ax, dxpush 29hmov byte[count], 5break: mov ebx, 10mov edx, 0div bxadd dl, 30hpush dxsub byte[count], 1cmp byte[count],0jne breakmov eax, 0pop axprint: mov byte[temp], alprintstr temp, 1mov eax, 0pop axcmp al, 29hjne printprintstr newline, 1mov eax, 1mov ebx, 0int 80h

Output:Enter the number(4 digits)1492120100291

Enter the number(4 digits)2405121101194

Program:section .data;2.2msg1: db "Enter how many no : "size1: equ $-msg1msg2: db "Enter integer : ",10size2: equ $-msg2msg3: db "Enter number to be found: "size3: equ $-msg3not: db "Given number not found" ,10size4: equ $-notfound: db "Given number found" ,10size5: equ $-foundnewline: db 10len: equ $-newline%macro printstr 2mov eax , 4mov ebx , 1mov ecx , %1mov edx , %2int 80h%endmacro%macro readstr 2mov eax , 3mov ebx , 0mov ecx , %1mov edx , %2int 80h%endmacrosection .bsscount resb 2count1 resb 2data1 resb 1data2 resb 1num resw 2srchnum resb 2temp resb 2section .textglobal _start_start:printstr msg1,size1readstr data1,1readstr data2,1readstr temp,2mov cl,byte[data1]mov ch,byte[data2]sub cl,30hsub ch,30hmov al,10mul cladd al,chmov byte[count],almov byte[count1],alreadarray:mov eax,4printstr msg2,size2readstr data1,1readstr data2,1readstr temp,2mov cl,byte[data1]mov ch,byte[data2]sub cl,30hsub ch,30hmov al,10mul cladd al,chmov ah,0push axdec byte[count]movzx ecx,byte[count]cmp ecx,0jne readarray;elem,ent to be searchprintstr msg3,size3readstr data1,1readstr data2,1readstr temp,2mov cl,byte[data1]mov ch,byte[data2]sub cl,30hsub ch,30hmov al,10mul cladd al,chmov byte[srchnum],alsearchno:mov ax,00pop axmovzx bx,byte[srchnum]cmp ax,bxje foundnodec byte[count1]movzx ecx,byte[count1]cmp ecx,0jne searchnojmp nfoundnofoundno:printstr found,size5jmp exitnfoundno:printstr not,size4

exit:mov eax,1mov ebx,0int 80h

Output:Enter how many no : 05Enter integer : 12Enter integer :36Enter integer : 45Enter integer : 89Enter integer : 11Enter number to be found: 25Given number not found

Enter how many no : 05Enter integer : 21Enter integer : 36Enter integer : 78Enter integer : 45Enter integer : 13Enter number to be found: 13Given number foundProgram:section .data;2.3msg1: db "Enter how many no : "size1: equ $-msg1msg2: db "Enter integer : "size2: equ $-msg2large: db " Largest Number :"size4: equ $-largenewline: db 10len: equ $-newline%macro printstr 2mov eax , 4mov ebx , 1mov ecx , %1mov edx , %2int 80h%endmacro%macro readstr 2mov eax , 3mov ebx , 0mov ecx , %1mov edx , %2int 80h%endmacrosection .bsscount resb 2count1 resb 2data1 resb 1data2 resb 1largeno resb 2temp resb 2section .textglobal _start_start: printstr msg1,size1readstr data1,1readstr data2,1readstr temp,1mov cl,byte[data1]mov ch,byte[data2]sub cl,30hsub ch,30hmov al,10mul cladd al,chmov byte[count],almov byte[count1],alreadarray:printstr msg2,size2readstr data1,1readstr data2,1readstr temp,2mov cl,byte[data1]mov ch,byte[data2]sub cl,30hsub ch,30hmov al,10mul cladd al,chmov ah,0push axdec byte[count]movzx ecx,byte[count]cmp ecx,0jne readarray;largeprintstr large,size4mov ax,00pop axcheckno:pop bxcmp ax,bxjnc next ;jnc or jgmov ax,bxnext:dec byte[count1]movzx ecx,byte[count1]cmp ecx,0jne checknomov bl,10div bladd ah,30hadd al,30hmov byte[data1],almov byte[data2],ahprintstr data1,1printstr data2,1printstr newline,lenmov eax,1mov ebx,0int 80h

Output:Enter how many no : 05Enter integer : 12Enter integer : 36Enter integer : 22Enter integer : 40Enter integer : 98

Largest Number:98Program:section .data;2.4askcount:db "Enter the array size:"lencount: equ $-askcountelement:db "Enter the array element:"lenelmnt: equ $-elementsort: db "Sorted list:"lensort: equ $-sortcomma: db ","len: equ $-commaendl: db 10%macro printstr 2mov eax , 4mov ebx , 1mov ecx , %1mov edx , %2int 80h%endmacro%macro readstr 2mov eax , 3mov ebx , 0mov ecx , %1mov edx , %2int 80h%endmacrosection .bssarray resb 50count resb 1count1 resb 1count2 resb 1digit0 resb 1digit1 resb 1temp resb 1pos resb 1i resb 1j resb 1section .textglobal _start_start:printstr askcount,lencountreadstr digit0,1readstr digit1,1readstr temp,1sub byte[digit0],30hsub byte[digit1],30hmov cl,byte[digit0]mov ch,byte[digit1]mov al,10mul cladd al,chmov byte[count],almov byte[count1],almov byte[count2],almov ebx,arrayrdloop:push ebxprintstr element,lenelmntreadstr digit0,1readstr digit1,1readstr temp,1sub byte[digit0],30hsub byte[digit1],30hmov cl,byte[digit0]mov ch,byte[digit1]mov al,10mul cladd al,chpop ebxmov byte[ebx],aladd ebx,1dec byte[count]cmp byte[count],0jne rdloopmov ebx,arraymov byte[i],0i_loop:mov byte[j],0j_loop:mov ebx,arraymovzx eax,byte[j]add ebx,eaxmov eax,ebxadd eax,1mov dl,byte[ebx]mov dh,byte[eax]cmp dh,dljc swapjmp nextswap:mov byte[ebx],dhmov byte[eax],dlnext:inc byte[j]mov al,byte[count1]sub al,byte[i]sub al,1cmp byte[j],aljl j_loopinc byte[i]mov al,byte[count1]cmp byte[i],aljl i_loopprintstr sort,lensortmov ebx,arrayprntloop:push ebxmov al,byte[ebx]mov byte[pos],almovzx ax,byte[pos]mov bl,10div blmov byte[digit0],almov byte[digit1],ahadd byte[digit0],30hadd byte[digit1],30hprintstr digit0,1printstr digit1,1printstr comma,lenpop ebxadd ebx,1dec byte[count2]cmp byte[count2],0jne prntloopext:printstr endl,1mov eax,1mov ebx,0int 80hOutput:

Enter the array size: 05Enter the array element: 36Enter the array element: 10Enter the array element: 88Enter the array element: 15Enter the array element: 32

Sorted list: 10, 15, 32, 36, 88