Goglides Dev 🌱

Goglides Dev 🌱 is a community of amazing users

Shaping the future of IT, one connection at a time.

Create account Log in
Cover image for Containerize your assembly language code
Balkrishna Pandey
Balkrishna Pandey

Posted on

Containerize your assembly language code

Not too long ago, I wrote a blog post on how to make a basic "Hello world" application in 8086 assembly language. It would be amazing if I could containerize the code so that it's easier for me and others to access it without worrying about downloading all of the proper dependencies each time. To that end, I decided to look into using Docker. So here is the Dockerfile I used to containerize the same application.

Note: Copy of the code from above blog post and save it as hello.asm

# Use an existing base image as the starting point
FROM ubuntu:20.04 AS build

# Update the package list
RUN apt-get update

# Install NASM and LD
RUN apt-get install -y nasm binutils

# Copy the assembly code into the container
COPY hello.asm /app/

# Assemble and link the code
RUN nasm -f elf -o /app/hello.o /app/hello.asm
RUN ld -m elf_i386 -s -o /app/hello /app/hello.o

# Use a minimal image as the final image
FROM alpine:latest

# Copy the executable from the build stage
COPY --from=build /app/hello /app/

# Run the executable
CMD ["/app/hello"]
Enter fullscreen mode Exit fullscreen mode

Here's an explanation of each step in the Dockerfile:

  • FROM ubuntu:20.04 AS build: This line specifies the base image to use for the build stage. In this case, it's using the latest version of Ubuntu 20.04.
  • RUN apt-get update: This command updates the package list on the base image.
  • RUN apt-get install -y nasm binutils: This command installs NASM and ld on the base image.
  • COPY hello.asm /app/: This command copies the assembly code (hello.asm) from the local machine into the container at the location /app/.
  • RUN nasm -f elf -o /app/hello.o /app/hello.asm: This command assembles the code into object file
  • RUN ld -m elf_i386 -s -o /app/hello /app/hello.o: This command links the object file to create an executable. Here we use the flag -m elf_i386 to specify the target architecture to the linker. This flag tells the linker to link the object file as if it's for 32-bit x86 architecture which is the architecture for 8086 processors.
  • FROM alpine:latest: This line specifies the minimal image to use for the final image.
  • COPY --from=build /app/hello /app/: This command copies the executable from the build stage to the final image.
  • CMD ["/app/hello"]: This command sets the default command to run when the container is started. In this case, it runs the executable that was copied over from the build stage.

Now you can build the code by simply running following docker command.

docker build . -t assembly
Enter fullscreen mode Exit fullscreen mode

To run the application, you can use the docker run command. For example,

docker run assembly  
Enter fullscreen mode Exit fullscreen mode

You should see output similar to this.

Hello, World!
Enter fullscreen mode Exit fullscreen mode

Discussion (0)