EASy68K  
It is currently Tue Nov 21, 2017 9:24 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Wed May 07, 2014 11:29 pm 
Offline

Joined: Wed May 07, 2014 11:16 pm
Posts: 5
Hey, so I'm currently running through the tutorial's on EASy68K, and I'm having trouble wrapping my head around the OFFSET directive. The example of code that's troubling is written below (I included more than necessary, just read the offset table).

Code:
*------------------------------------
* Add two numbers passed as parameters on the stack.
* The sum is returned on the stack.
* The OFFSET directive is used to access the parameters by name.
* 4 bytes for A0 saved by link, 4 bytes for return address
        offset  4+4
sum     ds.l    1
num2    ds.l    1
num1    ds.l    1
        org     *           end the offset

Adder2
    link    A0,#0           create stack frame
    move.l  D0,-(SP)        save D0
    move.l  (num1,A0),D0    access num1 parameter
    add.l   (num2,A0),D0    add num2 parameter
    move.l  D0,(sum,A0)     store result in sum parameter
    move.l  (SP)+,D0        restore D0
    unlk    A0
    rts                     return


My questions is this: when what the heck is the number to the right of OFFSET doing. In this example given, the OFFSET is four. However in the example given in the programs help, they use -3*4, and when they create the stack frame they also use -3*4 to allocate extra space in A0 for the stack.

I understand how the stack pointer is working and why the stack frame (LINK) command is necessary, but what determines the value next to OFFSET. Any help figuring out what OFFSET is actually doing can be useful. Thanks, -MOOSE.

EDIT-----------------------------------------------------------------------------------------

Okay I've furthered my understanding of the issue a little bit after taking a look through some of the compiled code. What I've gathered is that OFFSET is defining relative distances for a stack pointer. So in the example given, the first long was at 8, the next at 12 (8 + 4 for the last long), and the last at 16. And the reason 8 was give is because LINK moves SP forward 8 always.

I'm still confused though about the number next to LINK, the #n which according to the quick reference should be negative to allocate extra space in the local stack. What does #n do and how should #n in LINK be related to OFFSET. Why isn't the number next to OFFSET always 8? If OFFSET used for other purposes than defining variables in a stack?

Thanks for the help if you've actually read this far into my questions!


Last edited by skiingxmoose on Mon May 12, 2014 11:08 pm, edited 2 times in total.

Top
 Profile  
 
PostPosted: Fri May 09, 2014 2:03 pm 
Offline
User avatar

Joined: Thu Dec 16, 2004 6:42 pm
Posts: 1049
The LINK instruction is used to create a stack frame. Local variables created by a subroutine should be created in a stack frame. Using a stack frame allows the subroutine to be called recursively. The OFFSET directive may be used to temporarily set the assembler's location. The directives serve different purposes so it is not necessary to use them together. The following example does make use of both LINK and OFFSET.
Code:
*-----------------------------------------------------------
* Description   : Example of Stack Frame with Labels
*         Uses LINK & UNLINK to reserve a stack frame for local variables.   
*         The subroutine calculates R = (P^2 + Q^2)/(P^2 - Q^2)
*         D0 contains value of P
*         D1 contains value of Q
*         R is a reference parameter
*-----------------------------------------------------------
START   ORG   $1000
   MOVE.W   D0,-(SP)   Push the value of P on the stack
   MOVE.W   D1,-(SP)   Push the value of Q on the stack
   PEA   R      Push the reference to R on the stack
   BSR   CALC      Call the subroutine
   LEA   (8,SP),SP   Clean up the stack by removing P, Q, R
   
   MOVE.B   #9,D0
   TRAP   #15      ; halt simulator

*----------------------------------------------------
* Subroutine to calculate R = (P^2 + Q^2)/(P^2 - Q^2)
*
* Create references to arguments and local variables in stack frame so labels
* may be used for access

* Calling arguments
   OFFSET   8      ; calling arguments start 8 bytes deep on stack
R_a   ds.l   1
Q_a   ds.w   1
P_a   ds.w   1

* Local variables
SIZE   EQU   (-3*4)-2   ; 3 long words + 1 word for local variables
   OFFSET   SIZE      ; negative for local variables
P2mQ2   DS.W   1      ; P^2 - Q^2
P2pQ2   DS.L   1      ; P^2 + Q^2
Q2   DS.L   1      ; Q2
P2   DS.L   1      ; P2
   ORG   *

