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

YASM汇编二维两点距离计算程序编译报错求助

Hey there! Let's break down those YASM errors you're seeing and fix your code step by step.

What's causing the errors?

The error messages label or instruction expected at start of line happen because YASM (like NASM) requires every directive, label, variable definition, or instruction to be on its own line. Your original code mashed the segment directive and variable definitions onto the same line, which the assembler can't parse correctly.

Fixed Code Structure

First, let's correct the .data segment formatting, then add a complete working example for calculating the 2D distance (using the Pythagorean theorem: distance = sqrt((Bx-Ax)² + (By-Ay)²)):

; 2D Point Distance Calculator - YASM x86_64
segment .data
    ; Define coordinates for points A and B (using double-precision floats)
    Ax dq 5.0        ; X coordinate of point A
    Ay dq 3.0        ; Y coordinate of point A
    Bx dq 1.0        ; X coordinate of point B
    By dq 6.0        ; Y coordinate of point B
    ; Format string for printing the result
    print_fmt db "Distance between A and B: %.2lf", 10, 0

segment .text
    extern printf   ; Use C standard library's printf function
    global main     ; Entry point for linker

main:
    ; Step 1: Calculate (Bx - Ax) and square it
    movsd xmm0, [Bx]    ; Load Bx into xmm0
    subsd xmm0, [Ax]    ; xmm0 = Bx - Ax
    mulsd xmm0, xmm0    ; xmm0 = (Bx - Ax)²

    ; Step 2: Calculate (By - Ay) and square it
    movsd xmm1, [By]    ; Load By into xmm1
    subsd xmm1, [Ay]    ; xmm1 = By - Ay
    mulsd xmm1, xmm1    ; xmm1 = (By - Ay)²

    ; Step 3: Sum the squared differences
    addsd xmm0, xmm1    ; xmm0 = (Bx-Ax)² + (By-Ay)²

    ; Step 4: Take square root to get distance
    sqrtsd xmm0, xmm0   ; xmm0 = sqrt(sum of squares)

    ; Step 5: Print the result with printf
    mov rdi, print_fmt  ; First argument: format string
    mov rax, 1          ; Number of float arguments in xmm registers
    call printf         ; Call printf

    ; Exit the program cleanly
    mov rax, 60         ; syscall number for exit
    xor rdi, rdi        ; Exit code 0 (success)
    syscall

segment .bss
    distance resq 1     ; Reserve 8 bytes to store the result (optional)

How to Compile & Run

  1. Assemble with your original command:
    yasm -f elf64 -g dwarf2 -l distance.lst distance.asm
    
  2. Link with GCC (since we use printf from the C standard library):
    gcc -o distance distance.o
    
  3. Run the executable:
    ./distance
    

Key Takeaways for YASM Syntax

  • Always put directives like segment .data, variable definitions, and instructions on separate lines.
  • Indentation isn't required, but it makes your code way easier to read (especially as a beginner!).
  • Use comments to explain what each part does—this will save you tons of time later when debugging.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:10:03