Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Assembly (http://www.programmingforums.org/forum20.html)
-   -   tasm sorting 5 integers with array (http://www.programmingforums.org/showthread.php?t=14271)

akioshin Oct 26th, 2007 10:32 PM

tasm sorting 5 integers with array
 
iv been trying to code this but everytime i run the program it wont sort or it wont swap, can someone at least give me an example with sorting 5 integers using array, so that i will use this as my reference for my project,my project is that to sort names in alpabetical order,

DaWei Oct 26th, 2007 10:59 PM

Re: tasm sorting 5 integers with array
 
Well, we can't tell what is wrong with your program, unless we're mind readers, because you haven't provided any code for us to look at.

No, we won't write your code for you. We will help you with what you write. You have joined our community and asked for free help. I perceive that you have not read our rules. That seems rather rude, does it not?

akioshin Oct 26th, 2007 11:22 PM

Re: tasm sorting 5 integers with array
 
oh im realy sory heres my code for sorting 5 integers using array, its not finish yet im still testing for the first and second index to swap
:

extrn readnum:near
extrn writenum:near
extrn new_line:near
extrn clearscreen:near
public num
.286
.model small

printStr macro str
  pusha
  lea dx, str
  mov ah, 9
  int 21h
  popa
endm

.stack 0
.data

        msg db, 10, 13, 'Please enter one digit number: $'
        num dw 0
        ar1 dw 5 dup(?)

.code


        mov ax, @data
        mov ds, ax


        mov bx, offset ar1
        mov si, offset ar1
        mov cx, 5

aloop:

        printStr msg
        call readnum
        mov ax, num
        mov [bx], ax
        add bx, 2
        loop aloop

check:
       
        add bp, 2
        mov ax, [bx]       
        cmp ax, [si]
        jg swap

swap:

        mov ax, [bx]
        push ax
        mov ax, [bx+2]
        mov [bx], ax
        pop ax
        mov [bx+2], ax
       

        jmp disp1



;for displaying the array
disp1:
        mov bx, offset ar1
        mov cx, 5
disp:
       
        mov ax, word ptr [bx]
        mov num, ax
        call new_line
        call writenum       
        add bx, 2
        loop disp


        mov ah, 4ch
        int 21h
end


DaWei Oct 26th, 2007 11:32 PM

Re: tasm sorting 5 integers with array
 
Notice here:
:

check:
       
        add bp, 2
        mov ax, [bx]       
        cmp ax, [si]
        jg swap

swap:

You always wind up at swap, regardless of the relationship between the two items tested. I think you've also lost a handle on where bx points. I think you've also lost view of what happens after a swap.

Have you tried stepping through this with a debugger? I highly recommend it.

akioshin Oct 27th, 2007 1:08 AM

Re: tasm sorting 5 integers with array
 
ah ya im just a newbie at this, can you check this one this is my second attempt and can you advise me of wat gud books or site for assembly
:

extrn readnum:near
extrn writenum:near
extrn new_line:near
extrn clearscreen:near
public num
.286
.model small

printStr macro str
  pusha
  lea dx, str
  mov ah, 9
  int 21h
  popa
endm

.stack 0
.data

        msg db, 10, 13, 'Please enter one digit number: $'
        num dw 0
        ar1 dw 5 dup(?)

.code


        mov ax, @data
        mov ds, ax


        mov bx, offset ar1
        sub si, si
        sub di, di
        mov cx, 5
aloop:

        printStr msg
        call readnum
        mov ax, num
        mov [bx], ax
        add bx, 2
        loop aloop
       
        mov di, 2

comp:
        mov ah, [bx+si]
        mov al, [bx+di]
        cmp ah, al

        jge swap
swap:

       
        mov [bx+si], al
        mov [bx+di], ah
        jmp disp1

       

;for displaying the array
disp1:
        mov bx, offset ar1
        mov cx, 5
disp:
       
        mov ax,[bx]
        mov num, ax
        call new_line
        call writenum       
        add bx, 2
        loop disp


        mov ah, 4ch
        int 21h
end


RobEasy Oct 27th, 2007 2:48 AM

Re: tasm sorting 5 integers with array
 
http://www.xs4all.nl/~smit/asm01001.htm

Honestly, you are a lot farther along than the tutorial is but more info can never hurt.

DaWei Oct 27th, 2007 4:06 AM

Re: tasm sorting 5 integers with array
 
You're missing the point.
:

comp:
        mov ah, [bx+si]
        mov al, [bx+di]
        cmp ah, al

        jge swap  If it >= we jump to swap, else we fall through to swap.
swap:              What have you accomplished?

       
        mov [bx+si], al
        mov [bx+di], ah
        jmp disp1  Here you jump where?  Right where you'd go if you removed this

       

;for displaying the array
disp1:

You need to mentally trace your code, asking yourself what happens at every step. Conditionals are the heart of processing. If they don't result in different paths, they're worthless.

lectricpharaoh Oct 28th, 2007 7:39 AM

Re: tasm sorting 5 integers with array
 
Re-read DaWei's post. Then try plotting out the logical flow before you write any further code. I'll give you an example using a bubble sort, since it's very easy to understand. It should look something like this:
:

set unsorted items = array size
while (unsorted items > 1)
  current item index = 0
  while (current item index < unsorted items)
    if (array[current item index] > array[current item index+1])
      swap (array[current item index], array[current item index+1])
    increment current item index
  decrement unsorted items

Here is the process translated to assembly (assumes all segment registers are correctly initialized):
:

sort:
  mov cx, [arraysize] ; load size of array
  cmp cx, 2          ; and ensure we have two or more elements
  jl  done            ; we're done if there are fewer than two elements
  mov bx, [array]    ; point at start of array- note 'array' is a pointer,
                      ; so we will dereference it in the inner loop below
nextpass:
  mov si, 0          ; SI is our current item index
nextelement:
  mov ax, [bx+si]    ; fetch current element
  cmp ax, [bx+si+1]  ; and compare to following element
  jge noswap          ; if the second is not smaller, skip the swap
  xchg ax, [bx+si+1]
  mov ax, [bx+si]
noswap:
  inc si              ; increment index for next element
  cmp cx, si          ; and check if we've finished the current pass
  jl nextelement      ; still on this pass, so process next element
  loop nextpass      ; do next pass if any remain, else we're done
done:

Be warned. First, it's been ages since I've played with assembly, so there may very well be errors. Second, I've used NASM syntax, but it's easy to translate to TASM syntax. Just remember that if it's in square brackets, it refers to a memory location, and if it's not, it's an assembly-time constant, ie an offset in TASM parlance (even labels are offsets, albeit into the code segment). I'll leave the rest (printing, etc) up to you; I'm not going to do everything for you.

As a final suggestion, I'd really advise you to comment your assembly code. It's not like a higher-level language where you can derive a lot of meaning from variable names and other constructs; poorly commented asm is damn near opaque when you come back to it later.

DaWei Oct 28th, 2007 8:36 AM

Re: tasm sorting 5 integers with array
 
My guess is he's trying to accomplish an insertion sort.

lectricpharaoh Oct 28th, 2007 8:43 AM

Re: tasm sorting 5 integers with array
 
Quote:

Originally Posted by DaWei
My guess is he's trying to accomplish an insertion sort.

Actually, I was kinda hoping that, since the array was so small (perhaps below the magical cut-off point), bubble sort would actually be more efficient.

That's my story, and I'm sticking to it. It's got nothing to do with me being a lazy ass, and not wanting to write something marginally more complex in assembly. :)


All times are GMT -5. The time now is 4:45 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC