赛格。故障 - 组装(NASM)

时间:2014-04-02 11:53:13

标签: assembly nasm

我正在尝试将十六进制数转换为BCD然后打印它。 (我将结果保存在EDX中,然后将其传递给LC1)。 我从用户那里得到了字符串,它在ECX中。 你能告诉我为什么吗? :

section .rodata
LC0:
    DB  "The result is:  %s", 10, 0 ; Format string

section .data                       ; data section, read-write
        tmpChar:    DB 0                ; this is a temporary var
     BCD:        DD 0
section .bss
LC1:
    RESB    32

section .text
    align 16
    global my_func
    extern printf

my_func:
    push    ebp
    mov ebp, esp    ; Entry code - set up ebp and esp
    pusha           ; Save registers

    mov ecx, dword [ebp+8]  ; Get argument (pointer to string)
    mov dword [eax], 0
    mov byte [tmpChar], 0       ; initialize answer
    mov dword [edx], 0      ; initialize answer - BCD representation

loop:

    cmp byte [ecx],'A'
    setnc bl    ; bl = (ecx >= 'A') ? 1 : 0
    cmp byte [ecx],'F'+1
    setc bh     ; bh = (ecx <= 'F') ? 1 : 0
    and bl,bh   ; bl = (ecx >= 'A' && ecx <= 'F')
    cmp bl, 1
    jz labelUpperCase

    cmp byte [ecx],'a'
    setnc bl    ; bl = (ecx >= 'a') ? 1 : 0
    cmp byte [ecx],'f'+1
    setc bh     ; bh = (ecx <= 'f') ? 1 : 0
    and bl,bh   ; bl = (ecx >= 'a' && ecx <= 'f')
    cmp byte bl, 1
    jz labelLowerCase

    cmp byte [ecx],'0'
    setnc bl    ; bl = (ecx >= '0') ? 1 : 0
    cmp byte [ecx],'9'+1
    setc bh     ; bh = (ecx <= '9') ? 1 : 0
    and bl,bh   ; bl = (ecx >= '0' && ecx <= '9')
    cmp byte bl, 1
    jz labelDigit

labelUpperCase:
    MOV byte al, [ecx]
    SUB byte al, 55
    ADD [edx], al 
    SHL [edx], 4
    jmp endLoop

labelLowerCase:
    MOV byte al, [ecx]
    SUB byte al, 87
    ADD [edx], al 
    SHL [edx], 4
    jmp endLoop

labelDigit:
    MOV byte al, [ecx]
    SUB byte al, 48
    ADD [edx], al 
    SHL [edx], 4
    jmp endLoop

endLoop:
    inc ecx             ; increment pointer
    cmp byte [ecx], 0           ; check if byte pointed to is zero
    jnz loop            ; keep looping until it is null terminated

    mov dword [LC1], edx

    push    LC1     ; Call printf with 2 arguments: pointer to str
    push    LC0     ; and pointer to format string.
    call    printf
    add     esp, 8      ; Clean up stack after call

    popa            ; Restore registers
    mov esp, ebp    ; Function exit code
    pop ebp
    ret

我正在使用英特尔处理器x86,NASM。

谢谢!

1 个答案:

答案 0 :(得分:0)

这个怎么样:

您没有初始化eax所以这会存储在哪里?

mov ebp, esp    ; Entry code - set up ebp and esp
pusha           ; Save registers

mov ecx, dword [ebp+8]  ; Get argument (pointer to string)
mov dword [eax], 0