Microprocessor and Assembly Language
Microprocessor and Assembly Language
QUESTION 2
Take two numbers from users , store them in variables num1
and num2 respectively. Calculate their sum, difference,
product, quotient and remainder and print the result as per
following format: Num1=5 and Num2=3 Num1+num2= 8
Num1-num2=2 Num1*num2=15 quotient(Num1/num2)=1
remainder(Num1/Num2)=2
.data
num1 DWORD ?
num2 DWORD ?
.code
main PROC
call WriteString
call ReadInt
mov num1, eax
call WriteString
call ReadInt
mov num2, eax
calc:
mov eax, num1
add eax, num2
call WriteDec
call Crlf
sub eax, num2
sub eax, num2
call WriteDec
call Crlf
imul eax, num2
call WriteDec
call Crlf
mov eax, num1
cdq
idiv num2
call WriteDec
call Crlf
mov eax, edx
call WriteDec
call Crlf
3478922131
invoke ExitProcess, 0
main ENDP
END main
Question 3
Print the following format
*
**
***
****
*****
.data
newline BYTE 10, 0
.code
main PROC
mov ecx, 5
row_loop:
mov edx, ecx
mov eax, 6
sub eax, ecx
print_star:
cmp eax, 0
je newline
call WriteChar
dec eax
jmp print_star
newline:
call WriteString
loop row_loop
invoke ExitProcess, 0
main ENDP
END main
Question 4
Print the even numbers from 0 to N Where N is user input.
.data
num DWORD ?
.code
main PROC
call WriteString
call ReadInt
mov num, eax
xor ecx, ecx
mov eax, 2
even_loop:
cmp eax, num
jg end
call WriteDec
call Crlf
add eax, 2
jmp even_loop
end:
invoke ExitProcess, 0
main ENDP
END main
Question 5
Print the first N multiple of 3 ; where N is User input.
.data
num DWORD ?
.code
main PROC
call WriteString
call ReadInt
mov ecx, eax
mov eax, 3
mul_loop:
mov edx, 0
imul edx, ecx, 3
call WriteDec
call Crlf
loop mul_loop
invoke ExitProcess, 0
main ENDP
END main
Question 6
Print the first N prime numbers from 1 to N, where N is
User Input.
.data
num DWORD ?
.code
main PROC
call WriteString
call ReadInt
mov num, eax
xor edx, edx
mov ecx, 2
prime_loop:
mov eax, ecx
mov ebx, 2
check_loop:
cmp ebx, ecx
je prime_found
mov edx, 0
div ebx
test edx, edx
jz skip_prime
inc ebx
jmp check_loop
prime_found:
call WriteDec
call Crlf
dec num
skip_prime:
inc ecx
cmp num, 0
jne prime_loop
invoke ExitProcess, 0
main ENDP
END main
Question 7
Print the first N elements of Fibonacci sequence.
(Fibonacci Sequence is 0 1 1 2 3 5 8 …)
.data
num DWORD ?
a DWORD 0
b DWORD 1
temp DWORD ?
.code
main PROC
call WriteString
call ReadInt
mov ecx, eax
call WriteDec
call Crlf
mov eax, b
call WriteDec
call Crlf
fib_loop:
mov eax, a
add eax, b
mov temp, eax
call WriteDec
call Crlf
mov eax, b
mov a, eax
mov b, temp
loop fib_loop
invoke ExitProcess, 0
main ENDP
END main
Question 8
Print the sum of first n even numbers; N is User input
.data
num DWORD ?
sum DWORD 0
.code
main PROC
call WriteString
call ReadInt
mov ecx, eax
xor eax, eax
calc_sum:
shl ecx, 1
add sum, ecx
dec ecx
loop calc_sum
mov eax, sum
call WriteDec
call Crlf
invoke ExitProcess, 0
main ENDP
END main
Question 9
: Print the sum of square of N natural numbers ; N is user
Input
.data
num DWORD ?
sum DWORD 0
.code
main PROC
call WriteString
call ReadInt
mov ecx, eax
xor eax, eax
square_sum:
imul eax, ecx
add sum, eax
dec ecx
loop square_sum
call WriteDec
call Crlf
invoke ExitProcess, 0
main ENDP
END main
Question 10
: Print the sum of cube of first N natural numbers, where N
is input.
data
num DWORD ?
sum DWORD 0
.code
main PROC
call WriteString
call ReadInt
mov ecx, eax
cube_sum:
mov eax, ecx
imul eax, ecx
imul eax, ecx
add sum, eax
dec ecx
loop cube_sum
call WriteDec
call Crlf
invoke ExitProcess, 0
main ENDP
END main