NASM组装-为什么调用会产生与jmp不同的结果?

时间:2018-11-21 09:17:21

标签: assembly nasm x86-64

我试图确定为什么使用call printMemory vs jmp printMemory时会得到两个不同的结果。在这种情况下,似乎它们应该产生相同的结果,因为调用将返回到与jmp完成相同的位置。

section .data
    a: dq 3.0
    b: dq 4.0
    c: dq 0.0
    result: dq 0.0
    printMsg: db "a(%f)^2 + b(%f)^2 = c^2 (%f)",10,0
    printMsg2: db "c = %f",10,0
    printMsg3: db "%f",10,0

section .text
    extern printf
    extern sqrt
    global main


main:
    push rbp    
    mov rbp, rsp
    finit
    fld qword [a]
    fmul qword [a]
    fstp qword [result]
    fld qword [b]
    fmul qword [b]
    fadd qword [result]
    fstp qword [result]
    mov rdi, printMsg
    movq xmm0, qword [a]
    movq xmm1, qword [b]
    movq xmm2, qword [result]
    mov rax, 3
    call printf
    movq xmm0, qword [result]
    call sqrt
    mov rdi, printMsg2
    mov rax, 1
    call printf
    call printMemory
    mov rsp, rbp
    pop rbp
    mov rax, 60
    xor rdi, rdi
    syscall 


printMemory:
    mov rdi, printMsg3
    movq xmm0, qword [a]
    mov rax, 1
    call printf
    ret

这会导致细分错误

但这会输出预期的结果:

section .data
    a: dq 3.0
    b: dq 4.0
    c: dq 0.0
    result: dq 0.0
    printMsg: db "a(%f)^2 + b(%f)^2 = c^2 (%f)",10,0
    printMsg2: db "c = %f",10,0
    printMsg3: db "%f",10,0

section .text
    extern printf
    extern sqrt
    global main


main:
    push rbp    
    mov rbp, rsp
    finit
    fld qword [a]
    fmul qword [a]
    fstp qword [result]
    fld qword [b]
    fmul qword [b]
    fadd qword [result]
    fstp qword [result]
    mov rdi, printMsg
    movq xmm0, qword [a]
    movq xmm1, qword [b]
    movq xmm2, qword [result]
    mov rax, 3
    call printf
    movq xmm0, qword [result]
    call sqrt
    mov rdi, printMsg2
    mov rax, 1
    call printf
    jmp printMemory
finish:
    mov rsp, rbp
    pop rbp
    mov rax, 60
    xor rdi, rdi
    syscall 


printMemory:
    mov rdi, printMsg3
    movq xmm0, qword [a]
    mov rax, 1
    call printf
    jmp finish

0 个答案:

没有答案