代码改变世界
2009-11-26 20:57
HYrz的菜园子
阅读(389)
评论()
编辑
收藏
举报
Code
function IntToStr(Value: Integer): ShortString;
// Value = eax
// Result = edx
asm
push ebx
push esi
push edi
mov edi,edx
xor ecx,ecx
mov ebx,10
xor edx,edx
cmp eax,0 // check for negative
setl dl
mov esi,edx
jnl @reads
neg eax
@reads:
mov edx,0 // edx = eax mod 10
div ebx // eax = eax div 10
add edx,48 // '0' = #48
push edx
inc ecx
cmp eax,0
jne @reads
dec esi
jnz @positive
push 45 // '-' = #45
inc ecx
@positive:
mov [edi],cl // set length byte
inc edi
@writes:
pop eax
mov [edi],al
inc edi
dec ecx
jnz @writes
pop edi
pop esi
pop ebx
end;
Code
function StrToInt(Value: ShortString): Integer;
// Value = eax
// Result = eax
asm
push ebx
push esi
mov esi,eax
xor eax,eax
movzx ecx,Byte([esi]) // read length byte
cmp ecx,0
je @exit
movzx ebx,Byte([esi+1])
xor edx,edx // edx = 0
cmp ebx,45 // check for negative '-' = #45
jne @loop
dec edx // edx = -1
inc esi // skip '-'
dec ecx
@loop:
inc esi
movzx ebx,Byte([esi])
imul eax,10
sub ebx,48 // '0' = #48
add eax,ebx
dec ecx
jnz @loop
mov ecx,eax
and ecx,edx
shl ecx,1
sub eax,ecx
@exit:
pop esi
pop ebx
end;