News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

whats wrong with this??

Started by xponential, March 09, 2014, 12:44:59 PM

Previous topic - Next topic

xponential

well i m just testing my knowledge of conditional jumps and routines. so here is a program
i had intention of making read in 2 numbers. 1st will be the base and 2nd will be the power
so all its (supposed) to do is display the final value.
.386
.model flat, stdcall
option casemap: none

include windows.inc
include masm32.inc
include kernel32.inc

includelib masm32.lib
includelib kernel32.lib
.data
msg1 db "input number",0
msg2 db "input power", 0
msg3 db "number shud be 1 upwards", 0
.data?
number1 dd ?
number2 dd ?
power dd ?
.code
start:
power_finder proto :DWORD, :DWORD
invoke StdOut, addr msg1
invoke StdIn, addr number1, 20
invoke StdOut, addr msg2
invoke StdIn, addr number2, 20


_find_power: invoke power_finder,number1, number2
cmp eax, 5
je _Invalid
mov power, eax
invoke StdOut, addr power
invoke ExitProcess, 0
_Invalid: invoke StdOut, addr msg3
         jmp _find_power
         invoke ExitProcess, 0

power_finder proc number:DWORD, pow:DWORD
      mov edx, number
      mov eax, 1
      mov ecx, pow
      cmp edx, 1
      jl _Inv
_loop:dec ecx
      cmp ecx, 0
      je _ret
      mul edx 
      jmp _loop
_ret:mov eax, edx
     Ret
_Inv: mov eax, 5
    Ret
power_finder EndP



end start


please tell me whats wrong with it.. thanks

sinsi

A 32-bit MUL stores the result as a 64-bit number in EDX:EAX, so EDX is lost. A simple "MUL number" should do it.
Possibly you should also test for pow (ECX) after the MUL i.e. 3^0 and 3^1 are still valid numbers.

xponential

Can u shed more light on what u mean by 'a simple MUL Number' and also what u mean by testing for ecx. Thanks

raymond

What sinsi was suggesting should have been "mul number1" instead of copying the number (for which you want the exponential) into the edx register. Remember that you can use a 32-bit value directly from memory with the MUL instruction.

One detail sinsi forgot to mention is that you should check the content of the EDX register after each multiplication for the following reasons:
i) if the multiplication result exceeds 32 bits, the EDX register would stop being 0 and your code would need to include additional instructions to handle the 64-bit result in the EDX:EAX registers (i.e. transfer the 64-bit result to the calling code and display such 64-bit result).
ii) if you need to continue to multiply, you now have to multiply the 64-bit number (in EDX:EAX) by the 32-bit number in memory.
Whenever you assume something, you risk being wrong half the time.
https://masm32.com/masmcode/rayfil/index.html

sinsi

Something like this

    mov eax,1
    mov ecx,pow
@@:
    test ecx,ecx
    jz @f
    mul number
    dec ecx
    jmp @b
@@:
    ret

If pow=0 then it just returns with EAX=1 because x^0 is always 1.
This code won't give correct results if the result overflows 32 bits.

edit: what Raymond said

xponential

Thanks guys, now i understand.