Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is this piece of ASM for addition correct ?

Status
Not open for further replies.

plenderj

Programmer
Sep 7, 2001
5
0
0
IE
I'm trying to write the M68K ASM for the following piece of java code :

Code:
((a + b) + (c + d)) + ((e + f) + (g + h))

And have come up with :

Code:
         ORG     $2000
A        DC.L    $DEADBEEF
B        DC.L    $DEADBEEF
C        DC.L    $DEADBEEF
D        DC.L    $DEADBEEF
E        DC.L    $DEADBEEF
F        DC.L    $DEADBEEF
G        DC.L    $DEADBEEF
H        DC.L    $DEADBEEF
PART1    DC.L    0
PART2    DC.L    0
PART3    DC.L    0
PART4    DC.L    0
         ORG     $400
         MOVE.L  A,D0
         MOVE.L  B,D1
         ADD.L   D0,D1
         MOVE.L  D0,PART1
         MOVE.L  C,D0
         MOVE.L  D,D1
         ADD.L   D0,D1
         MOVE.L  D0,PART2
         MOVE.L  E,D0
         MOVE.L  F,D1
         ADD.L   D0,D1
         MOVE.L  D0,PART3
         MOVE.L  G,D0
         MOVE.L  H,D1
         ADD.L   D0,D1
         MOVE.L  D0,PART4
         MOVE.L  PART1,D0
         MOVE.L  PART2,D1
         MOVE.L  PART3,D2
         MOVE.L  PART4,D3
         ADD.L   D0,D1
         ADD.L   D0,D2
         ADD.L   D0,D3
         END

Is this correct, and does anyone know of a better approach ?
 
It APPEARS correct, although I think you should now most of us here use Intel microprocessors...

It also appears rather inefficient. You're using a Motorola 680x0 or compatible, right? I think it has some 8 registers, D0 to D7. And it's load-store architecture, the type where only the MOVE instructions can refer to memory. Data movement in instructions is from left to right, not right to left as in Intel uP's.

First approach is to "collapse" the expression to its simplest form using algebra. The equation above is very equivalent to:

a + b + c + d + e + f + g + h

So why not do something like this:
move.l a,d0
move.l b,d1
add.l d0,d1
move.l c,d1
add.l d0,d1
move.l d,d1
add.l d0,d1
move.l e,d1
add.l d0,d1
move.l f,d1
add.l d0,d1
move.l g,d1
add.l d0,d1
move.l h,d1
add.l d0,d1

The code above has 7 additions and 8 moves. Your code has 7 additions and 16 moves.

"Information has a tendency to be free. Which means someone will always tell you something you don't want to know."
 
Hmm thats a good approach.
I assume you'd have to declare a to h first though aswell ?

.. forgive my lack of assembly knowledge, I'm only just learning :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top