【发布时间】:2013-04-16 13:31:21
【问题描述】:
我有这个汇编代码(linux 80x86 nasm),假设将十六进制数字转换为十进制:
my_func:
push ebp
mov ebp, esp ; Entry code - set up ebp and esp
pusha ; Save registers
mov dword[LC1],0
mov ecx,dword[ebp+8] ; Get argument (pointer to string)
start_loop:
mov ebx,0 ;zero ebx register
mov eax,16
mov edx,0
mov bl, byte[ecx] ;bl is the curr number
jmp bl_to_num ;change it to be a number
continue:
add dword[LC1], ebx ;dword[LC1] = dword[LC1]+ebx
inc ecx
cmp byte[ecx],0
je end_func
mul dword[LC1] ;dword[LC1]*eax = edx:eax
mov dword[LC1],eax
jmp start_loop
dword[LC1] 是我返回给 C 函数的参数,而 ecx 是指向接收到的字符串的指针。 函数:bl_to_num 只是将 bl 字节转换为数字(a=10,b=11..) 当我使用输入 1 运行此代码时,我收到输出 234。 当我使用输入 2 运行此代码时,我收到输出 250。 所以一个.. 我的错误在哪里? 谢谢!
编辑: 这是 bl_to_num:
bl_to_num:
cmp bl,'A'
je con_a
cmp bl,'a'
je con_a
cmp bl,'B'
je con_b
cmp bl,'b'
je con_b
cmp bl,'C'
je con_c
cmp bl,'c'
je con_c
cmp bl,'D'
je con_d
cmp bl,'d'
je con_d
cmp bl,'E'
je con_e
cmp bl,'e'
je con_e
cmp bl,'F'
je con_f
cmp bl,'f'
je con_f
sub bl,48
jmp continue
con_a:
mov bl,10
jmp continue
con_b:
mov bl,11
jmp continue
con_c:
mov bl,12
jmp continue
con_d:
mov bl,13
jmp continue
con_e:
mov bl,14
jmp continue
con_f:
mov bl,15
jmp continue
【问题讨论】:
-
bl_to_num在哪里?当您说“十六进制到十进制”时,您是指浮点数(IEE754 数字)中的十进制吗?和 BCD 一样?有符号整数?十六进制输入应该是 UINT 吗? -
当我说十六进制到十进制时,我的意思是我收到一个包含(最多 8 位)1-9,a-f 的字符串
-
...您也将其转换为具有 (0-9) 十进制基数的字符串?
-
我的函数应该返回一个数字,在 dword[LC1]
-
听起来您打算转换为二进制整数,而不是十进制。 (这是一个重要的区别。)
标签: assembly hex decimal nasm multiplication