Segmentation Fault in Assembly

Asked

Viewed 192 times

2

I’m new to Assembly and I’m having a little problem here in a college problem.

    section .data
        SYS_EXIT_PROGRAM  equ 1
        SYS_STANDARD_OUTPUT equ 1
        SYS_STANDARD_INPUT equ 2
        SYS_READ_FILE  equ 3
        SYS_WRITE_FILE equ 4
        SYSTEM_CALL equ 0x80

        ;textos de exibicao para entrada do programa
        TextoPedeCarta: db 'Informe a Carta: ',0
        tamanhoTextoPedeCarta: equ $ - TextoPedeCarta 
        TextoPedeNaipe: db 'Informe o Naipe: ',0
        tamanhoTextoPedeNaipe: equ $ - TextoPedeNaipe 

        ;textos de exibicao para erros de entrada
        TextoErroCarta: db 'Carta digitada inválida. [Digite uma carta entre 2,3,4,5,6,7,Q,J,K e A] ',13,10,0
        tamanhoTextoErroCarta: equ $ - TextoErroCarta
        TextoErroNaipe: db 'Naipe digitado inválido. [Digite P para Paus, C para Copas, E para Espadas e O para Ouros] ',13,10,0
        tamanhoTextoErroNaipe: equ $ - TextoErroNaipe

        QuebraLinha: db 13,10,0
        tamanhoQuebraLinha: equ $ - QuebraLinha

        arrayCartas db '2','3','4','5','6','7','Q','J','K','A'
        arrayNaipes db 'P','C','E','O'

    section .bss
        cartaDigitada: resb 21
        tamanhoCartaDigitada equ $ - cartaDigitada
        naipeDigitado: resb 21
        tamanhoNaipeDigitado: equ $ - naipeDigitado

        CARTA: resb 4
        NAIPE: resb 4

    section .text
        global _start

    ;Esta rotina pede uma carta e verifica se 
    ; o valor é válido
    ; Entradas: 
    ; ECX - texto complementar da carta
    Pede1Carta:

        mov eax,SYS_WRITE_FILE
        mov ebx,SYS_STANDARD_OUTPUT
        mov ecx,TextoPedeCarta
        mov edx,tamanhoTextoPedeCarta
        int SYSTEM_CALL

        mov eax,SYS_WRITE_FILE
        mov ebx,SYS_STANDARD_OUTPUT
        pop edx
        pop ecx      
        ; precisa guardar os valores para
        ; caso ocorra erro de digitação
        push ecx
        push edx
        int SYSTEM_CALL

        mov eax,SYS_READ_FILE
        mov ebx,SYS_STANDARD_INPUT
        mov ecx,cartaDigitada
        mov edx,tamanhoCartaDigitada
        int SYSTEM_CALL
        call VerificaCarta

    Pede1Naipe:
        push edi
        push ecx
        push edx


        mov eax,SYS_WRITE_FILE
        mov ebx,SYS_STANDARD_OUTPUT
        mov ecx,TextoPedeNaipe
        mov edx,tamanhoTextoPedeNaipe
        int SYSTEM_CALL

        mov eax,SYS_WRITE_FILE
        mov ebx,SYS_STANDARD_OUTPUT
        pop edx
        pop ecx      
        ; precisa guardar os valores para
        ; caso ocorra erro de digitação
        push ecx
        push edx
        int SYSTEM_CALL

        mov eax,SYS_READ_FILE
        mov ebx,SYS_STANDARD_INPUT
        mov ecx,naipeDigitado
        mov edx,tamanhoNaipeDigitado
        int SYSTEM_CALL


    VerificaCarta:
        mov eax,10
        mov ecx, arrayCartas
        call loop

    loop:
        mov ebx,[cartaDigitada]
        cmp [ebx],ecx
        add ecx,1
        jz Pede1Carta
        dec eax
        jnz loop

    CartaIncorreta:
      mov eax,SYS_WRITE_FILE
      mov ebx,SYS_STANDARD_OUTPUT
      mov ecx,TextoErroCarta
      mov edx,tamanhoTextoErroCarta
      int SYSTEM_CALL

      jmp Pede1Carta

    CartaCorreta:
        mov eax,[cartaDigitada]
        mov [CARTA],eax
        ret

    _start:
      call Pede1Carta
      call Pede1Naipe

      mov eax,SYS_EXIT_PROGRAM
      mov ebx,0
      int SYSTEM_CALL

The program is basically a truco tutor. I am at first still trying to validate the letters. After entering any letter, I have a Segmentation fault. Can anyone help me why? Is some error in the decay of the vector?

  • Someone can give me a light? :(

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.