# 68K assembly problem

Discussion in 'Programmer's Corner' started by frogacult, Apr 30, 2008.

1. ### frogacult Thread Starter Member

Apr 30, 2008
27
0
hello there, i study Industrial Informatics and i am begginer in programming with assembly languange. I want make a program wich calculates the sum of 256 numbers starting from 00 - FF . My problem is wich subroutine i have to use when the sum is over 256 wich means that i have to to add the Carry with the sum (i think) . I calculated the sum from 00 - FF and it is 32896 (dec) or 8080 (hex). I calculated also that the sum gets beyond 256 at number 23 so i thought to make a loop for the sum of the first 23 numbers (NOCARRY) and then a second loop from 24 - FF (CARRY)

From the code i wrote below the sum is 805D (hex) or 32861 (dec)

ORG \$400400
SUM DS.L 1
ORG \$400410
MOVE.W #221,D0
MOVE.B #35,D4
LEA \$400700,A0
NOCARRY MOVE.B (A0)+,D1
SUBQ.B #1,D4
BNE NOCARRY
CLR.L D1
CLR.L D2
CARRY MOVE.B (A0)+,D1
MOVE.B #1,D2
SUBQ #1,D0
BNE CARRY
MOVE.L D3,SUM
END \$400410

the microprocessor that i'm working with is Motorola 68000
If anyone knows something, it would be very helpfull to me

2. ### Mark44 Well-Known Member

Nov 26, 2007
626
1
I have some questions:
1. Why don't you have any comments? It would be very helpful to me (and to you!) if you at least explained in comments what each register is being used for. You don't need comments on every line, but you should have them on most of your lines--they will help you understand what your code is doing when you look at it a few days or weeks or months after you have written it.
2. Why do you think you need to worry about a carry? You have 32-bit registers to work with, which can hold values up to FFFFFFFF, which is about 4.3 billion (4.3 x 10 ^9). They're not going to have any trouble holding 32896.
3. In your routine, you are using five data registers and a memory location (SUM) in the data segment. I think you can do this with two data registers and the memory location.
7. Why are you loading (via LEA) the address \$400700 into A0? Your program is written as if there were some data there, but the code you show didn't put any there, so reading it will give you garbage results.

Well, those are some things to think about, and should help you understand that you have a lot of stuff in your program that doesn't need to be there, and a few things (comments) that aren't there that should be.

See what you can come up with and I'll take another look.
Mark

3. ### frogacult Thread Starter Member

Apr 30, 2008
27
0
sorry for the mistakes but i said i am beggined you have right for the comments they would be very helpfull.
i asked for the carry because i read in the teachers book that when we talk for non pre-assignement arithmetic (sorry but i cant translate it right in english)
one byte can take values from 0 - 255 (dec) , a word 0 - 4294967295 (dec) and longword 0 - 4294967295 (dec) . In pre-assigment arithmetic the most significant byte presents the signal of the number ("0" is possitive and "1" is negative) and one byte can take values from (-128) - (+127) etc...
I also used the register D3 so i can watch the sum when i run the program with easy68k because if i send the registers directly to the sum i cant watch the value of the sum when i run the program step by step ( or i dont know how) Also the programs we write have that end with the address we begin. Every program in this book said by the teacher also and it works.
The reason i loaded 221 and 35 i have explained beyond

"""Originally Posted by frogacult
I calculated also that the sum gets beyond 256 at number 23 so i thought to make a loop for the sum of the first 23 numbers (NOCARRY) and then a second loop from 24 - FF (CARRY) """

D1 takes tha values from 256 numbers, D2 takes the carry byte, D3 takes the values from D1 and D2 show i cant watch the sum when i run the program step by step and D4 takes the value of 35 wich are the repeats to sum the first 23 numbers and D0 takes the values of the repeats for the sum of the next 221 numbers (D0 +D4 = 256 numbers)
also the lea code is needed because i have saved before the numbers 00 - FF in the address \$400700 - 4007FF and i have to set a pointer which shows this address and should look like this (A0)+,D1 so i can take the next value it's time I havent putted all the code but only the section of the sum.

i am trying a lot of days to make it but my final sum isn't right.
i hope i replied to your questions so you can help me more.
i will put some comments in the code so you can understand easier what i am trying to do.
thanx

4. ### frogacult Thread Starter Member

