无法找到最长递减序列的负数
Unable to find negative numbers for Longest Decreasing Sequence
我用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
按照@Jester 的建议,我将其更改为已签名的 cmp 并且有效。
代码:
cont:
mov eax, dword ptr [esi - type arr]
cmp dword ptr [esi], eax
jle incCurLen
jmp setCurLen
我用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
按照@Jester 的建议,我将其更改为已签名的 cmp 并且有效。
代码:
cont:
mov eax, dword ptr [esi - type arr]
cmp dword ptr [esi], eax
jle incCurLen
jmp setCurLen