作为彼得答案的补充,您可以使用 MASM/TASM 生成序言和尾声代码来为您设置 BP,并允许您按标签访问过程/函数参数。可以找到关于 MASM 和 TASM 使用的 PROC 子例程的相当好的教程here
我还将var 更改为WORD 而不是BYTE。生成的代码如下所示:
.MODEL SMALL
.STACK 100H
.DATA
var dw 5 ; Change to 16-bit WORD
.CODE
example proc C ; C Calling convention - parameters on stack right to left
ARG num:WORD ; We take one argument called `num` that is a word
mov cx, num ; Move the 16-bit value in `num` to CX counter
; same as: mov cx, [bp+4]
; [bp+0] is saved copy of BP put on stack by MASM's prologue
; [bp+2] return address placed on stack by CALL
cmp cx, 0
jle negative ; If we are less than or equal to 0, exit procedure
mov dl, 'X'
mov ah, 2h ; ah and dl not destroyed by int 21h/ah=2 so set them once
; before loop
print:
int 21h ; Print an 'X'
loop print ; Continue until loop is 0
negative:
ret
endp example
main proc
mov ax, @data ; initialize DS
mov ds, ax
push [var] ; Push 2-byte value at `var` (pushing by value)
call example
add sp, 2 ; Remove parameter from stack
; Not necessary since we use int 21h to exit right after
mov ah, 4ch ; return control to DOS
int 21h
main endp
end main ; Entry point = label main
上面的代码会为过程example生成这些指令:
example proc
push bp ; Save BP on stack \
mov bp, sp ; Set BP to SP / Function prologue
; [bp+0] is saved copy of BP put on stack by prologue
; [bp+2] return address placed on stack by CALL
; [bp+4] first parameter (NUM)
mov cx, [bp+4] ; Move the value at BP+4 (NUM) to CX counter
cmp cx, 0
jle negative ; If we are less than or equal to 0, exit procedure
mov dl, 'X'
mov ah, 2h ; ah and dl not destroyed by int 21h/ah=2 so set them once
; before loop
print:
int 21h ; Print an 'X'
loop print ; Continue until loop is 0
negative:
mov sp, bp ; Restore stack pointer \
pop bp ; Restore BP register / Function epilogue
ret
endp example
我把它作为一个练习留给读者来确定example PROC 更改的所有寄存器,并按照家庭作业中的要求保存/恢复它们。提示:在 ARG 指令之后 PUSH 并在 RET 之前以相反的顺序 POP 它们