【问题标题】:NASM Assembly converting from ASCII to decimalNASM 程序集从 ASCII 转换为十进制
【发布时间】:2014-03-24 10:39:48
【问题描述】:

我知道你可以加 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'

; add '0' to to convert the sum from decimal to ASCII
add eax, '0'

【问题讨论】:

  • 你应该谈论数字,而不是数字。
  • 只要eax 中的数字是09 之间的一个十进制数字,它就可以工作。因为 ASCII '0' 减去 '0' 是数字 0,ASCII '1' 减去 '0' 是数字 1 等等。如果您有更大的数字,您可以逐位转换。
  • 好的,所以它基本上在ascii中执行加法和减法。有道理,谢谢。

标签: assembly type-conversion decimal ascii nasm


【解决方案1】:

'0'48 的工作方式相同,因为'0'字符 0 的代码点,在ASCII 中,它确实是48

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

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

请记住,此方法仅适用于从 09 的值。如果要处理大于 9 的值,则需要将值分解为单个数字并一次处理一个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    • 2017-04-16
    • 2010-12-03
    相关资源
    最近更新 更多