微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将字符串 IP 地址转换为 32 位整数 (ASM)

如何解决将字符串 IP 地址转换为 32 位整数 (ASM)

我想弄清楚如何将空终止的普通 IP 地址作为字符串并将其转换为 32 位整数。在此示例中,我的字符串位于地址 ES:DI+Qapktdat。 Qapktdat 是定义为存储在 ES:DI 中的字符串的结构体的成员。

我在结构体中读取或写入字符串没有问题,但计算 IP 时遇到问题。

例如,如果我使用字符串“192.168.7.2”,那么 EDX 应该等于十六进制值 C0A80702,但我没有得到任何接近它。

我可能做错了什么?

ipconvmath:
  mov BX,Qapktdat-1 ;BX=data pointer. Set it to beginning of data -1
  mov CX,11h ;Look for null within the first 17 bytes of data
  ipend:
    inc BX
    mov AL,[ES:DI+BX]
    cmp AL,0
    je ipok
  loop ipend
  mov DX,-1 ;cant find null char so exit
  ret
  ipok:
  mov BP,10   ;BP = multiplier of multiplier
  xor EDX,EDX ;EDX = eventual IP address
  ;We scan IP address starting at LAST digit
  nextipseg:
    mov CX,1  ;CX= immediate multiplier
    xor DL,DL ;lower 8 bits of EDX = 0
    nxb:
      xor AX,AX
      dec BX
      mov AL,[ES:DI+BX]
      cmp AL,'.'
      je nodot
    ;Input isnt DOT so see if its number 0-9
    cmp AL,30h ;#0
    jb badipcnv
    cmp AL,39h ;#9
    ja badipcnv
    sub AL,30h
    mul CX ;multiply by 1,10,or 100 depending on number position
    add DL,AL ;add it to our numeric IP
    mov AX,CX ;temporarily make AX=CX so multiply can work
    mul BP ;multiply the immediate multiplier by 10
    mov CX,AX
    cmp BX,Qapktdat ;see if were at beginning of data
    jne nxb
    ;we are so skip beginning
    nodot:
    rol EDX,8 ;shift existing contents over by 1 byte
  cmp BX,Qapktdat ;see if were at beginning 
  jne nextipseg
  ;here we are at beginning so were done
  ;EDX = ip address converted
  ret

  ;Reach here if a character is not 0-9 or a dot
  badipcnv:
    mov AX,1
  ret

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。