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 April 19th, 2005, 08:03 PM
    aznium aznium is offline
    Junior Member
     
    Join Date: Apr 2005
    Posts: 16
    aznium is an unknown quantity at this point (<10)
    Quicksort

    my first assembly program

    quicksort . yah!

    Code:
    #include "stdafx.h"
    #include "stdio.h"
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        int nums = 0;
        int *list;
    
        printf("please enter the number of numbers\n");
        scanf("%d", &nums);
    
        list = new int[nums];
    
        for(int i = 0; i < nums; i++)
        {
            printf("please enter number %d\n", i);
            scanf("%d", &list[i]);
        }
    
        _asm
        {
    start:
            xor eax, eax                ; a = 0
            mov ebx, nums               ; b = nums
            call sort                   ; sort
            ret                         ; return
    
    sort:
            cmp ebx, eax                ; compare a, b
            jge valid                   ; if b > a, goto valid
            ret                         ; return
    
    valid:
            call pivot                  ; goto pivot
            push ebx                    ; store b into stack
            push eax                    ; store a into stack
            inc eax                     ; a++
            call mainloop               ; goto mainloop
            pop ecx                     ; retrieve original a from stack
            dec eax                     ; a--
            mov edx, list[eax]          ; d = nums[a]
            push edx                    ; store d into stack
            mov edx, list[ecx]          ; d = nums[original a]
            mov list[eax], edx          ; nums[a] = d
            pop edx                     ; retrieve d from stack
            mov list[ecx], edx          ; nums[original a] = d
            push ebx                    ; store b into stack
            mov ebx, eax                ; b = a
            mov eax, ecx                ; a = original a
            call sort                   ; recursion
            pop eax                     ; retrieve b from stack
            pop ebx                     ; retrieve original b from stack
            call sort                   ; recursion
            ret                         ; return
        
    pivot:
            mov ecx, list[eax]          ; pivot = nums[a]
            ret                         ; return
    
    mainloop:
            cmp eax, ebx                ; compare a, b
            jl compare                  ; if a < b goto compare
            jl mainloop                 ; if a < b, loop
            ret                         ; return
    
    compare:
            cmp list[eax], ecx          ; compare a, pivot
            jle lessequal               ; a <= e
            jg greater                  ; a > e
            ret                         ; return
    
    lessequal:
            inc eax                     ; a++
            ret                         ; return
    
    greater:
            dec ebx                     ; b--
            call swapnums               ; goto swapnums
            ret                         ; return
    
    swapnums:
            mov edx, list[eax]          ; d = a
            push edx                    ; store d into stack
            mov edx, list[ebx]          ; d = b
            mov list[eax], edx          ; a = d
            pop edx                     ; retrieve d from stack
            mov list[ebx], edx          ; b = d
            ret                         ; return
        }
    
        for(int i = 0; i < nums; i++)
        {
            printf("%d", list[i]);
        }
    
        scanf("%s");
    
        delete [] list;
    
        return 0;
    }
    ...sniff...and it doesnt work :'( ... im stumped

    it doesnt even get to output anything...the program just quits...

    i think my problem is with the jumps...the jg, jle...etc
    i think it doesnt return to the method that called it?

    also on a side note,
    why cant i do something like this?
    Code:
      mov list[0], list[1] //where list[0] and list [1] are defined in C++
    thx if u can help me
    Reply With Quote
      #2    
    Old April 20th, 2005, 05:09 AM
    Hobson's Avatar
    Hobson Hobson is offline
    Senior Member
     
    Join Date: Dec 2004
    Location: Poland
    Posts: 1,163
    Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)
    Re: Quicksort

    Gratz on your first assembly program!

    Quote:
    also on a side note, why cant i do something like this?
    Code:
          mov list[0], list[1] //where list[0] and list [1] are defined in C++
    That is because you are not allowed to move between two memory locations. At least one argument of mov should be reg or immed.

    Also, try debugging your code. You may use TurboDebugger (td.exe), shipped with Borland C++ 3.1 or Tasm/Tlink, or MSVC, or some disassembler, like WDasm32.
    __________________
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?
    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:01 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