News:

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

I needs your help

Started by shawn, April 09, 2010, 06:53:11 PM

Previous topic - Next topic

shawn

now I am coding a bubble sort with MASM
the goal is simpel
read numbers and store it in array and sort it.

I tried it several times but it's not working


.586
.MODEL FLAT

INCLUDE io.h            ; header file for input/output

.STACK 4096

.DATA

number   DWORD   ?
count    DWORD   ?

prompt   BYTE    "Enter the number", 0

string   BYTE     20 DUP (?)

sortLbl   BYTE  "The sorted array is", 0


sortAnswer    BYTE     11 DUP (?), 0
nbrArray      DWORD    100 DUP (?)


.CODE
_MainProc PROC

            lea    ebx,nbrArray     ; get address of nbrArray
            mov    ecx,0

            input  prompt, string, 20
            atod   string
            cmp    eax,0
            je     exitGo
            add    [ebx],eax
            add    ebx,4
            inc    ecx           

   goinput: input prompt, string, 20
            atod string
            add    [ebx],eax
            add    ebx,4
            inc    ecx           
            cmp    eax,0
            jz     goNext             
            jmp    goinput             

                   
   goNext: dec ecx
           call goSort
         
           
                lea    ebp,nbrArray     ; get address of nbrArray
  printArray  : mov    eax,[ebp]
                add    ebp,4
                dtoa   sortAnswer,eax        ;convert to ASCII character   
                output sortLbl,sortAnswer
                mov    eax,[ebp]
                cmp eax,0
                jz exitGo
                loop PrintArray




exitGo: 
            mov   eax, 0      ; exit with return code 0
            ret

_MainProc ENDP
                           

goSort      PROC NEAR32
            dec ecx           ; do not count last 0 input
            dec ecx ; decrement outer loop counter by 1 (arraysize -1)
           
       lp1: add edx, ecx                 ; save outer loop count
            mov ebx, nbrArray                      ; point to first value

      [color=Red] lp2: mov eax, [ebx]  [/color]                           
          cmp [ebx+4], eax                       
          jge lp3                              
          xchg eax, [ebx+4]                       
          mov [ebx], eax

       lp3:    add ebx, 4 ; move both pointers forward
        loop lp2      ; inner loop

        dec  edx ; retraeve outer loop count
     loop  lp1 ; else repeat outer loop
         
       lp4: ret
goSort  ENDP

END 

When I run it,

Unhandled exception at 0x00411aae in windows32.exe: 0xC0000005: Access violation reading location 0x00000002.

this message occured at lp2(red color)

I needs your help. I just want to figure out it's problem of syntax or algorithm

Thank you

Added code tags

Slugsnack


shawn

Quote from: Slugsnack on April 09, 2010, 06:58:34 PM
mov ebx, offset nbrArray

thank for your interest but when I changed it.

Microsoft Visual Studio C Runtime Library has detected a fatal error in windows32.exe.

Press Break to debug the program or Continue to terminate the program.

this messgae occur and  dbghook.c file poped up

Slugsnack

can you upload a compilable version of your code and i'll test it and find where the bug is. or the io.h file

redskull

You probably shouldn't be changing EBP, or at least preserving it for the function calls (before the 'output')

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

shawn

I would like to upload the file but I don't know how to upload it  :eek

this is url to download the necessary file

http://www.jblearning.com/catalog/samplefile.aspx?isbn13=9780763772239&isbn=0763772232&filename=windows32.zip&isOlder=0

I really appreciate your help

Slugsnack

that compiles and runs fine for me. but it's not a bubble sort, just adds two numbers..

dedndave

Shawn
in here, we all use the masm32 package
at the upper right corner of the forum is a link to download it
when you install it, close all programs until it is done building the libraries

your code makes no sense to us because it is written to be used with the C compiler   :bg

bsort.asm(29) : error A2008: syntax error : input
bsort.asm(30) : error A2008: syntax error : atod
bsort.asm(73) : error A2008: syntax error : sortAnswer
bsort.asm(74) : error A2008: syntax error : output
bsort.asm(79) : error A2006: undefined symbol : PrintArray


i am going to have a look at the windows32.zip file....

dedndave

ok - i have io.h in there, now   :P

bsort.obj : error LNK2001: unresolved external symbol __getInput
bsort.obj : error LNK2001: unresolved external symbol __showOutput
bsort.obj : error LNK2001: unresolved external symbol _atodproc
bsort.obj : error LNK2001: unresolved external symbol _dtoaproc
bsort.obj : error LNK2001: unresolved external symbol _wtoaproc
bsort.obj : error LNK2001: unresolved external symbol _atowproc

i think if i change some names with "crt__" prefixes, i can make it work

dedndave

QuoteI would like to upload the file but I don't know how to upload it

below the reply window is a link that says > Additional Options
click on that, then attach your file
it must be a ZIP file, less than 256 Kb

dedndave

ok - i have taken the io.asm file and created an obj - i chose to rename it DetmerIO.asm/DetmerIO.obj
also, i have renamed io.h to DetmerIO.inc

now my only problem   :P
how do you link multiple obj files with this new-fangled linker ? - lol
in the old days, you could use LINK file1.obj+file2.obj
that doesn't seem to work
any help ?

clive

You just enumerate them LINK moe.obj larry.obj curly.obj
-Clive
It could be a random act of randomness. Those happen a lot as well.

dedndave

thanks Clive
i still have unresolved externals
i don't know enough about C libraries to make it work, i guess

GregL

Yeah, I got it to compile and run with VS 2010 too, like Slugsnack says it's not a bubble sort, it asks for two numbers and then adds them and displays the result.

It uses Richard Detmer's IO library from his book. They are not C libraries they are MASM format despite the .h file extensions.



dedndave

i am sure i am missing something, then
if it builds for you guys, you don't need me   :P