0

I am trying to compile my assembly code

global main
extern printf

section .data
    msg db "Testing %i...", 0x0a, 0x00

section .text
main:
    push ebp
    mov ebp, esp
    push 123
    push msg
    call printf
    mov eax, 0
    mov esp, ebp
    pop ebp
    ret

using these commands:

$ nasm -f elf32 ex10.asm
$ gcc -m32 ex10.o ex10

but it gives me this error

/usr/bin/ld: cannot find Scrt1.o: No such file or directory
/usr/bin/ld: cannot find crti.o: No such file or directory
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/11/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/11/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc
collect2: error: ld returned 1 exit status

Normal .c files work with gcc, but this one doesn't.

What should I do to fix this?

  • Did you install multilib packages for your distro? The should contain the necessary 32-bit CRT, libgcc, and glibc files. – Peter Cordes Mar 28 '22 at 09:58
  • @PeterCordes I'm new to linux, but I use linux kali. I think these tools are preinstalled. But in the case they are not, can you give me a command to install them? Thanks! –  Mar 28 '22 at 10:33
  • I think Peter Cordes' comment and your reply is a separate question by its own, which is not related to assembly. Not sure if stackoverflow or unix stackexchange is appropriate. – xiver77 Mar 28 '22 at 10:48
  • Your code is correct, but you don't need `push ebp; mov ebp, esp` unless you want to backtrace the stack in runtime. Such cases are rare. Also, each Linux distro has different package managers, so your question is really specific to Kali Linux. – xiver77 Mar 28 '22 at 10:53
  • @xiver77 I just want to fix the error. I don't know why gcc throws that error. I just need anythink that will fix my error. The `push ebp; mov ebp, esp` and also the function epilog are thinks i wanna have in every function, because it is a good practice. –  Mar 28 '22 at 11:41
  • Peter explained the reason. You didn't install libc and libgcc. How to install it for your distro is a separate question not related to assembly. – xiver77 Mar 28 '22 at 11:44
  • There is no reason to `push ebp; mov ebp, esp` for every function unless you really need it (for runtime stack backtracing). Who says that is a good practice? When you have to write something directly in assembly, it's when you want to extract the maximum speed from the machine or when you want to keep your code very small. Writing that function epilogue achieves neither. Also, compilers don't do that by default with any level of optimization on. – xiver77 Mar 28 '22 at 11:47
  • Bros, I just need something to do with this, it's good that I must install libc and libgcc but i don't know if i already don't have it with kali and HOW to install it –  Mar 28 '22 at 12:17
  • Always use `@username`. Took me 5 seconds to find this link googling "kali linux 32 bit gcc". https://stackoverflow.com/q/31141794/17665807 – xiver77 Mar 31 '22 at 00:12

0 Answers0