0% found this document useful (0 votes)
14 views

Assembly

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Assembly

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

1.

Write a program to read a character from the keyboard and display it at


the next position of the same line.

Program:
.MODEL SMALL
.STACK 100h
.DATA
charInput DB ? ; Variable to store the input character
prompt DB 'Enter a character: $'
newline DB 0Dh, 0Ah, '$'
.CODE
MAIN PROC
MOV AX, @DATA ; Initialize data segment
MOV DS, AX
; Display prompt
LEA DX, prompt
MOV AH, 09h
INT 21h ; Read a character from the keyboard
MOV AH, 01h
INT 21h
MOV charInput, A ; Display the character at the next position on the same
line
MOV AH, 02h
MOV DL, charInput
INT 21h
; Display a newline
LEA DX, newline
MOV AH, 09h
INT 21h
MOV AH, 4Ch ; Exit the program
INT 21h
MAIN ENDP
END MAIN

2. Write a program to read an uppercase character from the keyboard and


display it at the beginning of the next line in lower case, with an appropriate
message.
Program:
.MODEL SMALL
.STACK 100h
.DATA
charInput DB ? ; Variable to store the input character
prompt DB 'Enter an uppercase character: $'
newline DB 0Dh, 0Ah, '$'
errorMsg DB 'Invalid input! Please enter an uppercase character.', 0Dh, 0Ah, '$'
.CODE
MAIN PROC
MOV AX, @DATA ; Initialize data segment
MOV DS, AX
inputLoop:
; Display prompt
LEA DX, prompt
MOV AH, 09h
INT 21h
; Read a character from the keyboard
MOV AH, 01h
INT 21h
MOV charInput, AL
; Check if the character is an uppercase letter (ASCII values: 65 to 90)
CMP AL, 65
JB inputLoop ; If it's below 'A', invalid input, repeat the loop
CMP AL, 90
JA inputLoop ; If it's above 'Z', invalid input, repeat the loop
; Convert the uppercase character to lowercase (ASCII difference: 32)
ADD AL, 32
; Display the lowercase character at the beginning of the next line
LEA DX, newline
MOV AH, 09h
INT 21h
; Display the lowercase character
MOV AH, 02h
INT 21h
; Display a newline
LEA DX, newline
MOV AH, 09h
INT 21h
MOV AH, 4Ch ; Exit the program
INT 21h
MAIN ENDP
END MAIN

3. Write a program to prompt the user, and read first, middle and read last
initials of a person’s name, and display them down the left margin. Sample
execution:
ENTER THREE INITIALS: UFR
U
F
R

Program:
.MODEL SMALL
.STACK 100h
.DATA
initials DB 3 DUP(?) ; Array to store three initials
prompt DB 'ENTER THREE INITIALS (e.g., ABC): $'
newline DB 0Dh, 0Ah, '$'
outputMsg DB 'OUTPUT:', 0Dh, 0Ah, '$'
.CODE
MAIN PROC
MOV AX, @DATA ; Initialize data segment
MOV DS, AX
; Display prompt
LEA DX, prompt
MOV AH, 09h
INT 21h
; Read three initials from the keyboard
MOV AH, 01h
MOV CX, 3 ; Read three characters
LEA DI, initials ; Point DI to the initials array
readLoop:
INT 21h ; Read a character from the keyboard
MOV [DI], AL ; Store the character in the array
INC DI ; Move to the next position in the array
LOOP readLoop ; Repeat until three characters are read
; Display the output message
LEA DX, outputMsg
MOV AH, 09h
INT 21h
; Display initials down the left margin
LEA DI, initials ; Point DI to the initials array
displayLoop:
MOV AL, [DI] ; Load the character from the array
MOV AH, 02h ; Function to display character
INT 21h
MOV AH, 09h ; Function to display a newline
LEA DX, newline
INT 21
INC DI ; Move to the next character in the array
LOOP displayLoop ; Repeat until all initials are displayed
MOV AH, 4Ch ; Exit the program
INT 21h
MAIN ENDP
END MAIN

