在程序集中将字符串解析为整数

时间:2016-12-27 22:52:41

标签: assembly x86 type-conversion masm

我的代码应该将String格式解析为int但它会改变f.e. 0到658688,我不知道该怎么做。 lodsd命令在这里是否正确?

toparse DB 128 dup(?)
mov toparse, "0"

  atoi proc uses esi edx inputBuff:DWORD
mov esi, inputBuff
xor edx, edx
.Repeat
lodsd
.Break .if !eax
imul edx, edx, 10
sub eax, "0"
add edx, eax
.Until 0
mov EAX, EDX
ret
  atoi endp

它返回658688

1 个答案:

答案 0 :(得分:1)

你需要invoke atoi proc与ascii的内存偏移量或push之前call atoi的偏移量。现在esi将只包含inputbuff中已有的随机值。

我修改了proc,以便它能成功处理完整的字符串。

toparse db "12345678",0

;mov toparse,"0" ; Max is 4 bytes so define string above

push offset toparse ; |
call atoi           ; - or: invoke atoi,offset toparse  

...

atoi proc uses esi inputBuff:DWORD ; Generally only need to preserve esi, edi and ebx
mov esi,inputBuff
xor edx,edx
xor eax,eax         ; Clear register
.Repeat
lodsb               ; Get one byte
.Break .if !eax
imul edx,edx,10     ; Multiply total by 10
sub eax,"0"         ; Subtract 48
add edx,eax         ; Add result to the total
.Until 0
mov eax,edx         ; Will result in 00BC614E (hex 12345678)
ret
atoi endp