Tuesday, May 3, 2011

Decimal to Octal

Share Orkut
print macro msg mov ah,09h lea dx,msg int 21h endm readmul macro d,j1,j2 mov cx,0ah j1:mov ah,01h int 21h cmp al,0dh je j2 sub al,30h mov bl,al mov ax,d xor bh,bh mul cx add ax,bx mov d,ax jmp j1 j2:nop endm .model small .stack 100h .data str1 db 10,13,'Enter a decimal number $' n dw 0 f db 0 str2 db 10,13,'Octal is $' .code main proc mov ax,@data mov ds,ax print str1 readmul n,jump1,jump2 mov cx,6 mov ax,n loop11:xor bx,bx shr ax,1 rcr bx,1 shr ax,1 rcr bx,1 shr ax,1 rcr bx,1 mov bl,bh rol bl,1 rol bl,1 rol bl,1 push bx loop loop11 print str2 mov cx,6 l11:pop dx cmp dl,f je j45 dec f add dl,30h mov ah,02h int 21h j45:loop l11 mov ah,4ch int 21h main endp end

Sort the characters in a string

Share Orkut
print macro msg lea dx,msg mov ah,09h int 21h endm .model small .stack 100h .data msg1 db 10,13,'Enter the string $' msg2 db 10,13,'Sorted String $' str1 db 50 dup('$') len1 db 0 .code mov ax,@data mov ds,ax print msg1 mov ah,3fh lea dx,str1 mov bx,00 int 21h sub ax,02 mov len1,al mov dl,len1 dec dl j1:lea si,str1 xor cx,cx mov cl,dl j3:mov al,[si] cmp al,1[si] jc j2 mov ah,1[si] mov 1[si],al mov [si],ah j2:inc si loop j3 dec dl cmp dl,00 jnz j1 print msg2 print str1 mov ah,4ch int 21h end

Quick Sort

Share Orkut
print macro msg lea dx,msg mov ah,09h int 21h endm read macro n,j1,j2 j1:mov ah,01h int 21h cmp al,0dh je j2 sub al,30h mov bl,al mov ax,n mov dx,0ah mul dx xor bh,bh add ax,bx mov n,ax jmp j1 j2 :nop endm printmul macro n1,l2,l3 mov bx,000ah mov ax,n1 xor cx,cx ;push into stack l2:xor dx,dx div bx push dx inc cx cmp ax,0000h jne l2 ;pop from stack l3:pop dx add dl,30h mov ah,02h int 21h loop l3 endm .model small .stack 100h .data msg1 db 10,13,'Enter a number: $' msg2 db 10,13,' Sorted Array $' msg3 db 10,13,' $' msg4 db 10,13,'Enter the size of array: $' msg5 db 10,13,' Orginal Array $' n dw 0 num dw 50 dup(0) sml dw 0 .code main proc mov ax,@data mov ds,ax print msg4 read n,jump1,jump2 ;read n elements mov cx,n mov si,00h loop1:print msg1 ;read a multidigit number read num[si],jump3,jump4 add si,02 loop loop1 ;printing unsorted array print msg5 call display mov ax,n mov ah,2 mul ah mov si,ax sub si,02 lea di,num[si] lea si,num call quicksort ;printing sorted array print msg2 call display mov ah,4ch int 21h main endp quicksort proc cmp si,di jnc j1 push si push di call part mov dx,si pop di pop si push di mov di,dx push di sub di,02 call quicksort pop si add si,02 pop di call quicksort j1:ret quicksort endp part proc mov ax,[di] mov cx,di mov di,si sub si,02 j21:cmp ax,[di] jc j11 add si,02 mov bx,[si] mov dx,[di] mov [si],dx mov [di],bx j11:add di,02 cmp di,cx jnz j21 add si,2 mov bx,[si] mov di,cx mov [di],bx mov [si],ax ret part endp display proc mov cx,n mov si,00h l4: push cx print msg3 printmul num[si],l5,l6 add si,02h pop cx loop l4 ret display endp end