CALC   LINK   A0,#SIZE   Reserve 3 long words + 1 word frame
   MOVEM.L   D6/A6,-(SP)   Save working registers on the stack
   MOVE.W   (P_a,A0),D6   Get value of P from stack
   MULU   D6,D6      Calculate P^2
   MOVE.L   D6,(P2,A0)   Save P^2 on the stack frame
   MOVE.W   (Q_a,A0),D6   Now get Q from the stack
   MULU   D6,D6      Calculate Q^2
   MOVE.L   D6,(Q2,A0)   Store Q^2 on the stack frame
   MOVE.L   (P2,A0),D6   Get P^2
   ADD.L   (Q2,A0),D6   Add Q^2
   MOVE.L   D6,(P2pQ2,A0)   Store P^2 + Q^2 on the stack frame
   MOVE.L   (P2,A0),D6   Get P^2
   SUB.L   (Q2,A0),D6   Subtract Q^2
   MOVE.W   D6,(P2mQ2,A0)   Store P^2 - Q^2 on the stack frame (as a word)
   MOVE.L   (P2pQ2,A0),D6   Copy P^2 + Q^2 from stack frame to D6
   DIVU   (P2mQ2,A0),D6   Calculate (P^2 + Q^2) / (P^2 - Q^2) Note 16-bit
   LEA   (R_a,A0),A6   Get pointer to address of R
   MOVEA.L   (A6),A6      Get actual address of R
   MOVE.W   D6,(A6)      Modify R in the calling routine
   MOVEM.L   (SP)+,D6/A6   Restore working registers
   UNLK   A0      Collapse the stack frame
   RTS

*---------------------------------------
* Variable declarations
R   DS.W   1   Result

   END   START

_________________
Prof. Kelly


Top
 Profile  
 
PostPosted: Fri May 09, 2014 2:10 pm 
Offline
User avatar

Joined: Thu Dec 16, 2004 6:42 pm
Posts: 1049
Here is another example that uses OFFSET. This program creates a C style structure.
Code:
*-----------------------------------------------------------
* Title      : Structure Example
* Written by : Charles Kelly
* Date       : May-9th-2014
* Description: Demonstrates a C style structure in 68K Assembly Language
*-----------------------------------------------------------

* Structure Declaration
        OFFSET 0
model   ds.l    1       ; Model number of part
cost    ds.l    1       ; Cost of part
style   ds.b    1       ; Style of part 'A-Z'
P_SIZE  equ     *       ; Size of Part structure
        ORG     *       ; End of Part declaration

    ORG    $1000        ; Location of program code
START:                  ; first instruction of program
    lea     msg1,A1
    move.b  #14,D0
    trap    #15         ; prompt for model number
    move.b  #4,D0
    trap    #15         ; read number into D1
    move.l  D1,part1+model  ; save model number in structure
   
    lea     msg2,A1
    move.b  #14,D0
    trap    #15         ; prompt for cost
    move.b  #4,D0
    trap    #15         ; read number into D1
    move.l  D1,part1+cost  ; save cost in structure
   
    lea     msg3,A1
    move.b  #14,D0
    trap    #15         ; prompt for style
    move.b  #5,D0
    trap    #15         ; read character into D1
    move.b  D1,part1+style  ; save style in structure

    SIMHALT             ; halt simulator

* Put variables and constants here
msg1    dc.b    'Enter model number: ',0
msg2    dc.b    'Enter cost: ',0
msg3    dc.b    'Enter style (A-Z): ',0

        org $2000       ; Location of variables
part1   ds.b    P_SIZE  ; Allocate memory for 1 Part

    END    START        ; last line of source

_________________
Prof. Kelly


Top
 Profile  
 
PostPosted: Fri May 09, 2014 8:21 pm 
Offline

Joined: Wed May 07, 2014 11:16 pm
Posts: 5
Aha! That second example is useful in showing what OFFSET can do; I'm assuming its useful if you wanted to build a data structure that has a variable length (like a list of *structures* for example), or just define some kind of "object". Also recursive methods make sense for the allocation of negative size in LINK. I thought it was just some useless parameter. I'll have to try solving some recursive problems and maybe I'll figure this out.

Thanks for the examples man.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group