Why the hell is this so hard?

Ok so I've been given a fairly basic assignment. For some reason, I can't get my head around it right now, and it's driving me nuts.

It's another assembly assignment, read in 3 numbers, print out the numbers, sort from highest to lowest, and then print out numbers in that order.

For some reason, this is impossible to me. I can't do any compound comparisons, and I think that's what's killing me. I am beyond frustrated from this.
The trick is to read the numbers into an array of size 3. Then you sort the array before displaying it.

Are you familiar with arrays?
Yea I've done plenty with arrays lol. I actually have multiple sorting algorithms laying around in C++, but they don't work so nicely in assembly. I could probably make an array in this assignment, but it would add a lot more work and I'm not exactly sure how to work with arrays at machine level
It's a block of memory that you index thru in much the same way you do in C. You will find that C is very close to assembly language.
http://www.cse.yorku.ca/~jeff/notes/compiler/Marie/DocumentationFromWeb/2.2.pdf

This is what I'm dealing with. Not exactly an ideal ISA
To sort 3 elements, you don't need to write a complete sorting algorithm.
Assume a function sort(a, b) that compares the numbers in addresses a and b and then swaps them if value in a > value in b.
Then to sort 3 values in addresses 1, 2, 3, do
sort(1, 2)
sort(2, 3)
sort(1, 2)
That is, sorting 3 number is just as hard as sorting 2.

I see that instruction set is a bit restricted. Having only one register and no xchg makes things harder. You can still use a fourth address for the swap. There is also a trick with + to swap a and b
a = a + b;
b = a - b;
a = a - b;
Although this would be just more work than using a temporary.
Checking three times was actually my original idea, but it seemed to be unnecessary. I was trying to get it done in only two checks, but I now think that's impossible. I'll probably do it that way, using a swap to get it done. Granted, I'm not sure how to do a function in an assembly, but I can figure that out.

Could you explain the addition trick? I think I can see it, but not sure if I have it right it my head.
Heres my attempt at hamsterman's +/- swap, seems to work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
org 100
clear

load a
output a
load b
output b
load c
output c

start, load a
subt b
skipcond 800
jump swapab
load b
subt c
skipcond 800
jump swapbc
jump print

swapab, load a
add b
store a
load a
subt b
store b
load a
subt b
store a
jump start

swapbc, load b
add c
store b
load b
subt c
store c
load b
subt c
store b
jump start

print, load a
output a
load b
output b
load c
output c
halt

a, DEC 6
b, DEC 3
c, DEC 7
Granted, I'm not sure how to do a function in an assembly, but I can figure that out.
With that set any kind of function would be a pain (you'd have to make a stack of your own, I guess). I was expecting you to just copy the same code three times.

Could you explain the addition trick?
The point is that if you have any two of the values "a+b", "a", "b", then you can get the third. I'm not sure what to explain. Writing down the values after each line could help.
Topic archived. No new replies allowed.