Uploaded by Areebaejaz100

Assembly Language

advertisement
Question No 1:
Write a program to add three numbers?
Answer:-
Addition
.386
Include irvine32.inc
.model small
.stack 100h
.data
msg db "Enter the first number",0
msg1 db "Enter the second number",0
msg2 db "The sum of two numbers is",0Dh,0Ah,0
msg3 db "Enter the third number",0Ah,0Dh,0
msg4 db "Your result is",0Ah,0Dh,0
var dword ?
var1 dword ?
.code
main proc
mov edx,offset msg
call writeString
call ReadInt
mov var,eax
mov edx,offset msg1
call WriteString
call ReadInt
add eax,var
mov edx,offset msg2
call WriteString
call WriteInt
mov var1,eax
call crlf
mov edx,offset msg3
call WriteString
call ReadInt
add eax,var1
mov edx,offset msg4
call WriteString
call WriteInt
exit
main endp
end main
Question No 2:
.386
include irvine32.inc
.model small
.stack 100h
.data
msg db "Enter the first number",0
msg1 db "Enter the second number",0
msg2 db "The subtraction of two numbers is",0
msg3 db "Enter the third number",0
msg4 db "Your result is",0
var dword ?
var1 dword ?
.code
main proc
mov edx,offset msg
call WriteString
call ReadInt
mov var,eax
mov edx,offset msg1
call WriteString
call ReadInt
sub var,eax
mov eax,var
mov edx,offset msg2
call WriteString
call writeInt
mov var1,eax
call crlf
mov edx,offset msg3
call WriteString
call ReadInt
sub eax,var1
mov edx,offset msg4
call WriteString
call WriteInt
exit
main endp
end main
Question No 3:-
.386
include irvine32.inc
.model small
.stack 100h
.data
msg db "Enter the first number dividend",0
msg1 db "Enter the second number divisor",0
msg2 db "Your quotient result is",0
msg3 db "your remainder is",0
var dword ?
.code
main proc
mov edx,offset msg
call WriteString
call readInt
mov var,eax
mov edx,offset msg1
call WriteString
call ReadInt
mov ebx,eax
mov eax,var
mov edx,0
div ebx
mov eax,edx
mov edx,offset msg2
call WriteString
call writeInt
exit
main endp
end main
Question No 4
Answer
If else Comparison
.386
include irvine32.inc
.model small
.stack 100h
.data
var dword 20
var1 dword 5
msg2 db "Your answer is an even number",0
msg3 db "Your answer is an odd number",0
.code
main proc
mov eax,var
mov edx,0
div var1
call WriteInt
call crlf
mov eax,edx
call WriteInt
call crlf
cmp edx,0
je evencode
jne oddcode
evencode:
mov edx,offset msg2
Call WriteString
exit
oddcode:
mov edx,offset msg3
call WriteString
exit
invoke exitProcess,0
main endp
end main
Question No 5
Write a program that enters two numbers and a character from user and character?
Answer:
.386
include irvine32.inc
.model small
.stack 100h
.data
msg db "Enter the first number",0
msg1 db "Enter the second number",0
msg2 db "Enter a character",0
msg3 db "Addition:",0
msg4 db "Subtraction",0
msg5 db "Multiplication",0
msg6 db "Division",0
msg7 db "Remainder",0
msg8 db "Wrong character",0
var dword ?
var1 dword ?
var2 db ?
.code
main proc
mov edx,offset msg
call WriteString
call ReadInt
mov var,eax
mov edx,offset msg1
call WriteString
call ReadInt
mov var1,eax
mov edx,offset msg2
call WriteString
call Readchar
call Writechar
mov var2,al
cmp var2,'+'
je addition
cmp var2,'-'
je subtraction
cmp var2,'*'
je multiplication
cmp var2,'/'
je division
cmp var2,'%'
je remainder
call crlf
mov edx,offset msg8
call WriteString
exit
addition:
mov eax,var
add eax,var1
call crlf
mov edx,offset msg3
call WriteString
call crlf
call WriteInt
exit
subtraction:
mov eax,var
sub eax,var1
call crlf
mov edx,offset msg4
call WriteString
call crlf
call writeInt
exit
multiplication:
mov eax,var
mov edx,0
mov ebx,var1
mul ebx
call crlf
mov edx,offset msg5
call WriteString
call crlf
call WriteInt
exit
division:
mov eax,var
mov ebx,var1
mov edx,0
div ebx
call crlf
mov edx,offset msg6
call WriteString
call crlf
call WriteInt
exit
remainder:
mov eax,var
mov ebx,var1
mov edx,0
div ebx
mov eax,edx
call crlf
mov edx,offset msg7
call WriteString
call crlf
call writeInt
exit
invoke exitProcess,0
main endp
end main
Question No 6:Answer
.386
Include irvine32.inc
.model small
.stack 100h
.data
msg db "I study in Punjab University Gujranwala Campus",0
msg1 db "My name is Areeba",0
.code
main proc
mov edx,offset msg
call WriteString
mov eax,20000
call Delay
call crlf
mov edx,offset msg1
call WriteString
exit
main endp
end main
Question No 7
.386
include irvine32.inc
.model small
.stack 100h
.data
msg db "Enter the first no ",0
var dword ?
.code
main proc
mov edx,offset msg
call WriteString
call ReadInt
mov var,eax
mov ebx,1
mov ecx,20
l1:
mov eax,var
mov edx,0
mul ebx
call writeint
inc ebx
call crlf
loop l1
exit
main endp
end main
Question No 8
.386
include irvine32.inc
.model small
.stack 100h
.data
msg db "Enter the first no ",0
msg1 db "Enter the second number",0
msg2 db "*",0
msg3 db "=",0
var dword ?
var1 dword ?
.code
main proc
mov edx,offset msg
call WriteString
call ReadInt
mov var,eax
mov edx,offset msg1
call Writestring
call ReadInt
mov var1,eax
mov ecx,20
l1:
mov eax,var
call WriteInt
mov var,eax
mov edx,offset msg2
call WriteString
mov eax,var1
call WriteInt
mov var1,eax
mov edx,offset msg3
call WriteString
mov eax,var
mul var1
call WriteInt
call crlf
inc var1
loop l1
exit
main endp
end main
Question No 9:Answer:
.386
include irvine32.inc
.model small
.stack 100h
.data
.code
msg db "Enter the first number",0
msg1 db "Divisible",0
msg2 db "Not Divisible",0
var dword ?
var1 dword 6
main proc
mov edx,offset msg
call WriteString
call ReadInt
mov edx,0
div var1
call WriteInt
call crlf
cmp edx,0
je divisible
jne notdivisible
divisible:
mov edx,offset msg1
call WriteString
exit
notdivisible:
mov edx,offset msg2
call WriteString
exit
invoke exitProcess,0
main endp
end main
Question No 10
.386
Include irvine32.inc
.model small
.stack 100h
.data
msg db "Enter a character",0
char db ?
msg1 db "Invalid Character",0
msg2 db "It is a vowel character",0
msg3 db "It is a consonanat character",0
.code
main proc
mov edx,offset msg
call WriteString
call Readchar
call WriteChar
call crlf
mov char,al
cmp char,'a'
je v1
cmp char,'e'
je v1
cmp char,'i'
je v1
cmp char,'o'
je v1
cmp char,'u'
je v1
mov edx,offset msg3
call WriteString
exit
v1:
call crlf
mov edx,offset msg2
call WriteString
exit
invoke exitProcess,0
main endp
end main
Question No 11
.386
Include irvine32.inc
.model small
.stack 100h
.data
msg db "Enter a character",0
char db ?
msg1 db "Invalid Character",0
msg2 db "It is a vowel character",0
msg3 db "It is a consonanat character",0
.code
main proc
mov edx,offset msg
call WriteString
call Readchar
call WriteChar
call crlf
mov char,al
cmp char,'a'
je v1
cmp char,'e'
je v1
cmp char,'i'
je v1
cmp char,'o'
je v1
cmp char,'u'
je v1
cmp char,'A'
je v1
cmp char,'E'
je v1
cmp char,'I'
je v1
cmp char,'O'
je v1
cmp char,'U'
je v1
cmp char,' '
je v2
mov edx,offset msg3
call WriteString
exit
v1:
call crlf
mov edx,offset msg2
call WriteString
exit
v2:
mov edx,offset msg1
call WriteString
exit
exit
main endp
end main
Question No 11:Answer
.386
Include irvine32.inc
.model small
.stack 100h
.data
msg db "Enter the first number",0
msg1 db "A+",0
msg2 db "A",0
msg3 db "B+",0
msg4 db "B",0
msg5 db "C+",0
msg6 db "D+",0
msg7 db "D",0
msg8 db "Fail",0
var dword ?
.code
main proc
mov edx,offset msg
call WriteString
call ReadInt
mov var,eax
cmp var,85
jge g1
cmp var,80
jge g2
cmp var,75
jge g3
cmp var,65
jge g4
cmp var,55
je g5
cmp var,45
jge g6
cmp var,35
jge g7
cmp var,35
jl g8
exit
g1:
mov edx,offset msg1
call WriteString
exit
g2:
mov edx,offset msg2
call WriteString
exit
g3:
mov edx,offset msg3
call WriteString
exit
g4:
mov edx,offset msg4
call WriteString
exit
g5:
mov edx,offset msg5
call WriteString
exit
g6:
mov edx,offset msg6
call WriteString
exit
g7:
mov edx,offset msg7
call Writestring
exit
g8:
mov edx,offset msg8
call WriteString
exit
invoke exitProcess,0
main endp
end main
Question no 12
.386
Include irvine32.inc
.model small
.stack 100h
.data
msg db "Your answer is an even number",0
msg1 db "Your answer is an odd number",0
var dword 7
.code
main proc
call randomize
mov eax,100
call randomrange
call WriteInt
mov edx,0
div var
call crlf
call writeInt
cmp edx,0
je e
jne o
e:
call crlf
mov edx,offset msg
call WriteString
exit
o:
mov edx,offset msg1
call WriteString
exit
call MsgBox
invoke exitProcess,0
main endp
end main
Call MsgBox
.386
Include irvine32.inc
.model small
.stack 100h
.data
msg db "Dialogue Title",0
msg1 byte "Would you like to read my content? ",0ah,0dh
byte "If yes, then please click Ok to continue....",0
.code
main proc
mov ebx,offset msg
mov edx,offset msg1
call MsgBoxAsk
exit
main endp
end main
Question No 13
Write a program that takes starting and ending number from user and prints even and odd
numbers between them?
Answer:.386
include Irvine32.inc
.model small
.stack 100
.data
msg1 db" Starting Number : ",0
msg2 db" Ending number : ",0
msg3 db" EVEN NUMBERS are : ",0
msg4 db" ODD NUMBERS are : ",0
msg5 db" TOTAL EVEN NUMBERS are : ",0
msg6 db" TOTAL ODD NUMBERS are : ",0
var1 dword ?
var2 dword ?
var3 dword 2
var4 dword ?
var5 dword ?
var6 dword 0
var7 dword 0
.code
main proc
mov edx,offset msg1
call writestring
call readint
mov var1,eax
mov var4,eax
mov edx,offset msg2
call writestring
call readint
mov var2,eax
mov var5,eax
mov eax,var2
sub eax,var1
add eax,1
mov ecx,eax
mov edx,offset msg3
call writestring
call crlf
l1:
mov edx,0
mov eax,var1
div var3
cmp edx,0
je ok1
jne no1
ok1:
mov eax,var1
call writeint
call crlf
mov eax,1
add var6,eax
no1:
inc var1
loop l1
call crlf
mov edx,offset msg5
call writestring
mov eax,var6
call writeint
mov eax,var5
sub eax,var4
add eax,1
mov ecx,eax
call crlf
call crlf
mov edx,offset msg4
call writestring
call crlf
l2:
mov edx,0
mov eax,var4
div var3
cmp edx,1
je ok2
jne no2
ok2:
mov eax,var4
call writeint
call crlf
mov eax,1
add var7,eax
no2:
inc var4
loop l2
call crlf
mov edx,offset msg6
call writestring
mov eax,var7
call writeint
invoke exitprocess,0
main endp
end main
Question No 14:By Moin
.386
include Irvine32.inc
.model small
.stack 100h
.data
msg1 db "Enter starting number: ", 0
msg2 db "Enter ending number: ", 0
msg3 db " is even.", 0
var3 dword 2
var1 dword ?
var2 dword ?
.code
main proc
; Prompt user for starting number
mov edx, offset msg1
call WriteString
call ReadInt
mov var1, eax
; move starting number to var1
; Prompt user for ending number
mov edx, offset msg2
call WriteString
call ReadInt
mov var2, eax
mov eax,0
; move ending number to var2
; Loop through the range of numbers
mov ecx,var1
; initialize loop counter to starting number
LoopStart:
cmp ecx, var2
jg LoopEnd
number
; compare current number to ending number
; jump out of the loop if current number is greater than ending
; Check if current number is even
mov eax, ecx
; move current number to ebx
mov edx,0
div var3
cmp edx, 0
jne LoopNext
; divide current number by 2
; check if remainder is zero
; jump to next iteration if current number is odd
je PrintEven
PrintEven:
; Print even number
mov eax,ecx
call writeint
mov edx, offset msg3
; print current number
; print " is even" message
call WriteString
call crlf
LoopNext:
inc ecx ;
increment loop counter
jmp LoopStart ;
jump back to start of the loop
LoopEnd:
exit
loop LoopStart
invoke exitprocess,0
main endp
end main
Question No 15:Write a program that takes starting and ending number from user and takes randomize
using the range and decides whether even and odd.
.data
msg1 db "Enter the starting point of your range: ", 0
msg2 db "Enter the ending point of your range: ", 0
msg3 db "Even", 0
msg4 db "Odd", 0
endpoint dword ?
startpoint dword ?
.code
main proc
mov edx, offset msg1
call writestring
call readint
mov startpoint, eax
mov edx, offset msg2
call writestring ;70
call readint
;40
sub eax, startpoint
inc eax
;70-40
;30
;31
mov endpoint, eax
call randomize
mov eax, endpoint
;(0-30)
call randomrange
call writeint
;just to see the original output
add eax, startpoint
call writeint
mov edx, 0
mov ebx, 2
div ebx
cmp edx, 0
je evens
mov edx, offset msg4
call writestring
exit
evens:
mov edx, offset msg3
;num+40
call writestring
exit
invoke exitprocess, 0
main endp
end main
Download