Bubble sort in assembly

Pages: 12
Ah yes I have the whole book, actually. I do like the book, goes into a lot of stuff I hadn't heard of before. I'm just not too fond of this simulator the author uses.

IR = Instruction Register
Out = Output register
In = Input register
AC = Accumulator (Only thing you have direct control over)
MBR = Memory buffer register
PC = Program counter
MAR = Memory address register
closed account (4z0M4iN6)
Yesterday evening I had a short look at the instruction set in:

MARIE: An Introduction to a Simple Computer

But I only looked up table 4.2, which contained only direct addressing. And I thought, this would become tricky, because you couldn't use your knolledge, which you had learned, but would have to doubt what you learned. Otherwise the problem couldn't be solved.

Or can you imagine, how to make an indirect address access with only an instruction set for a direct address access?

But then I saw, there is an extended instruction set, and all is quite normal.

Nothing complicated.

Maybe you can explain, what's the problem?
How to make a loop, or what else?

You got the algorithm, you got the register description and the instruction set, why don't you code it?

Maybe I should give you a hint.

Think about the meaning of "for" and express it by using "while".
And then you express while by using a jump.

And what's inside the loops is also not complicated. The difference to C is:

What you can express in C in one line, you have to write in more than one lines.

Do you know about jumps? You could become used by learn something about batch programming - a pity you couldn't use arrays.

But now I found something very interesting - an old Commodore 64 basic. It's the ideal thing for getting used to jumps (in Basic GOTO)

http://www.pagetable.com/?p=48

