[ot] need help FAST: 680x0 asembler

Allan Herriman allan.herriman at fujitsu.com.au
Thu Jan 20 09:00:20 EST 2000

At 12:31 20/01/00 +0100, you wrote:
>my computer architecture exam is comming nearer every day.
>I'm looking for a printable-usefull reference for the motorola 680x0
>The sylabus is a mess.
>Thanx a bunch.
>PS: what the f*ck does 'link' do
>PPS: pardon my language...

To continue on from my previous post about what 'link' does, here is an
example of stack frame usage.  (This is copied from the output of a real C
compiler (and edited a lot).)

_function:		; label to call (C code is "function();")
	link	a6,#-48 ; reserve 48 bytes for local variables on stack
			; (48 is just the number needed by this function.
			; sometimes this will even be 0 (?).)
			; a6 is the base pointer (I'm sure there's a
			; better name for it.)
	movem.l	d2/d3/d4/d5/d6/d7/a2/a3/a4/a5,-(a7)
			; save all these registers on the stack
			; as this function uses them, and by convention
			; this compiler needs them to not be changed
			; by the time we return
			; (a7 is the user stack pointer)

	move.l	8(a6),a5	; example of getting a parameter from the stack
			; in this case it is a pointer, so it goes into 
			; an address register

	move.l	d3,-8(a6)	; example of moving register d3 to a
				; local variable

	; lots of other instructions deleted

	; example of calling another function
	move.l	-4(a6),-(a7)	; push a local variable onto the stack
				; as a parameter for other_function we
				; are about to call
	jbsr _other_function	; call other_function();
					; jbsr is one of those psuedo-ops.
					; the assembler will decide which 
					; "real" instruction to use 
					; (either bsr or jsr) and
					; substitute it later...
					; The C compiler doesn't care
					; which one is used.
	addq.l	#4,a7		; clean up the stack

	; lots of other instructions deleted

	; now return from this function
	movem.l	-88(a6),d2/d3/d4/d5/d6/d7/a2/a3/a4/a5
			; restore all the registers we used
	unlk	a6	; unlink the stack (put a6 in sp, and pop a6 from stack)
	rts		; return to the calling function
			; (pop return address from stack into pc)

And here is a stack frame...
(Remember the stack grows towards low memory.  Sometimes these are drawn
the other way, with low memory at the top of the page.  Try not to get

high addresses
|                |
| parent function|
| local variables|
|                |
|params for      |
|function        |
|return addr     |<- SP at beginning of _function points here
|old a6          |<- a6 contains this address after link
|                |
|  x bytes       |
|  of local      |
|  variables     |
| belonging      |
|to function     |
|                |
|                |<- SP after link
|param for other_function|
|return addr     |
|a6 pointing to previous a6 location (above)|
|local variables |
|                |
low addresses

The important thing is that the a6 values stored in the nested stack frames
form a linked list.  The current value in the register a6 points to a
location in the stack that holds the a6 value for the calling function,
which points to a location in the stack which holds the a6 value for the
function which called the function which called the current function, and
so on.

Note: a6 is just the register chosen by the writer of the compiler.  Any
address register could have been used (as long as it was the same one all
the time).  The stack pointer *must* be a7, as this is defined by the 68k

I don't really know how the pascal calling sequence works.  Can anyone help?

Allan Herriman                        Senior Design Engineer
mailto:allan.herriman at fujitsu.com.au  Fujitsu Australia
Tel: (+61 3) 9845 4341                5 Lakeside Drive, Burwood East
Fax: (+61 3) 9845 4572                Victoria, 3151, AUSTRALIA

dupswapdrop -- the music-dsp mailing list and website: subscription info,
FAQ, source code archive, list archive, book reviews, dsp links

More information about the music-dsp mailing list