user3083522
user3083522

Reputation: 211

Writing to running assembly program

I have a general question about assembly programming.

When writing a high level language, a compiler takes complex high level instructions and breaks them down into a bunch of machine level instructions in binary.

Assembly does not need this since you are writing in machine code.

But how do you actually get the code you wrote into binary? I mean you are obviously writing in a text editor. What do you use to put those instructions into a binary program?

Upvotes: 2

Views: 1186

Answers (1)

paxdiablo
paxdiablo

Reputation: 881373

Assembly does not need this since you are writing in machine code.

Actually, unless you're entering hex or binary digits, you're not programming in machine language. Assembly language is the symbolic representation (e.g., mov $1, %eax) of the underlying machine code (e.g., 53 32 01). There's usually a one-to-one mapping of assembly to machine code but not always.

In terms of how to convert your assembly language into machine code that the CPU can run, you use as assembler, which is basically just a "compiler" for assembly language.

For example, you can look into Microsoft's masm, the Netwide assembler nasm, GNU's own gas and many, many more.


By way of example, here's a transcript showing how to assemble a short program (potentially buggy, I haven't actually attempted to link and run it but that's irrelevant to the point I'm trying to get across here). First, let's show the assembly source code:

pax> cat testfile.nasm
section .text
         mov   ah, 09h        ; int 21/09 write string
         mov   dx, msg
         int   21h

         mov   ax, 4c00h      ; int 21/4c exit
         int   21h

section  .data
msg:     db   'Hello, world.'
         db   10, '$'

Then we'll assemble it with nasm and show you the resulting object file:

pax> nasm -f elf testfile.nasm
pax> objdump -ds testfile.o

testfile.o:     file format elf32-i386

Contents of section .text:
 0000 b40966ba 0000cd21 66b8004c cd21      ..f....!f..L.!
Contents of section .data:
 0000 48656c6c 6f2c2077 6f726c64 2e0a24    Hello, world..$

Disassembly of section .text:

00000000 <.text>:
   0:   b4 09                   mov    $0x9,%ah
   2:   66 ba 00 00             mov    $0x0,%dx
   6:   cd 21                   int    $0x21
   8:   66 b8 00 4c             mov    $0x4c00,%ax
   c:   cd 21                   int    $0x21

You can see that the assembly source file has been translated into the equivalent machine code. The objdump program has disassembled it for you back into assembly language for you but the actual machine code is simply binary information in the .text section:

b4 09 66 ba 00 00 cd 21 66 b8 00 4c cd 21

Upvotes: 6

Related Questions