4. Write a program to display a ”?”, read two decimal digits whose sum is less
than 10, display them and their sum on the next line, with an appropriate
message. Sample execution:
?35
THE SUM OF 3 AND 5 IS 8
Program 4:
MODEL SMALL
.STACK 100h
.DATA
digit1 DB ? ; Variable to store the first decimal digit
digit2 DB ? ; Variable to store the second decimal digit
questionMark DB '? $'
outputMsg DB 'THE SUM OF ', 0, ' AND ', 0, ' IS ', 0, 0Dh, 0Ah, '$'
errorMsg DB 'Invalid input! Please enter two decimal digits with a sum less than
10.', 0Dh, 0Ah, '$'
.CODE
MAIN PROC
MOV AX, @DATA ; Initialize data segment
MOV DS, AX
inputLoop:
; Display "?" prompt
LEA DX, questionMark
MOV AH, 09h
INT 21h
; Read the first decimal digit
MOV AH, 01h
INT 21h
SUB AL, 30h ; Convert ASCII character to decimal digit
MOV digit1, AL
; Read the second decimal digit
MOV AH, 01h
INT 21h
SUB AL, 30h ; Convert ASCII character to decimal digit
MOV digit2, AL
; Check if the sum is less than 10
ADD AL, digit1
CMP AL, 10
JAE inputLoop ; If sum is greater or equal to 10, repeat the loop
; Display the digits and their sum with the output message
LEA DX, outputMsg
MOV AH, 09h
MOV DL, digit1 ; Display the first digit
ADD DX, 9 ; Move DX to the second placeholder
MOV DL, digit2 ; Display the second digit
ADD DX, 8 ; Move DX to the third placeholder
ADD AL, 30h ; Convert sum to ASCII character
MOV DL, AL ; Display the sum
INT 21h
MOV AH, 4Ch ; Exit the program
INT 21h
MAIN ENDP
END MAIN

5. Write a program to read one of the hex digits A-F; and display it on the
next line in decimal. Sample execution:
ENTER A HEX DIGIT (A-F): C
IN DECIMAL IT IS 12
Program 5:
.MODEL SMALL
.STACK 100h
.DATA
hexInput DB ? ; Variable to store the input hex digit
prompt DB 'ENTER A HEX DIGIT (A-F): $'
newline DB 0Dh, 0Ah, '$'
outputMsg DB 'IN DECIMAL IT IS ', 0, 0Dh, 0Ah, '$'
errorMsg DB 'Invalid input! Please enter a valid hex digit (A-F).', 0Dh, 0Ah, '$'
.CODE
MAIN PROC
MOV AX, @DATA ; Initialize data segment
MOV DS, AX
inputLoop:
; Display prompt
LEA DX, prompt
MOV AH, 09h
INT 21h
; Read a hex digit from the keyboard
MOV AH, 01h
INT 21h
MOV hexInput, AL
; Check if the input is a valid hex digit (A-F)
CMP AL, 'A'
JB invalidInput
CMP AL, 'F'
JA invalidInput
; Convert the hex digit to decimal and display the result
SUB AL, 'A' ; Convert ASCII character to hexadecimal value
ADD AL, 10 ; Convert hexadecimal value to decimal
; Display the result with the output message
LEA DX, outputMsg
MOV AH, 09h
MOV DL, AL ; Display the decimal value
ADD DX, 18 ; Move DX to the third placeholder
INT 21h
JMP exitProgram
invalidInput:
; Display an error message for invalid input
LEA DX, errorMsg
MOV AH, 09h
INT 21h
exitProgram:
MOV AH, 4Ch ; Exit the program
INT 21h
MAIN ENDP
END MAIN

