0% found this document useful (0 votes)
45 views12 pages

Lab - Assignment - 1 SP21-BCS-022

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)
45 views12 pages

Lab - Assignment - 1 SP21-BCS-022

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
You are on page 1/ 12

Class Assignment 4

Microprocessor and Assembly Language


Hafiz Ahsan ullah Farooqi

(SP21-BCS-022 6A)

Submitted To
Sir Taimur Shahzad
Question No 1
section .data

msg1 db "Enter the first string: ", 0

msg2 db "Enter the second string: ", 0

result_msg db "Hamming distance: ", 0

section .bss

str1 resb 100

str2 resb 100

section .text

global _start

_start:

; Display prompt for the first string

mov eax, 4

mov ebx, 1

mov ecx, msg1

mov edx, 23

int 0x80

; Read the first string

mov eax, 3

mov ebx, 0

mov ecx, str1


mov edx, 100

int 0x80

; Display prompt for the second string

mov eax, 4

mov ebx, 1

mov ecx, msg2

mov edx, 24

int 0x80

; Read the second string

mov eax, 3

mov ebx, 0

mov ecx, str2

mov edx, 100

int 0x80

; Calculate and display the Hamming distance

mov eax, 0 ; Initialize Hamming distance counter

check_strings:

cmp byte [str1], 0 ; Check if end of the string

je end_of_program

; Load the current characters from both strings


mov al, [str1]

mov bl, [str2]

; XOR to find differing bits

xor al, bl

; Count differing bits using the carry flag

xor ah, ah ; Clear AH register

mov cx, 8 ; Loop 8 times for each bit

count_bits:

shr al, 1

adc ah, 0

loop count_bits

; Add the count to the total Hamming distance

add eax, cx

; Move to the next characters in the strings

inc str1

inc str2

jmp check_strings

end_of_program:
Question No 2
section .data

msg db "Enter a 10-digit ISBN: ", 0

valid_msg db "Valid ISBN.", 0

invalid_msg db "Invalid ISBN.", 0

section .bss

isbn resb 11 ; 10 digits + null terminator

section .text

global _start

_start:

; Display prompt for the ISBN

mov eax, 4

mov ebx, 1

mov ecx, msg

mov edx, 25

int 0x80

; Read the ISBN from the user

mov eax, 3

mov ebx, 0

mov ecx, isbn

mov edx, 11
int 0x80

; Convert ASCII digits to integer and validate ISBN

mov eax, 0 ; Initialize weighted sum

mov ecx, 10 ; Initialize digit position

validate_isbn:

cmp byte [isbn], 0 ; Check if end of the ISBN

je end_of_program

; Convert ASCII digit to integer

sub byte [isbn], '0'

movzx ebx, byte [isbn]

; Calculate weighted sum

imul ebx, ecx

add eax, ebx

; Move to the next digit position

dec ecx

inc isbn

jmp validate_isbn

end_of_program:

; Check if the ISBN is valid (sum modulo 11 is 0)


mov edx, 11

mov ecx, 11

div ecx ; Calculate the remainder (weighted sum modulo 11)

; Display the result message

cmp edx, 0

je valid_isbn

jmp invalid_isbn

valid_isbn:

mov eax, 4

mov ebx, 1

mov ecx, valid_msg

mov edx, 12

int 0x80

jmp end_of_program

invalid_isbn:

mov eax, 4

mov ebx, 1

mov ecx, invalid_msg

mov edx, 14

int 0x80

; Exit the program

mov eax, 1

xor ebx, ebx

int 0x80
Question No 3
section .data

msg db "Enter a postfix expression: ", 0

result_msg db "Result: ", 0

section .bss

stack resb 100

result resd 1

section .text

global _start

_start:

; Display prompt for the postfix expression

mov eax, 4

mov ebx, 1

mov ecx, msg

mov edx, 29

int 0x80

; Read the postfix expression from the user

mov eax, 3

mov ebx, 0

mov ecx, stack

mov edx, 100


int 0x80

; Evaluate the postfix expression

mov ebx, stack ; Set the stack pointer

call evaluate_postfix

; Display the result message

mov eax, 4

mov ebx, 1

mov ecx, result_msg

mov edx, 8

int 0x80

; Display the result

mov eax, 1

mov ebx, 1

mov ecx, result

mov edx, 4

int 0x80

; Exit the program

mov eax, 1

xor ebx, ebx

int 0x80
evaluate_postfix:

; Initialize registers

mov ecx, 0 ; Counter for the result array

mov edx, 10 ; Maximum length of the stack

; Loop through the postfix expression

loop_postfix:

cmp byte [ebx], 0 ; Check if end of the expression

je end_evaluation

; Check if the character is an operand

cmp byte [ebx], '0'

jl is_operator

; Convert ASCII digit to integer

sub byte [ebx], '0'

movzx eax, byte [ebx]

; Push operand onto the stack

mov [result + ecx], eax

inc ecx

inc ebx

jmp loop_postfix

is_operator:
; Pop two operands from the stack

pop eax

pop ebx

; Perform the operation based on the operator

cmp byte [ebx], '+'

je add_operands

cmp byte [ebx], '-'

je sub_operands

cmp byte [ebx], '*'

je mul_operands

cmp byte [ebx], '/'

je div_operands

add_operands:

add eax, ebx

jmp push_result

sub_operands:

sub eax, ebx

jmp push_result

mul_operands:

imul eax, ebx

jmp push_result
div_operands:

; Avoid division by zero

cmp ebx, 0

je push_result

; Perform division (ignore the remainder)

xor edx, edx

div ebx

jmp push_result

push_result:

; Push the result back onto the stack

push eax

inc ebx

jmp loop_postfix

end_evaluation:

; The final result is now on top of the stack

pop eax

mov [result], eax

ret

You might also like