Writing x86 Assembly Using repl.it
Whether it be for a computer systems class or just for fun, programming in assembly can be a powerful tool in understanding C programs and computer architecture in general. Although
repl.it doesn't have support for x86 assembly just yet, I think I found a pretty easy workaround that allows you to write x86 assembly source code within a C
repl. For me, using
repl.it as an online IDE is a huge advantage as a macOS user that finds SSHing all the time cumbersome and VMs too laggy.
I'll be using x86 64-bit assembly with AT&T syntax for the example
How It Works
Basically, the way this method works is that I think C
repls already have all of the necessary C-associated tools already "preloaded" and available, so we can use standard bash commands and things like
gdb among others that would typically be found on a normal Linux computer. We can therefore simply use terminal commands to compile and run assembly code.
To start, simply create a new
repl in C and press the
Add file button to create a new file
fibonacci.s. As an example, I programmed a function to calculate the
nth Fibonacci number, but you can change the name of the
.s file to whatever you'd like. Structure the
.s source code as you normally would :
.globl main main: # Write code here! .data
To convert your assembly source code in your
.s file into an executable, don't press the
Run button at the top of the
repl! Instead, in the terminal associated with the
repl, run the following bash command:
> gcc fibonacci.s -o fibonacci
Of course, you can change
fibonacci with whatever the name of your program is. This compiles as a 64-bit executable using
gcc. (For 32-bit executable, use the
ld bash commands to assemble and link, and add the
-m elf_i386 flag to the
ld command. Thanks Stack Overflow).
Running the Program
Run the program using the bash command
If you happen to have command line arguments too, you can add them in a format like
> ./fibonacci arg1 arg2 arg3
Getting the Return Value
To find out what your assembly code returned, the easiest way (for integer return values) is to use this single-line bash command after running the executable:
> echo $?
So far to my knowledge, the only real limitation for me is that there's (as of 09/2020) no support for syntax highlighting assembly code that you'd normally see using
vim or some other editor, since assembly isn't technically a language that's supported officially yet by
repl.it. That being said, this tool has been extremely helpful for me and hopefully for you too!