我写了一个与C组合的汇编代码。只有在数组中的数字为正数时,它才有效。如果我要测试任何负数,它将跳过这些数字。
这是我的汇编代码:
INCLUDE Irvine32.inc
.data
; LSD
bestIndex dword 0
bestLen dword 0
curIndex dword 0
curLen dword 1
i dword 1
strLSD byte "Longest Decreasing Sequence: ", 0
strBestIndex byte "Begins at index ", 0
strBestLen byte " and has a length of ", 0
.code
findLDS proc C arr:sdword, len:dword
;arr - array passed from C
;len is the length of the array
mov ecx, len
mov esi, arr
add esi, type arr
compare:
.IF i <= ecx
jmp cont
.ELSE
jmp ex
.ENDIF
cont:
mov eax, dword ptr [esi - type arr]
.IF dword ptr [esi] <= eax
jmp incCurLen
.ELSE
jmp setCurLen
.ENDIF
incCurLen:
inc curLen
jmp compareCurLen
setCurLen:
mov curLen, 1
mov eax, i
mov curIndex, eax
jmp compareCurLen
compareCurLen:
mov eax, bestLen
.IF curLen > eax
jmp changeBest
.ELSE
jmp incI
.ENDIF
changeBest:
mov eax, curIndex
mov bestIndex, eax
mov eax, curLen
mov bestLen, eax
jmp incI
incI:
inc i
add esi, type arr
jmp compare
ex:
; when our loop ends, print the sequence
mov ecx, bestLen
mov ebx, bestIndex
mov edx, offset strLSD
call writestring
L1:
push ecx
mov esi, arr
mov eax, dword ptr [esi + type arr*ebx]
call writeint
mov al, ','
call writechar
mov al, ' '
call writechar
inc ebx
pop ecx
loop l1
call crlf
mov edx, offset strBestIndex
call writestring
mov eax, bestIndex
call writedec
mov edx, offset strBestLen
call writeString
mov eax, bestLen
call writedec
call crlf
ret
findLDS endp
END
这是我的C代码:
int main()
{
int arr[] = { -5, 10, 20, 80, 73, 32, 20, 22, 19, -5 };
int len = (sizeof(arr) / sizeof(*arr));
findLDS(arr, len);
return 0;
}
此数组的输出:
最长降序:+ 80,+ 73,+ 32,+ 20。从索引3开始,长度为4
这是正确的,但是如果我将数组的20(索引6)更改为-20,则输出为
最长降序:+ 80,+ 73,+ 32。从索引3开始,长度为3
答案 0 :(得分:1)
按照@Jester的建议,我将其更改为带签名的cmp,并且可以使用。
代码:
cont:
mov eax, dword ptr [esi - type arr]
cmp dword ptr [esi], eax
jle incCurLen
jmp setCurLen