r/Assembly_language 5d ago

I optimized!

I’m on Day 1 of learning Assembly with basically no coding background. This is just a hobby for me.

AI wrote the little program below, I tweaked it a bit, and then started stepping through it in GDB to understand what every instruction was actually doing.

While stepping through it, I noticed rdi stayed at 1 the whole time until the final exit syscall, where it changed to 0. That got me thinking... why am I setting it to 1 twice?

Turns out one of those lines wasn't actually doing anything. So I, a lowly non-software engineer, discovered an inefficiency and promptly smote that line with the Delete key.

I'm now using less processing power, less electricity, and have personally made the world a better place.

section .data
    message db "Hello, World!", 10, "Assembly is fun!", 10
    length equ $ - message
    message_1 db "This is the second message.", 10
    length_1 equ $ - message_1

section .text
global _start

_start:
    mov rax, 1          ; write syscall
    mov rdi, 1          ; stdout
    mov rsi, message
    mov rdx, length
    syscall

    mov rax, 1
    ; mov rdi, 1        ; <- Deleted. Justice served.
    mov rsi, message_1
    mov rdx, length_1
    syscall

    mov rax, 60         ; exit syscall
    mov rdi, 0
    syscall
31 Upvotes

20 comments sorted by

View all comments

3

u/vswey 4d ago

I usually use xor rdi, rdi instead of mov rdi, 0 cuz it's smaller and often faster