uses a simple little trick to allow you to compile and run raw assembly on repl.it
/usr/bin/ld: /tmp/ccrqA7G3.o: relocation R_X86_64_32 against
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
/bin/sh: 1: ./run: not found
@JameezPlays I haven't been able to figure out why yet, but it seems that the linker is defaulting to 32bit mode. Assembling the code into an object file and then linking seems to work though.
ld hello.o -o hello -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lc --format elf64-x86-64
The program as a whole still doesn't work though, so getting it to link seems like a bit of a pointless achievement, since it will still segfault on execution.
It looks like he wrote this program in C and then compiled it to assembly, so I cleaned up the syntax into a more readable GNU assembler syntax that will actually print "hello world".
.global main .text main: pushq %rbp movq %rsp, %rbp movl $.LC0, %edi movl $0, %eax call printf movl $0, %eax popq %rbp ret ;leave ;xorl %edi, %edi ;call exit .LC0: .string "hello world\n"
I'm assembling and linking it like this:
#!/usr/bin/bash # # Assemble the program. as -o hello.o hello.s # Link the program using the 64-bit dynamic interpreter. ld hello.o -o hello -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lc --format elf64-x86-64 # Execute the resulting binary. ./hello
Unfortunately, while this code will print the message, it too segfaults at the end, and I'm not sure why yet.
Still, this example is great for demonstrating how to use the
popen function to be able to write assembly language in Repls, and the best part is that Repl.it seems to include NASM by default, so we can "solve" the problem by just cutting the Gordian knot and not bothering with GNU assembler syntax lol.