0% found this document useful (0 votes)
24 views29 pages

Computer Organization and Assembly Language: Assignment 2

Uploaded by

i227423
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)
24 views29 pages

Computer Organization and Assembly Language: Assignment 2

Uploaded by

i227423
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/ 29

EL2003

Computer Organization And


Assembly Language

Assignment 2

Submitted by: Hassan Ali


Roll number: I22-7423
Date: October 7, 2024

Table of Contents

Page 1 of 29
∙ Introduction 3

∙ Steps 3

∙ Summary 3

∙ References 3

Page 2 of 29
● Question 1

Write an assembly program to count the total number of digits in a string.

INCLUDE Irvine32.inc

.data

array dword 10 DUP(?)

msg1 byte"Enter the numbers : ", 0

msg2 byte"Array in Ascending order : ", 0

msg3 byte"Array in Descending order : ", 0

newline byte 13, 10, 0

.code

main PROC

mov ecx, 10

mov esi, OFFSET array

input_loop:

mov edx, OFFSET msg1

call WriteString

call ReadInt

mov [esi], eax

Page 3 of 29
add esi, 4

loop input_loop

mov ecx, 10

mov esi, OFFSET array

ascending_sort:

mov edi, 0

outer_loop_asc:

mov ebx, [esi + edi * 4]

mov edx, edi

inc edx

inner_loop_asc:

cmp edx, 10

jge outer_loop_asc_done

mov eax, [esi + edx * 4]

cmp ebx, eax

jle no_swap

xchg ebx, [esi + edx * 4]

mov [esi + edi * 4], ebx

Page 4 of 29
no_swap:

inc edx

jmp inner_loop_asc

outer_loop_asc_done:

inc edi

cmp edi, 9

jl outer_loop_asc

mov edx, OFFSET msg2

call WriteString

mov ecx, 10

mov esi, OFFSET array

print_asc:

mov eax, [esi]

call WriteInt

mov edx, OFFSET newline

call WriteString

add esi, 4

loop print_asc

mov ecx, 10

mov esi, OFFSET array

Page 5 of 29
descending_sort:

mov edi, 0

outer_loop_desc:

mov ebx, [esi + edi * 4]

mov edx, edi

inc edx

inner_loop_desc:

cmp edx, 10

jge outer_loop_desc_done

mov eax, [esi + edx * 4]

cmp ebx, eax

jge no_swap_desc

xchg ebx, [esi + edx * 4]

mov [esi + edi * 4], ebx

no_swap_desc:

inc edx

jmp inner_loop_desc

outer_loop_desc_done:

inc edi

Page 6 of 29
cmp edi, 9

jl outer_loop_desc

mov edx, OFFSET msg3

call WriteString

mov ecx, 10

mov esi, OFFSET array

print_desc:

mov eax, [esi]

call WriteInt

mov edx, OFFSET newline

call WriteString

add esi, 4

loop print_desc

call waitmsg

exit

main endp

end main

Output

Page 7 of 29
● Question 2
Convert the following C++ code into assembly language. [10 marks]

int t=1,

k=2;

while(t!=11)

cout<<k*t<<endl;

t=t+1;

INCLUDE Irvine32.inc

.data

Page 8 of 29
var1 dword 1

var2 dword 2

.code

main PROC

mov eax, var1

l1:

cmp eax, 11 ; (var1(t) != 11)

je end_loop

mov ebx, var2

imul ebx, eax

call writedec

call crlf

inc eax

loop l1

end_loop:

call waitmsg

exit

main ENDP

END main

Page 9 of 29
● Question 3
Write an assembly program that take a string input from user and ask user for a specific
character to be

searched for. Display the count of that character in a string.

INCLUDE Irvine32.inc

.data

input BYTE 100 DUP(0)

prompt BYTE "Enter a string: ", 0

msg byte "Enter alphabet for count : ", 0

count BYTE 0

.code

main PROC

mov edx, OFFSET prompt

call WriteString

xor ecx, ecx

read_loop:

call ReadChar

cmp al, 13 ; for enter

je end_read

Page 10 of 29
mov [input + ecx], al

inc ecx

jmp read_loop

end_read:

