Peterの答えの補足として、MASM/TASMを使用してプロローグとエピローグコードを生成し、BPをセットアップし、手続き/関数の引数にラベルでアクセスすることができます。 MASMとTASMが使用するPROCサブルーチンについて合理的に良いチュートリアルでは、私が代わりBYTEのWORDするvar
を変更したhere
を見つけることができます。上記のコードは、手順example
ため、これらの命令を生成しているだろう
.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 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が変更され、宿題の要求に応じて保存/復元されます。ヒント: `dw`を(使用して` var`を宣言する必要がありますので、逆の順序でARGディレクティブとPOPそれらの後PUSHそれらは直前にRET
に何の 'PUSHのR/m8'は、ありませんまたは 'dd')。また、 'push word ptr [var]'(または 'dword ptr')を使用して、いくつのバイトをプッシュしたいかを明示する必要があります。 – Michael
スタックにパラメータを渡す必要がありますか? –