【问题标题】:Convert Hexadecimal String to Binary in Assembly x86 MASM在汇编 x86 MASM 中将十六进制字符串转换为二进制
【发布时间】:2022-11-14 03:14:55
【问题描述】:

例如,我有: n1 db "1234" 它代表一个十六进制值。我想将其转换为二进制并将其存储在 32 位寄存器中,在这种情况下,结果将是: EAX = 0000 0000 0000 0000 0001 0010 0011 0100

方法是什么?

【问题讨论】:

标签: assembly x86 masm


【解决方案1】:

Inputting multi-radix multi-digit signed numbers with DOS 显示了如何在答案代码 sn-ps 中进行此特定转换2a2b.您还可以学习如何从八进制和二进制转换。
不要被提到的“DOS”误导。原理保持不变,只是使用更大的寄存器:AX 变为 EAX。

sn-p2a

    ; Hexadecimal
.a: inc  si             ; Next character
    shl  ax, 1          ; Result = Result * 16
    shl  ax, 1
    shl  ax, 1
    shl  ax, 1
    mov  dl, [si]       ; -> DL = {["0","9"],["A","F"]} (NewDigit)
    cmp  dl, "9"
    jbe  .b
    sub  dl, 7
.b: sub  dl, 48
    or   al, dl         ; Result = Result + NewDigit
    loop .a

    ; Octal
.a: inc  si             ; Next character
    shl  ax, 1          ; Result = Result * 8
    shl  ax, 1
    shl  ax, 1
    mov  dl, [si]       ; -> DL = ["0","7"] (NewDigit)
    sub  dl, 48
    or   al, dl         ; Result = Result + NewDigit
    loop .a

    ; Binary
.a: inc  si             ; Next character
    cmp  byte [si], "1" ; -> CF=1 for "0", CF=0 for "1"
    cmc                 ; -> CF=0 for "0", CF=1 for "1"
    rcl  ax, 1          ; Result = Result * 2 + NewDigit
    loop .a

sn-p2b带字符验证

    ; Hexadecimal
.a: inc  si             ; Next character
    mov  dl, [si]       ; -> DL = {["0","9"],["A","F"]} (NewDigit) ?
    cmp  dl, "9"
    jbe  .b
    sub  dl, 7
.b: sub  dl, 48
    cmp  dl, 15
    ja   .z             ; Stop if not a digit
    rol  ax, 1          ; Result = Result * 16
    rol  ax, 1
    rol  ax, 1
    rol  ax, 1
    test al, 15
    jnz  .o
    or   al, dl         ; Result = Result + NewDigit
    loop .a

    ; Octal
.a: inc  si             ; Next character
    mov  dl, [si]       ; -> DL = ["0","7"] (NewDigit) ?
    sub  dl, 48
    cmp  dl, 7
    ja   .z             ; Stop if not a digit
    rol  ax, 1          ; Result = Result * 8
    rol  ax, 1
    rol  ax, 1
    test al, 7
    jnz  .o
    or   al, dl         ; Result = Result + NewDigit
    loop .a

    ; Binary
.a: inc  si             ; Next character
    mov  dl, [si]       ; -> DL = ["0","1"] (NewDigit) ?
    sub  dl, 48
    cmp  dl, 1
    ja   .z             ; Stop if not a digit
    shl  ax, 1          ; Result = Result * 2
    jc   .o
    or   al, dl         ; Result = Result + NewDigit
    loop .a

【讨论】:

    猜你喜欢
    • 2012-04-02
    • 2014-05-24
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    相关资源
    最近更新 更多