Ass#2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

COMSATS

University Lahore

Computer Organization
and Assembly language
Assignment # 2

Submitted by: Muhammad Awais


SP22-BCS-073

Section: BCS-A

Submitted to: Usman Rafique

Due Date

October 18, 2023


Question:
Write an 8086 assembly language program that performs the following tasks: 1. Reads a string
from keyboard consisting upon upper and lower case alphabets. 2. Converts the upper case
alphabets to lower case alphabets and lower case alphabets to upper case alphabets. 3. Counts the
total number of lower case and upper case alphabets in original string and prints the count of each
case on monitor.

Sample interface:

Enter a string xddAERxErDY

Inverted string is: XDDaerXeRdy

Lower case count = 5

Upper case count = 6

Code:
.MODEL SMALL
.STACK 100h
.DATA
MSG DB 'Enter a string: $'
MSG_INV DB 'Inverted string is: $'
MSG_LOWER_COUNT DB 'Lower case count = $'
MSG_UPPER_COUNT DB 'Upper case count = $'
NEW_LINE DB 0xA, 0xD, '$'
INPUT_STRING DB 51,? ; Buffer for user input string (maximum size 50), last byte reserved for input
length
INVERTED_STRING DB 51 DUP(?) ; Buffer for inverted string
LOWER_COUNT DB 0
UPPER_COUNT DB 0

.CODE
MAIN PROC
; Seting up the DS register
MOV AX, @DATA
MOV DS, AX
; Print prompt for user input
MOV AH, 9
LEA DX, MSG
INT 21H

; Read user input string


MOV AH, 0Ah
MOV DX, OFFSET INPUT_STRING
INT 21H

; Find input length


MOV SI, OFFSET INPUT_STRING
ADD SI, 1 ; Move SI to the byte after the input length
XOR CX, CX ; Clear CX (will store input length)
MOV CL, [SI] ; Load input length into CL

; Convert case and count upper and lower case letters


CONVERT_LOOP:
CMP CL, 0 ; Check if we have processed all characters
JZ PRINT_RESULTS

MOV AL, [SI] ; Load the next character into AL


CMP AL, 'a' ; Compare with 'a'
JB NOT_LOWER ; Jump if below 'a'

;assuring it not count other than alphabets


CMP AL, 'z' ; Compare with 'z'
JA NOT_LOWER ; Jump if above 'z'
SUB AL, 32 ; Convert lowercase to uppercase
MOV [SI] ,AL ;moving converted alphabet back to the address
INC LOWER_COUNT
JMP CONTINUE_LOOP

NOT_LOWER:
CMP AL, 'A' ; Compare with 'A'
JB CONTINUE_LOOP ; Jump if below 'A'
; CMP AL, 'Z' ; Compare with 'Z'
; JA CONTINUE_LOOP ; Jump if above 'Z'
ADD AL, 32 ; Convert uppercase to lowercase
MOV [SI], AL ;saving to the same location after converting
INC UPPER_COUNT

CONTINUE_LOOP:
INC SI ; Move to the next character
DEC CL ; Decrement the character count
JMP CONVERT_LOOP ; Continue the loop

PRINT_RESULTS:
; Print new line
MOV AH, 09H
MOV DX, OFFSET NEW_LINE
INT 21H
; Print inverted string
MOV AH, 09H
LEA DX, MSG_INV
INT 21H
MOV AH, 09H
LEA DX, INVERTED_STRING
INT 21H

; Print new line


MOV AH, 09H
MOV DX, OFFSET NEW_LINE
INT 21H
; Print lower case count
MOV AH, 09H
LEA DX, MSG_LOWER_COUNT
INT 21H

MOV AH, 02H


MOV DL, [LOWER_COUNT]
ADD DL, 30H ; Convert the count to ASCII
INT 21H

; Print new line


MOV AH, 09H
MOV DX, OFFSET NEW_LINE
INT 21H

; Print upper case count


MOV AH, 09H
LEA DX, MSG_UPPER_COUNT
INT 21H
MOV AH, 02H
MOV DL, [UPPER_COUNT]
ADD DL, 30H ; Convert the count to ASCII
INT 21H

; Exit the program


MOV AH, 4CH
XOR AL, AL
INT 21H
MAIN ENDP
END MAIN
Output Screenshot:

You might also like