Computer Organization and Assembly Language: Assignment 2
Computer Organization and Assembly Language: Assignment 2
Assignment 2
Table of Contents
Page 1 of 29
∙ Introduction 3
∙ Steps 3
∙ Summary 3
∙ References 3
Page 2 of 29
● Question 1
INCLUDE Irvine32.inc
.data
.code
main PROC
mov ecx, 10
input_loop:
call WriteString
call ReadInt
Page 3 of 29
add esi, 4
loop input_loop
mov ecx, 10
ascending_sort:
mov edi, 0
outer_loop_asc:
inc edx
inner_loop_asc:
cmp edx, 10
jge outer_loop_asc_done
jle no_swap
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
call WriteString
mov ecx, 10
print_asc:
call WriteInt
call WriteString
add esi, 4
loop print_asc
mov ecx, 10
Page 5 of 29
descending_sort:
mov edi, 0
outer_loop_desc:
inc edx
inner_loop_desc:
cmp edx, 10
jge outer_loop_desc_done
jge no_swap_desc
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
call WriteString
mov ecx, 10
print_desc:
call WriteInt
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
l1:
je end_loop
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
INCLUDE Irvine32.inc
.data
count BYTE 0
.code
main PROC
call WriteString
read_loop:
call ReadChar
je end_read
Page 10 of 29
mov [input + ecx], al
inc ecx
jmp read_loop
end_read:
call writestring
call ReadChar
count_loop:
je display_count
jne next_char
next_char:
inc ecx
jmp count_loop
Page 11 of 29
display_count:
call writedec
call crlf
call waitmsg
exit
main ENDP
END main
● Question 4
INCLUDE Irvine32.inc
.data
Page 12 of 29
experience BYTE "Experience: 2 years as Software Developer at
CAIRE International", 0
.code
main PROC
call WriteString
call crlf
call WriteString
call crlf
call WriteString
; Display Email
call WriteString
call crlf
Page 13 of 29
call WriteString
call crlf
call WriteString
call crlf
call WriteString
call crlf
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 ?
.code
main PROC
call WriteString
call ReadDec
reverse_loop:
Page 15 of 29
test eax, eax
jz end_loop
mov edx, 0
div ebx
jmp reverse_loop
end_loop:
call WriteString
call WriteDec
call Crlf
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
.code
main PROC
Page 17 of 29
call WriteString
call WriteString
call CrLf
call WriteString
call WriteString
call CrLf
add edx, 5
call WriteString
call WriteString
call CrLf
add edx, 6
call WriteString
call WriteString
call CrLf
Page 18 of 29
mov edx, OFFSET space
add edx, 5
call WriteString
call WriteString
call CrLf
add edx, 4
call WriteString
call WriteString
call CrLf
add edx, 3
call WriteString
call WriteString
call CrLf
call waitmsg
exit
main ENDP
Page 19 of 29
END main
● Question 7
INCLUDE Irvine32.inc
.data
● Question 8
INCLUDE Irvine32.inc
.data
mostFrequent DWORD 0
maxCount DWORD 0
.code
Page 20 of 29
main PROC
mov ecx, 10
input_loop:
call WriteString
call ReadInt
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 esi, 0
Page 21 of 29
inner_loop:
cmp esi, 10
jge check_max
jne next_element
inc edx
next_element:
inc esi
jmp inner_loop
check_max:
jle skip_update
skip_update:
inc ebx
jmp outer_loop
Page 22 of 29
end_outer_loop:
call WriteString
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 ?
.code
main PROC
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:
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:
mov eax, c
cmp eax, a
jle end_program
mov eax, e
cmp eax, c
jl end_program
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
character byte ?
.code
main PROC
call WriteString
read_loop:
call readchar
Page 27 of 29
cmp al, 13 ; for enter
je end_read
inc ecx
jmp read_loop
end_read:
call WriteString
call readchar
mov character, al
find_char:
je not_found
je found
inc ecx
jmp find_character
Page 28 of 29
not_found:
call WriteString
found:
call WriteString
call WriteChar
call WriteString
call WriteDec
call Crlf
jmp end_program
end_program:
call waitmsg
exit
main ENDP
END main
Page 29 of 29