University of Kashmir, North Campus Course Code Course Name Course Instructor MCA-104-DCE Assembly Language Programming Bilal Ahmad Dar CONTENTS 1. Display a Message 2 2. Display a one Digit Number 3 3. Accept a Character from keyboard and display the character 4 4. Addition of two 1 digit numbers 5 5. Subtraction of two 1 digit numbers 7 6. Multiplication of two 1 digit numbers 9 7. Division of two 1 digit numbers 11 8. Check whether a number is Even or Odd(one digit) 13 9. Check whether a number is Prime or not(one digit) 14 10. Find Factorial of a number (number<=6) 16 11. Check whether a string is Palindrome or not 19 1
Display a Message msg DB Hello World$" ; define memory model ; beginning of stack segment ; beginning of data segment ;message to be displayed ; $ is required (same as \0 in C) MOV DX, OFFSET MSG ; beginning of code segment ; start execution ; display message using 09h DOS function MOV AH, 09H END ; return to DOS ; end of file 2
Display a one Digit Number ; define memory model ; beginning of stack segment ; beginning of data segment Var DB 10 ;declare Var as data byte and initialize it with 10 ; beginning of code segment ; start execution MOV DL, Var ADD DL, 48 ; store 58 (ASCII of 10) in DL MOV AH, 02 ; Display 5 END ; return to DOS ; end of file 3
Accept a Character from keyboard and display the character MOV AH, 01H ; define memory model ; beginning of stack segment ; beginning of data segment ; beginning of code segment ; start execution ; accept a character from keyboard MOV DL, AL MOV AH, 02H ; move ASCII of character into DL ; Print the Character END ; return to DOS ; end of file 4
Addition of two 1 digit numbers TEN DB 10 Msg1 DB "Enter First Number:$" Msg2 DB " Enter second Number:$" Msg3 DB "SUM:$ MOV DX,OFFSET Msg1 ; Display message Enter first Number MOV AH,01H ; accept first number SUB AL,48 MOV BL,AL MOV DX,OFFSET Msg2 ; convert ASCII value of number into decimal ; Store first number in BL ; Display message Enter second Number MOV AH,01H ; accept second number SUB AL,48 MOV BH,AL ADD BL,BH MOV DL,10 ; convert ASCII value of number into decimal ; Store second number in BH ; BL = BL +BH ;Ascii value of newline 5
;display newline MOV DL,13 ;Ascii value of Carriage return ;display carriage return MOV DX,OFFSET Msg3 ; Display message Sum MOV AL, BL MOV AH,0H DIV TEN ; store result in AL ; clear AH ; divide result(al) with 10, after operation ; AL=quotient, AH= remainder MOV CL,AH CMP AL,0 JZ LAB MOV DL,AL ; store remainder in CL ;check if quotient = 0 then ; jump to label LAB ;display Ascii value of quotient if sum is 2 digit ADD DL,48 LAB: MOV DL,CL ;display AScii value of remainder ADD DL,48 END 6
Subtraction of two 1 digit numbers ;first number should be greater than second number Msg1 DB "Enter First Number:$" Msg2 DB " Enter Second Number:$" Msg3 DB "Difference:$" MOV DX, OFFSET Msg1 ; Display message Enter first Number MOV AH, 09H MOV AH, 01H ; accept first number SUB AL, 48 MOV BL, AL ; convert ASCII value of number into decimal ; Store first number in BL MOV DX, OFFSET Msg2 MOV AH, 09H ; Display message Enter second Number MOV AH, 01H ; accept second number SUB AL, 48 MOV BH,AL SUB BL,BH ; convert ASCII value of number into decimal ; Store second number in BH ; BL=BL-BH 7
MOV DL,10 ;Ascii value of newline ; print Newline MOV DL,13 ;Ascii value of Carriage return ; print carriage return MOV DX,OFFSET Msg3 ; Display message Difference MOV DL,BL ADD DL,48 MOV AH, 02H ; convert decimal value of result into ASCII ; display result END 8
Multiplication of two 1 digit numbers TEN DB 10 Msg1 DB "Enter First Number:$" Msg2 DB " Enter second Number:$" Msg3 DB "PRODUCT:$ MOV DX,OFFSET Msg1 ; Display message Enter first Number MOV AH,01H ; accept first number SUB AL,48 MOV BL,AL MOV DX,OFFSET Msg2 ; convert ASCII value of number into decimal ; Store first number in BL ; Display message Enter second Number MOV AH,01H ; accept second number SUB AL,48 MOV BH,AL MOV AL,BL MOV AH,0 MOV BL,BH ; convert ASCII value of number into decimal ; Store second number in BH ; multiplicand(8bit) must reside in AL for Multiplication ; clear AH ;move second number in BL 9
MOV BH,0 MUL BX MOV BX,AX MOV DL,10 ;clear BH ;AX=AX*BX ;move result in BX ;Ascii value of newline ;display newline MOV DL,13 ;Ascii value of Carriage return ;display carriage return MOV DX,OFFSET Msg3 ; Display message Product MOV AL, BL MOV AH,0H ; store result in AL ; clear AH DIV TEN ; divide result(al) with 10, after operation ; AL=quotient, AH= remainder MOV CL,AH CMP AL,0 JZ LAB MOV DL,AL ; store remainder in CL ;check if quotient = 0 then ; jump to label LAB ;display Ascii value of quotient if sum is 2 digit ADD DL,48 LAB: MOV DL,CL ;display AScii value of remainder ADD DL,48 END 10
Division of two 1 digit numbers TEN DB 10 Msg1 DB "Enter Dividend:$" Msg2 DB " Enter Divisor:$" Msg3 DB "Quotient:$ Msg4 DB Remainder:$ MOV DX,OFFSET Msg1 ; Display message Enter first Number MOV AH,01H ; accept first number SUB AL,48 MOV BL,AL MOV DX,OFFSET Msg2 ; convert ASCII value of number into decimal ; Store first number in BL ; Display message Enter second Number MOV AH,01H ; accept second number SUB AL,48 MOV BH,AL MOV AL,BL ; convert ASCII value of number into decimal ; Store second number in BH ; Dividend(8bit) must reside in AL for Division 11
MOV AH,0 DIV BH MOV BH,AH MOV BL, AL MOV DL,10 ; clear AH ; AL=quotient, AH= remainder ;move remainder in BH ; move quotient in BL ;Ascii value of newline ;display newline MOV DL,13 ;Ascii value of Carriage return ;display carriage return MOV DX,OFFSET Msg3 ; Display message Quotient MOV DL,BL ;display Ascii value of quotient ADD DL,48 MOV DX,OFFSET Msg4 ; Display message Quotient MOV DL,BH ;display AScii value of remainder ADD DL,48 END 12
Check whether a number is Even or Odd(one digit) Msg DB "Enter a Number$" EvenMsg DB "The number is Even$" OddMsg DB "The number is Odd$" MOV DX, offset Msg ; display message Enter a Number MOV AH, 09H MOV AH,01H ; accept a number SUB AL,48 MOV BL,2 DIV BL CMP AH,0 JNE odd MOV DX, offset EvenMsg ; convert ASCii of number into decimal ; move divisor in BL ; AL=quotient, AH= remainder ; check if remainder = 0 then ; jump to label odd ; display even message odd: MOV DX,offset OddMsg ; display odd message END 13
Check whether a number is Prime or not(one digit).model small MSG1 DB "NUMBER IS PRIME$" MSG2 DB "NUMBER IS NOT PRIME$" MSG DB "ENTER A NUMBER :$" A DB 2 MOV DX,OFFSET MSG ; Display message Enter a number MOV AH,01H ; accept a number SUB AL,48 MOV BL,AL CMP BL,1 ; convert ASCII value of number into decimal ; move number in BL ; if number is 1 jump to COMPO JZ COMPO CMP BL,0 ; if number is 0 exit JZ EXT MOV DL,10 ; display newline MOV AH,02 MOV DL,13 ; carriage return MOV AH,02 14
LAB: MOV AL,BL MOV AH,0 DIV A CMP AH,0 ; divide the number repeatedly by (2 to num) ; if number is divisible by A then jump to CHECK JZ CHECK INC A JMP LAB ; else increment A ; jump to start of the loop PRIME: MOV DX,OFFSET MSG1 ; display message prime JMP EXT ; exit CHECK: CMP BL, A ; if the number is divisible by itself jump to prime COMPO: JZ PRIME MOV DX,OFFSET MSG2 ; display message not prime EXT: END 15
Find Factorial of a number (number<=6) MSG1 DB "FACTORIAL IS:$" MSG2 DB "ENTER A NUMBER <=6:$" MSG3 DB "OUT OF ORDER$" C DB 10 ARY DB 4 DUP(?) MOV DX,OFFSET MSG2 MOV AH,09 MOV AH,01 SUB AL,48 MOV AH,00 CMP AX,1 JB EXITT CMP AX,6 JA EXITT MOV CX,AX MOV AX,1 LABE: MUL CX LOOP LABE 16
MOV CX,00H MOV SI,OFFSET ARY LABELL: DIV C MOV [SI],AH MOV AH,00 INC SI INC CX CMP AL,00 JNZ LABELL MOV DL,10 MOV AH,02 MOV DL,13 MOV AH,02 MOV DX,OFFSET MSG1 MOV AH,09 DEC SI LABEE: MOV DL,[SI] ADD DL,48 MOV AH,02 DEC SI LOOP LABEE 17
EXITT: JMP EXII MOV DL,10 MOV AH,02 MOV DL,13 MOV AH,02 MOV DX,OFFSET MSG3 MOV AH,09 EXII: END 18
Check whether a string is Palindrome or not MSG DB "ENTER A STRING$" BUF DB 10,0,10 DUP(0) YES DB 13,10," STRING IS PALINDROME$" NO DB 13,10,"STRING IS NOT PALINDROME$" FLAG DB? MOV DX,OFFSET MSG MOV AH,09 MOV DX, OFFSET BUF MOV AH,0AH MOV AL,[BUF+1] MOV BX,OFFSET BUF+2 MOV AH,0 ADD BX,AX MOV [BX],BYTE PTR '$' MOV AL,[BUF+1] MOV CL,2 DIV CL MOV CL,AL MOV CH,0 19
MOV AL,[BUF+1] MOV AH,0 MOV DI,OFFSET BUF+1 ADD DI,AX MOV SI,OFFSET BUF+2 LOGIC: CMP CX,0 JE DONE MOV [FLAG], 0H MOV AL,[SI] CMP AL,[DI] JNE DONE MOV [FLAG], 1 DEC CX INC SI DEC DI JMP LOGIC DONE: CMP [FLAG],0 JE NO_PL MOV DX,OFFSET YES NO_PL: MOV DX,OFFSET NO 20