Inputting multi-radix multi-digit signed numbers with DOS 显示了如何在答案代码 sn-ps 中进行此特定转换2a和2b.您还可以学习如何从八进制和二进制转换。
不要被提到的“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