CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Other Programming > Assembly
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Assembly Questions and Answers for Assembly here!

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 6th, 2004, 11:51 PM
    laura laura is offline
    Junior Member
     
    Join Date: Nov 2004
    Posts: 3
    laura is an unknown quantity at this point (<10)
    improper operand type

    trying to simulate insertion sort in descending order in ASM.. based off this Java code:

    Code:
    public static void insertionSort (int [] a) {
    // method to sort using the insertion sort
       for (int k = 1; k < a.length; k++) 
       {
          int item = a[k];
     
          int i = k-1;
          while ((i >= 0) && a[i] < item)
          {
             a[i+1] = a[i];
             i--;
          }
          a[i+1] = item;
       }
    in ASM:
    Code:
    _asm
    {
      mov esi, 1           ; k = 1
      mov eax, a           ; pointer to a[k-1]
    forloop:
      mov ebx, [eax + 4]   ; item = a[k]
      mov ecx, esi         ; i = k
      dec ecx              ; i = k-1
      cmp ecx, 0           ; compare i with 0
      jl next              ; if less than skip loop
      cmp [eax],ebx        ; compare a[i] with ebx (item)
      jge next             ; if >= skip loop
    whileloop:
      mov edx, [eax]       ; a[i]
      mov [eax+4], edx     ; a[i+1] = a[i]
      sub eax, 4           ; i--, move to previous element
      dec ecx              ; decrement the counter
      cmp ecx, 0           ; compare with 0
      jl next              ; if less than 0 break out of loop
      cmp [eax],ebx        ; compare a[i] with item
      jge next             ; if >= skip loop
      jmp whileloop        ; otherwise go through while loop again
    next:
      mov [eax+4],ebx      ; a[i+1] = item
      inc esi              ; k++
      cmp esi, 5           ; compare with 5 (which is the size of the array)
      jge done             ; if >= we're done
      jmp forloop          ; otherwise begin the for loop again
    done:
    }
    I fixed my illegal operand problem by using a separate register to store the value [eax].. anyway.. now this code compiles, but doesn't seem to do a **** thing. The array is exactly the same.

    Last edited by laura; November 7th, 2004 at 01:58 AM. Reason: fixed code
    Reply With Quote
      #2    
    Old November 8th, 2004, 12:35 AM
    Amit_Roy Amit_Roy is offline
    Member
     
    Join Date: Jul 2003
    Location: DELHI
    Posts: 62
    Amit_Roy is on a distinguished road (10+)
    Re: improper operand type

    hi ,
    have u fixed your code ?
    i tried to fixed it and found 2 errors see below
    I have checked this code in vc++ 6.0 it is working fine


    Code:
    _asm{
      mov ESI, 1           //; k = 1
      mov EBX, b          // ; pointer to a[k-1]
    forloop:
      mov EAX, [EBX + 4]    // ; item = a[k]
      mov ECX, ESI              // ; i = k
      dec ECX                      // ; i = k-1
      cmp ECX, 0                //; compare i with 0
      jl next                       //  ; if less than skip loop
      cmp [EBX],EAX        //; compare a[i] with BX (item)
      jge next           //  ; if >= skip loop
    whileloop:
      mov Edx, [EBX]      // ; a[i]
      mov [EBX+4], Edx    // ; a[i+1] = a[i]
      sub EBX, 4          // ; i--, move to previous element
      dec ECX             // ; decrement the counter
      cmp ECX, 0           //; compare with 0
      jl next            //  ; if less than 0 break out of loop
      cmp [EBX],EAX        //; compare a[i] with item
      jge next            // ; if >= skip loop
      jmp whileloop       // ; otherwise go through while loop again
    next:
      mov [EBX+4],EAX     // ; a[i+1] = item
      inc ESI
      add EBX , 8         // ; k++
      cmp ESI, 6           //; compare with 5 (which is the size of the array)
      jg done            // ; if >= we're done
      jmp forloop         // ; otherwise begin the for loop again
    done:
             }
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Other Programming > Assembly


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 11:37 PM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009