You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于X86 AT&T语法的汇编程序多功能实现技术问询

Alright, let's build this x86 AT&T syntax assembly program step by step. I'll cover everything from data declarations to function implementations, and walk through the exact execution flow you need.

1. Data Section Setup

First, we'll declare our two long arrays, plus helper strings for printing (since we'll use printf from the C standard library). We'll also define a format string for printing long integers and a newline for readability.

.data
    array1: .quad 1, 2, 3, 4, 5   # First long array (5 elements)
    array2: .quad 10, 20, 30, 40, 50 # Second long array (matching size)
    fmt_ld: .string "%ld "        # Format string for long integer
    newline: .string "\n"         # Newline for line breaks
2. Function Implementations

We'll follow the System V AMD64 calling convention (standard for Linux/macOS x86-64):

  • First 6 arguments go in rdi, rsi, rdx, rcx, r8, r9
  • Non-volatile registers (rbx, rbp, r12-r15) must be preserved across function calls
  • Stack must be 16-byte aligned before calling external functions like printf

printQArray(int size, long *array1)

This function iterates through the array and prints each element.

.text
.global printQArray
printQArray:
    push %rbp               # Set up stack frame
    mov %rsp, %rbp
    push %rbx               # Preserve non-volatile rbx (we'll use it as a counter)

    mov %rdi, %rbx          # rbx = size (loop counter)
    mov %rsi, %rdi          # rdi = array pointer (first arg for printf)
    xor %rcx, %rcx          # rcx = index (starts at 0)

print_loop:
    cmp $0, %rbx
    jle print_end           # Exit loop if counter hits 0

    mov (%rdi, %rcx, 8), %rsi # rsi = array1[rcx] (second arg for printf)
    mov $fmt_ld, %rdi       # rdi = format string (first arg)
    xor %rax, %rax          # rax = 0 (no floating-point args for printf)
    call printf

    inc %rcx                # Increment index
    dec %rbx                # Decrement counter
    jmp print_loop

print_end:
    mov $newline, %rdi      # Print newline after array
    xor %rax, %rax
    call printf

    pop %rbx                # Restore preserved registers
    pop %rbp
    ret

invertArray(int size, long *array1)

This function reverses the array using a two-pointer approach: swap elements from the start and end, moving towards the center.

.global invertArray
invertArray:
    push %rbp
    mov %rsp, %rbp
    push %rbx               # Preserve rbx (we'll use it for end index)

    mov %rdi, %rbx          # rbx = size
    lea -1(%rbx), %rdx      # rdx = size - 1 (end index)
    xor %rcx, %rcx          # rcx = start index (0)

invert_loop:
    cmp %rcx, %rdx
    jle invert_end          # Exit if start >= end

    # Swap array1[rcx] and array1[rdx]
    mov (%rsi, %rcx, 8), %rax
    mov (%rsi, %rdx, 8), %r8
    mov %r8, (%rsi, %rcx, 8)
    mov %rax, (%rsi, %rdx, 8)

    inc %rcx                # Move start pointer right
    dec %rdx                # Move end pointer left
    jmp invert_loop

invert_end:
    pop %rbx
    pop %rbp
    ret

multQuad(int size, long *array1, long *array2)

This function calculates the product of corresponding elements in array1 and array2, then prints each product.

.global multQuad
multQuad:
    push %rbp
    mov %rsp, %rbp
    push %rbx               # Preserve rbx (loop counter)

    mov %rdi, %rbx          # rbx = size
    xor %rcx, %rcx          # rcx = index

mult_loop:
    cmp $0, %rbx
    jle mult_end

    # Load elements: rax = array1[rcx], r8 = array2[rcx]
    mov (%rsi, %rcx, 8), %rax
    mov (%rdx, %rcx, 8), %r8
    imul %r8, %rax          # rax = rax * r8 (product)

    # Print the product
    mov $fmt_ld, %rdi
    mov %rax, %rsi
    xor %rax, %rax
    call printf

    inc %rcx
    dec %rbx
    jmp mult_loop

mult_end:
    mov $newline, %rdi
    xor %rax, %rax
    call printf

    pop %rbx
    pop %rbp
    ret
3. Main Program Flow

The main function will execute your required steps in order:

  1. Print products of original array1 and array2
  2. Print original array1
  3. Reverse array1 and print it
  4. Print products of reversed array1 and array2
.global main
main:
    push %rbp
    mov %rsp, %rbp

    # Step 1: Print products of original array1 and array2
    mov $5, %rdi            # size = 5
    mov $array1, %rsi       # array1 pointer
    mov $array2, %rdx       # array2 pointer
    call multQuad

    # Step 2: Print original array1
    mov $5, %rdi
    mov $array1, %rsi
    call printQArray

    # Step 3: Reverse array1 and print it
    mov $5, %rdi
    mov $array1, %rsi
    call invertArray
    mov $5, %rdi
    mov $array1, %rsi
    call printQArray

    # Step 4: Print products of reversed array1 and array2
    mov $5, %rdi
    mov $array1, %rsi
    mov $array2, %rdx
    call multQuad

    # Exit program
    mov $0, %rdi
    call exit

    pop %rbp
    ret
4. Compilation & Execution

To compile and run this program (on Linux):

gcc -o array_ops array_ops.s
./array_ops

You should see output like:

10 40 90 160 250 
1 2 3 4 5 
5 4 3 2 1 
50 80 90 80 50 

内容的提问来源于stack exchange,提问作者Kyle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:57:47