Новичок
Профиль
Группа: Участник
Сообщений: 19
Регистрация: 26.1.2008
Репутация: нет Всего: нет
|
1.Есть 2 ф-ии, кто может помочь переделать их на ассемблер.Очень нужно. | Код | const cuthalf = 100; var buf : array[0..((cuthalf*2)-1)] of Integer; a, b : string[cuthalf];
function min3(a, b, c: integer): integer; var Result : Integer; begin Result := a; if b < Result then Result := b; if c < Result then Result := c; min3 := Result; end;
function LeveDist(s, t: string): integer; var i, j, m, n : integer; cost, Result : integer; flip : boolean; begin s := copy(s, 1, cuthalf - 1); t := copy(t, 1, cuthalf - 1); m := length(s); n := length(t); if m = 0 then Result := n else if n = 0 then Result := m else begin flip := false; for i := 0 to n do buf[i] := i; for i := 1 to m do begin if flip then buf[0] := i else buf[cuthalf] := i; for j := 1 to n do begin if s[i] = t[j] then cost := 0 else cost := 1; if flip then buf[j] := min3((buf[cuthalf + j] + 1), (buf[j - 1] + 1), (buf[cuthalf + j - 1] + cost)) else buf[cuthalf + j] := min3((buf[j] + 1), (buf[cuthalf + j - 1] + 1), (buf[j - 1] + cost)); end; flip := not flip; end; if flip then Result := buf[cuthalf + n] else Result := buf[n]; end; LeveDist := Result; end;
Begin
readln(a); readln(b); writeln('leveDist=', leveDist(a,b));
End.
|
2. Вот есть некоторые наработки по фун-ии Левенштейна на ассемблере, но почему она не правильно считает расстояние=( | Код | ;ml.exe /c /coff test.asm ;link.exe /subsystem:console test.obj
.386 .model flat,stdcall option casemap:none
include d:\masm32\include\windows.inc include d:\masm32\include\kernel32.inc includelib d:\masm32\lib\kernel32.lib
lenstr PROTO :dword, :dword output PROTO :dword input PROTO :dword NumberInANSII PROTO :dword, :dword LevenshteinDistance PROTO :dword, :dword
.data hOut dd ? hIn dd ? str1 db "Enter first string: ",0 str2 db "Enter second string: ",0 str3 db "Levenshtein Distance: ",0 str4 db "Press 'Enter' for exit...",0 tab db ' ',0Dh,0Ah,0 buffer1 db MAX_PATH dup(0) buffer2 db MAX_PATH dup(0) Buffer3 db 32 dup(0)
.code Start: invoke GetStdHandle,STD_OUTPUT_HANDLE mov hOut,eax invoke GetStdHandle,STD_INPUT_HANDLE mov hIn,eax
invoke output,offset str1 invoke input,offset buffer1 invoke output,offset str2 invoke input,offset buffer2 ;подготовка строк mov edi,offset buffer1 invoke lenstr,offset buffer1,MAX_PATH mov byte ptr [edi+eax-3],0 mov edi,offset buffer2 invoke lenstr,edi,MAX_PATH mov byte ptr [edi+eax-3],0 invoke LevenshteinDistance,offset buffer1,offset buffer2 invoke NumberInANSII,offset Buffer3,eax invoke output,offset str3 invoke output,offset Buffer3 invoke output,offset tab invoke output,offset str4 invoke input,offset buffer1
invoke ExitProcess,0
;output procedure ;ebp+8 - lpString output proc uses ebx lpString:dword LOCAL len:dword invoke lenstr,lpString,MAX_PATH xchg eax,ebx invoke WriteConsole,hOut,lpString,ebx,addr len,0 ret output endp
;input procedure input proc lpBuffer:dword LOCAL len:dword invoke ReadConsole,hIn,lpBuffer,MAX_PATH,addr len,0 ret input endp
;получение длинны строки в байтах ;ebp+8 - lpString ;ebp+0Ch - max len lenstr proc uses ecx edi lpString:dword, maxlen:dword push ecx push edi mov edi,lpString push maxlen pop ecx xor eax,eax cld repne scasb sub edi,[ebp+8] xchg eax,edi pop edi pop ecx ret lenstr endp
;преобразование числа в строку NumberInANSII proc uses ebx edx ecx lpBuff:dword, num:dword mov ebx,10 xor ecx,ecx mov eax,num mov edi,lpBuff test eax,eax jz null cicl: test eax,eax jz end_cicl xor edx,edx div ebx add edx,30h push edx inc ecx jmp short cicl
null: mov dword ptr [edi],'0000' add edi,4 mov byte ptr [edi],0 jmp ext_na
end_cicl: pop eax stosb loop end_cicl mov byte ptr [edi],0 ext_na: ret NumberInANSII endp
LevenshteinDistance proc uses ebx edx ecx esi edi lpBuff1:dword, lpBuff2:dword
;eсли длинна одной из строк равна нулю, возвращается длинна другой строки invoke lenstr,lpBuff1,MAX_PATH xchg ebx,eax invoke lenstr,lpBuff2,MAX_PATH mov edx,eax test edx,edx jne stp2 mov eax,ebx jmp ld_ret stp2: test ebx,ebx jne stp3 mov eax,edx jmp ld_ret
;выясним какая строка длиннее stp3: cmp ebx,edx jz identy jg bigger ;если меньше sub edx,ebx mov ecx,edx ;ecx - разница jmp stp4 identy: xor ecx,ecx jmp stp4 bigger: sub ebx,edx mov ecx,ebx xchg ebx,edx
stp4: mov edi,lpBuff1 mov esi,lpBuff2 xor edx,edx dec ebx ;учет нуля в конце get_LevenshteinDistance: ;если какаято из строк кончилась test ebx,ebx je stp6 dec ebx ;сравниваем cmpsb je get_LevenshteinDistance inc edx jmp short get_LevenshteinDistance
stp6: add edx,ecx xchg eax,edx
ld_ret: ret LevenshteinDistance endp
end Start
|
|