How to import a C library to a code in Assembly

Asked

Viewed 36 times

0

I am trying to convert to Assembly a C code that uses the function getch() which is imported from the conio.h. library The problem is that when I try to run the Assembly program there is an error that the getch() function is undefined. There is how I import this function into my Assembly code?

Code in c:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main()
{
    int contador=0;
    char *string, aux;
    string = (char*)malloc(sizeof(char)*20);
    do{
        string[contador] = getch();
        contador++;
    }while (string[contador] != '\0');
    printf("%s", string);
    return 0;
}

Assembly code:

.intel_syntax
.global main
.text

main:
        push    %rbp
        mov     %rbp, %rsp
        push    %rbx
        sub     %rsp, 40
        mov     DWORD PTR [%rbp-28], 0
        mov     %edi, 20
        call    malloc
        mov     QWORD PTR [%rbp-24], %rax
.L2:
        mov     %eax, DWORD PTR [%rbp-28]
        cdqe
        mov     %rbx, %rax
        add     %rbx, QWORD PTR [%rbp-24]
        mov     %eax, 0
        call    getch
        mov     BYTE PTR [%rbx], %al
        inc     DWORD PTR [%rbp-28]
        mov     %eax, DWORD PTR [%rbp-28]
        cdqe
        add     %rax, QWORD PTR [%rbp-24]
        movzx   %eax, BYTE PTR [%rax]
        test    %al, %al
        jne     .L2
        mov     %rsi, QWORD PTR [%rbp-24]
        mov     %edi, OFFSET FLAT:.LC0
        mov     %eax, 0
        call    printf
        mov     %eax, 0
        add     %rsp, 40
        pop     %rbx
        leave
        ret

.LC0:
        .string "%s"
No answers

Browser other questions tagged

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