Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

364
Visualizações
Why does my string-print function print extra garbage / file contents after the C string I pass it?

I decided to make a function that prints a string in the EAX register to the console, works from a part by displaying a message first, then something

Code (NASM) :

newLine equ 0xa

section .data
    msg db "Hello world!", newLine, 0

section .text
    global _start

_start:
    mov eax, msg
    call printf
    call exit

printf:
    push eax
    push ebx
    push ecx 
    push edx 

    mov ecx, eax
    call lenFind
    mov edx, eax
    mov eax, 4
    mov ebx, 1
    int 0x80

    pop edx
    pop ecx
    pop ebx
    pop eax 
    ret

lenFind:
    push edx
    mov edx, 0
    .find:
        cmp [eax+edx], byte 0
        je .close
        inc edx
        jmp .find
    .close:
        pop edx
        ret

exit:
    mov eax, 1
    mov ebx, 0
    int 80h 

Output:

Hello world!
.shstrtab.text.data
                   �J�  

idk what it could be, but most likely the problem is that I'm using 32 bit registers

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Your lenFind does not return any result!
The length is calculated in the EDX register but you have restored that from the stack. However the calling program seems to expect the length to be in the EAX register:

lenFind:
    push edx
    mov edx, 0
    .find:
        cmp [eax+edx], byte 0
        je .close
        inc edx
        jmp .find
    .close:
        MOV EAX, EDX   <<< Add this line
        pop edx
        ret
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda