assembly x86汇编中堆栈上的返回值

9avjhtql  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(90)

对于一个赋值,我必须在x86 ASM中创建一个函数,该函数应该计算矩阵的行列式,并将其结果通过堆栈返回给main函数。
我现在的问题是如何将结果推到堆栈中。
我知道返回值的标准是寄存器%rax,但赋值语句声明使用堆栈。
所以大体上看起来是这样的:

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

字符串
其中a,B,c,d是矩阵的值,determinant现在应该将结果推送到main的堆栈框架中,并且使用popq %r12,我们希望将其写入寄存器r12。
不是字面上把它推到堆栈,而是Assignment说:

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


关于如何在函数中将值推送到堆栈的一个小技巧将有很大帮助。
谢了,贾斯汀

ldfqzlk8

ldfqzlk81#

下面是一个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

;.....

字符串

相关问题