Results 1 to 4 of 4

Thread: Question: finding cube of a number

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    265

    Question: finding cube of a number

    I wrote this to multiply the x by itself 3times then add a number y to it and save result in z. but it dosn't giving me the right result.
    Code:
    .DATA
    X DB 12h
    Y DB 34h
    Z DW ?,?
    
    mov al,x
    mov bl,x
    mul bl; ax=bl*al
    mul ax; dxax=ax*ax
    add ax,y
    mov z,ax
    mov z+2,bx
    I wrote it in Debug.
    thank's

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Question: finding cube of a number

    Is that .NET or Assembly.. or IL ??
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    265

    Re: Question: finding cube of a number

    It is assembly code dealing with 8086 processor.
    I want to find the cube of a number.
    My problem, for ex:
    MOV AX,0012h;
    MUL AX; that will do DXAX=AX*AX

    now how can I multiply the DXAX with AX again to get the cube stored in DXAX ?

    thank's in advance

  4. #4
    Member
    Join Date
    Oct 2006
    Posts
    53

    Re: Question: finding cube of a number

    If x always is 2 i.e 2^3 you can do it like this
    Code:
    mov eax,2
    shl eax,2
    Result in eax

    If you use mul you do it like this (5^3)
    Code:
    mov eax,5
    mov ecx,2
    jmp @F
    back:
    mov edx,5
    mul edx
    dec ecx
    @@:
    cmp ecx,0
    ja back
    Result in eax

    If you use co-processor you do i like this (5^3)
    Code:
    finit	;Initialize the co-processor
    mov eax,0		;Load
    push eax		;3
    mov eax,3		;to stack
    push eax		;as a qword
    fild qword ptr [esp]	;Load 3 from stack to 80-bit register st(0)=3
    add esp,8		;Restore stack
    mov eax,0		;Load
    push eax		;5
    mov eax,5		;to stack
    push eax		;as a qword
    fild qword ptr [esp]	;Load 5 from stack to next register  st(0)=5, st(1)=3
    add esp,8		;Restore stack
    fyl2x			;st(0)=st(1)*log2(st(0)) => st(0)=5*log2(3)
    fst st(1)		;Store st(1)=st(0)=5*log2(3)
    frndint			;Round ST(0) to an integer
    fsub st(1),st(0)	;Subtract st(0) from st(1) => st(0)=integer and st(1)=fraction
    fxch st(1)		;Exchange st(0) and st(1)
    f2xm1			;computes the exponential value of 2 to the power of the value of ST(0) and subtracts 1
    fld1			;Load value 1 to st(0). st(0)=1, st(1)=2^st(0), st(2)=integer
    fadd			;st(0)=2^st(0), st(1)=integer
    fscale			;Scale ST(0) by ST(1)
    fwait			;Wait while co-processor is busy
    fstp fResult		;Store in fResult(qword) and pop ST(0)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width