Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

– ASSIGNMENT # 03 –

– COMP ORG & ASS. LANG –


SUBMITTED TO:

Mr. Muhammad Jabbar

SUBMITTED BY:

Najam ul Hassan (21011519-025)

Suleman Latif (21011519-037)

Fazal Tanveer (21011519-090)

Mohammad Noor Haider (21011519-116)

DATE:

February 1, 2023

DEPARTMENT:

Computer Science

SECTION – B

COURSE CODE – CS - 221

UNIVERSITY OF GUJRAT
– HAFIZ HAYAT CAMPUS –
– ARFA KARIM –
2

INDEXING
Q1. Write a program that display first five numbers and their sum? ............. 3
Q2. Write a program that display first five numbers. Also display square of each
number in front of the number. ................................................. 4
Q3. Write a program that input a number from user and display the factorial of
that number? ................................................................... 5
Q4. Write a program that input a number greater than 10. It then displays sum of
all odd numbers and sum of all even numbers from 1 to the number entered by the
user? .......................................................................... 6
Q5. Write a program that inputs numbers from user until the user enters a
negative number. Then calculate the average, maximum and minimum of all positive
numbers? ....................................................................... 8
Q6. Write a program that input numbers from user until user enters -1? ........ 10
Q7. Write a program that get two numbers from user and display the result of
first number raise to the power of second number? ............................. 11
Q8. Write a program that get a number from user and display either the user
entered number is prime number or not? ........................................ 13
Q9. Write a program that get a number from user and display the table of that
entered number, this process continues until user enter 0. .................... 15
Q10. Write a program that use nested loop and display the following output: ... 17
12345 ....................................................................... 17
1234 ........................................................................ 17
123 ......................................................................... 17
12 .......................................................................... 17
1 ........................................................................... 17
Q11. Write a program using nested loop and display the following output: ...... 18
***** ....................................................................... 18
**** ........................................................................ 18
*** ......................................................................... 18
** .......................................................................... 18
* ........................................................................... 18
Q12. Write a program using nested loop and display the following output: ...... 20
***** ....................................................................... 20
***** ....................................................................... 20
***** ....................................................................... 20
***** ....................................................................... 20
***** ....................................................................... 20
3

STATEMENT

Q1. Write a program that display first five numbers and their sum?

CODE

include irvine32.inc
.data
out1msg db "Following are the first five natural numbers: ", 10,
10, 0
out2msg db 10, "The sum of first five natural numbers is = ", 0
BeautifulSoup db 9, "|--->", 9, 0

; THE FOLLOWING ARRAY stores the indexes of the strings at offset of 4


