Programming skill tester - killing myself!

...let us know what you think, free speech!
Post Reply
User avatar
floodhound2
∑lectronic counselor
∑lectronic counselor
Posts: 2117
Joined: 03 Sep 2006, 16:00
17
Location: 127.0.0.1
Contact:

Programming skill tester - killing myself!

Post by floodhound2 »

Man this is a hard one for me to program. Perhaps I am tired so before you snarl at me keep in mind I am programming in a low level language. No objects here, No floating point math etc.

I am trying to program the "mean". Simple eh' well my code works till I get this small freaking road block...

Take 10 numbers that ranges from 0-10. Then make a program arrange the numbers from small to large. Its a mind game perhaps you can help.

All the operators I have are <, >, =, IF, CASE, NOT. This sucks!

Also I have to keep the loop as small as possible meaning, least amount of stack space and clock cycles.

Code: Select all

Before
1,2,1,9,4,4,0,6,1

After
0,1,1,1,2,4,4,6,9
Help!
₣£ΘΘĐĦΘŮŇĐ

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
18
Location: In your eye floaters.
Contact:

Re: Programming skill tester - killing myself!

Post by bad_brain »

hm, no idea if this is a valid option for you, but that's how I would do it in Perl:

Code: Select all

#!/usr/bin/perl
my @numbers = (1,2,1,9,4,4,0,6,1);
my @sorted = sort {$a <=> $b} @numbers;
print join "\n",@sorted;
Image

User avatar
ph0bYx
Staff Member
Staff Member
Posts: 2039
Joined: 22 Sep 2008, 16:00
15
Contact:

Re: Programming skill tester - killing myself!

Post by ph0bYx »

Could you post the code that you've been working on with this? Just to see how those commands are suppose to work (me doesn't know low level programming :oops: )

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Re: Programming skill tester - killing myself!

Post by Gogeta70 »

Can you allocate ten bytes on the stack and have a pointer to byte 0?
If so, try something like this:

This is c++, but you should be able to do the same in assembly. This is just to show the concept.

Code: Select all

#include <cstdio>


int main(int argc, char* argv[])
{
	unsigned char range[10] = {2, 5, 4, 0, 3, 9, 8, 1, 6, 7};
	unsigned char stack[10] = {0};
	
	for(unsigned short i = 0; i < 10; i++)
	{
		*(stack+((unsigned int)range[i])) = range[i];
	}
	
	for(unsigned short i = 0; i < 10; i++)
	{
		printf("%u ", range[i]);
	}
	
	printf("\n");
	
	for(unsigned short i = 0; i < 10; i++)
	{
		printf("%u ", stack[i]);
	}
	
	printf("\n");
	
}

Output:
2 5 4 0 3 9 8 1 6 7
0 1 2 3 4 5 6 7 8 9
The idea is to have a pointer to your array on the stack. Then, you add the value of the range[x] to the pointer address and assign the value.

Basically:

*(pointer+range[0]) = range[0]

In asm, i think it would be something like this:

eax = pointer to your array that you allocated on the stack
ebx = pointer to the array of your range of values

:loopstart (not real asm, i know lol)
mov ecx, [ebx] (copy the value of range[x] to ecx)
mov [eax+ecx], ecx
inc ebx
goto loopstart

Anyway, i think i got my point across.
¯\_(ツ)_/¯ It works on my machine...

User avatar
floodhound2
∑lectronic counselor
∑lectronic counselor
Posts: 2117
Joined: 03 Sep 2006, 16:00
17
Location: 127.0.0.1
Contact:

Re: Programming skill tester - killing myself!

Post by floodhound2 »

Thanks for the help - I got it working.

This you tube video clears up very nicely

" onclick="window.open(this.href);return false;
₣£ΘΘĐĦΘŮŇĐ

User avatar
Lundis
Distorter of Reality
Distorter of Reality
Posts: 543
Joined: 22 Aug 2008, 16:00
15
Location: Deadlock of Awesome
Contact:

Re: Programming skill tester - killing myself!

Post by Lundis »

Gog, that's a clever solution, but as far as I can see it only works if the array consists of 10 different consecutive numbers. If I'm wrong, you'll be known throughout history as the guy who invented the O(n) sorting algorithm Gogsort. haha :D

Post Reply