Division via subtraction. In assembly!

OK, so this is kicking my ass, and I don't even know why. As the title says, I need to do some division using only subtraction. And it's in this ridiculous assembly language called MARIE. Somehow, it gets stuck in an infinite loop. I ran through this by hand with a few sets of values and I saw no problem. So maybe more eyes might catch some issue I'm not seeing. Here's the segment that does the division.

BTW, this is meant to find an average. Tmp holds the initial sum, and nums is the number of numbers. So basically it is doing tmp/nums.

AC = Accumulator

1
2
3
4
5
6
7
8
9
10
11
Lp, Load tmp
Subt nums
Store tmp
Load avg
Add one
Store avg
Load tmp     //Next three lines are a condition. Should be if tmp - nums < 0, 
Subt nums   //then skip an instruction. I dont see why it doesnt work
Skipcond 000 //If AC < 0, skip next instruction
Jump Lp      //Never gets past this
...
Last edited on
What is the full code - I just want to run it and see.
On just a quick glance (won't have time to test untill later) you are subtracting twice before the condition check. also you should probably be checking if <= 0 and not just <0
Seems the easiest way to deal with the conditions is to check if tmp is greater then zero. Spoiler alert, the code below works for the few inputs I tested and I assume your looking for integer division.









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
Org 100
clear
Lp, load tmp
Subt nums
Store tmp
Load avg
Add one
Store avg
Load tmp     
Skipcond 800 //rather then checking if temp 0 or <0, test if tmp > 0
Jump check
Jump Lp

check, load tmp// if temp is negative we need to sub 1 from avg
skipcond 400
jump fix
jump End

End, load avg
output 
halt

fix, load avg//
subt one
store avg
jump End

one, dec 1
tmp, dec 50
nums, dec 5
avg, dec 0
Ooh wow that makes sense! And looks much simple than the mess I had. And yes, only have to worry about whole ints. It's not a requirement for the assignment, but I was trying to figure out how to store the remainder. I couldn't quite figure it out though. I figured it would be something like
If tmp - sum < 0 then remainder = tmp?
I haven't tested this much but it looks like adding nums to tmp in the fix block
1
2
3
4
5
6
7
fix, load avg
subt one
store avg
load tmp 
add nums
store rem
jump End

works for a remainder.
Wooow ok so after about 3 hours of banging my head against this problem, I found out I made a common but annoying mistake. I had nums as this

 
Nums, Dec 0


Meaning, Nums was set to equal 0. And so the reason for my infinite loop :/
And I wish to thank you. This helped me a lot! And I actually understand how to write these programs a little better now
And to guestgulkan, here is the working code. As far as I know, there aren't any bugs. I tested a bunch of values.

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
Org 100
Input
Store A
Input
Store B
Input
Store C
Input
Store D
Input
Store E
Add A
Add B
Add C
Add D
Store Tmp
Store Sum
Clear
Lp, Load Tmp
Subt Nums
Store Tmp
Load Avg
Add c1
Store Avg
Clear
Load Tmp
Skipcond 800
Jump chck
Jump Lp
chck, Load Tmp
Skipcond 400
Jump fix
Jump End
fix, Load Avg
Subt c1
Store Avg
Load Tmp
Add Nums
Store Rem
End, Clear
Load A
Output
Load B
Output
Load C
Output
Load D
Output
Load E
Output
Load Sum
Output
Load Avg
Output
Load Rem
Skipcond 400
Output
Halt

A, Dec 0
B, Dec 0
C, Dec 0
D, Dec 0
E, Dec 0
Sum, Dec 0
Avg, Dec 0
Nums, Dec 5
Rem, Dec 0
Tmp, Dec 0
c1, Dec 1
Topic archived. No new replies allowed.