2014-04-02 1 views
1

16 진수를 BCD로 변환 한 다음 인쇄하려고합니다. (결과를 EDX에 저장 한 다음 LC1에 전달합니다.) 사용자로부터 문자열을 받았는데 ECX 상태입니다. 이유를 말씀해 주시겠습니까? :Seg. Fault - ASSEMBLY (NASM)

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 

Intel proccessor x86, NASM을 사용하고 있습니다.

감사합니다. 당신은 eax를 초기화하지 않는

그래서 곳이 저장됩니까 :이 약

+0

디버깅을하셨습니까? – Devolus

답변

0

무엇입니까?

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 
+0

무엇을 의미합니까? init이 좋지 않은가요? – user3479031

+1

eax를 값으로 지정하지 않고 포인터로 사용하고 있기 때문에 이것이 segfault라고 확신 할 수 있습니다. – Devolus

관련 문제