Apr 30, 2008
27
0
ORG \$400400
SUM DS.L 1 //// sum
ORG \$400410
MOVE.W #221,D0 // put the number of repeats for numbers that we have carry
MOVE.B #35,D4 //put the number of the first 23 number tha the dont have carry
LEA \$400700,A0 // A0 shows the address \$400700 wich has the first number
NOCARRY MOVE.B (A0)+,D1 //put the 1st number do and incresing A0 by one
SUBQ.B #1,D4 // until all the reapets are done
BNE NOCARRY
CLR.L D1
CLR.L D2
CARRY MOVE.B (A0)+,D1 // put D1 in A0 wich points the address \$400723
MOVE.B #1,D2 // this is the Carry byte
SUBQ #1,D0 // until all numbers are sumed
BNE CARRY
MOVE.L D3,SUM // Take the value of D3 and put it in Sum
END \$400410

5. ### Mark44 Well-Known Member

Nov 26, 2007
626
1
An unsigned word, assuming it's 16 bits, can take any integer value from 0 to 65535. The range you gave for a byte is for an unsigned byte, and the range you gave for a longword (also called a dword--for double word) is for an unsigned longword.

The sign of the number, not its signal. To clarify the other part, a signed byte has a range of -128 to +127; an unsigned byte's range is 0 to 255. As mentioned before, the ranges of signed words and signed longwords are different from those of unsigned words and unsigned longwords.

And the usual practice is to use only the most significant bit for the sign, not the whole byte.

OK, lolo, thanks for taking the time to answer my questions. Based on your original post, you want to add the numbers 1 through 256. You did that manually and came up with the right answer, so you're not dealing with negative numbers.

The largest number you need to work with won't fit in a byte, which means you will need to store them in words, not bytes. You'll need to modify the code you have that stores numbers starting at \$400700 to use MOVE.W instead of MOVE.B. Since your numbers are nonnegative, you don't have to worry about carries, so you can do your addition in one loop, using, for example, D0 to hold the accumulated sum and D1 as your loop counter. Just after the branch instruction for your loop you can store the number in D0 in your SUM memory location.

If my suggestions don't satisfy the problem requirements, let me know and we'll keep working on it.
Mark

6. ### Mark44 Well-Known Member

Nov 26, 2007
626
1
Here's the routine I have in mind. I'm not sure what the exact requirements of your problem are, so I have done things in the simplest way I can think of. Instead of getting bytes previously stored at the location starting at \$400700, I am using the loop counter values to add to my temporary sum variable.

Also, my loop is counting down from 256 to 1, so the first number to be added is 256, then 255, and so on down to 1.

If you actually need to read values from a memory location, you can add a line inside the loop to do that, rather than adding the current value of the loop counter. However, when you store the values at the location starting at \$400700, you should store them as words, not bytes, since you won't be able to store 256 as a byte.

Code ( (Unknown Language)):
1.
2.       ORG \$400400
3.       SUM  DS.L 1
4.       ORG \$400410
5.
6.       CLR.L D0              ; temporary sum
7.       CLR.L D1              ; loop counter
8.       MOVE.W #256, D1 ; run loop 256 times
9.
10. ;; The loop adds 256, 255, 254, ..., 3, 2, 1
11. LOOP  ADD.L  D1, D0    ; add the current value to the temp sum
12.       SUBQ.W #1, D1     ; decrement loop counter
13.       BNE LOOP             ; branch to LOOP if D1 != 0
14.
15.       MOVE.L D0, SUM    ; store final value in SUM
16.       END
17.
Mark

7. ### frogacult Thread Starter Member

Apr 30, 2008
27
0
Dude i tried this and it worked!!

Code ( (Unknown Language)):
1.
2.                MOVE.W    #256,D0
3.                LEA       \$400700,A0
4. LOOP3        MOVE.B     (A0)+,D1
6.                SUBQ.B    #1,D0
7.                BNE       LOOP3
8.
Thanx Mark

8. ### Mark44 Well-Known Member

Nov 26, 2007
626
1
Glad to hear it, and glad I could be some help to you!

One thing to watch out for is being consistent with the way you store data and the way you read it. I haven't seen your code that stores values at \$400700 and up. I assuming you're storing them as bytes in a loop with a MOVE instruction something like this:
Code ( (Unknown Language)):
1.
2.     MOVE.B D0, (A0)+
3.
When the value in D0 (or whatever register you use) is 256 or larger, it's too big to fit in a byte, either signed or unsigned. Make sure that you use MOVE and other instructions that have .B, .W, or .L suffixes with some understanding of exactly what is getting moved, and use the ones that will work correctly with the data you need to move, add, subtract, and so on.
Mark

9. ### frogacult Thread Starter Member

Apr 30, 2008
27
0
yeap i mentioned that , the rest code is good but now i have some new problems. if u check i created a new thread i dont think it's something difficult but i cant find it out