2013-04-23 49 views
2

我有這樣的彙編代碼(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,I接收輸出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 
+0

其中是'bl_to_num'?當你說「十六進制到十進制」時,你的意思是浮點數(IEE754數)的小數?和BCD一樣?一個有符號整數?十六進制輸入是否應該是UINT? – 2013-04-23 11:09:16

+0

當我說十六進制到十進制,我的意思是我收到一個字符串包含(最多8位數字)1-9,a-f – Javi 2013-04-23 11:13:16

+0

...並且您還將它轉換爲(0-9)小數基數的字符串? – 2013-04-23 11:13:53

回答

3

您的結果表明您的輸入字符串包含空值終止前一個換行符字符(0x0A)。所以,如果你輸入'1'你:

第一次迭代:
LC1 += 1
LC1 *= 16 (= 16)

第二次迭代:
LC += zero_extend(10-48)LC += 218 (= 16 + 218 = 234)


bl_to_num程序也被簡化了不少:

bl_to_num: 
    cmp bl,'9' 
    jbe digit 
    cmp bl,'F' 
    jbe already_uppercase 
    sub bl,' ' 
    already_uppercase: 
    sub bl,7 
    digit: 
    sub bl,'0' 
    jmp continue 


編輯:我的機器上的以下工作對我罰款(至強/ Ubuntu的64位):

format: .asciz "%d\n" 

.bss 
    LC1: .long 0 

.text 
.globl main 
.type main, @function 
main: 

movl 8(%esp),%ecx # get argv  
movl 4(%ecx),%ecx # get argv[1] (argv[0] is the program name) 

start_loop: 
xor %ebx,%ebx  
movl $16,%eax 
xor %edx,%edx 
movb (%ecx),%bl 

sub $48,%bl  # simplified bl_to_num that handles only '0'..'9' 

continue: 
add %ebx,LC1 
inc %ecx 
cmpb $0,(%ecx) 
je end_func 

mull LC1 
movl %eax,LC1 

jmp start_loop 

end_func: 

movl LC1,%eax 
pushl %eax 
pushl $format  # push the format string 
call printf 
addl $8,%esp  # pop the arguments 

movl $0, %eax # return value 
ret 

$ GCC -m32 -o hexarg hexarg.s
$ ./hexarg 67

+0

感謝您的回答! 的bl_to_num不能正常工作 我不明白你的意思的解決方案應該是... 謝謝! – Javi 2013-04-23 12:15:32

+0

如果您發現除「A-Fa-f0-9」之外的任何字符,而不僅僅是在找到NULL終止符時,一種解決方法是退出「start_loop」。 – Michael 2013-04-23 12:19:10

+0

我可以預期字符串是正確的.. 什麼是不工作的乘法(在我看來) – Javi 2013-04-23 12:25:11