0% found this document useful (0 votes)
28 views8 pages

Assembly Program for Hamming Distance and ISBN Validation

Assignment Solution

Uploaded by

m.omarafzal12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views8 pages

Assembly Program for Hamming Distance and ISBN Validation

Assignment Solution

Uploaded by

m.omarafzal12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

202

COAL ASSIGNMENT 03

(FA22-BCS-016) ARslan ijaz


Q1- [CLO-4] Write an assembly language program that computes the
Hamming distance between the two user provided strings. The Hamming
distance is the number of bit positions where the two strings differ.
For example, the ASCII representations of the strings “foo” and “bar” in binary are:
“foo” = 0110 0110 0110 1111 0110 1111
“baa” = 0110 0010 0110 0001 0110 0001
So, the Hamming distance between “foo” and “baa” is 7. If two strings are of different lengths,
your program should display the Hamming distance up to the length of the shorter string.
(Hint: The easiest way to examine the contents of a register bit-by-bit is to use successive SHR
instructions to shift the least significant bit into the carry flag)
Answer:

INCLUDE [Link]
.data
str1 BYTE 100 DUP (0)
str2 BYTE 100 DUP (0)
len1 DWORD ?
len2 DWORD ?
minLen DWORD ?
hamming DWORD 0
prompt1 BYTE "Enter first string:",0
prompt2 BYTE "Enter second string: ",0
msgResult BYTE "Hamming Distance =",0

.code
main PROC
mov edx, OFFSET prompt1
call WriteString
mov edx, OFFSET str1
mov ecx, 100
call ReadString
mov len1, eax

mov edx, OFFSET prompt2


call WriteString
mov edx, OFFSET str2
mov ecx, 100
call ReadString
mov len2, eax
mov eax, len1
cmp eax, len2
jbe @continue
mov eax, len2

@continue:
mov minLen, eax
xor esi, esi
xor ebx, ebx

compare_loop:
cmp esi, minLen
jge done
mov al, str1[esi]
mov bl, str2[esi]
xor al, bl
mov ecx, 8
xor edx, edx

count_bits:
shr al, 1
jnc no_inc
inc edx

no_inc:
loop count_bits
add ebx, edx
inc esi
jmp compare_loop

done:
mov hamming, ebx

mov edx, OFFSET msgResult call WriteString


mov eax, hamming
call WriteInt
call Crlf
exit

main ENDP
END main
Q2- [CLO-4] Write an assembly language program that validates a user provided 10-digit
International Standard Book Number (ISBN). For an ISBN to be valid, the following
weighted sum modulo 11 must be equal to 0.
The formula to validate an ISBN-10 is:

(∑𝑖 × 𝑑𝑖 10 𝑖=1 ) 𝑚𝑜𝑑 11


Answer:

INCLUDE [Link]

.data
isbn BYTE 20 DUP(0)
promptISBN BYTE "Enter 10-digit ISBN (only digits): ", 0
validMsg BYTE "ISBN is VALID", 0
invalidMsg BYTE "ISBN is INVALID",
.code
main PROC
mov edx, OFFSET promptISBN
call WriteString
mov edx, OFFSET isbn
mov ecx, 20
call ReadString
cmp eax, 10

jne invalid

mov esi, OFFSET isbn


mov ecx, 10
mov ebx, 10
xor eax, eax
isbn_loop:
movzx edx, BYTE PTR [esi]
sub edx, '0'
cmp edx, 9

ja invalid
imul edx, ebx
add eax, edx
inc esi
dec ebx
loop isbn_loop
mov edx, 0
mov ebx, 11
div ebx
cmp edx, e
jne invalid

call Crlf
mov edx, OFFSET validMsg
call WriteString
jmp finish

invalid:
call Crlf
mov edx, OFFSET invalidMsg
call WriteString
finish:
exit
call Crlf
main ENDP
END main

Q3- [CLO-4] Write an assembly language program that evaluates a user


provided postfix expression and displays the result on console. The algorithm
for the conversion is as follows:
 Traverse the postfix expression from left to right.
 If the current character is an operand, push it onto the stack.
 If the current character is an operator, there must be at least two operands on the stack.
Pop the top two operands, apply the operator, and push the result back onto the stack.
Note: the first operand popped becomes the right operand, and the second becomes the
left operand.
 Once the entire expression has been processed, the stack will contain a single element—
the final result of the expression.
Assumptions:
 Only the operators +, -, *, and / are allowed.
 All operands are positive, single-digit numbers.
 When performing division, discard any remainder (use integer division).
Answer/Code:

INCLUDE [Link]
.stack 4096
.data
expr BYTE 100 DUP (0)
prompt BYTE "Enter postfix expression (e.g., 23+5*): ", 0
resultMsg BYTE "Result = ", 0
invalidOp BYTE "Invalid character in expression.",
stack DWORD 100 DUP (0)
top DWORD 0
.code
main PROC
mov edx, OFFSET prompt
call WriteString
mov edx, OFFSET expr mov ecx, 100
call ReadString
xor esi, esi
xor eax, eax
next_char:
mov al, expr[esi]
cmp al, e
je eval_done
cmp al, '0'
jb invalid
cmp al, '9'
ja check_operator
sub al, '0'
movzx eax, al
call Push
inc esi
jmp next_char
check_operator:
cmp al, '+'
je do_add cmp al, '-'
je do_sub cmp al, *** je do_mul
cmp al, '/'
je do_div
jmp invalid
do_add:
call Pop
mov ebx, eax call Pop
add eax, ebx
call Push
inc esi
jmp next_char
do_sub:
call Pop
mov ebx, eax
call Pop
sub eax, ebx
call Push
inc esi
jmp next_char
do_mul:
call Pop
mov ebx, eax call Pop
imul eax, ebx
call Push
inc esi
jmp next_char
do_div:
call Pop
mov ebx, eax call Pop
xor edx, edx
div ebx
call Push
inc esi
jmp next_char
eval_done:
call Pop
mov edx, OFFSET resultMsg call WriteString
call WriteInt
call Crlf
exit

invalid:
call Crlf
mov edx, OFFSET invalidOp
call WriteString
call Crlf
exit
main ENDP

Push PROC

mov ebx, top


mov stack[ebx*4], eax
inc top
ret
Push ENDP

Pop PROC
dec top
mov ebx, top
mov eax, stack[ebx*4]
ret
Pop ENDP
END main

You might also like