2

これは 2 部構成の課題でした。最初に、スタックを使用して pow というプロシージャに参照パラメータを送信する方法を理解する必要がありましたが、これは正しく使用したと思いpush offset resultます自分がしなければならないことをどのように達成できるかをまだ理解できていません。参照パラメーターを送信した後、pow プロシージャーでの計算結果を参照パラメーターに格納して、後でプログラムで出力できるようにする必要があります。私はこれまでにいくつかの異なることを試しましたが、役に立ちませんでした。コードにはコメントが付けられているため、アセンブリに精通している人は、私が何をしようとしているのかを理解する必要があります。誰かが私を助けることができれば、私はそれを大いに感謝します. ありがとう

INCLUDE Irvine32.inc
.data
XPrompt BYTE "Enter the value of the base(X):",0
YPrompt BYTE "Enter the value of the exponent(Y):",0
ResultMessage BYTE "X to the power of Y is",0
result DWORD ?

.code
main PROC
    call Clrscr

   ;;;;Prompt for X 
    mov  edx,OFFSET XPrompt
    call WriteString

    call ReadInt
    push eax     ;;;;pass the 1st number to POW
                 ;;;;this will represent the base

   ;;;; Prompt for Y 
    mov  edx,OFFSET YPrompt
    call WriteString

    call ReadInt
    push eax            ;;;;pass the 2nd number to POW
                        ;;;;this will represent the EXPONENT

    push OFFSET result  ;;;;pass the third parameter to pow, using offset makes it a reference parameter                         

    call Pow
                             ;;; Print Result (Assumes the answer is in eax)
    mov  edx,OFFSET ResultMessage
    call WriteString

    ;;;;;;;;;;;;;;;;;NOTE: NEW "POW" MODIFICATIONS HERE;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    mov eax,result     ; If the pow function correctly returns it answer by reference
                       ; then this should be all that's necessary to print
                       ; the answer with the call to "WriteInt"
   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;                     
    call WriteInt
    call ReadInt             ;;;; screen pause
    exit
main ENDP

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Pow PROC
COMMENT !
PUT FUNCTION CODE IN THIS SECTION

This current  pow function returns its answer via register "eax." Modify it as necessary
so that it returns its answer by a reference parameter. In C++ the function interface/prototype
would look like:

 void pow(int base,int exp, int & result)

where "base" is the base and "exp" is the exponent. In other words "pow" should calculate "base" to the
power of "exp," then return the answer via "result."  Let your function return its result via a
3rd REFERENCE parameter "result." Which will be a REFERENCE parameter on the stack. 
 !

 base EQU DWORD PTR [ebp + 12]
 exponent  EQU DWORD PTR [ebp + 8]

 push ebp
 mov ebp, esp
 push ecx  ;<------------ecx must also be preserved since it is modified
           ; by the "loop" instruction.

 mov ecx, exponent ;set ecx as our counter
 mov eax, 1        ; eax will be our multiplier
 L1:
  mul base
  loop L1

pop ecx  ;<------------restore ecx
pop ebp  ;<------------restore ebp

ret 8
Pow ENDP
END main
4

1 に答える 1

3

わかりました。C スタイルの関数プロトタイプを使用しているので、C を知っていると思います。関数にポインターを渡す場合、そのポインターを逆参照してそのデータにアクセスする必要があります。つまり、逆参照とは、変数名の前にアスタリスクを置くことです。例:

void swap(int *a, int *b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

これらのアスタリスクは、「a (または b) の値ではなく、アドレス a (または b) の値を取得する」ことを意味します。これは、結果を保存するためにプログラムで行う必要があることです。
アセンブリには、「~の値」ではなく「~のアドレス」を指定する特別な方法があります。これさえ分かれば大丈夫です(;_;)

編集

結果をレジスタに格納したら、別のレジスタにオフセットをロードし、[] をそのレジスタに適用します。

mov eax, pointer
mov ecx, result
mov [eax], ecx

または、C:

*pointer = result;
于 2011-05-11T19:10:12.440 に答える