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.
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
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.
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.