x86 汇编中堆栈上的返回值

问题描述 投票:0回答:1

对于作业,我必须在 x86 ASM 中创建一个函数,它应该计算矩阵的行列式并将其结果通过堆栈返回到主函数。

我现在的问题是如何将结果推送到堆栈。

我知道返回值的标准是寄存器%rax,但是赋值声明使用堆栈。

所以总的来说,它看起来像这样:

    pushq a
    pushq b
    pushq c
    pushq d
    call determinante
    popq %r12

a、b、c、d 是矩阵的值,行列式现在应该将结果推送到 main 的堆栈帧中,并且使用 popq %r12,我们希望将其写入寄存器 r12。

并不是字面上将其推入堆栈,而是分配说:

determinant: # returns the result in the last parameter in stack

只要有一个关于如何在函数中将值压入堆栈的小技巧就会有很大帮助。

谢谢, 贾斯汀

assembly x86 stack x86-64
1个回答
0
投票

这里是一个 16 位模式的例子,来解释这个概念:

xor ax, ax
push ax       ; we'll gonna use this to return a value in the function
call my_func

pop ax        ; the returned value is at top of the stack (actually at the
              ; bottom of the stack because the stack grow down)
;...


my_func:
  pusha
  mov bp, sp
  
  ;... do something here ..
  
  mov [bp + 18], ax ; we are returning the value of ax
                    ; 18 because pusha pushes 8 registers + IP register pushed
                    ; pushed by default when calling a function
                    ; so the offset = 8*2 + 1*2
  popa
  ret

;.....
© www.soinside.com 2019 - 2024. All rights reserved.