Reputation: 1
Question regarding Assembly Language Programming (using Masm Irvine32 Library).
I am trying to check and count negative numbers in the given array:
arrayW: 3, -2, 5, 7, 2, 9, -11, 32, -19, 18, 17, 15, -5, 2, 3, 1, -21, 27,-29, 20,
In the output the number that is displayed will be 6 because there are 6 negative elements in arrayW, namely:
-2, -11, -19, -5, -21, -29.
Here I have tried a code which is calculating numbers between (-2, 17) in the given array, but unable to understand how to code to check and count negative elements is given array, can anyone please help me here
INCLUDE Irvine32.inc
.DATA
arrayW SDWORD 3, -2, 5, 7, 2, 9, -11, 32, -19, 18, 17, 15, -5, 2, 3, 1, -21, 27,-29, 20
initVal SDWORD -2
finalVal SDWORD 17
finalCount SDWORD ?
.CODE
between PROC
cmp eax,ebx
jg next3 ; if eax>ebx, 0<-EAX
cmp ebx,ecx
jg next3 ; if ebx>ecx, 0<-EAX
mov eax,1 ; 1 in EAX register if eax<=ebx and ebx<=eax
jmp next4
next3:
mov eax,0 ; if (eax<=ebx and ebx<=ecx) evaluates to false,
then 0<-EAX
next4:
;call DumpRegs ;Display the register contents
ret
between ENDP
main PROC
mov edi, 0
mov ecx, LENGTHOF arrayW
mov edx,0 ;EDX will hold the count of the elements in the array in the range [-2,17]
L1:
push ecx ;push the contents of counter register ecx to
stack
mov eax, initVal ;the element in the array should be <= -2
mov ebx,arrayW[edi] ;move the element in the array to ebx
mov ecx, finalVal ;the element in the array should be <= 17
call between ;between proc call
add edx,eax ;if the element is in the range [-2,17], add 1 to
EDX
add edi,TYPE arrayW ;add 4 to edi to move to the next element
pop ecx ;pop the value of counter register
loop L1 ;repeat the above for all the elements in the
array (until ecx is 0)
mov eax,edx ;Move the count to eax register
call WriteInt ;To display the output in decimal format
;call DumpRegs ;Display the register contents
exit
main ENDP
END main
Upvotes: 0
Views: 274
Reputation: 58467
There may be more efficient ways, but one possible solution would be to replace call between
/ add edx,eax
with this:
bt ebx,31 ; CF = (ebx < 0) ? 1 : 0
adc edx,0 ; edx += CF
Upvotes: 1