获取过程参数从堆栈 [英] Getting Procedure Arguments from Stack
问题描述
我想学习如何调用汇编语言程序。下面是一个简单的例子,显示我的问题。我推 7
对栈,调用程序;当procudeure从堆栈中弹出,这个值是不是我推的人。是否有人可以帮助我明白发生了什么,什么我可以做,使这项工作?
PUSH 7
CALL FOOBAR
MOV AH,4CH
INT 21HFOOBAR PROC
POP AX;没有7
RET
FOOBAR ENDP
的呼叫
指令将返回地址在堆栈上,所以当你流行斧
在你的程序,它不会让你7推,但返回地址。在 RET
将无法正常工作,无论是(它希望找到返回地址有!)试着这么做...
FOOBAR PROC
推基点;保存调用者的章
MOV BP,SP
MOV AX,[BP + 4]
;用它做什么
;离开 - 相当于:
MOV SP,BP
流行基点RET
有一个可能的疑难杂症在这里。 A远的过程既有段(CS)和堆栈上的偏移,所以4轮空,返回地址,并为推BP
两个字节将第一个参数在 [BP + 6]
。我猜只是 PROC
默认为附近
PROC - 你可能想要说的是,仅仅是为了清楚。如果你需要一个 PROC远
它可能是时候升级到32位code(或64位)。 16位code是这样的皮塔饼 - 我们真的很高兴算了吧! :)
I am trying to learn how to call procedures in assembly language. Below is a simple example that shows my problem. I push 7
unto the stack, call the procedure; when the procudeure pops from the stack, the value is not the one I pushed. Can someone please help me understand what is happening and what I can do to make this work?
PUSH 7
CALL FOOBAR
MOV AH, 4CH
INT 21H
FOOBAR PROC
POP AX ; Not 7
RET
FOOBAR ENDP
The call
instruction puts the return address on the stack, so when you pop ax
in your procedure, it doesn't get the 7 you pushed, but the return address. The ret
won't work, either (it expects to find the return address there!) try something like...
FOOBAR proc
push bp ; save caller's reg
mov bp, sp
mov ax, [bp + 4]
; do something with it
; leave - equivalent to:
mov sp, bp
pop bp
ret
There's a possible "gotcha" here. A "far" procedure has both the segment (cs) and offset on the stack, so 4 byes for the return address, and two bytes for the push bp
puts the first parameter at [bp + 6]
. I guess just proc
defaults to proc near
- you might want to say that, just for clarity. If you need a proc far
it's probably time to graduate to 32-bit code (or 64-bit). 16-bit code is such a PITA - we're really glad to forget it! :)
这篇关于获取过程参数从堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!