2

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
rkhb
  • 14,159
  • 7
  • 32
  • 60
darko
  • 2,438
  • 8
  • 42
  • 54

1 Answers1

3

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:

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

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:

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

or, in C:

*pointer = result;
BlackBear
  • 22,411
  • 10
  • 48
  • 86
  • @Matt: no.. ;) surrounding a pointer with [] example: mov [ecx], 5 – BlackBear May 11 '11 at 19:30
  • Ah yea, I already know about using indirect addressing. What I am more confused about is what I am actually supposed to surround with [] and where – darko May 11 '11 at 19:39
  • 1
    @Matt: here it is. Sorry if I answer slowly, I'm quite busy right now – BlackBear May 11 '11 at 20:05
  • @blackbear, no rush, thanks for the help. where would the pointer be? [ebp + 8]? – darko May 11 '11 at 20:08
  • Cool, Where would it go in the pow procedure? I tried it right after the pops, right before the pops, and in the beginning right after the block comment but the output was still incorrect. – darko May 11 '11 at 20:15
  • @Matt: it should be there. What do you get instead? – BlackBear May 11 '11 at 20:20
  • With this code: 'mov eax, [ebp + 8] mov ecx, result mov eax, ecx' right before the pops I get +0 when expecting 100 (input of 10 and 10) – darko May 11 '11 at 20:22
  • @Matt:: 10^ 10 is 10000000000 :P anyway it should be mov eax, [ebp + 8]; mov ecx, result mov [eax], result – BlackBear May 11 '11 at 20:26
  • Bare in mind all I did was add those three mov statements, was I supposed to change the EQU statements or anything like that? – darko May 11 '11 at 20:27
  • lol, who actually knows the result of powers now a days? (sarcasm). anyway, only getting +0 as the output for anything with: mov eax, [ebp + 8]; mov ecx, result mov [eax], result as well (when put in right before final pops) – darko May 11 '11 at 20:30
  • actually, mov eax, [ebp + 8]; mov ecx, result mov [eax], result gives me an error and is not what you put in the original answer. mov eax, [ebp + 8]; mov ecx, result mov [eax], ecx (what you put in the original answer) runs but gives incorrect output – darko May 11 '11 at 20:36
  • @Matt: weird. Perhaps your code isn't working? :P try initializing result with 5 or so – BlackBear May 11 '11 at 20:37
  • Are you referring to result as the result of the calculation? I am thinking of it as the result variable – darko May 11 '11 at 20:49
  • @Matt: yeah the variable's initial value. If now you get five half of the problem is solved; at least we know where it is – BlackBear May 11 '11 at 20:58
  • When I initialized 5 the output is just 5 as if pow doesn't touch change result at all. – darko May 11 '11 at 21:01
  • @Matt: weird.. sorry but for me is time to go to bed, I have to get up early for morning, damn school.. -.- I'll think about this tomorrow ok? sorry again :) – BlackBear May 11 '11 at 21:05
  • 1
    here was the solution i was looking for: mov ecx, [ebp + 8] mov [ecx], eax thanks for the help! – darko May 11 '11 at 21:16