(bcz it's a 32-bit type)
strings dd offset out1msg, offset out2msg, offset BeautifulSoup
sum dword 0

.code
main proc
mov eax, 1 ; because 1 is the first natural
number

mov edx, strings[0 * type strings] ; move the index of the first message
call writestring ; print that string
mov edx, strings[2 * type strings] ; move the index of the third message

.while eax < 6 ; LOOP STARTS


call writestring ; prints the third message (for
beauty �)
call writedec ; prints the value of {eax}
call crlf ; prints '\n'
add sum, eax ; adds the value to the variable
{sum}
inc eax ; increments the {eax} by one
.endw ; LOOP ENDS

mov edx, strings[1 * type strings] ; moves the index of second string
call writestring ; prints that string
mov eax, sum ; moves the value of sum to {eax}
call writedec ; prints that value

call readchar ; waits for the user to press any key


(return) to stop execution

exit
main endp
end main
4

STATEMENT

Q2. Write a program that display first five numbers. Also display
square of each number in front of the number.

CODE

include irvine32.inc
.data
out1msg db "Following are the first five natural numbers: ", 10,
0
JustSoup db 9, "When squared = ", 0
BeautifulSoup db 10, 9, "|--->", 9, 0

strings dd offset out1msg, offset BeautifulSoup, offset JustSoup

.code
main proc

mov edx, strings[0]


call writestring

mov ecx, 1
.while ecx < 6 ; LOOP STARTS
mov eax, ecx

mov edx, strings[4] ; for the arrow string


call writestring

call writedec ; displays the number 1 to 5

mov edx, strings[8] ; for the "When squared = " string


call writestring

mul eax ; squares each number by multiplying to


itself
call writedec

inc ecx ; increments the {ecx} by one


.endw ; LOOP ENDS

call crlf
call readchar

exit
main endp
end main
5

STATEMENT

Q3. Write a program that input a number from user and display the
factorial of that number?

CODE

include irvine32.inc
.data
out1msg db "Enter a number:> ", 0
BeautifulSoup db 10, 9, "|---> Fatorial of the number is = ", 0

strings dd offset out1msg, offset BeautifulSoup

.code
main proc
mov edx, strings[0] ; msg to take an input
call writestring
call readdec
mov ecx, eax ; copying the input value to {ecx}

.while ecx > 1 ; LOOP STARTS


dec ecx ; decrements {ecx} by one BEFORE the next
statement that
mul ecx ; multiplies {eax} by decremented value of the
{ecx}
.endw ; LOOP ENDS

mov edx, strings[4] ; moves the index of the output message


call writestring
call writedec

call crlf
call readchar

exit
main endp
end main
6

STATEMENT

Q4. Write a program that input a number greater than 10. It then
displays sum of all odd numbers and sum of all even numbers from 1 to
the number entered by the user?

CODE

include irvine32.inc
.data
out1msg db "Enter a number greater than 10:> ", 0
OtherSoup db 10, 9, "|---> Sum of odd numbers is = ", 0
BeautifulSoup db 10, 9, "|---> Sum of even numbers is = ", 0

strings dd offset out1msg, offset OtherSoup, offset BeautifulSoup


_bytwo_ dd 2
OddDude dd ?

.code
main proc
mov edx, strings[0] ; to take the input
.repeat ; REPEATS
call writestring
call readdec
.until eax > 10 ; until eax is smaller or equal to 10

comment !
The next part is a bit tricky!
We check to see if the number input
is even or odd. (We don’t know if there’s
an actual formula but we did this by ourselves)

1. Sum of even numbers from 1


to any given boundary, is
nothing but number of even
numbers squared minus the
number of even numbers

2. Similarly, sum of odd numbers


from 1 to any given boundary
is mere the square of the
number of odd numbers in that
range.

NOTE: We can find the number of


odds/even by dividing the
7

boundary by 2.

For odds, we
simply square that number.
(Add one to it if the
boundary is an odd number)
For evens, we subtract the
number of even numbers
after squaring it.
!

mov edx, 0
div _bytwo_
mov OddDude, edx

mov ebx, 1 ; Start from 1 so that we won't


; have to increment later
add ebx, eax
mov ecx, ebx

.if OddDude == 1
inc eax ; add one if the boundary is one
.endif

mul eax ; Odd numbers sum


imul ebx, ebx ; Even numbers sum (immediate multiplication)
sub ebx, ecx ; this will give us the sum
; even numbers in that range

mov edx, strings[4] ; Odd sum


call writestring
call writedec

mov eax, ebx


mov edx, strings[8] ; Even sum
call writestring
call writedec

call crlf
call readchar

exit
main endp
end main
8

STATEMENT

Q5. Write a program that inputs numbers from user until the user
enters a negative number. Then calculate the average, maximum and
minimum of all positive numbers?

CODE

include irvine32.inc
.data
in1msg db "Enter a number:> ", 0
line db 10, 10, "--------------------------------------------
-----", 10, 0
out1msg db 10, 9, "|---> Average of all numbers is = ", 0
out2msg db 10, 9, "|---> Maximum of the numbers is = ", 0
out3msg db 10, 9, "|---> Minimum of the numbers is = ", 0

messages dd offset in1msg, offset line, offset out1msg, offset


out2msg, offset out3msg
_sum_ dd 0
_min_ dd 0
_max_ dd 0

.code
main proc
mov edx, messages[0] ; helper string for inputing
mov eax, 0 ; initialized for the first loop
mov ecx, 0 ; to loop indefinitely

.while ecx >= 0


call writestring
call readint ; take an integer input

js _out ; if negative, we break out of the loop

.if ecx == 0 ; first loop only, set minimum


mov _min_, eax
.endif

.if eax > _max_ ; if the new number is greater, update


the mem
mov _max_, eax
.elseif eax < _min_
mov _min_, eax ; if the new number is smaller, update
the mem
.endif

add _sum_, eax ; add each number to the {sum} memory


label
9

inc ecx ; to keep track of how many values are


input
.endw

_out:
mov edx, 0
mov eax, _sum_ ; copying the value to {eax}
div ecx ; divide the sum by n (to get avg)

mov edx, messages[1 * 4] ; normal printing stuff


call writestring

mov edx, messages[2 * 4]


call writestring
call writedec

mov edx, messages[3 * 4]


call writestring
mov eax, _max_
call writedec

mov edx, messages[4 * 4]


call writestring
mov eax, _min_
call writedec

mov edx, messages[1 * 4]


call writestring

call readchar

exit
main endp
end main
10

STATEMENT

Q6. Write a program that input numbers from user until user enters -1?

CODE

include irvine32.inc
.data
in1msg db "Enter a number:> ", 0
out1msg db 10, 9, "|---> You just entered a negative number
which was = ", 0

messages dd offset in1msg, offset out1msg

.code
main proc
mov edx, messages[0]
mov eax, 0

.while eax >= 0


call writestring ; same as previous
call readint
js _out ; jumps out of the loop if sign flag
; gets set by the user input
.endw

_out:
mov edx, messages[4]
call writestring
call writeint

call readchar

exit
main endp
end main
11

STATEMENT

Q7. Write a program that get two numbers from user and display the
result of first number raise to the power of second number?

CODE

include irvine32.inc
.data
in1msg db "Enter first number:> ", 0
in2msg db "Enter second number:> ", 0
line db 0Ah, 0Ah, 09h, "-------------------------------------
--------------------------------------", 0Ah, 0h
out1msg db 10, 9, "|---> First number raised to the power of the
second number is = ", 0

messages dd offset in1msg, offset in2msg, offset line, offset out1msg

first_number dd ?
second_number dd ?

.code
main proc
mov edx, messages[0]
call writestring
call readdec
mov first_number, eax

mov edx, messages[1 * 4]


call writestring
call readdec
mov second_number, eax

mov eax, first_number


mov ecx, 1

.while ecx < second_number


imul eax, first_number ; multiply the number by itself and keep the
result
inc ecx
.endw

mov edx, messages[2 * 4] ; normal printing stuff


call writestring

mov edx, messages[3 * 4]


call writestring
call writedec
12

mov edx, messages[2 * 4]


call writestring

call readchar

exit
main endp
end main
13

STATEMENT

Q8. Write a program that get a number from user and display either the
user entered number is prime number or not?

CODE

include irvine32.inc
.data
in1msg db "Enter a number:> ", 0
line db 0Ah, 0Ah, 09h, "-------------------------------------
-------------------", 0Ah, 0h
out1msg db 10, 9, "|---> The number you entered is a PRIME
number <---|", 0
out2msg db 10, 9, "!---> The number you entered is NOT a prime
number <---!", 0

messages dd offset in1msg, offset line, offset out1msg, offset


out2msg

_number_ dd ?
_halfNumber_ dd ?
_isEven_ dd 2

.code
main proc
mov edx, messages[0]
call writestring
call readdec
mov _number_, eax

mov edx, messages[4]


call writestring

.if eax == 1 ; special case


jmp _@int_
.elseif eax < 4 ; special case
jmp _shureaz_
.endif

mov edx, 0
div _isEven_
mov _halfNumber_, eax
mov eax, _number_

.if edx == 0
jmp _@int_ ; prime number can never be an even number
.else
mov ebx, 3 ; to divide by the input number
14

.while ebx < _halfNumber_ ; we will only loop to the half of the
number
mov edx, 0 ; for division
div ebx ; if the number is perfectly divisible by the
odd number

.if edx == 0
jmp _@int_ ; we say that it's not a prime number
.endif

mov eax, _number_ ; copy the number back into {eax}


add ebx, 02h ; add 2 to the ebx for only checking for odd
numbers
.endw

jmp _shureaz_

.endif

_@int_:
mov edx, messages[3 * 4]
jmp _0ut_

_shureaz_:
mov edx, messages[2 * 4]
jmp _0ut_

_0ut_:
call writestring
mov edx, messages[4]
call writestring

call readchar

exit
main endp
end main
15

STATEMENT

Q9. Write a program that get a number from user and display the table
of that entered number, this process continues until user enter 0.

CODE

include irvine32.inc
.data
in1msg db "Enter any number for its table or 0 to quit:> ", 0
line db 0Ah, 0Ah, 09h, "-------------------------------------
-------------------", 0Ah, 0h
out1msg db 10, 9, "|---> Following is the table for the input
number, ", 0
out2msg db 10, 9, "|--->", 9, 0
out3msg db " x ", 0
out4msg db " = ", 0

messages dd offset in1msg, offset line, offset out1msg, offset


out2msg, offset out3msg, offset out4msg

_number_ dd ?

.code
main proc
.repeat
mov edx, messages[0]
call writestring
call readdec

.if eax == 0
jmp _out_ ; if the value entered is 0 (like they
said in the ad)
.endif ; we jump out of the loop and stop the
program

mov edx, messages[4] ; normal msg printing


call writestring
mov edx, messages[8]
call writestring
call writedec

call crlf

mov _number_, eax ; save the value for later use


mov ecx, 1
.while ecx < 11 ; we'll loop from 1 to 10
mov edx, messages[12] ; printing strings
call writestring
16

call writedec

mov edx, messages[16]


call writestring

mov eax, ecx


call writedec

mov edx, messages[20]


call writestring

mov eax, _number_


imul eax, ecx ; immediate multiplication with {ecx}'s
changing value

call writedec

mov eax, _number_ ; use the saved value from the memory for
next iteration
inc ecx ; increment the counter register for loop
as well as table
.endw

mov edx, messages[4]


call writestring

.until eax == 0

_out_:

call readchar ; wait for the user to press enter


(return)

exit
main endp
end main
17

STATEMENT

Q10. Write a program that use nested loop and display the following
output:
12345
1234
123
12
1

CODE

include irvine32.inc
.data
line db 0Ah, 0Ah, 09h, "-------------------------------------
-------------------", 0Ah, 0h
out1msg db 10, 9, "|--->", 9, 0

messages dd offset line, offset out1msg

_number_ dd ?

.code
main proc
mov edx, messages[0]
call writestring

mov edx, messages[4]


mov ecx, 5 ; because we wanna print 5 values in the first
row
.while ecx > 0
mov eax, 1
call writestring
.while eax <= ecx ; as long as {eax} is smaller than {ecx}
call writedec
inc eax ; increment {eax} for the next iteration
.endw
dec ecx ; keep decrementing {ecx} from 5 to 0, one by one
.endw

_out_:
mov edx, messages[0] ; prints a line
call writestring

call readchar

exit
18

main endp
end main

STATEMENT

Q11. Write a program using nested loop and display the following
output:
*****
****
***
**
*

CODE

include irvine32.inc
.data
line db 0Ah, 0Ah, 09h, "-------------------------------------
-------------------", 0Ah, 0h
out1msg db 10, 9, "|--->", 9, 0
out2msg db "*", 0

messages dd offset line, offset out1msg, offset out2msg

_number_ dd ?

.code
main proc
mov edx, messages[0]
call writestring

mov ecx, 5
.while ecx > 0
mov eax, 1

mov edx, messages[4]


call writestring

mov edx, messages[8]

.while eax <= ecx ; the same process as the previous one
call writestring ; difference is that, instead of printing
inc eax ; {eax}, we print a string which is "*"
.endw ; for a number of times
dec ecx
.endw ; finally becomes a triangle
19

_out_:
mov edx, messages[0]
call writestring

call readchar

exit
main endp
end main
20

STATEMENT

Q12. Write a program using nested loop and display the following
output:
*****
*****
*****
*****
*****

CODE

include irvine32.inc
.data
line db 0Ah, 0Ah, 09h, "-------------------------------------
-------------------", 0Ah, 0h
out1msg db 10, 9, "|--->", 9, 0
out2msg db "*", 0

messages dd offset line, offset out1msg, offset out2msg

_number_ dd ?

.code
main proc
mov edx, messages[0]
call writestring

mov ecx, 5
.while ecx > 0
mov eax, 1

mov edx, messages[4]


call writestring

mov edx, messages[8]

.while eax < 6 ; pretty much the same process


call writestring ; but this time we always iterate
inc eax ; exactly 5 coloums each time
.endw ; row is visited
dec ecx
.endw

_out_:
mov edx, messages[0]
21

call writestring

call readchar

exit
main endp
end main

You might also like