【问题标题】:given hex input should be convert to decimal给定的十六进制输入应转换为十进制
【发布时间】:2015-06-13 00:18:52
【问题描述】:

对组装很陌生!

我的汇编代码采用十六进制输入,即 0-9 和 a-f 其他输入将导致错误,输入的每个字符都存储在一个数组中,并且数组中的每个元素都将被用于打印它们的整数值,但是对于某些原因元素地址正在打印。 请查看下面的代码并为我的问题提供解决方案。如果您需要完整的代码,请告诉我。

(下面代码的思想是获取十六进制数的每个元素并以十进制数打印元素)

        mov     ebx, array              

next_loop:                              ;this loop is to convert the character

        mov     al, [ebx]
        cmp     al, NEWLINE        ; here NEWLINE is 10 which is already defined
        je      end_loop          

        cmp     al, 97            
        jge     subblock
        jl      numsub

numsub:
        sub     al, 48
        mov     [ebx], al
        jmp     next

subblock:
        sub     al, 97
        add     al, 10
        mov     [ebx], al
        jmp     next

next:
        inc     ebx
        loop    next_loop

; below code is to print the value which is stored in array


end_loop:
        mov     ebx, array
        mov     eax, message2
        call    print_string

print_loop:
        mov     al, [ebx]
        inc     ebx
        call    print_int      ; addresses of ebx is printing but not the value
        cmp     al, NEWLINE
        jne     print_loop         
        jmp     finalend

请解决我的问题。 非常感谢。

【问题讨论】:

  • 如果没有看到print_int 做了什么,很难知道发生了什么。你怎么知道它正在打印地址,而不仅仅是其他一些无效值?另外,由于您使用NEWLINE 作为循环结束条件,您真的要使用loop 吗? ecx 是否具有您为循环计数器设置的值?如果不是,那么它只会循环在ecx 或直到NEWLINE 中给出的次数,以先到者为准。
  • 您还会遇到冲突:10NEWLINE,并且在您的最终答案中也是一个有效的十六进制数字值(您的打印代码无法区分 NEWLINE以及最初的 a 十六进制数字)。

标签: assembly x86 nasm


【解决方案1】:

在对 print_int 过程一无所知的情况下,我会假设问题出在某个位置。假设您正在使用 printf,则在推送具有您价值的寄存器时应该只涉及添加几个方括号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 2011-07-28
    • 2014-12-31
    • 1970-01-01
    • 2014-03-19
    相关资源
    最近更新 更多