我是汇编语言编程的新手,无法找到给定数组中的负数

时间:2019-04-09 16:53:17

标签: arrays assembly masm

有关汇编语言编程的问题(使用Masm Irvine32库)。

我正在尝试检查并计算给定数组中的负数:

arrayW:  3, -2, 5, 7, 2, 9, -11, 32, -19, 18, 17, 15, -5, 2, 3, 1, -21, 27,-29, 20, 

在输出中显示的数字将为6,因为arrayW中有6个负数元素,即:

 -2, -11, -19, -5, -21, -29.

在这里,我尝试了一个代码,用于计算给定数组中(-2,17)之间的数字,但是无法理解给定数组的代码以检查和计算负数,有人可以在这里帮助我吗

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

1 个答案:

答案 0 :(得分:1)

也许有更有效的方法,但是一种可行的解决方案是用以下方法替换call between / add edx,eax

bt ebx,31   ; CF = (ebx < 0) ? 1 : 0
adc edx,0   ; edx += CF