If you could express it in this BASIC [B(eginner's) A(ll-purpose) S(ymbolic) I(nstruction) C(ode)], it requires only a little step more, to express it in assembler.
Last edited on
I dont know if you are still working on this but here is what I was able to come up with. It doesn't seem to be particularly fast but does seem to work (minimally tested). I changed your original code quite a bit to simulate functions, I felt it may be clearer this way.
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
Org 100

JnS ldArr
JnS in
JnS sort
JnS out

Halt

//sort array
sort,   Hex 0
   sl2, JnS ldArr
	Load N
	Subt c1
	Store N
    sl, Load swapped//zero swapped flag
	Subt swapped
	Store swapped	
	LoadI ArrL
	Store temp
	Load ArrL
	Add c1
	Store ArrL
	LoadI ArrL
	Subt temp
	Skipcond 800
	JnS swap
	Load N
	Subt c1
	Store N	
	Skipcond 400//test N
	Jump sl
	Load Size//reset N
	Subt c1
	Store N
	Load swapped//end if nothing swapped
	Skipcond 400
	Jump sl2
	JumpI sort

//swap curent and previous
swap,	Hex 0
	Load swapped
	Add c1
	Store swapped
	LoadI ArrL
	Store temp2
	Load temp	
	StoreI ArrL
	Load ArrL
	Subt c1
	Store ArrL
	Load temp2
	StoreI ArrL
	Load ArrL
	Add c1
	Store ArrL
	JumpI swap

//input N and N values
in,	Hex 0
	Input
	Store Size
	Loop, Store N
	Input
	Output
	StoreI ArrL
	Load ArrL
	Add c1
	Store ArrL
	Load N
	Subt c1
	Skipcond 400
	Jump Loop
	Store ArrL
	load Arr
	jumpI in

//output array
out,	Hex 0
	JnS ldArr	
    ol, LoadI ArrL
	Output
	Load ArrL
	Add c1
	Store ArrL
	Load N
	Subt c1   
	Store N
	Skipcond 400
	Jump ol
	JumpI out

//reset N and ArrL pointer
ldArr,	Hex 0
	Load Size
	Store N
	l, Load Arr
	Load l
	Subt H1000
	Store ArrL
	JumpI ldArr

Size, Dec 5

c1, Dec 1
H1000, Hex 1000

ArrL, Dec 0
N, Dec 0
swapped, Dec 0	//flag if swapped
temp, Hex 0	//temps for swapping
temp2, Hex 0

Arr,	Dec 0
	Dec 0
	Dec 0 
     	Dec 0
	Dec 0
Last edited on
Hey thanks naraku, you seem to be forum specialist in this marie ordeal haha. Sadly, I had to turn in what I had and the class is finished. But I will definitely take a look at this tomorrow when I'm not so tired and pick apart how you did this.

I am curious how functions work in Marie, I could never figure that out. I actually don't know how they work at the machine level in any architecture.
closed account (4z0M4iN6)
@naraku9333

I think it's not I good idea, to post this listing. If you don't post it, ResidentBiscuit can think about it himself and can learn something.
Ok dadabe, what is your issue? You come into every thread and criticize everything. Naraku has been a big help in various issues I've had, whether I learn something or not should be no matter to you. I am greatful the code. The assignment is done, I literally gain nothing but knowledge from this code snippet. So stop being an ignorant ass, and leave my threads.
closed account (4z0M4iN6)
ResidentBiscuit wrote:

Ok dadabe, what is your issue

Sorry ResidentBiscuit and also naraku9333, that maybe I formulated this, that it looked like criticizm. It could also be, that you thought, it would be criticzm, because we had a terrible discussion about one topic, about which you maybe have heard.

I wrote CodeMonkey what was my mistake. But I will not post it.
My goodness I made another mistake, I wanted to send it to CodeMonkey, but I din't look carefully and sent it to you. You shouldn't have known this.

I know, now you are used, that I would criticize all.

What I ment was not criticizm, but a suggestion. But when I look it over, I see it was not well formulated. Maybe better would have been, if I would have written naraku9333, what I thought, instead of posting, because if I think different, then I shall tell naraku9333 and nobody else need to know.

I hope naraku9333 can forgive me and that we come soon to a better understanding each other.

And I hope also, that I will soon become better in doing well.

But I don't know ResidentBiscuit, why you tell, I would criticize all. What I remember about you, I gave you useful information for solving your issue.

If you think, I would have criticized you - I can't remember - then let me know, but don't post it.

It could also be, that I shouldn't post an answer, which I want to give somebody, not in a forum, if it's an answer only for him and for nobody else.

If you like, we also could discuss an answer, which I gave, whether it was a good answer or not a good answer - but please not about one specific topic with many replies.

If you think about criticizm of me, please forget one specific topic. I have learned something of it, whether it's well now or not we will see. Nobody except you complained something or wanted to discuss something and maybe also didn't think I wanted to criticize him.

But it's not critics about you, which you complain, but critics about naraku9333.
How can you complain about some suggestion, which I gave naraku9333. Could be also naraku9333 think's I was right, if he sees what you do.

And if you want to understand my answers:

I can help people, without wasting my time, without wasting their time and especially without any discussions.

Because that's answers, which people understand immediately and which they don't like to discuss.

Or do you think, it would be better, to tell something this way, which I heard so much in this forum: you troll, why did you forget this or that, and many discussions about mistakes, and who is the troll following?

And what's wrong about such answers:

Do you think, you can copy a char array to a string with strcpy?

Nobody will answer yes.
But if he would do this.
I wouldn't answer.
I don't need.
Because you will answer.

But this answer works also the other way around.

Sometimes I could think, this would be wrong - but it isn't.

Then he would answer yes, and explain why.
And then I can think, and maybe he is right.

Nobody said, you are wrong.
And then there isn't any problem.

And ResidentBiscuit you wrote:

I literally gain nothing but knowledge from this code snippet

Yes, maybe I was wrong. I thought, you would gain experience.

And you wrote also:

So stop being an ignorant ass, and leave my threads.

I am not interested in your threads, if you don't gain experience.
Last edited on
@ResidentBiscuit
I posted what I had prematurely, it works for 5 values but whTen I tested 6 and 7 they where only partialy sorted. Ill take another look at it tonite. I'm definately no expert in it, had never heard of MARIE until you posted about it. The JnS/JumpI instruction combination simulates a function call by jumping to the specified location and storing the return address at the labels memory location then returns with JumpI.
Last edited on
Looks like it was just a misplaced label for a loop in sort which resulted in only one pass through the array, tested with seven numbers seems to be working now.
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
Org 100

JnS ldArr
JnS in
JnS sort
JnS out

Halt

//sort array
sort,   Hex 0
   sl2, JnS ldArr
	Load N
	Subt c1
	Store N
	Load swapped//zero swapped flag
	Subt swapped
	Store swapped	
    sl, LoadI ArrL
	Store temp
	Load ArrL
	Add c1
	Store ArrL
	LoadI ArrL
	Subt temp
	Skipcond 800
	JnS swap
	Load N
	Subt c1
	Store N	
	Skipcond 400//test N
	Jump sl
	Load Size//reset N
	Subt c1
	Store N
	Load swapped//end if nothing swapped
	Skipcond 400
	Jump sl2
	JumpI sort

//swap curent and previous
swap,	Hex 0
	Load swapped
	Add c1
	Store swapped
	LoadI ArrL
	Store temp2
	Load temp	
	StoreI ArrL
	Load ArrL
	Subt c1
	Store ArrL
	Load temp2
	StoreI ArrL
	Load ArrL
	Add c1
	Store ArrL
	JumpI swap

//input N and N values
in,	Hex 0
	Input
	Store Size
	Loop, Store N
	Input
	Output
	StoreI ArrL
	Load ArrL
	Add c1
	Store ArrL
	Load N
	Subt c1
	Skipcond 400
	Jump Loop
	Store ArrL
	load Arr
	jumpI in

//output array
out,	Hex 0
	JnS ldArr	
    ol, LoadI ArrL
	Output
	Load ArrL
	Add c1
	Store ArrL
	Load N
	Subt c1   
	Store N
	Skipcond 400
	Jump ol
	JumpI out

//reset N and ArrL pointer
ldArr,	Hex 0
	Load Size
	Store N
	l, Load Arr
	Load l
	Subt H1000
	Store ArrL
	JumpI ldArr

Size, Dec 5

c1, Dec 1
H1000, Hex 1000

ArrL, Dec 0
N, Dec 0
swapped, Dec 0	//flag if swapped
temp, Hex 0	//temps for swapping
temp2, Hex 0

Arr,	Dec 0
	Dec 0
	Dec 0
     	Dec 0
	Dec 0
I like how this looks, I still don't understand functions in this though.
In C++ (and most languages I assume) when you call a function the return address (address of where function call executed) is pushed onto the stack and poped when funtion exits. That is what the JnS instruction does. Notice where ldArr is declared, it appears the same as a variable declaration (Hex 0) so when JnS ldArr is executed the address of that line is stored in ldArr's memory, this is the push on the stack. When JumpI ldArr is executed it jumps to the address stored in ldArr, this is the pop from the stack.
Well, how would you handle parameters in that scenario?
Good question, I'm not sure to be honest. Ill take a look tonight and see if I can figure it out.
A possible implementation
You push the parameters on the stack.
So you keep a 'base pointer' to were you stored the return address. One your function exits, you empty the stack (until the base pointer), recover the return address and the next value for the base pointer.
param_n
...
param_0
previous_base_pointer  (our base pointer points here)
return_address
1
2
3
4
5
function_exit:
top = base; //empty the stack
base = *top; //get the base for the previous function
return_adress = *(top+1);
top += 2;
Last edited on
Topic archived. No new replies allowed.
Pages: 12