Monday, May 2, 2011

Sum of row elements of a matrix

Share Orkut
print macro msg lea dx,msg mov ah,09h int 21h endm reads macro m mov ah,01h int 21h sub al,30h mov m,al endm read macro n,j1,j2 j1:mov ah,01h int 21h cmp al,0dh je j2 sub al,30h mov bl,al mov ax,n mov dx,0ah mul dx xor bh,bh add ax,bx mov n,ax jmp j1 j2 :nop endm readmat macro mat,l1,j3,j4,newmsg mov cx,noe mov si,00h print msg6 l1:print newmsg ;read a multidigit number read mat[si],j3,j4 add si,02 loop l1 endm printmul macro n1,l2,l3 mov bx,000ah mov ax,n1 xor cx,cx ;push into stack l2:xor dx,dx div bx push dx inc cx cmp ax,0000h jne l2 ;pop from stack l3:pop dx add dl,30h mov ah,02h int 21h loop l3 endm printmat macro mat1,l4,l5,l6,j5 mov cx,noe mov si,00h l4: push cx print msg7 printmul mat1[si],l5,l6 add si,02h inc f mov bl,f cmp bl,colum jne j5 print msg6 mov f,00h j5: pop cx loop l4 endm .model small .stack 100h .data msg1 db 10,13,'Enter the no of rows: $' msg2 db 10,13,'Enter the no of column: $' msg3 db 10,13,'Enter the element of matrix : $' msg5 db 10,13,' Sum of elements of row $' msg6 db 10,13,' $' msg7 db ' $' msg9 db ': $' msg8 db 10,13,' 1st Matrix$' row db 0 colum db 0 matrix1 dw 100 dup(0) sum dw 0 corow dw 1 f db 0 noe dw 0 .code main proc mov ax,@data mov ds,ax ;read no of rows print msg1 reads row ;read no of column print msg2 reads colum ;calculating total no of elements mov al,row mov bl,colum mul bl mov noe,ax ;read 1st matrix elements readmat matrix1,loop1,jump1,jump2,msg3 ;print matrix print msg8 print msg6 printmat matrix1,loop4,loop5,loop6,jump5 ;row element addition mov cx,noe mov si,00h mov di,00h xor ax,ax loop3:add ax,matrix1[si] add si,02h inc di mov bx,di cmp bl,colum jne j10 mov sum,ax print msg5 mov dx,corow add dl,30h mov ah,02h int 21h print msg9 push cx printmul sum,loop2,jump4 pop cx inc corow xor ax,ax mov di,ax j10:loop loop3 mov ah,4ch int 21h main endp end

Insert a string in middle of another string

Share Orkut
.model small .stack 100h .data msg1 db 'Enter 1st string $' msg2 db 'Enter 2nd string $' msg4 db 'Result string is-> $' s1 db 50 dup('$') s2 db 50 dup('$') s3 db 50 dup('$') len1 dw ? len2 dw ? pos dw 0 .code main proc mov ax,@data mov ds,ax mov es,ax ;display 'enter 1st string' lea dx,msg1 call display ;read the 1st string lea di,s1 call readstr endinpt:mov len1,cx ;jump to nextline call newline ;display 'enter 2nd string' lea dx,msg2 call display ;read the 2nd string lea di,s2 call readstr endinp1:mov len2,cx ;find the position xor dx,dx mov ax,len1 mov bx,02h div bx mov pos,ax ;insert a string into another string endpos: xor cx,cx lea si,s1 lea di,s3 cld mov cx,pos rep movsb mov dx,si lea si,s2 mov cx,len2 rep movsb mov cx,len1 sub cx,pos mov si,dx rep movsb ;jump to nextline call newline ;display 'Result string is-> ' lea dx,msg4 call display ;print result string lea dx,s3 call display mov ah,4ch int 21h main endp display proc mov ah,09h int 21h ret display endp newline proc mov dl,0ah mov ah,02h int 21h ret newline endp readstr proc xor cx,cx cld loop1: mov ah,01 int 21h cmp al,0dh je endread stosb inc cx jmp loop1 endread:ret readstr endp end

