0

I'm trying to convert a C code to Assembly but I have a problem in the Assembly code. In the C code i'm importing the 'conio.h' library to use the getch() function, but in the Assembly code when I have the call of this function the output says error because the getch() function is undefined. I've already tried use the extern command in the asm code but it didnt work. Any ideias how to solve this?

C code for reference:

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

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

Assembly code for reference:

.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   ; Call of the getch function
        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"
  • 1
    Which assembler are you using? `` is a Windows/DOS library – Govind Parmar Mar 17 '21 at 13:45
  • Actually, I'm using a site name Try It Online (https://tio.run/) to run Assembly codes. I'm using the GCC x64 Linux Compiler. – Pedro Victor Fonseca Mar 17 '21 at 15:40
  • `.extern getch` should work if the function is provided by whatever library you are linking. If it doesn't then the problem is with your libraries or the way you are linking them, not in your code. How do you know that tio.run supports this function in its standard library? Most Linux systems don't, AFAIK. – Nate Eldredge Mar 17 '21 at 15:45
  • By the way, `` is a header file, separate from the library which contains the actual code of the function. You may want to read https://stackoverflow.com/questions/924485/whats-the-difference-between-a-header-file-and-a-library/924495#924495 to better understand the distinction. – Nate Eldredge Mar 17 '21 at 15:49
  • I understand. So I think the best solution is use a proper compiler wich the library is defined. – Pedro Victor Fonseca Mar 17 '21 at 16:25
  • Or, maybe use a function which the standard library supports. `getchar` should suffice for your purposes, and then you can forget about `` altogether. Note though that it returns `EOF` (usually `-1`) on end of file instead of `\0`, and that you'll have to check all of `eax` (not just `al`) in order to distinguish this from character `0xff`. – Nate Eldredge Mar 17 '21 at 16:52
  • 1
    I think tio.run links your asm program against libc (and it's Linux, so this is glibc). You're not going to find weird DOS stuff like conio.h functions. Input is redirected anyway, it's not like there's any use for ioctl / tty stuff to read a character without a newline (TTY raw mode) – Peter Cordes Mar 17 '21 at 17:06
  • Note that Linux does not support the `getch` function. That is a DOS/Windows only function. Find a different solution. – fuz Mar 17 '21 at 17:58

0 Answers0