UNIVERSITY OF GUJRAT
ASSIGNMENT NO: 3
SUBMITTED TO: MR. MUHAMMAD JABBAR
COURSE TITLE : COMPUTER ORGANIZATION
AND ASSEMBLY LANGUAGE.
DEPT : COMPUTER SCIENCE
Question#1:
include irvine32.inc
.data
str1 byte "Programming makes life interesting", 0
.code
main proc
call mainfunc
exit
main endp
mainfunc proc
mov edx, offset str1
call writestring
call crlf
ret
mainfunc endp
end main
Question#2:
Write a program which get two numbers from user in MAIN function. Then MAIN function
will pass these two data to another function which will find the GREATER number and return the
GREATER number to MAIN function. GREATER number would be displayed by MAIN function.
include irvine32.inc
.data
str1 byte "Enter first number: ", 0
str2 byte "Enter second number: ", 0
str3 byte "The greatest number is: ", 0
num1 dd 0
num2 dd 0
.code
main proc
mov edx, offset str1 ; displaying string one
call writestring
call readint
call crlf
mov num1, eax ; storing value in a variable
mov edx, offset str2 ; displaying string two
call writestring
call readint
call crlf
mov num2, eax ; storing in a variable
mov eax, num1 ; moving values to registers: eax, ebx.
mov ebx, num2
call mainfunc ; calling funcion
mov edx, offset str3 ; displaying the result returned by function
call writestring
call writeint
call crlf
exit
main endp
mainfunc proc ; function definition
.if eax > ebx ; checking condition
ret ; returning value to a register eax
.else
mov eax, ebx
.endif
ret ; returning value to a register eax
mainfunc endp ; function closes here
end main ; program ends here
Question#3:
Write a program which get a number from user in MAIN function. MAIN function passes this
number to a user defined function which print TABLE of the number.
include irvine32.inc
.data
str1 byte "Enter a number: ", 0
str2 byte "The table of a given number is: ", 0
str3 byte "*", 0
str4 byte "=", 0
num1 dd 0
num dd 0 ; declaring variable for functions
.code
main proc
mov edx, offset str1 ; displaying string one
call writestring
call readint
call crlf
mov num1, eax ; storing value in a variable
mov eax, num1 ; moving values to registers: eax, ebx.
mov edx, offset str2
call writestring
call crlf
call mainfunc ; calling funcion
exit
main endp
mainfunc proc ; function definition
mov ebx, eax
mov ecx, 1
.while ecx <= 10 ; iterations for table
mul ecx ; generating result
mov num, eax ; storing the result
mov eax, ebx ; table number
call writedec
mov edx, offset str3 ;*
call writestring
mov eax, ecx ; table counter
call writedec
mov edx, offset str4 ;=
call writestring
mov eax, num ; e.g, 2*2=4
call writedec
mov eax, ebx ; setting the eax to table number again
call crlf
inc ecx ; incrementing by one
.endw
ret ; returning value to a register eax
mainfunc endp ; function closes here
end main ; program ends here
Question#4:
Write a program which get a number from use in MAIN function. It then passes the number to
user-defined function which print FACTORIAL of the number.
include irvine32.inc
.data
str1 byte "Enter a number: ", 0
str2 byte "The factorial of a given number is: ", 0
num1 dd 0
num dd 0 ; declaring variable for functions
.code
main proc
mov edx, offset str1 ; displaying string one
call writestring
call readint
call crlf
mov num1, eax ; storing value in a variable
mov eax, num1 ; moving values to registers: eax, ebx.
mov edx, offset str2
call writestring
call crlf
call mainfunc ; calling funcion
exit
main endp
mainfunc proc ; function definition
mov ecx, 1
mov eax, 1
.while ecx <= num1 ; iterations for factorial
mul ecx
inc ecx ; incrementing by one
.endw
call writedec
ret ; returning value to a register eax
mainfunc endp ; function closes here
end main ; program ends here
Question#5:
Write a program which get MARKS in percentage from user in MAIN function. It then passes
the MARKS to a user-defined function which prints GRADE of the student.
A grad if mark greater than 80%
B grad if mark greater than 70%
C grad if mark greater than 60%
D grad if mark greater than 50%
F grad if mark less than 50%
include irvine32.inc
.data
str1 byte "Enter marks in percentage: ", 0
str2 byte "your grade is: ", 0
str3 byte "A ", 0
str4 byte "B ", 0
str5 byte "C ", 0
str6 byte "D ", 0
str7 byte "F ", 0
num1 dd 0
num dd 0 ; declaring variables for functions
.code
main proc
mov edx, offset str1 ; displaying string one
call writestring
call readint
call crlf
mov num1, eax ; storing value in a variable
mov eax, num1 ; moving values to registers: eax, ebx.
mov edx, offset str2
call writestring
call crlf
call mainfunc ; calling funcion
exit
main endp
mainfunc proc ; function definition
.if eax > 80
mov edx, offset str3
call writestring
.elseif eax > 70
mov edx, offset str4
call writestring
.elseif eax > 60
mov edx, offset str5
call writestring
.elseif eax > 50
mov edx, offset str6
call writestring
.else
mov edx, offset str7
call writestring
.endif
ret ; returning value to a register eax
mainfunc endp ; function closes here
end main ; program ends here
Question#6:
Write a program in which Open a file for input, read the file, and displays its contents on the
console. Make the input buffer small –about 300 bytes.
include irvine32.inc
.data
inputFileName db "input.txt", 0
outputFileName db "output.txt", 0
inputBuffer db 300 dup(?)
.code
main proc
; Open the input file
mov esi, offset inputFileName
call OpenInputFile
; Read the contents of the input file into the input buffer
mov edx, offset inputBuffer
mov ecx, 300
call ReadString
; Close the input file
mov esi, eax ; Move the file handle returned by ReadString to ESI
call CloseFile
; Display the contents of the input buffer on the console
mov edx, offset inputBuffer
call WriteString
; Create the output file
mov esi, offset outputFileName
call CreateOutputFile
; Write the contents of the input buffer to the output file
mov edx, offset inputBuffer
mov ecx, LengthOf inputbuffer ; Calculate the length of the input buffer
call WriteToFile
; Close the output file
mov esi, eax ; Move the file handle returned by WriteToFile to ESI
call CloseFile
; Exit the program
call Crlf
ret
main endp
end main
Question#7:
Write a program in which Create an output file; get the data from user on console (user can
write multiple lines on console), and save user input data on the output file. Make the buffer small
–
about 300 bytes.
include irvine32.inc
.data
outputfile db "output.txt", 0
outputbuffer db 300 dup(?)
.code
main proc
mov esi, offset outputfile
call createoutputfile
mov edx, offset outputbuffer
mov ecx, lengthof outputbuffer
call readstring
mov esi, eax
mov edx, offset outputbuffer
mov ecx, offset lengthof outputbuffer
call writetofile
mov esi, eax
call closefile
exit
main endp
end main
=================================