Académique Documents
Professionnel Documents
Culture Documents
Page 1
; Macro definitions ; Macro definition to display the message PRINTF MACRO MSG ; Here, MSG is the parameter to the macro MOV AH, 09H MOV DX, OFFSET MSG INT 21H ENDM ; Macro to terminate the program (return control to DOS) EXIT MACRO MOV AH, 4CH INT 21H ENDM ;---------------------------Data segment starts-----------------------------.DATA A DW 1111H, 2222H, 3333H, 4444H, 5555H N DW ($-A)/2 ; Number of terms of N KEY DW 5555H ; Key to be searched LOW_ DW ? HIGH_ DW ? MID DW ? MSG1 DB SUCCESSFUL SEARCH, 0AH, 0DH, 24H MSG2 DB UNSUCCESSFUL SEARCH, 0AH, 0DH, 24H ;---------------------------Data segment ends-------------------------------
Page 2
;Initialization for the loop MOV LOW, 0 MOV MOV DEC AX, N HIGH_, AX HIGH_
L1:
;Search using binary search MOV SI, LOW_ CMP SI, HIGH_ JG L4 ; mid (low + high ) / 2 ADD SI, HIGH_ SHR SI, 1
MOV MID, SI
;if ( low > high ) L4 ; SI low + high ; SI (low + high)/2 ; mid (low + high ) / 2
AX, KEY SI, MID SI, 1 AX, A[SI] L2 MSG1 AX, A[SI] L3
Page 3
L3:
; low mid + 1 MOV AX, MID INC AX MOV LOW_, AX JMP PRINTF EXIT L1 MSG2
L4:
END begin ;---------------------------Code segment ends-----------------------------Note: Since LOW and HIGH are assembler directives in 8086 assembly language, we use the identifiers LOW_ and HIGH_ instead of LOW and HIGH in the above program
Page 4
2A.ASM
Page 5
; Initialization of data segment register ; printf(Enter the characters); ; Initialize the index si to 0 ; Read a character ; Is character read is end of line
printf mov next: call cmp je mov inc jmp over: mov printf mov up: mov call inc loop mov int end ;end of the program
msg1 si, 00 getchar al, 0dh over a[si], al si next cx, si msg2 si, 00 al,a[si] putchar si up ah,4ch 21h begin
; Store the character in the array ; point to next location in array a ; Read the next character ; Numer of characters to be displayed ; printf(The typed characters are:); ; Initialize si to point to beginning of a ; Obtain the character from array ; Display the current character ; Point si to next character
Note: After typing 2A.ASM, Give the following commands: C:\>MASM 2A; C:\>EDIT 2B.ASM and type the program given in next page
Page 6
2B.ASM
; ---------------------Start of data segment ----------------------------------.data public a ;The data array is being made public allocating a db 10H dup(?) ; Reserved 10 locations. external function can access a ;-------------------- End of data segment -----------------------------------;--------------------Start of code segment---------------------.code public getchar ; External program can access getchar() public putchar ; External program can access putchar() ;Procedure to read key is made public getchar proc far mov ah,01 ; Function to read a character int 21h ; Character read is copied to AL register ret getchar endp ;Procedure to write a character putchar proc far mov ah,02 ; Function to display the character mov dl,al ; Character to be displayed int 21h ; Display the character ret putchar endp end; ;---------------------End of code segment ------------------------
Note: After typing 2B.ASM, Give the following commands: C:\>MASM 2b; If no errors, type the following command C:\>LINK 2A + 2B C:> 2A Enter the characters: PADMAREDDY The typed characters:PADMAREDDY
Page 7
Problem 3: Sort a given set of 'n' numbers in ascending and descending orders using the bubble sort algorithm. (Refer section 8.10.4, page 8.81)
; Program Name: BUBBLE SORT (BSORT.ASM) ; Author : A.M. Padma Reddy and YJ ; Purpose : To sort the given elements using bubble sort ; Inputs : List of elements to sort ; Outputs : Sorted array .MODEL SMALL ;---------------------------Data segment starts-----------------------------.DATA A DW 5555H, 4444H, 3333H, 2222H, 1111H N DW ($-A)/2 ; Number of terms of N I DW ? J DW ? TEMP DW ? ;---------------------------Data segment ends------------------------------;---------------------------Code segment starts-----------------------------.CODE begin: ; Initialization of data segment register MOV MOV MOV MOV DEC L1: MOV AX, @DATA DS, AX J, 1 CX, N CX SI, 0 ; Initial pass ; CX N-1 ; Index to the first item ; of array A ; DX = N J is the ; counter for inner loop ; AX a[si] ; If a[si] <= a[si+2])
Page 8
MOV SUB
DX, N DX, J
L2:
MOV CMP
; since AX contains ; temp ; Point to next word ; decrement inner loop ; if (DX !=0) goto L2 ; if (CX !=0) goto L1 ; i.e., goto next pass ; Stop execution at this ; point
SI, 2 DX L2 L1
INT
3H
Page 9
;--------------------------Data segment starts-----------------------.DATA MSG1 DB "Enter a character",0ah, 0dh, 24h MSG2 DB "The ASCII value: ",24h ;--------------------------Data segment ends--------------------------
Page 10
; Convert the lower nibble into ASCII AND BL, 0FH ; Mask the higher nibble CMP BL, 0AH JL L1 ADD BL, 07H ADD BL, 30H
L1:
; Obtain ASCII value of higher nibble AND BH, 0F0H ; Mask the lower nibble MOV CL, 04 SHR BH, CL ; Move to higher nibble to lower nibble CMP BH, 0AH JL L2 ADD BH, 07H ADD BH, 30H PRINTF MSG2 PUTCHAR BH PUTCHAR BL EXIT END begin
L2:
; If greater than 9, add 7 ; Conver into ASCII by adding 30H ; Display "The ASCII Value =" ; display higher nibble of ASCII value ; display lower nibble of ASCII value
Page 11
; Macro definitions ; Macro definition to display the message PRINTF MACRO MSG ; Here, MSG is the parameter to the macro MOV AH, 09H MOV DX, OFFSET MSG INT 21H ENDM ; Macro to terminate the program (return control to DOS) EXIT MACRO MOV AH, 4CH INT 21H ENDM ;----------- Data segment starts-----------------.DATA STR1 DB LIRIL N DW $ STR1 STR2 DB 10 DUP(?) ; Memory representation ; Given string ; Length of given string ; Storage space for reversed string
MSG1 DB String is palindrome, 0AH, 0DH, 24H MSG2 DB String in not a palindrome, 0AH, 0DH, 24H ;------------ Data segment ends----------------;---------------------------Code segment starts-----------------------------.CODE begin: ; Initialization of data segment register MOV MOV AX, @DATA DS, AX
Page 12
; Copy STR1[SI] to ; STR2[DI] ; Point to previous character ; Point to next character ; Copy all characters of STR1
LOOP L1 ; Reversing of string is complete ; Check for the palindrome ; Initialization MOV SI, 0 MOV CX, N L2: MOV CMP JE PRINTF EXIT L3: INC LOOP AL, STR1[SI] AL, STR2[SI] L3 MSG2
; SI points to beginning of STR1 ; No. of characters to compare ; Compare STR1[SI] with STR2[DI]
; String is not a palindrome ; Terminate the program ; Point to next character of STR1&2 ; Compare N times
SI L2
PRINTF MSG1 ; String is a palindrome EXIT ; Terminate the program END begin ; ----------------------------Code segment ends --------------------------------
Page 13
; Function to read a string ; Address of the buffer ; AL has the character read
;--------------------------Data segment starts-----------------------.DATA MSG1 DB 0AH, 0DH,"Enter the first string",0ah, 0dh, 24h MSG2 DB 0AH,0DH,"Enter the second string, 0ah, 0dh,24h MSG3 DB 0AH,0DHThe two strings are equal, 0AH, 0DH, 24H Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore
Page 14
; Check length of two strings MOV AL, N1 CMP AL, N2 JE L1 ; If lengths are equal compare strings PRINTF JMP MSG4 L3 ; printf(Two strings are not equal);
; Initialization before comparing L1: LEA LEA MOV CLD SI, A1 DI, A2 CL, N1 ; Offset value of first string ; Offset value of second string ; No of characters to be compared ; To increment increment index registers ; Compare as long as they are equal
Page 15
REPE CMPSB
Page 16
;Attribute of character ;Activity is to scroll ;No. of lines to scroll-0 blank screen ;y-coordinate of upper left corner ;x-coordinate of upper right corner ;Number of rows; Lower right row ;Number of columns; Lower right column ; clear the screen
;Macro to set the cursor ; setcursor macro row, col mov ah, 02 mov bh, 0 mov dh, row mov dl, col int 10h endm ;Macro equvivalent to ; printf() ; scanf() pri_scn macro mov mov int endm
; row and col are the parameters ; Function to set the cursor ; Current page ; Which row ; Which column ; Set the cursor
; val function to read or display ; msg is the parameter to be displayed dx, offset msg ; DX holds offset of message to be displayed ah, 09 ; Function to display the message 21h ; Display the message val, msg
Page 17
endm ;-------------Start of the data segment--------.data name1 db "What is your name?",24h msg db "Enter the name",0ah,0dh,24h buffer db 10 ; Max. no. of charcters in the name n db ? ; Space to store length of name a db 20 dup(?) ; Space to store the name row col db 10 db 20 ; Display at this row ; Display at this column
;---------------Start of code segment ----------.code mov ax, @data mov ds, ax prn_scn prn_scn clrscr 09, msg 0ah, buffer ;printf("Enter the numbers"); ;scanf("%s",a); ; Clear the screen
Page 18
; CX: no. of characters to displayed ; initial offset value w.r.t. array a ; Display the character ; Point to next character ; If not over go to top ; Terminate the program
Page 19
L2:
L3:
Page 20
; k = k -1 ; Compute n-1Ck-1
; Initialization of DS register
;Compute nCk
INT 3H ; Terminate the program END begin ;-------------------------------end of the code segment -----------------------------------------Output: Give the following commands C:\> MASM NCK; C:\> LINK NCK; C:\> DEBUG NCK.EXE -G -D DS:0000 The following memory dump is obtained: 14D4:0000 D8 A0 0C 00 8A 1E 0D 00-E8 B5 FF CC 06 03 14 8F n k nCk = 14H
6
C3 = (20)10 = (14)16
Page 21
;---------------Start of the code segment ----------------.code ; Procedure to convert from HEX to ASCII and display hex_asc proc mov mov div
ah,00 bl,0ah bl
; Divide by 10 to obtain unpacked BCD ; Obtain the unpacked BCD in AH and AL ; Higher nibble in AL ; Lower nibble in AH ; Convert to ASCII value ; Display the character in AL ; Display the character in AH
ax,3030h al ah
disp begin:
; Function to read the system time ; Read the system time : CH:CL contains hours:minutes ; DH:DL contains second:millisec
Page 22
al,cl disp
al,dh disp
Page 23
dec_asci
Page 24
Page 25
ah,01h 21h
;Macro to display the message printf macro msg mov ah, 09 mov dx, offset msg int 21h endm .data
; msg is the parameter to be displayed ; Function to display the message ; DX hold offset of message to be displayed ; Display the message
row db ? ; Space to store row value col db ? ; Space to store column value msg1 db "Enter the row value :",0ah,0dh,24h msg2 db "Enter the col value :",0ah,0dh,24h .code Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore
Page 26
C:\MASM615>cursor Enter the row value :10 Enter the col value :20 Note: Observe on the screen that cursor will be at row 10 and column 20
Page 27
db 0ah,0dh,"File successfully deleted",24h db 0ah,0dh,"Error in deleting: File may not exist or readonly",24h
;--------------------Start of code segment ------------------.code mov ax,@data mov ds,ax prn_scn prn_scn 09,msg1 0ah, buff; ; printf(Enter the file name);; ; scanf(%s,fname);
Page 28
; Function to delete the file ; DX contains name of file ; Delete the file ; If Carray is set delete failed ; File deleted successfully ; Terminate the program ; File not deleted ; Terminate the program
09h,msg3
Page 29