6. Write a program to display a ”?”, read three characters, and then display
them in the middle of an 11×11 box of asterisks.
Program 6:
.MODEL SMALL
.STACK 100h
.DATA
characters DB 3 DUP(?) ; Array to store the three characters
questionMark DB '? $'
asterisk DB '*', '$'
boxSize DW 11 ; Size of the box (11x11)
.CODE
MAIN PROC
MOV AX, @DATA ; Initialize data segment
MOV DS, AX
inputLoop:
; Display "?" prompt
LEA DX, questionMark
MOV AH, 09h
INT 21h
; Read three characters from the keyboard
MOV AH, 01h
MOV CX, 3 ; Read three characters
LEA DI, characters ; Point DI to the characters array
readLoop:
INT 21h ; Read a character from the keyboard
MOV [DI], AL ; Store the character in the array
INC DI ; Move to the next position in the array
LOOP readLoop ; Repeat until three characters are read
; Calculate the starting position to display characters in the box
MOV DX, boxSize ; Load DX with the size of the box (11)
SUB DX, 3 ; Subtract 3 to get the starting position (middle)
; Display the box with characters in the middle
displayLoop:
MOV CX, boxSize ; Load CX with the size of the box (11)
innerLoop:
CMP CX, DX ; Compare current row with the starting position (middle)
JB drawAsterisk ; If the current row is before the middle, draw an asterisk
MOV SI, DX ; If the current row is at or after the middle, calculate the
offset
SUB SI, 5 ; 5 is half the width of the characters (3 characters and 2
spaces)
CMP CX, SI ; Compare current row with the calculated offset
JA drawAsterisk ; If the current row is after the calculated offset, draw an
asterisk
; Display the characters in the box
MOV AL, [characters + SI] ; Load the character from the array
MOV AH, 02h ; Function to display character
INT 21h
DEC CX ; Move to the next row
JMP innerLoop ; Repeat until the box is displayed
drawAsterisk:
; Display an asterisk
LEA DX, asterisk
MOV AH, 09h
INT 21h
DEC CX ; Move to the next row
LOOP displayLoop ; Repeat until the box is displayed
MOV AH, 4Ch ; Exit the program
INT 21h
MAIN ENDP
END MAIN
7. Write a program to prompt the user, read two decimal digits whose sum is
greater than 10, and display them and their sum on the next line, with an
appropriate message. Sample execution:
ENTER TWO DECIMAL DIGITS: 8 7
THE SUM OF 8 AND 7 IS 15
Program 7:

.MODEL SMALL
.STACK 100h
.DATA
digit1 DB ? ; Variable to store the first decimal digit
digit2 DB ? ; Variable to store the second decimal digit
prompt DB 'ENTER TWO DECIMAL DIGITS: $'
newline DB 0Dh, 0Ah, '$'
outputMsg DB 'THE SUM OF ', 0, ' AND ', 0, ' IS ', 0, 0Dh, 0Ah, '$'
errorMsg DB 'Invalid input! Please enter two decimal digits with a sum greater
than 10.', 0Dh, 0Ah, '$'
.CODE
MAIN PROC
MOV AX, @DATA ; Initialize data segment
MOV DS, AX
inputLoop:
; Display prompt
LEA DX, prompt
MOV AH, 09h
INT 21h
; Read the first decimal digit
MOV AH, 01h
INT 21h
SUB AL, 30h ; Convert ASCII character to decimal digit
MOV digit1, AL
; Read the second decimal digit
MOV AH, 01h
INT 21h
SUB AL, 30h ; Convert ASCII character to decimal digit
MOV digit2, AL
; Check if the sum is greater than 10
ADD AL, digit1
CMP AL, 10
JBE inputLoop ; If sum is less than or equal to 10, repeat the loop
; Display the digits and their sum with the output message
LEA DX, outputMsg
MOV AH, 09h
MOV DL, digit1 ; Display the first digit
ADD DX, 9 ; Move DX to the second placeholder
MOV DL, digit2 ; Display the second digit
ADD DX, 8 ; Move DX to the third placeholder
ADD AL, 30h ; Convert sum to ASCII character
MOV DL, AL ; Display the sum
INT 21h
MOV AH, 4Ch ; Exit the program
INT 21h
MAIN ENDP
END MAIN

8. Write a program to prompt the user, read a character, and if it is an


uppercase letter, display it at the beginning of the next line. H.W: Write a
program to prompt the user, read a character, and if it is an uppercase letter,
display it at the beginning of the next line. If the user enters an illegal
character, display message “ILLIGAL
CHARACTER”
Program 8:

.MODEL SMALL
.STACK 100h
.DATA
charInput DB ? ; Variable to store the input character
prompt DB 'Enter a character: $'
newline DB 0Dh, 0Ah, '$'
illegalMsg DB 'ILLEGAL CHARACTER', 0Dh, 0Ah, '$'
.CODE
MAIN PROC
MOV AX, @DATA ; Initialize data segment
MOV DS, AX
inputLoop:
; Display prompt
LEA DX, prompt
MOV AH, 09h
INT 21h
; Read a character from the keyboard
MOV AH, 01h
INT 21h
MOV charInput, AL
; Check if the character is an uppercase letter (ASCII values: 65 to 90)
CMP AL, 65
JB illegalChar ; If it's below 'A', display "ILLEGAL CHARACTER"
message
CMP AL, 90
JA illegalChar ; If it's above 'Z', display "ILLEGAL CHARACTER"
message
; Display the character at the beginning of the next line
LEA DX, newline
MOV AH, 09h
INT 21h
MOV AH, 02h
MOV DL, charInput
INT 21h
JMP exitProgram
illegalChar:
; Display "ILLEGAL CHARACTER" message
LEA DX, illegalMsg
MOV AH, 09h
INT 21h
exitProgram:
MOV AH, 4Ch ; Exit the program
INT 21h
MAIN ENDP
END MAIN
9. Write a program to display the extended ASCII characters (ASCII codes
from 80h to FFh). Display 10 characters per line separated by blanks.
Program 9:

.MODEL SMALL
.STACK 100h
.DATA
extendedChars DB 80h, 81h, 82h, 83h, 84h, 85h, 86h, 87h, 88h, 89h,
8Ah, 8Bh, 8Ch, 8Dh, 8Eh, 8Fh, 90h, 91h, 92h, 93h,
94h, 95h, 96h, 97h, 98h, 99h, 9Ah, 9Bh, 9Ch, 9Dh,
9Eh, 9Fh, 0Dh, 0Ah, '$' ; List of extended ASCII characters (from
80h to FFh)
.CODE
MAIN PROC
MOV AX, @DATA ; Initialize data segment
MOV DS, AX
; Display the extended ASCII characters, 10 characters per line
MOV CX, 32 ; Total number of characters (32 characters from 80h to
FFh)
MOV SI, 0 ; Initialize SI to point to the first character in the array
displayLoop:
MOV AH, 02h ; Function to display character
MOV DL, [extendedChars + SI] ; Load the character from the array
INT 21h
ADD SI, 1 ; Move to the next character in the array
; Check if 10 characters have been displayed (i.e., end of line)
MOV AH, 00h
CMP SI, 10
; Display a blank between characters
MOV AH, 02h
MOV DL, ' ' ; Display a blank
INT 21h
LOOP displayLoop ; Repeat until all characters are displayed
exitProgram:
MOV AH, 4Ch ; Exit the program
INT 21h
MAIN ENDP
END MAIN

10. Write a program that will prompt the user to enter a decimal digit. If it is
an even number then display “EVEN” otherwise display “ODD”.[ Use TEST
and Jump Instructions]
Program 10:
.MODEL SMALL
.STACK 100h
.DATA
digit DB ? ; Variable to store the input decimal digit
prompt DB 'Enter a decimal digit: $'
newline DB 0Dh, 0Ah, '$'
evenMsg DB 'EVEN', 0Dh, 0Ah, '$'
oddMsg DB 'ODD', 0Dh, 0Ah, '$'
.CODE
MAIN PROC
MOV AX, @DATA ; Initialize data segment
MOV DS, AX
inputLoop:
; Display prompt
LEA DX, prompt
MOV AH, 09h
INT 21h
; Read a decimal digit from the keyboard
MOV AH, 01h
INT 21h
SUB AL, 30h ; Convert ASCII character to decimal digit
MOV digit, AL
; Check if the digit is even (test the least significant bit)
MOV AL, digit
AND AL, 1 ; Test the least significant bit (1 for odd, 0 for even)
JNZ odd ; Jump to odd label if AL is not zero (odd)
JMP even ; Jump to even label if AL is zero (even)
even:
; Display "EVEN" message
LEA DX, evenMsg
MOV AH, 09h
INT 21h
JMP exitProgram
odd:
; Display "ODD" message
LEA DX, oddMsg
MOV AH, 09h
INT 21h
exitProgram:
MOV AH, 4Ch ; Exit the program
INT 21h
MAIN ENDP
END MAIN

You might also like