mov esi, OFFSET input

mov edx, offset msg

call writestring

call ReadChar

xor ecx, ecx

count_loop:

cmp byte ptr [esi + ecx], 0

je display_count

cmp byte ptr [esi + ecx], al

jne next_char

inc count ; character matches

next_char:

inc ecx

jmp count_loop

Page 11 of 29
display_count:

movzx eax, count

call writedec

call crlf

call waitmsg

exit

main ENDP

END main

● Question 4

INCLUDE Irvine32.inc

.data

name_ BYTE "Name: Hassan Ali", 0

address BYTE "Address: I-8, islamabad", 0

phone BYTE "Phone: 123456789 ", 0

email BYTE "Email: [email protected]", 0

objective BYTE "Looking for software engineer jobs in


Islamabad.", 0

education BYTE "Education: Bachelor of Cyber Security From


FAST University Islamabd", 0

skills BYTE "Skills: Assembly Language, C++, Java, Python", 0

Page 12 of 29
experience BYTE "Experience: 2 years as Software Developer at
CAIRE International", 0

newline BYTE 13, 10, 0

.code

main PROC

mov edx, offset name_

call WriteString

call crlf

mov edx, OFFSET address

call WriteString

call crlf

mov edx, OFFSET phone

call WriteString

; Display Email

mov edx, OFFSET email

call WriteString

call crlf

mov edx, OFFSET objective

Page 13 of 29
call WriteString

call crlf

mov edx, OFFSET education

call WriteString

call crlf

mov edx, OFFSET skills

call WriteString

call crlf

mov edx, OFFSET experience

call WriteString

call Crlf

call waitmsg

exit

main ENDP

END main

Page 14 of 29
● Question 5

INCLUDE Irvine32.inc

.data

num DWORD ?

reverse DWORD ?

sum DWORD ?

msg BYTE "Enter an integer: ", 0

msg2 BYTE "Reversed Number: ", 0

msg3 BYTE "Sum of Digits: ", 0

.code

main PROC

mov edx, OFFSET msg

call WriteString

call ReadDec

mov num, eax

mov eax, num

xor ebx, ebx

xor ecx, ecx

reverse_loop:

Page 15 of 29
test eax, eax

jz end_loop

mov edx, 0

mov ebx, 10 ; dividing by 10 for last digit

div ebx

add ecx, edx ; ecx will contain sum

; Build the reverse number

mov ebx, reverse

imul ebx, ebx, 10

add ebx, edx

mov reverse, ebx

jmp reverse_loop

end_loop:

mov edx, OFFSET msg2

call WriteString

mov eax, reverse

call WriteDec

call Crlf

mov edx, OFFSET msg3

call WriteString

Page 16 of 29
mov eax, ecx

call WriteDec

call Crlf

call waitmsg

exit

main ENDP

END main

● Question 6
INCLUDE Irvine32.inc

.data

line1 BYTE "1", 0

line2 BYTE "212", 0

line3 BYTE "32123", 0

line4 BYTE "4321234", 0

space BYTE " ", 0 ;

.code

main PROC

mov edx, OFFSET space

add edx, 3 ; 3rd place

Page 17 of 29
call WriteString

mov edx, OFFSET line1

call WriteString

call CrLf

mov edx, OFFSET space

add edx, 4 ; 4th place and so on...

call WriteString

mov edx, OFFSET line2

call WriteString

call CrLf

mov edx, OFFSET space

add edx, 5

call WriteString

mov edx, OFFSET line3

call WriteString

call CrLf

mov edx, OFFSET space

add edx, 6

call WriteString

mov edx, OFFSET line4

call WriteString

call CrLf

Page 18 of 29
mov edx, OFFSET space

add edx, 5

call WriteString

mov edx, OFFSET line3

call WriteString

call CrLf

mov edx, OFFSET space

add edx, 4

call WriteString

mov edx, OFFSET line2

call WriteString

call CrLf

mov edx, OFFSET space

add edx, 3

call WriteString

mov edx, OFFSET line1

call WriteString

call CrLf

call waitmsg

exit

main ENDP

Page 19 of 29
END main

● Question 7

INCLUDE Irvine32.inc

.data

