Re: LF: Once more: prog stuff


[Prev][Next][Index][Thread]

Re: LF: Once more: prog stuff



~

On Fri, 20 Jun 1997 Hmlacage@aol.com wrote:

> I finally made a prog wich works but i don' t understand why: i thought that
> : 
> "pea    ess(pc)" is an instruction which does: "ess adress" + pc --->
> stack...
> This seems to be wrong: pea is 3 lines before the move a1,-(a7) wich pushes
> the param for the puttext sub. Thus, pc is smaller when pea is executed than
> when the adress is pushed for in param. Thus, the adress given to puttext is
> diff from that which was given by the very simple hello world prog in last
> message. However, it works... Thus, i am wrong...
> Could someone please explain me exactly what is PC, what is his value and
> what do "pea ess(PC)" do...
> ( see end for suite of this message ) 

Suite of this message? French?

First, PC is the Program Counter.  It tells the processor where in memory
the next instruction is.

Since Fargo programs are moved around in memory be the operating system,
the assembler can never know exactly where in memory a given datum
is.  But it is easy for it to figure out just how far ahead of the current
instruction (i.e., 5 bytes ahead of the current instruction, etc.) that a
datum is.  Thus PC-relative addressing, as in ess(PC):  The
assembler counts that number, and tells the processor to add it to the
current PC to get the real, absolute address of the datum.  That way, the
address of the datum can be known even if the program has been moved.

Next, pea ess(PC).  This does what you said, moreover: "address of ess(PC)
--> stack."  Let me expand that, as some of the details of how the stack
works are being hidden:

A7 - 4 --> A7	; pre-decrement the stack pointer (A7)
ess(PC)--> (A7) ; store address of "ess(PC)" in memory location to which
	        ; A7 points.

Now, let's look at the code:

> 
> prog_code:
> 
>     jsr     flib[clr_scr]

Clear the screen.

>     pea     ess(PC)

Push the (Effective) Address of "ess(PC)"

>     move.l  (A7)+,A1

Pop a Long word off of the stack, into the A1 register..

Note, that rather than pushing the address, then popping it off of the
stack, you could just: lea ess(PC),A1 ; Load (Effective) Address into A1

>     bsr     affich

Call the sub. affich.

>     jsr     flib[idle_loop]

Wait for a key.

>     jmp     quit

This shouldn't work! It does, because the assembler is "smart" and instead
assembles the proper instruction: bra quit.

Despite that, you should probably just put "rts" here and be done with it.

>     
> 
> affich:
>     move.w   #4,-(A7)
>     move.l   A1,-(A7)
>     move.w   #10,-(A7)
>     move.w   #10,-(A7)
>     jsr      romlib[puttext]
>     lea      10(A7),A7

Push everything onto the stack (10 bytes...), call puttext.

Now, load the address of something 10 bytes after (A7) into A7.  After a
little thought, the same thing as adding 10 to a7: A7 + 10 --> A7.


> 
> quit:
>     rts

And finally, return from the subroutene....


> 
> 
> ess: dc.b "coucou",0

and, your message.

> 
> 
> suite: btw, answering to shawn: what you said about the sub is that when
> writing "bsr  something" , the processor stores the value of PC in the stack
> and pops it back when writing " rts" . Thus, if i need to use the stack in a
> sub, i need to bring  the stack pointer to exactly the same adress than at
> the begening of the sub ?

Right!  Additionally, you must not overwrite the return address!  Better
never to pop anything off of the stack that you didn't push yourself...
 
> If so, it is not very interesting to use the stack to pass params to a sub:
> 1st, it takes more time than using the registers, 2nd, you need to care to
> the stack pointer at the end of sub...

Well, correct.  Except that there are only 19 registers (D0-D7, A0-A7,
A7', PC, Flags Register)  and you can only really use 15 of them.  You can
generally use much more stack (Supervisor Stack starts out with 300
bytes, User Stack with 8kb).  Also, the use of the stack to pass values is
the method of almost every high-level language including C... The TI-92's
ROM is written in C (We think...And for ROM 2.0+, we know...)

 
> BTw, could someone show me the peice of code which does: 2+5=7( in fact
> decimal add, mult....) 
	
	move.l	#2,d0
	move.l	#5,d1
	add.l	d0,d1	; d0+d1 --> d1.  So, afterwards, d1=7

 
> Sorry for my english...

It's better than many native speakers that I know...

> Mathieu, <hm lacage@aol.com>
> 
> 

Hope this helps.

-- Shawn Walker (swalker@joe.math.uga.edu / swalker@earthling.net)


References: