Wednesday, January 27, 2010

Two steps to using Assembly in Linux (ubuntu 9.10)

 ⚠️ Update (2026)

This article was originally written several years ago and uses older 32-bit Linux assembly methods.

While the concepts are still useful for learning, modern Linux systems now use 64-bit architecture by default, and some commands shown below may no longer work as expected.

 ✅ Recommended Modern Approach

* Use **64-bit assembly (`elf64`)**
* Use **`syscall` instead of `int 0x80`**I
* Use registers like `rax`, `rdi`, `rsi`, `rdx`

🔧 Quick Working Example (64-bit)

```
section .data
    msg db "Hello, world!", 10
    len equ $ - msg

section .text
    global _start

_start:
    mov rax, 1
    mov rdi, 1
    mov rsi, msg
    mov rdx, len
    syscall

    mov rax, 60
    xor rdi, rdi
    syscall
```

🛠 Compile & Run

```
nasm -f elf64 hello.asm -o hello.o
ld hello.o -o hello
./hello
```

---

📌 Note

The original content below is kept unchanged for reference and for those learning 32-bit assembly, but for modern systems, the updated method above should be used.




Most programmers shy away from assembly language. The reason is not that they don't have a solid background of assembly but that they don't know how to write code outside the simulators etc that they are familiar. Especially when it comes using assembly under linux.

It comprises two steps.

1. Compiling with assembler

The assembly language compiler in linux is as. The code to compile a program say helloWorld.s is the following
$ as helloWorld.s -o helloWorld.o

2. Linking and Executing the code

The output of assembling is object code which needs linking. The code for linking the object file "helloWorld.o".
$ ld -helloWorld.o -o  helloWorld.out
To execute the code we need to do the following.
$ ./helloWorld.out

The code for helloWorld.s is attached here and thanks to reference 1.

.text                                  
                           .global _start
_start:



        movl    $len,%edx       # third argument: message length
        movl    $msg,%ecx       # second argument: pointer to message to write
        movl    $1,%ebx         # first argument: file handle (stdout)
        movl    $4,%eax         # system call number (sys_write)
        int     $0x80           # call kernel

# and exit

        movl    $0,%ebx         # first argument: exit code
        movl    $1,%eax         # system call number (sys_exit)
        int     $0x80           # call kernel

.data                                   # section declaration

msg:
        .ascii  "Hello, world!\n"       # our dear string
        len = . - msg                   # length of our dear string

References :
1. http://asm.sourceforge.net/howto/hello.html

3 comments:

SpiceyCurry said...

Great post, I took your example and made a simple program to read a sudo language and compile it to an executable... Check it out http://spiceycurry.blogspot.com/2010/05/simple-compilable-programming-language.html

also, there is a typo in your post, there is a '-' on the ld line tht should not be there.

drprachya said...

Mine (ubuntu 10.04) has to link like this to work.

ld -o helloWorld.out helloWorld.o

--Thanks

Anonymous said...

lies beyond lies omni get a job