input BYTE 100 DUP(0)

prompt BYTE "Enter a string: ", 0

● Question 8
INCLUDE Irvine32.inc

.data

array DWORD 10 DUP(?)

msg1 BYTE "Enter the numbers: ", 0

msg2 BYTE "Most frequently occurring element: ", 0

mostFrequent DWORD 0

maxCount DWORD 0

.code

Page 20 of 29
main PROC

mov ecx, 10

mov esi, OFFSET array

input_loop:

mov edx, OFFSET msg1

call WriteString

call ReadInt

mov [esi], eax

add esi, 4

loop input_loop

mov ecx, 10

mov ebx, 0

mov mostFrequent, 0

mov maxCount, 0

outer_loop:

cmp ebx, 10

jge end_outer_loop

mov eax, [array + ebx * 4]

xor edx, edx

mov esi, 0

Page 21 of 29
inner_loop:

cmp esi, 10

jge check_max

mov ecx, [array + esi * 4]

cmp eax, ecx

jne next_element

inc edx

next_element:

inc esi

jmp inner_loop

check_max:

cmp edx, maxCount

jle skip_update

mov maxCount, edx

mov mostFrequent, eax

skip_update:

inc ebx

jmp outer_loop

Page 22 of 29
end_outer_loop:

mov edx, OFFSET msg2

call WriteString

mov eax, mostFrequent

call WriteDec

call Crlf

call waitmsg

exit

main ENDP

END main

● Question 9

INCLUDE Irvine32.inc

.data

a DWORD ?

b DWORD ?

Page 23 of 29
c DWORD ?

d DWORD ?

e DWORD ?

f DWORD ?

g DWORD ?

msgInput BYTE "Enter seven integers: ", 0

msgOutput BYTE "Output: ", 0

.code

main PROC

mov edx, OFFSET msgInput

call WriteString

call ReadInt

mov a, eax

call ReadInt

mov b, eax

call ReadInt

mov c, eax

call ReadInt

mov d, eax

call ReadInt

mov e, eax

call ReadInt

mov f, eax

call ReadInt

Page 24 of 29
mov g, eax

mov eax, g

cmp eax, f

je condition_met

mov eax, e

cmp eax, d

jne condition_met

jmp check_second_condition

condition_met:

mov eax, g

cmp eax, d

je check_third_condition

mov eax, f

cmp eax, e

jg check_second_condition

jmp end_program

check_second_condition:

; Check if a > g and b <= e

mov eax, a

cmp eax, g

jle end_program

Page 25 of 29
mov eax, b

cmp eax, e

jg end_program

check_third_condition:

; Check if c > a and e >= c

mov eax, c

cmp eax, a

jle end_program

mov eax, e

cmp eax, c

jl end_program

mov edx, OFFSET msgOutput

call WriteString

mov eax, a

call WriteDec

end_program:

call waitmsg

exit

main ENDP

END main

Page 26 of 29
● Question 10

INCLUDE Irvine32.inc

.data

input BYTE 100 DUP(0)

msg1 BYTE "Enter a string: ", 0

msg2 byte "Character you want to search for: ", 0

msg3 byte "Character found at index: ", 0

msg4 byte "Character not found. ", 0

character byte ?

.code

main PROC

mov edx, OFFSET msg1

call WriteString

xor ecx, ecx

read_loop:

call readchar

Page 27 of 29
cmp al, 13 ; for enter

je end_read

mov [input + ecx], al

inc ecx

jmp read_loop

end_read:

mov edx, OFFSET msg2

call WriteString

call readchar

mov character, al

mov esi, OFFSET inputString

xor ecx, ecx

find_char:

cmp byte ptr [esi + ecx], 0

je not_found

cmp byte ptr [esi + ecx], character

je found

inc ecx

jmp find_character

Page 28 of 29
not_found:

mov edx, OFFSET msg4

call WriteString

found:

mov edx, OFFSET msg3

call WriteString

mov edx, character

call WriteChar

mov edx, OFFSET msgOutput

call WriteString

mov eax, ecx

call WriteDec

call Crlf

jmp end_program

end_program:

call waitmsg

exit

main ENDP

END main

Page 29 of 29

You might also like