Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 26th, 2007, 11:32 PM   #1
akioshin
Newbie
 
akioshin's Avatar
 
Join Date: Oct 2007
Posts: 6
Rep Power: 0 akioshin is on a distinguished road
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,
akioshin is offline   Reply With Quote
Old Oct 26th, 2007, 11:59 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 27th, 2007, 12:22 AM   #3
akioshin
Newbie
 
akioshin's Avatar
 
Join Date: Oct 2007
Posts: 6
Rep Power: 0 akioshin is on a distinguished road
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

Last edited by DaWei; Oct 27th, 2007 at 12:23 AM. Reason: Added code tags. Read the rules again.
akioshin is offline   Reply With Quote
Old Oct 27th, 2007, 12:32 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 27th, 2007, 2:08 AM   #5
akioshin
Newbie
 
akioshin's Avatar
 
Join Date: Oct 2007
Posts: 6
Rep Power: 0 akioshin is on a distinguished road
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

Last edited by DaWei; Oct 27th, 2007 at 2:10 AM. Reason: Added code tags. Try to get the point.
akioshin is offline   Reply With Quote
Old Oct 27th, 2007, 3:48 AM   #6
RobEasy
Programmer
 
RobEasy's Avatar
 
Join Date: May 2006
Location: The US duhhhhh!
Posts: 42
Rep Power: 0 RobEasy is on a distinguished road
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.
__________________
Work Hard... Play Harder!

Last edited by RobEasy; Oct 27th, 2007 at 3:55 AM. Reason: I am completely stupid
RobEasy is offline   Reply With Quote
Old Oct 27th, 2007, 5:06 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 28th, 2007, 8:39 AM   #8
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,119
Rep Power: 5 lectricpharaoh will become famous soon enough
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.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Oct 28th, 2007, 9:36 AM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: tasm sorting 5 integers with array

My guess is he's trying to accomplish an insertion sort.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 28th, 2007, 9:43 AM   #10
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,119
Rep Power: 5 lectricpharaoh will become famous soon enough
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.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting an array by field cwl157 C 4 Apr 4th, 2007 3:48 PM
Sorting an array of objects oNe8 Java 2 Feb 22nd, 2006 11:59 PM
Sorting without a large array sim_maroon C 12 Nov 21st, 2005 9:45 PM
Sorting a Numeric Array little_valaree Java 2 Nov 21st, 2005 12:00 PM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 3:36 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:47 AM.

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