Implementing a for loop in AT&T syntax assembly (typically for x86-64 Linux) involves managing a counter, a condition check, and an increment/decrement step, along with conditional and unconditional jumps.
Let's consider a C-style for loop:
c
for (int i = 0; i < 5; i++) {
// Loop body: e.g., print the value of i
}
Here's how it translates to AT&T assembly:
Initialization: Set the counter variable (i) to its initial value.
Condition Check: Compare the counter with the limit. If the condition is no longer met, jump out of the loop.
Loop Body: Execute the code inside the loop.
Increment/Decrement: Update the counter variable.
Unconditional Jump: Jump back to the condition check to repeat the process.
Example: A for loop to print numbers from 0 to 4
This example will create a simple executable that uses a for loop to print "i = X" for X from 0 to 4.
for_loop.s:
assembly
.section .data
.fmt_str_i: .string "i = %d\n" # Format string for printf
.section .text
.globl _start # Standard entry point for Linux executables (no C runtime)
_start:
# ------------------------------------
# FOR LOOP: for (int i = 0; i < 5; i++)
# ------------------------------------
# Register conventions (common for loops/counters):
# %rcx: Loop counter 'i'
# %r8: Loop limit 'n' (e.g., 5)
mov $0, %rcx # Initialization: i = 0 (move 0 to rcx)
mov $5, %r8 # Limit: n = 5 (loop while i < 5)
loop_start:
cmp %r8, %rcx # Condition check: Compare i with n (computes rcx - r8)
# If rcx >= r8 (i >= 5), jump to loop_end
jge loop_end # JGE (Jump if Greater or Equal)
# --- LOOP BODY START ---
# Print "i = %d\n" where %d is the current value of i (%rcx)
# Using printf (requires linking with C standard library)
# ABI for printf:
# %rdi: format string address
# %rsi: first argument
# %rdx: second argument, etc.
lea .fmt_str_i(%rip), %rdi # Load effective address of format string into rdi
mov %rcx, %rsi # Move current 'i' value (from rcx) to rsi for printf argument
xor %rax, %rax # Clear %rax (printf expects number of XMM registers used in %rax, 0 here)
call printf@PLT # Call printf (PLT for Position-Independent Code)
# --- LOOP BODY END ---
inc %rcx # Increment: i++ (increment rcx by 1)
jmp loop_start # Unconditional jump back to condition check
loop_end:
# ------------------------------------
# END OF FOR LOOP
# ------------------------------------
# Exit the program (syscall)
# mov $60, %rax # syscall number for exit (sys_exit)
# mov $0, %rdi # exit code 0
# syscall
# Or, if compiled with GCC and linked with glibc,
# simply return from _start (acting like main)
xor %rax, %rax # Set return value to 0
ret # Return from _start
How to Compile and Run:
Save the code: Save the assembly code above as for_loop.s.
Assemble and Link: You need to use gcc to assemble and link it with the C standard library (for printf).
bash
gcc -o for_loop for_loop.s
Execute:
bash
./for_loop
Expected Output:
code
i = 0
i = 1
i = 2
i = 3
i = 4
Explanation of AT&T Syntax and Instructions:
.section .data: Declares a section for initialized data (like strings).
.section .text: Declares a section for executable code.
.globl _start: Makes the _start label globally visible so the linker can find it as the program's entry point.
mov $value, %reg: Move an immediate value into a register reg.
$: Denotes an immediate value.
%: Denotes a register.
mov $0, %rcx: rcx = 0;
cmp %src, %dest: Compares dest with src by computing dest - src and setting condition flags.
cmp %r8, %rcx: Compares rcx with r8. The flags are set based on rcx - r8.
jge label: Jump if Greater or Equal. This is a conditional jump. If the flags set by cmp indicate that rcx >= r8, the program jumps to label. This implements the i >= 5 part of the i < 5 condition.
inc %reg: Increment the value in reg by 1.
inc %rcx: rcx++
jmp label: Unconditional jump to label. This always executes.
lea .fmt_str_i(%rip), %rdi: Load Effective Address. This instruction calculates the address of .fmt_str_i relative to the Instruction Pointer (%rip) and puts that address into %rdi. This is crucial for Position-Independent Code (PIC).
xor %rax, %rax: Sets the rax register to 0. This is a common idiom for mov $0, %rax because xor is often faster. For printf, rax must contain the number of vector registers used (which is 0 for standard integers/pointers).
call printf@PLT: Calls the printf function. @PLT (Procedure Linkage Table) is used for dynamically linked libraries to ensure the correct function address is resolved at runtime.
ret: Returns from the current function (in this case, from ). If acts like and is called by the C runtime, will return control to the runtime.
This structure allows you to build any for loop logic by changing the initialization, condition, increment/decrement, and the code within the loop body.