Showing posts with label Systems Programming. Show all posts
Showing posts with label Systems Programming. Show all posts
Monday, April 18, 2011
Friday, March 18, 2011
LIBRARY FILE
;*******************************************************
; Some esential macros for elegant programming in masm *
;*******************************************************
exit macro
mov ax,4c00h
int 21h
endm
space macro
mov ah,02h
mov dl,20h
int 21h
endm
;----------------------------------------------------
;Accepts single 2-digit hex nos.
;output: input value stored in AL
;regs. affected: AX,BH
;----------------------------------------------------
input macro ;two hex-digit input
local ok,p,ok1,p1
mov ah,01h
int 21h
cmp al,39h ;ascii value of 9
jbe ok
sub al,61h ;ascii value of a
add al,0ah
jmp p
ok: sub al,30h
p: rcl al,01h
rcl al,01h
rcl al,01h
rcl al,01h
mov bh,al
mov ah,01h
int 21h
cmp al,39h ;ascii value of 9
jbe ok1
sub al,61h ;ascii value of a
add al,0ah
jmp p1
ok1:sub al,30h
p1: add al,bh
endm
;---------------------------------------------------
;Prints two hex digits on console
;input: provide the digits in BH
;regs. affected: AX,DX
;---------------------------------------------------
output macro ;two hex-digit output
local ok1,show,ok2,show2
mov al,bh ;the digits to display is in BH
and al,0fh
mov dh,al
mov al,bh
and al,11110000b ;even binary values allowed in masm
rcr al,01h
rcr al,01h
rcr al,01h
rcr al,01h
mov dl,al
cmp dl,09h
jbe ok1
sub dl,0ah
add dl,41h ;hex digit handler
jmp show
ok1:add dl,30h
show:
mov ah,02h
int 21h
mov dl,dh
cmp dl,09h
jbe ok2
sub dl,0ah
add dl,41h ;hex digit handler
jmp show2
ok2:add dl,30h
show2: int 21h
endm
showmsg macro str ;str data pssed through macro
mov ax,@data
mov ds,ax
lea dx,str
mov ah,09h
int 21h
endm
str_length macro str ;computes length of a string
local lp1,done
mov ax,@data
mov ds,ax
mov si,00h
mov cx,0000h ;clear cx
lp1:
cmp str[si],24h ;check if its '$'
je done
;else still chars left
inc si
inc cx
jmp lp1
done:
mov ax,cx
endm
;---------------------------------------------------
;Accepts input, and places them in an array
;array: is the address of the location from where inputs would be stored
;count: the no. of inputs
;---------------------------------------------------
input_array macro array,count,str
local lp
mov cx,count ;the array size
mov si,0000h
mov bh,00h
showmsg str
lp: input ;call the single input macro
mov array[si],al
inc si
space
loop lp
endm
;---------------------------------------------------
;Setting the cursor
;Input: set row number in DH & column number in DL
;Registers affected: AH,BH
;---------------------------------------------------
cursor macro
mov ah,02h ;request set cursor
mov bh,00h ;page number 0
int 10h ;Interrupt-call BIOS
endm
;---------------------------------------------------
;Clearing the screen
;Registers affected: AX,BH,CX,DX
;---------------------------------------------------
clrscrn macro
mov ax,0600h ;ah 06(scroll), al 00 (full screen)
mov bh,71h ;attribute: white (7) on blue (1)
mov cx,0000h ;upper left row:column
mov dx,184fh ;lower right row:column
int 10h ;Interrupt-call BIOS
endm
;---------------------------------------------------
;display array
;Input: Array address, the size of the array and an output message
;---------------------------------------------------
display_array macro array,count,str
local lp
pusha
mov cx,count ;the array size
mov si,0000h
mov bh,00h
showmsg str
lp: mov bh,array[si] ;retrieving the individual terms of the array before calling the output macro
output ;calling output macro
inc si
space
loop lp
pop a
endm
Labels:
Systems Programming
Thursday, February 10, 2011
Binary to Hexadecimal
%OUT-----------------------------------------------------------------------------------------
%OUT- Prints a 8-bit binary number in its equivalent hexadecimal form
%out- input: 8-bit binary string accepted from the user
%out- output: 1 byte Hexadecimal equivalent of the binary string is printed
%out – author: Ayon and Ariyam
%OUT-----------------------------------------------------------------------------------------
if1
include macrolib.lby
endif
.model small
.stack
.data
count equ 08h ;for 8-bit binary string
ip db "Enter a 8-bit binary number: ","$" ;a request to prompt the user for entering the binary no.
op db " Hexadecimal form: ","$" ;output message
err db " Invalid Entry!","$" ;error message
heading db "***** BINARY to HEXADECIMAL *****","$" ;a heading
.code
main proc
;clrscrn ;clearing the screen
mov dx,0105h
;cursor ;setting up the cursor
showmsg heading ;displaying heading
mov dx,0303h ;pointing to next line
;cursor ;setting up the cursor
showmsg ip ;prompt the user to enter the binary string
xor bh,bh ;clearing register Bh
mov cx,count ;loading cx with length of the binary string
continue: rol bh,01h ;rotate BH left, 0 will come in the LSB of BH, as intially BH was cleared
mov ah,01h
int 21h
sub al,30h ;getting the binary digit (bit 0 or 1) in AL
cmp al,01h ;when bit is greater than 0 or 1
ja WRONG
add bh,al ;add the input bit (0 or 1) to BH
loop continue ;iterate till 8 bits are accepted from the user
showmsg op ;display output message, content of BH not hampered
output ;calling output macro
jmp FINISH
WRONG: showmsg err ;displaying error message
FINISH: exit
main endp
end main
Labels:
Systems Programming
Merge Sort
%OUT------------------------------------------------------------------------------------------------------------------------------------
%OUT- Accept two arrays, merge them and then sort them in ascending order using any SORTING algorithm
%out- input: Each term of the each of the two arrays is accepted from the user
; Sizes of the arrays are mentioned in the program itself
%out- output: Print the arrays after merging them and reprint after sorting them
%out – author: Ayon and Ariyam
%OUT---------------------------------------------------------------------------------------------------------------------------------------
if1
include macrolib.lby
endif
.model small
.stack
.data
array_size1 equ 4 ;assuming array_size1 to be equal to 3
array1 db array_size1 dup(?) ;reserving bytes = array_size1 for the array1, uninitialised
array_size2 equ 3 ;assuming array_size2 to be equal to 3
array2 db array_size2 dup(?) ;reserving bytes = array_size2 for the array2, uninitialised
merge_size equ 7 ;this will be equal to array_size1+array_size2
ip1 db "Enter terms for 1st array: ","$" ;a request to prompt the user for entering terms of the 1st array
ip2 db "Enter terms for 2nd array: ","$" ;a request to prompt the user for entering terms of the 2nd array
op1 db "After merging: ","$" ;output message
op2 db "After sorting: ","$" ;output message
heading db "***** MERGE & THEN SORT *****","$" ;a heading
.code
main proc
clrscrn ;clearing the screen
mov dx,0105h
cursor ;setting up the cursor
showmsg heading ;displaying heading
mov dx,0303h ;pointing to next line
cursor ;setting up the cursor
input_array array1,array_size1,ip1 ;accept array from user
mov dx,0503h ;pointing to next line
cursor ;setting up the cursor
input_array array2,array_size2,ip2 ;accept array from user
; ============= begin merging ==================================
mov si,array_size1
mov di,00h
mov cx,array_size2
merge: mov ah,array2[di]
mov array1[si],ah ;merge the element from second array into first at the end
inc di
inc si
loop merge
mov dx,0703h ;pointing to next line
cursor ;setting up the cursor
display_array array1,merge_size,op1 ;print the merged array
; ============== begin sorting (bubble sort) ===================
mov cx,merge_size
sub cx,01h
outer: push cx ;saving the current iteration number
mov bx,0001h
;loading bx (displacement for based indexed addressing with displacement)
mov di,00h ;pointer to first array element
inner: mov al,array1[di]
cmp al,array1[di+bx] ;comparing 2 consecutive array elements
jbe continue
swap: mov ah,array1[di+bx]
;performing swap by using two registers ah and al, when succeeding array element is smaller
mov array1[di],ah
mov array1[di+bx],al
continue: inc di ;point to next array element
loop inner ;iterate till the maximum is bubbled down
pop cx ;loader count for outer loop
dec cx
cmp cx,0000h ;short jump not possible
jne outer
mov dx,0903h ;pointing to next line
cursor ;setting up the cursor
display_array array1, merge_size, op2 ;print the merged array after sorting
exit
main endp
end main
Labels:
Systems Programming
Bubble Sort
%OUT-----------------------------------------------------------------------------------------
%OUT- Arranges an array in ascending order using BUBBLE SORT
%out- input: Each term of the array is accepted from the user
; Size of the array is mentioned in the program itself
%out- output: Accepted array printed in ASCENDING order
%out – author: Ayon and Ariyam
%OUT-----------------------------------------------------------------------------------------
if1
include macrolib.lby
endif
.model small
.stack
.data
array_size equ 6 ;assuming array_size to be equal to 6
array db array_size dup(?) ;reserving bytes = array_size for the array, uninitialised
ip db "Enter terms of the array: ","$"
;a request to prompt the user for entering terms of the array
op1 db "Array in ascending order: ","$" ;output message
op2 db "Next iteration: ","$" ;intermediate output message
heading db "***** BUBBLE SORT *****","$" ;a heading
.code
main proc
clrscrn ;clearing the screen
mov dx,0105h
cursor ;setting up the cursor
showmsg heading ;displaying heading
mov dx,0303h ;pointing to next line
push dx ;saving current cursor position
cursor ;setting up the cursor
input_array array,array_size,ip ;accept array from user
mov cx,array_size
sub cx,01h
outer: pop dx ;retrieving the current cursor position
add dh,02h ;pointing the cursor to the next line
push dx ;saving current cursor position
cursor
push cx ;saving the current iteration number
mov bx,0001h ;loading bx (displacement for based indexed
;addressing with displacement)
mov di,00h ;pointer to first array element
inner: mov al,array[di]
cmp al,array[di+bx] ;comparing 2 consecutive array elements
jbe continue
swap: mov ah,array[di+bx] ;performing swap by using two registers ah and al,
;when succeeding array element is smaller
mov array[di],ah
mov array[di+bx],al
continue: inc di ;point to next array element
loop inner ;iterate till the maximum is bubbled down
display_array array,array_size,op2 ;display the result of this iteration
pop cx ;loader count for outer loop
dec cx
cmp cx,0000h ;short jump not possible
jne outer
pop dx
add dh,02h ;pointing the cursor to the final line
cursor
display_array array,array_size,op1 ;printing array after sorting
exit
main endp
end main
Labels:
Systems Programming
Second Maximum & Second Minimum
%OUT---------------------------------------------------------------------------------------------------------
%OUT- Find second maximum & second minimum from an array accepted from user
%out- input: Each term of the array is accepted from the user
; Size of the array is mentioned in the program itself
%out- output: Print second maximum & second minimum
%out – author: Ayon and Ariyam
%OUT---------------------------------------------------------------------------------------------------------
if1
include macrolib.lby
endif
.model small
.stack
.data
array_size equ 6 ;assuming array_size to be equal to 6
array db array_size dup(?) ;reserving bytes = array_size for the array, uninitialised
ip db "Enter terms of the array: ","$"
;a request to prompt the user for entering terms of the array
op1 db "Second Maximum: ","$" ;output message
op2 db "Second Minimum: ","$" ;output message
heading db "** SECOND MAX - SECOND MIN **","$" ;a heading
.code
main proc
clrscrn ;clearing the screen
mov dx,0105h
cursor ;setting up the cursor
showmsg heading ;displaying heading
mov dx,0303h ;pointing to next line
cursor ;setting up the cursor
input_array array,array_size,ip ;accept array from user
mov dx,0503h ;advancing to next line
cursor
mov dh,00h ;dh will store first maximum
mov dl,00h ;dl will store second maximum
mov cx,array_size ;loading cx with number of times iteration to take place
mov si,00h ;setting up the index to point to the array element
outer: cmp array[si],dl
jbe done
cmp array[si],dh
jae replace
mov dl,array[si]
jmp done
replace: mov dl,dh
mov dh,array[si]
done: inc si
loop outer
mov bh,dl ;storing 2nd maximum in BH to call 'output' macro
showmsg op1
output
mov dx,0703h ;advancing to next line
cursor
mov dh,0FFh ;dh will store first minimum
mov dl,0FFh ;dl will store second minimum
mov cx,array_size ;loading cx with number of times iteration to take place
mov si,00h ;setting up the index to point to the array element
outer1: cmp array[si],dl
jae done1
cmp array[si],dh
jbe replace1
mov dl,array[si]
jmp done1
replace1: mov dl,dh
mov dh,array[si]
done1: inc si
loop outer1
mov bh,dl ;storing 2nd maximum in BH to call 'output' macro
showmsg op2
output
exit
main endp
end main
Labels:
Systems Programming
Subscribe to:
Posts (Atom)