NASM 汇编从 ASCII 转换为十进制

ner*_*ion 3 assembly ascii decimal nasm type-conversion

我知道你可以加 48 从十进制转换为 ASCII 或减去 48 从 ASCII 转换为十进制,但为什么下面的代码也执行相同的转换?

; moving the first number to eax register and second number to ebx
; and subtracting ascii '0' to convert it into a decimal number
mov eax, [number1]
sub eax, '0'
Run Code Online (Sandbox Code Playgroud)

; add '0' to to convert the sum from decimal to ASCII
add eax, '0'
Run Code Online (Sandbox Code Playgroud)

pax*_*blo 6

'0'工作原理与 相同,48因为是字符'0'的代码点,在 ASCII 中,确实是。 048

因此所有这些都是等价的:

sub  al, 48          ; decimal
sub  al, '0'         ; character code
sub  al, 30h         ; hex
sub  al, 0x30        ; hex again
sub  al, 60q         ; octal
sub  al, 00110000b   ; binary
Run Code Online (Sandbox Code Playgroud)

请记住,此方法仅适用于从09包含在内的值。如果要处理 9 以上的值,则需要将该值分解为单独的数字并一次处理一个数字。