指数幂程序的汇编错误结果
这是一个由两部分组成的作业。首先,我必须弄清楚如何使用堆栈将引用参数发送到名为 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
This was a 2 part assignment. first I had to figure out how to send a reference parameter to a procedure called pow using the stack which I think I did correctly using push offset result
The second part of this assignment has me completely clueless, i've read and read in my text but I am still unable to figure out how I can accomplish what I need to do. After sending the reference parameter I need to make the result of the calculations in the pow procedure become stored in the reference parameter so it can be output later in the program. I've tried a few different things so far to no avail. The code is commented so those familiar with assembly should understand what i'm trying to do. If anyone could help me out I will greatly appreciate it. thanks
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,既然您使用的是 C 风格的函数原型,我想您了解 C。现在,当您将指针传递给函数时,您需要取消引用该指针来访问其数据。换句话说,取消引用是在变量名称之前添加星号。举个例子:
这些星号的意思是:“获取地址a(或b)处的值,而不是a(或b)的值”。这是您需要在程序中执行的操作来存储结果。
在汇编中,有一种特殊的方法来指定“地址”而不是“值”,它就是……。一旦你知道了这一点,你就应该没问题了;)
编辑
一旦你将结果存入寄存器,只需在另一个寄存器中加载偏移量并将 [] 应用于该寄存器:
或者,在 C 中:
Ok, since you're using C-style function prototype, I guess you know C. Now, when you pass a pointer to a function you need to dereference that pointer to access its data. In other words, dereferencing is that asterisk you put before variables name. An example:
Those asterisk means: "grab the value at address a (or b) instead of a's (or b's) value". This is what you need to do in your program to store the result.
In assembly there's a special way to specify "the address of" instead of "the value of", and it is ......... . Once you know this, you should be ok ;)
EDIT
Once you have the result in a register, just load in another register the offset and apply the [] to that register:
or, in C: