![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2007
Posts: 6
Rep Power: 0
![]() |
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,
|
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2007
Posts: 6
Rep Power: 0
![]() |
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 26th, 2007 at 11:23 PM. Reason: Added code tags. Read the rules again. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Re: tasm sorting 5 integers with array
Notice here:
check: add bp, 2 mov ax, [bx] cmp ax, [si] jg swap 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 |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Oct 2007
Posts: 6
Rep Power: 0
![]() |
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 1:10 AM. Reason: Added code tags. Try to get the point. |
|
|
|
|
|
#6 |
|
Programmer
Join Date: May 2006
Location: The US duhhhhh!
Posts: 42
Rep Power: 0
![]() |
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 2:55 AM. Reason: I am completely stupid |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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:
__________________
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 |
|
|
|
|
|
#8 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,033
Rep Power: 5
![]() |
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 itemssort:
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: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 |
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#10 | |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,033
Rep Power: 5
![]() |
Re: tasm sorting 5 integers with array
Quote:
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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| sorting an array by field | cwl157 | C | 4 | Apr 4th, 2007 2:48 PM |
| Sorting an array of objects | oNe8 | Java | 2 | Feb 22nd, 2006 10:59 PM |
| Sorting without a large array | sim_maroon | C | 12 | Nov 21st, 2005 8:45 PM |
| Sorting a Numeric Array | little_valaree | Java | 2 | Nov 21st, 2005 11:00 AM |
| Installing IPB 2.03 | bh4575 | Other Web Development Languages | 0 | Apr 23rd, 2005 2:36 AM |