HITEC UNIVERSITY
Department of Computer Science
4th Semester
Lab Mid Report
Submitted To:
Mam Fatima Rauf
Submitted By:
Waqar Ahmad
(22-cs-148)
Section:
“D”
HITEC UNIVERSITY TAXILA DEPARTMENT
OF COMPUTER SCIENCE
QUSTION NO 01:
Simulate an ASCIl Converter in assembly language that gives a choice of taking either integer
or ASCIl as input and convert it to other form. The program should perform the following
tasks:
• Prompt the user to enter his choice 1 for an integer value and 2 for ASCIl.
• Then convert it to other form.
• Use Procedure for implementation of above task.
• Also define a procedure that moves cursor to next line and call it properly.
• Also display proper message in the code.
CODE:
.model small
.stack 100h
.data
choice_msg db 'Enter your choice (1 for integer, 2 for ASCII): $'
input_msg db 'Enter the value: $'
output_msg db 'Converted value: $'
newline db 0Dh, 0Ah, '$'
invalid_choice_msg db 'Invalid choice. Please enter 1 or 2.$'
.code
main:
mov ax, @data
mov ds, ax
; Display choice message
mov ah, 09h
lea dx, choice_msg
int 21h
; Read user choice
mov ah, 01h
int 21h
sub al, '0' ; Convert character to integer
mov bl, al
; Handle user choice
cmp bl, 1
je convert_to_ascii
cmp bl, 2
je convert_to_integer
; Invalid choice
mov ah, 09h
lea dx, invalid_choice_msg
int 21h
jmp exit_program
convert_to_ascii:
; Display input message
mov ah, 09h
lea dx, input_msg
int 21h
; Read integer input
mov ah, 01h
int 21h
sub al, '0' ; Convert character to integer
mov dl, al
; Convert integer to ASCII
add dl, '0' ; Convert integer to ASCII character
mov ah, 02h
int 21h
jmp exit_program
convert_to_integer:
; Display input message
mov ah, 09h
lea dx, input_msg
int 21h
; Read ASCII input
mov ah, 01h
int 21h
; Convert ASCII to integer
sub al, '0' ; Convert ASCII character to integer
mov dl, al
mov ah, 02h
int 21h
jmp exit_program
exit_program:
; Move cursor to next line
mov ah, 09h
lea dx, newline
int 21h
; Exit program
mov ah, 4Ch
int 21h
end main
OUTPUT:
QUESTION NO 2:
A) Simulate a program that takes inputs a string in array store and then print the stored
string in reverse order on screen and check if string is palindrome or not.
CODE:
.model small
.stack 100h
.data
prompt_msg db 'Enter a string: $'
reverse_msg db 'Reverse of the string: $'
palindrome_msg db 'The string is palindrome.$'
not_palindrome_msg db 'The string is not palindrome.$'
newline db 0Dh, 0Ah, '$'
max_size equ 50
input_string db max_size+1, '$'
reversed_string db max_size+1, '$'
.code
main:
mov ax, @data
mov ds, ax
; Display prompt message
mov ah, 09h
lea dx, prompt_msg
int 21h
; Read input string
mov ah, 0Ah
lea dx, input_string
int 21h
; Calculate string length
mov si, offset input_string + 1
mov cx, 0
count_length:
cmp byte ptr [si], '$'
je end_count_length
inc cx
inc si
jmp count_length
end_count_length:
; Reverse the string
mov si, offset input_string + cx
mov di, offset reversed_string
dec si
mov cx, 0
reverse_loop:
cmp si, offset input_string
jb end_reverse_loop
mov al, [si]
mov [di], al
inc di
dec si
inc cx
jmp reverse_loop
end_reverse_loop:
mov byte ptr [di], '$'
; Display reversed string
mov ah, 09h
lea dx, reverse_msg
int 21h
mov ah, 09h
lea dx, reversed_string
int 21h
; Check if string is palindrome
mov si, offset input_string + 1
mov di, offset reversed_string + 1
mov cx, 0
check_palindrome:
cmp cx, bx
jge is_palindrome
mov al, [si]
cmp al, [di]
jne not_palindrome
inc si
inc di
inc cx
jmp check_palindrome
is_palindrome:
mov ah, 09h
lea dx, palindrome_msg
int 21h
jmp exit_program
not_palindrome:
mov ah, 09h
lea dx, not_palindrome_msg
int 21h
jmp exit_program
exit_program:
; Move cursor to next line
mov ah, 09h
lea dx, newline
int 21h
; Exit program
mov ah, 4Ch
int 21h
end main
OUTPUT:
QUESTION NO 3:
b) Construct a Program in assembly language that take perform following Calculations
Output= 1s complement of (X) +2"d complement of (Y)+Z.
Here x=3, y=2 and Z=9
CODE:
.model small
.stack 100h
.data
x dw 3
y dw 2
z dw 9
output dw ?
.code
main:
mov ax, @data
mov ds, ax
; Calculate 1's complement of x
mov ax, x
not ax
mov bx, ax ; Store 1's complement of x in bx
; Calculate 2's complement of y
mov ax, y
neg ax
mov dx, 1
add ax, dx
mov cx, ax ; Store 2's complement of y in cx
; Add z to the result
mov ax, bx
add ax, cx
add ax, z
mov output, ax
; Display the result
mov ax, output
call print_num
; Exit program
mov ah, 4Ch
int 21h
print_num proc
push ax
push bx
push cx
push dx
mov bx, 10
mov cx, 0 ; Counter for number of digits
calc_digits:
mov dx, 0
div bx
push dx ; Store remainder in stack
inc cx ; Increment digit counter
test ax, ax
jnz calc_digits
print_digits:
pop dx ; Pop digit from stack
add dl, '0'
mov ah, 02h
int 21h
loop print_digits
pop dx
pop cx
pop bx
pop ax
ret
print_num endp
end main
OUTPUT: