News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Exponential .. Help if possible

Started by gusto, October 08, 2005, 05:38:45 AM

Previous topic - Next topic

gusto

I started taking a class this semester dealing with Assembler, I am completely new to this language, and my prof. is not very helpful. He himself said he hasnt used the language in a long time, and he started giving assignments telling us to hand in the results, but we have to figure out how to do everything on our own. He gave us one example on how to add and mov values, and ive been working off of that. My current project is to list 9^0 to 9^11 (exponentials 12 terms total).. 1, 9, 81, etc.by using addition only.. the code i am using is bellow..it lists 12 numbers but not the ones i need  :(  any sort of help in what i should do would be very appreciated.. if either editing what i have, or going about it a different way (this is the only model i have on how to write code in assembler, so any other route would be totally new to me).

-------------------------------

include \masm615\include\irvine16.inc

      .data
x      dword      0
y      dword      9

      .code
      main      proc
      startup
      mov      ecx,12
      
lp:      mov      eax,x
      add      y,eax
      mov      eax,y
      mov      eax,x
      call      writedec
      call      crlf
      add      x,9
      loop      lp
      
      exit
main      endp
      end      main

--------------------------------------

the list i get in Dos is...

0
9
18
27
36
45
54
63
72
81
90
99
---------

if you can help, thank you...

-gus

p.s. im not looking for anyone to write me out a code that will give me the solution, im trying to figure if im going about it the right way, if im totally off and what commands or whats even going on.... thanks  :(

MichaelW

Welcome to the forum.

90 = 1
91 = 9
92 = 81 = 9*9
93 = 729 = 9*9*9
...

You could do the calculation using only addition, but using multiplication would be much easier.

For MASM the operand order is destination, source. The first instruction accomplishes nothing here because the second instruction overwrites the destination:

mov  eax,y
mov  eax,x


eschew obfuscation

bozo

hi gusto,

you might want to check out Raymonds FPULib.

http://www.ray.masmcode.com/downloads/FPULIB21.zip

good tutorial here too.

http://www.ray.masmcode.com/tutorial/index.html

a little c code (if you know some) works with pow() function.

#include <stdio.h>
#include <math.h>

void main(void)
{
double y;

for(y=0;y<=12;y++)
printf("\n9 ^ %d = %.0f",(int)y,pow(9,y));
}


output is

Quote9 ^ 0 = 1
9 ^ 1 = 9
9 ^ 2 = 81
9 ^ 3 = 729
9 ^ 4 = 6561
9 ^ 5 = 59049
9 ^ 6 = 531441
9 ^ 7 = 4782969
9 ^ 8 = 43046721
9 ^ 9 = 387420489
9 ^ 10 = 3486784401
9 ^ 11 = 31381059609
9 ^ 12 = 28242953648

roticv

It would become an interesting problem if you are only allowed addition. I think your professor is interesting sicne he had not touched the lanuguage for a long time, yet expects others to know how to use it.

9^0 = 1
9^1 = 9
9^2 = 9 * 9 = add up 9 9s.
9^3 = 9 * 9 * 9 = add 81 up 9 times.
and so on..

I leave the code to you... just ask if you need more help using just addition.

AeroASM

Your task is:
1. Start with 1 in a variable
2. Print
3. Multiply by 9
4. Loop round steps 2-3

This gives the sequence 1,9,81,729 etc, because to get the next power you only need to multiply by nine.
To multiply by a constant using addition, there are three methods:
1. Straight addition: just nine adds
2. A combination of doubling and adding: 9=8+1=2^3+1, to double use add eax,eax
3. A loop, put 9 in ecx and add each iteration.

The advantage of number three is that it works with variables too.

roticv

 :(

You guys should read what he wrote.

QuoteMy current project is to list 9^0 to 9^11 (exponentials 12 terms total).. 1, 9, 81, etc.by using addition only..

gusto

thanks for the replys.. Kernel_Gaddafi, i understand C more than i do assembler, so doing it in C is not a problem for me, i will check out the links you put. MichaelW muliptlication would be 200x easier, but its not what the project is  :( , hence my dilema of addition only.  roticv the help is with the addition.. in this line of my code:

.code
      main      proc
      startup
      mov      ecx,12

if u put a higher value than 12, more numbers are listed, but i dont know how to add the numbers exponentially so only the 12 i need are listed.. and  AeroASM , the code i posted is the only type of structure i have been taught, so i guess im suppose to work with loops, my problem comes into play when all the additions are being listed, and not the 12 that i need.. how can i go about making it skip the additions in the middle? . . . im gonna look at the links Kernel_Gaddafi posted.. any more replys are welcome...


p.s. thank you for the welcome.. it seems like i will be here all semester if not longer  :U

mnemonic

Hi gusto,

you may have to write 2 other loops.

Let us break the whole thing down:
- you need a procedure that multiplies by addition (mulByAdd)
- you need a procedure that implements the raising to the power (pow) by using mulByAdd
mulByAdd will take two parameters and pow will take two parameters too.
That way you may get a better grip to the issue.

You may also want to get familiar with the conditional jumps.
I would recommend you reading "Conditional Jump Table" from the file "OPCODES.HLP" in the directory "\masm32\help".
If you do not have the MASM32 package installed I would also recommend you installing it anyway.

Regards
Be kind. Everyone you meet is fighting a hard battle.--Plato
-------
How To Ask Questions The Smart Way

MichaelW

gusto,

Yea, I failed to see the "by using addition only". And I also failed to realize the 911 will not fit in a 32-bit integer, so unless you implement special handling for the 911 value you cannot use 32-bit additions and you cannot display the result with the 32-bit WriteDec procedure. I didn't have the time to work through the entire problem, but you should be able to synthesize a 64-bit add with an ADD followed by an ADC (eg ADC r/m32,0) and modify the WriteDec procedure to handle 64-bit integers.

eschew obfuscation

gusto

Kernel i checked the example .zip you posted.. it all seems like russian to me  :( .. my prof. has touched on non of these calls/codes, and im tryin to read them line by line but it doesnt make much sense to me... my prof. never even did a 'hello world' example, so im unclear how to even get my program to print out a number, all im working with is the structure he gave me... i just tried printing an intial number of "1" and then starting the 9 addition loop.. but all i got was an infinite line of 1's... i feel like such a retard for asking help with even the most basic things  ::) .. does anybody know where i can find basic examples i can learn off of?
thanks alot so far guys...

ps. mnemonic.. the mulbyAdd, is that a call, or an example of what i should name my procedure?.. sorry, once agian i am a noooob  :toothy

dsouza123

#10
I couldn't resist, here is a pascal version.
The code shown uses floating point doubles ( real8 for MASM ) because the last value exceeds 32 bits.
64 bit integer (which can be simulated with two 32 bit integers)
would be a direct substitution for 64 bit floating point.


Program NinePow;
var
  i,j : longint;
  r,t : double;
 
begin
  r := 1.0;
  for i := 0 to 11 do begin
    if i > 0 then
      for j := 2 to 9 do begin
        r := r + t;
      end;
    t := r; 
    writeln('9 ^ ',i:2,' = ',r:0:0);
  end;
end.

or more MASM like with repeat

begin
  i := 0;
  repeat
    if i > 0 then begin
      j := 8;
      repeat
        r := r + t;
        j := j - 1;
      until j < 1;
    end;
    t := r; 
    writeln('9 ^ ',i:2,' = ',r:0:0);
    i := i + 1;
  until i > 11;
end.

[attachment deleted by admin]

gusto

hey dsouza, thanks for the input.. can you use that kinda format in masm615 (what im using).. didnt work for me  :( ... as for my own code, i started playing a little and made little but some progress.... :

include \masm615\include\irvine16.inc


.data
x                 dword                       1
y                 dword                       9

.code
main                       proc
startup
mov                       ecx,12

lp:              mov                      eax,y
add                       y,eax
mov                      ebp,y
add                       y,ebp
mov                       edx,y
add                       y,edx
mov                       edx,y
call                       writedec
call                       crlf
add                         y,9
loop                        lp

exit
main            endp
end                         main


i get as output:

9
81

657
5265
42129
337041
2696337
21570705
172565649
1380525201
2454267025
2454267025

--------------
i now have gotten 2 numbers in my series.....  :U ... i looked at the folder where masm615 was installed and found some examples, but they dont quite deal with what i am trying to do.. can somebody tell me if im even thinking the right way?.. the double looping seems like a good route, 1 loop for adding, 1 loop counting the powers, but i have no idea how to do that in masm615, only in c++..  :'(
once again.. thanks for any input.. this is due the 17th of oct. and i have 1 class with my prof. this coming week, i am going to see what he can help with  :eek

edit: i have x set to 1 in order for it to be my first number in my list.. but i dont know how to just print 'x' to the screen  ::)

dsouza123

MASM 6.14 which is the assembler in MASM32, the development package to create 32 bit Windows programs,
supports

.if x > y
.else       ----  the .else is optional
.endif

.repeat
.until x < y

with x, y one of the following (or reverse the order)
reg, reg
reg, imm
reg, mem
mem, imm
reg = register, imm = immediate value ( a number ), mem = memory content ( a variable)

>, >=, <, <=, ==, !=  (and others) for comparison operators

OceanJeff32

Kip Irvine is the writer of that book you are studying too, he has all the source code for the book online, if that helps!

He's a teacher out of Florida. If I find his website again, I'll update this message, but yeah, I want to read that book...can I have it when you are done?  :lol

Oh well,

Jeff C
:'(
Any good programmer knows, every large and/or small job, is equally large, to the programmer!