Problem:
Taking two inputs from user and taking decision on the basis of these numbers:- If both numbers are positive and even, than multiply those using shift command.
- It both numbers are positive but one of them is odd, than divide those using div or idiv.
- If one is negative and both numbers are even, than again multiply using shift.
- If one number is negative and one of them is odd than divide using idiv or div.
Code
include irvine32.inc.data
array1 sdword 10 dup(?)
array2 sdword 10 dup(?)
array3 sdword 10 dup(?)
array4 sdword 5 dup(?)
array5 sdword 5 dup(?)
mul_array sdword 32 dup(0)
mine dword ?
temp dword 0
temp1 dword 0
temp2 dword 0
two dword 2
num1_msg byte "Enter 1st value: ",0
num2_msg byte "Enter 2nd value: ",0
equation_msg byte "Solving the equation: ",0ah,0dh,0
result byte " Result = ",0
even1_msg byte " 1st number is even.",0ah,0dh,0
even2_msg byte " 2nd number is even.",0ah,0dh,0ah,0dh,0
pos_msg byte "1st number is positive.",0ah,0dh,0
pos1_msg byte "2nd number is positive.",0ah,0dh,0
odd_msg byte " one number is odd.",0ah,0dh,0ah,0dh,0
neg_msg byte "number is negative.",0ah,0dh,0
ans_msg byte "Answer = ",0
.code
main proc
mov esi, offset array1
mov edi, offset array2
mov ebp, offset array3
mov ecx, 10
main_loop:
mov edx, offset num1_msg
call writestring
call readint
mov [esi], eax
mov edx, offset num1_msg
call writestring
call readint
mov [edi], eax
mov eax, [esi]
cmp eax, 0
jge positive_value1
jmp negative_value1
positive_value1:
mov edx, offset pos_msg
call writestring
mov eax, [edi]
cmp eax, 0
jge positive_value2
jmp negative_value1
positive_value2:
call positive
jmp jmp_to_loop
negative_value1:
call negative
jmp_to_loop:
add esi, 4
add edi, 4
add ebp, 4
loop main_loop
mov esi, offset array4
mov edi, offset array5
mov ecx, 5
mov ebp, offset array3
l1:
call crlf
mov eax, [ebp]
;call writeint
mov [esi], eax
add ebp, 4
add esi,4
loop l1
mov ecx, 5
l2:
call crlf
mov eax, [ebp]
;call writeint
mov [edi], eax
add ebp, 4
add edi, 4
loop l2
mov esi, offset array4
mov ecx, 5
l3:
call crlf
mov eax, [esi]
call writeint
add esi, 4
loop l3
mov edi, offset array5
mov ecx, 5
l4:
call crlf
mov eax, [edi]
call writeint
add edi, 4
loop l4
;call equation
call crlf
call crlf
call crlf
exit
main endp
positive proc
mov edx, offset pos1_msg
call writestring
mov edx, 0
mov eax, [esi]
cdq
idiv two
cmp edx, 0
je even_value1
jmp odd_value1
even_value1:
call crlf
mov edx, offset even1_msg
call writestring
mov edx, 0
mov eax, [edi]
cdq
idiv two
cmp edx, 0
je even_value2
jmp odd_value1
even_value2:
mov edx, offset even2_msg
call writestring
call multiply
call display
jmp jmp_from_even
odd_value1:
mov edx, offset odd_msg
call writestring
call divide
call display
jmp_from_even:
ret
positive endp
negative proc
call crlf
mov edx, offset neg_msg
call writestring
mov edx, 0
mov eax, [esi]
cdq
idiv two
cmp edx, 0
je even_value3
pop edx
jmp odd_value3
even_value3:
mov edx, offset even1_msg
call writestring
mov edx, 0
mov eax, [edi]
cdq
idiv two
cmp edx, 0
je even_value4
jmp odd_value3
even_value4:
mov edx, offset even2_msg
call writestring
call multiply
call display
jmp jmp_from_even
odd_value3:
mov edx, offset odd_msg
call writestring
call divide
call display
jmp_from_even:
ret
negative endp
multiply proc
push ecx
mov edx, offset mul_array
mov ebx, [esi]
mov mine, 0
mov temp, 0
mov ecx, 32
l:
shr ebx,1
jc one_jump
jmp else_jump
one_jump:
mov eax, mine
mov temp, eax
mov eax, [edi]
l1:
cmp temp, 0
je outside_loop
sub temp, 1
shl eax, 1
jmp l1
outside_loop:
mov [edx], eax
add edx, 4
else_jump:
add mine, 1
loop l
mov ecx, lengthof mul_array
mov edx, offset mul_array
mov eax, 0
add_array:
add eax,[edx]
add edx, 4
loop add_array
mov ecx, 32
mov ebx, 0
mov edx, offset mul_array
empty_mul_array:
mov [edx],ebx
add edx, 4
loop empty_mul_array
pop ecx
ret
multiply endp
divide proc
push ecx
mov edx, 0
mov eax, [esi]
cdq
mov ecx, [edi]
idiv ecx
pop ecx
ret
divide endp
display proc
call crlf
mov edx, offset ans_msg
call writestring
call writeint
call crlf
call crlf
mov [ebp], eax
ret
display endp
equation proc
mov edx, 0
mov eax, [array3+24]
cdq
idiv [array3+12]
mov temp2, eax
mov esi, offset temp2
mov edx, 0
mov eax, array3
cdq
idiv [array3+4]
mov temp1, eax
mov edi, offset temp1
call multiply
mov edx, [array3+16]
add edx, [array3+20]
add eax, edx
mov temp1, eax
mov esi, offset temp1
mov edi, offset [array3+8]
call multiply
add eax, array3[28]
call crlf
call crlf
mov edx, offset equation_msg
call writestring
call crlf
mov edx, offset result
call writestring
call writeint
ret
equation endp
end main
No comments :
Post a Comment
Note: Only a member of this blog may post a comment.