TweetFollow Us on Twitter

MDS->MPW
Volume Number:5
Issue Number:6
Column Tag:Developer Notes

MDS->MPW

By Dan Weston, Portland, OR

Converting MDS files to MPW

This article describes how to convert MDS assembler source files to the MPW environment. I don’t go into all the details, but try and give you enough of the major points so that you can get your old files over to MPW and get more productive. MPW is a very rich and wonderful environment and I am purposely ignoring many of its subtle and not-so-subtle features in this article for the sake of clarity (and deadline).

Declare a MAIN Procedure

MPW expects you to declare at least one code module in your source file. There are several ways to start a code module, but the easiest is to put the MAIN directive on the same line at the label at the start of your code producing statements. For example, the following code fragment establishes the label “Start” and the main code module.

Start MAIN; code statements start here

If you don’t declare a code module, either by using MAIN or PROC or FUNC, MPW will complain heartily.

Declare Global Data Record

In MDS, it is customary to declare global variables with the DS directive. The MDS assembler gathers all DS directives together into a common global data block and tells the linker to locate the data block at -256(a5) when the program is loaded. (This location is the default and you can change it with Link directives, although most people don’t bother to change it. Later in this article when we discuss the QuickDraw globals, you will see that the location of the application globals in MDS is important.) All the labels associated with the DS statements become negative offsets from A5 to the proper location in the data area. For example, if you have the following data statements in your MDS file:

thePointds.l1  ;space for a Point
theRect ds.w4  ;4 words for a Rect

You can access those variables by using the name of the variable as an offset from register a5, as in this line of code.

  move.l   thePoint(a5),-(sp) 

In MDS files, DS directives can be placed anywhere in the file, and the assembler will gather them all together and make a single data block. The MPW assembler also gathers up DS statements and makes code modules and the MPW linker combines all the data modules and place them just below the a5 boundary points when the program is loaded. The MPW linker does not give you an automatic 256 byte cushion below the a5 boundary the way the MDS linker does. Labels for DS statements become negative offsets from register a5, however, in much the same way as in MDS. You can access MPW assembler data variables by using the variable name (label) as as offset from register a5, as shown in the previous example.

A better way to declare global variables in MPW is to gather all your DS statements together into a RECORD and then use that data template to access all your globals as fields of the template. For example, to convert the global variables from the MDS example above, you would place the following data declaration at the beginning of your file (after the ‘INCLUDES’ and ‘EQU’ statements but before the first code statements).

MyData RECORD

thePoint ds.l 1

theRect ds.w 4

ENDR

Once you declare a block of globals this way, you can access the individual variables without referencing register a5, since the MPW assembler will automatically generate the ar reference for you. You access a variable by using its data module name ( eg. ‘MyData’) and a field name from the data template (eg. ‘thePoint’), much like a record structure in C or Pascal. For example, you can access the variable at the label ‘thePoint’ with the following code.

    move.l   MyData.thePoint,-(sp)  

You can also declare implicit data modules by using the WITH directive. For example, if you preceded the previous example with the statement:

Start MAIN
 WITH   MyData
 ; Code statements begin here

you could dispense with the data module name when accessing individual fields of the data module, so that the access statement becomes:

    move.l   thePoint,-(sp)  

In summary then; when converting an MDS file to MPW, gather all your DS directive lines into a single data module and give the data module name (the label that appears on the same line as the RECORD directive) in a WITH statement before accessing any of the variables in the data module. You can then access the variables by using their names without worrying about referencing register a5.

Declare QuickDraw Globals

One of the hidden features of the MDS linker is that it places the application globals 256 bytes below the a5 boundary by default. The MPW linker places the application globals right below the a5 boundary, without any cushion. Most casual assembly language programmers (is there such a thing?) just accept the default and don’t really think about it, but it can get you in a lot of trouble when converting between MDS and MPW if you don’t take this difference into account.

In particular, it is common for MDS programs to assume that the QuickDraw globals will be placed just below the a5 boundary, extending downward in memory for 206 bytes. The ROM call InitGraf expects a pointer to the 203rd byte of a 206 byte area for the QuickDraw globals. Because MDS gives a 256 byte cushion below the a5 boundary before the application’s globals, MDS programs typically call InitGraf with a pointer to a long word four bytes below the a5 boundary, as shown below.

; typical MDS technique
 pea    -4(a5)   
 _InitGraf

This works great in MDS programs because the QuickDraw globals grow downward from the a5 boundary but are not large enough to outgrow the 256 byte cushion provided by the MDS linker.

In MPW, you are not so lucky. You must explicitly allocate some global storage for the Quickdraw globals in a data module and then pass a pointer to that module when calling InitGraf. If you don’t do this and use the previous code to initialize QuickDraw, your globals and the QuickDraw globals will share overlapping data space beneath the a5 boundary, and that will cause your program to crash as your program and QuickDraw write over each others globals.

The sample program provided with MPW shows a good method for allocating data space for the QuickDraw globals. Listed below is a simplified version of that method. First, declare a data module for the QD globals. This declaration should come just before or just after your application’s data declaration, but before any code statements, as shown below.

QDGlobals RECORD ,DECREMENT

thePort DS.L 1

white DS.B 8

black DS.B 8

gray DS.B 8

ltGray DS.B 8

dkGray DS.B 8

arrow DS.B cursRec

screenBits DS.B bitmapRec

randSeed DS.L 1

ORG -grafSize

ENDR

Once the QD globals data module is declared, it will be combined with any other data modules in your program and loaded just below the a5 boundary when the program is loaded. Because you don’t know exactly where the QD globals will be loaded in relation to the other data modules (alright, if you dig through the MPW manuals you can probably figure out how the data modules are ordered).

In order to use the QuickDraw globals that are defined as a data module, you should place the name of the data module in the WITH directive along with the name of your application’s data module, as shown below.

Start MAIN
 WITH   MyData,QDGlobals

 ; Code statements begin here

And, once you are using the QD globals you have defined, you can point directly to the QD data area when initializing QuickDraw, as shown below.

; thePort is a variable in QDGlobals
 pea    thePort  
 _InitGraf

By explicitly defining a data area for the QuickDraw globals and pointing to it when initializing QuickDraw you can avoid the data overlap that occurs when you convert an MDS program without thinking about the details.

Misc Details

-- MPW uses IMPORT and EXPORT instead of the XDEF and XREF directives found in MDS.

-- Macros are defined differently in MPW and MDS. For example, take a look at the following macro defined for both MDS and MPW:

MDS macro:

 MACRO   _SFGetFile =
 MOVE.W #2,-(SP)
 _Pack3
 |

MPW equivalent macro:

 MACRO   
 _SFGetFile 
 MOVE.W #2,-(SP)
 _Pack3
 ENDM

-- The labels used with EQU and SET statements in MPW must be in the first column, unlike MDS.

-- RMaker and rez source files are totally incompatable, but you use your old resource files by either “including” them in your rez file or running derez on them to get a rez compatable source file.

-- Make files made by the “Create Build Commands...” menu option in MPW typically are a good way to build your programs, but you can remove the link files “Runtime.o” and “Interface.o” put in the make file by default.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.