Skip to content
← Back to Community
Assembly compiler
Profile icon
h
has Hacker Plan
paulfears

uses a simple little trick to allow you to compile and run raw assembly on repl.it

Voters
Profile icon
jflopezfernandez
Profile icon
AdawareEssemaal
Profile icon
Vandesm14
Profile icon
_Nobody_
Profile icon
amasad
Profile icon
timmy_i_chen
Profile icon
paulfears
Comments
hotnewtop
Profile icon
JameezPlays

/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

Profile icon
jflopezfernandez

@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.

Profile icon
paulfears

@jflopezfernandez this repl.it is from a different time, little is known about it construction or purpose

Profile icon
JameezPlays

@jflopezfernandez Your code works, but can't you just press run instead of assembling and executing the binary?

Profile icon
jflopezfernandez

@paulfears Completely understandable. Attempting to troubleshoot it made for a fun morning project though haha

Profile icon
jflopezfernandez

@JameezPlays You could, yea. I got caught up more in the details of just trying to get it to work, but you're absolutely right; you could just set it up to just run through the repl configuration file.