To find nCr

Share Orkut
n & r should be less than 8, since from 9! onwards we need more than 16 bits to store it.
print macro msg lea dx,msg mov ah,09h int 21h endm read macro n,j1,j2 mov cx,0ah j1:mov ah,01h int 21h cmp al,0dh je j2 sub al,30h mov bl,al mov ax,n mul cx xor bh,bh add ax,bx mov n,ax jmp j1 j2 :nop endm .model small .stack 100h .data msg1 db 10,13,'Enter n: $' msg2 db 10,13,'Enter r: $' msg3 db 'C$' msg4 db ' = $' err db 10,13,'Math Error. n should not be less than r $' newline db 10,13,' $' n dw 0 r dw 0 nfact dw 0 rfact dw 0 nminur dw 0 temp dw 0 .code main proc mov ax,@data mov ds,ax print msg1 ;reading n read n,jump1,jump2 print msg2 ;reading n read r,jump11,jump21 ;find n! mov ax,n mov temp,ax call fact mov nfact,ax ;find r! mov ax,r mov temp,ax call fact mov rfact,ax ;find (n-r)! mov ax,n sub ax,r jc error1 mov temp,ax call fact mov nminur,ax ;find n!/(r!*(n-r)!) mov ax,rfact mov bx,nminur mul bx mov bx,ax mov ax,nfact div bx mov temp,ax print newline mov ax,n call printmul print msg3 mov ax,r call printmul print msg4 mov ax,temp jump3:call printmul jmp finish error1:print err finish:mov ah,4ch int 21h main endp printmul proc mov bx,000ah xor cx,cx ;push into stack l2:xor dx,dx div bx push dx inc cx cmp ax,0000h jne l2 ;pop from stack l3:pop dx add dl,30h mov ah,02h int 21h loop l3 ret printmul endp fact proc mov ax,01 loop1:cmp temp,00 je ju12 mul temp dec temp jmp loop1 ju12:ret fact endp end

Saturday, February 19, 2011

Cursor operations-Change size and Move

Share Orkut
print macro msg lea dx,msg mov ah,09h int 21h endm .model small .stack 100h .data msg1 db 10,13,'Press 1 to increase and 0 to decrease
     the size of cursor (only 4 sizes, initially minimum) $'
msg2 db 10,13,'Press 8,2,4 and 6 to move the cursor
     up,down,left and right respectively $'
msg3 db 10,13,'Press ENTER to finish $' .code main proc mov ax,@data mov ds,ax mov cl,8 mov ch,8 ;clear screen mov ah,0 mov al,03 int 10h print msg1 print msg2 print msg3 loop1:; wait key press: xor ax, ax int 16h cmp al,0dh je exit cmp al,31h je incr cmp al,30h je decr cmp al,38h je up cmp al,32h je down cmp al,36h je right cmp al,34h jne loop1 call getcursor dec dl call putcursor jmp loop1 up: call getcursor dec dh call putcursor jmp loop1 down:call getcursor inc dh call putcursor jmp loop1 right:call getcursor inc dl call putcursor jmp loop1 decr:cmp ch,08 je loop1 rol ch,1 call chgcursor jmp loop1 incr:cmp ch,01 je loop1 ror ch,1 call chgcursor jmp loop1 exit:mov ah,4ch int 21h main endp chgcursor proc ;change cursor size mov ah, 1 int 10h ret chgcursor endp getcursor proc ;get cursor position and size mov ah,03h int 10h ret getcursor endp putcursor proc ;show the cursor mov ah,02h int 10h ret putcursor endp end