-
March 1st, 2003, 06:19 PM
#1
Junior Member
Question in Assembly Language
Hello, everyone
I am fairly new to this site, but i know you guys really know your stuff. I am a second year computer science major and i am currently in Assembly Language. My questions is: How can i find the median of a list of numbers? I am trying to use a SHR or SHL....
If any of you could help, i would much appreciate it.
Thank you
TL
-
March 1st, 2003, 07:21 PM
#2
Can you explain much what you mean by the median of a list of numbers.
What I understand is that you have for example : 1,3,4,5,7 so the median is 4. I think the numbers are store in an array. You just have to count how numbers you have and divide by 2 to have the position of the median number (the list have to be sort).
The SHR and SHL just Shift a number (in binary), not a list of number. SHR for Shift Right and SHL for Shift Left. if you do shr ax, 2 and [ax]=10 ( 1010 in binary) then the result is 2 (0010). It usefull to do multiplie or divide by a power of 2 (it's quicker than the mul or div)
-
March 1st, 2003, 07:54 PM
#3
Member
You should indeed indicate what exactly you're trying to do here.
I believe indeed the median of a list is the center number, or the average value.
What kind of list are you using?
Right on Ghostdog, he ain't gonna get it done using SHR and SHL.
Grtz.
-
March 1st, 2003, 10:06 PM
#4
Member
so you'll probably start off with an array of numbers. generally in exercise like this you are either given the size or the array will be terminated with some kind of null character. here's some dumb pseudocode:
1) find length L of array a[] (either given or count until you reach terminating character)
2) sort a[] (probably easiest just to use bubble sort)
3) use SHR to divide array size L by 2
4) a[L/2] is the median
the hardest, and most essential part of the assignment is to sort the array, unless of course it's already sorted. you can probably find some reference code somewhere if you get stuck.
-
March 1st, 2003, 10:29 PM
#5
greetings, what language are you trying to assemble? 8086, 68K etc.
Code:
equ PROGRAM $2000
equ DATA $6000
org PROGRAM
lea ARRAY,A0
move.W #0,d0 //d0 == counter
move.W #0,d1 //d1 == stores element of the array
move.W #0,d2 //d2 == running total
loop: move.W (A0)+,d1
cmp #0,d1
beq DONE
add.W d1,d2
add.W #1,d0
bra loop
DONE divs d2,d0
move.W d0,RESULT
org DATA
ARRAY DC.W 1,6,3,5,7,8,9,4,6,5,0
RESULT DS.W 1
this might work, but i haven't tested it 'cause i think you are doing 8086.
Hmm...theres something a little peculiar here. Oh i see what it is! the sentence is talking about itself! do you see that? what do you mean? sentences can\'t talk! No, but they REFER to things, and this one refers directly-unambigeously-unmistakably-to the very sentence which it is!
-
March 2nd, 2003, 04:28 AM
#6
Junior Member
I am sorry for not specifying which assembly i am using... It is 80x86 Masm 615... I am very new to ASM and I appreciate your replies to my post. I indeed believe that moby pinpointed what i am trying to do. It was just an idea, but i am going to give it a shot. I am a firm believer of efficient code and I do, however, have one more thing to ask... and that is whether or not i can fine tune this block of code:
sort PROC
push count
mov ecx, count
mov esi, offset array
mov edi, offset array
add edi, 4
L8:
push ecx
mov esi, offset array
mov edi, offset array
add edi, 4
L3:
mov eax,[esi]
mov ebx,[edi]
cmp eax,[edi]
ja next
mov [edi], eax
mov [esi], ebx
next:
add esi, 4
add edi, 4
loop L3
pop ecx
loop L8
mov ecx, count
mov esi, offset array
T:
mov eax, [esi]
call WriteInt
add esi, 4
call Crlf
loop T
;average
mov esi, offset array
mov ecx, count
mov eax, 0
mov edx, 0
L4:
add eax, [esi]
add esi, 4
loop L4
mov ebx, count
idiv ebx
mov edx, offset message3
call WriteString
call WriteInt
call Crlf
;median
mov esi, offset array
mov eax, count
and count, 1h
mov edx, 0
mov ebx, 2
idiv ebx
jz a
inc eax
mov ecx, eax
jmp here
a:
mov ecx, eax
here:
L5:
mov eax, [esi]
add esi, 4
loop L5
mov edx, offset message4
call WriteString
call WriteInt
call Crlf
pop count
ret
sort ENDP
I am simply asking if anyone has any tips that might make my code more efficient... I am working on fine tuning the median section as we "speak." i promise this is the last thing i'll bug you guys for... thanks for your help...
TL
woah i didnt mean to post all that... im just workin on maybe trying something else for the actual sort routine
i really needed to document that
you guys must think i am a total idiot
-
March 4th, 2003, 01:20 PM
#7
Junior Member
There's a very good book about asembly language (using MASM), called The Art of Assembly (programming?)
You can learn a lot of things from it...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|