TweetFollow Us on Twitter

Assembler Intro
Volume Number:1
Issue Number:1
Column Tag:Mac Assembler

“Introducing the Mac Assembler”

By David E. Smith

“Introducing the Mac Assembler”

Welcome to the Assembly Language Lab. In this column, we will be discussing programming techniques and applications using the Macintosh Assembler, due to be released. With the assembler, Apple has at last provided a complete stand-alone development system for the Macintosh computer.

The Mac assembler was written by Bill Duvall under contract from Apple and is an excellent product. It includes an editor, assembler, linker and debugger in addition to a number of useful utilities for manipulating icons, fonts and resources. All of the Macintosh toolbox and operating system trap calls are fully supported with macros and equate files. This month’s column is based on the August 29, 1984 pre-release version of the assembler/editor system.

EDITOR

The editor is nicely integrated with the whole system and provides a very flexible tool for examining and modifying any text file, including BASIC, MacWrite and C as well as assembly source files. The editor is fully disk-based, fast, and outputs text files that can be read directly by MacWrite. Of course, it does not provide formatting capability for word processing as MacWrite does, but it is very efficient for source code editing, or for examining any text file; in this regard, it is the closest thing we have to a universal editor on the Macintosh.

EDITOR FILES

A number of text files are created by the editor, as shown in Fig. 1. These are:

.asm Assembler source file input

.files List of source file names

.link Linker input instructions

.job Exec type input file

.R Resource maker input file

Each of these file extensions indicates a different type of text file created by the editor for use with the other development system tools. The ‘.files’ type is simply a file containing a list of assembly source code file names. This allows a batch of files to be assembled at one time. The ‘.job’ type is similar to the old EXEC file on the Apple II in that it is a list of assembly and link commands that are executed one after another as if they were selected with the mouse. The ‘.R’ type file is unique to the Macintosh. This is a text file of resource descriptions that are compiled into a binary format that can be inserted into the system resource file or an application resource. The ‘.link’ type provides a list of linker instructions to tell the linker how to create a stand-alone application from the relocatable output of the assembler.

ASSEMBLER

The assembler takes text files with the ‘.asm’ extension and produces relocatable code files with the ‘.rel’ extension. These files are not runable however. To be runable, they must first be linked together with the linker. Other files created by the assembler include the listing file and an error file. One non-standard feature is the directive ‘.dump’ that creates a symbol table file from equate files. The purpose of this is to allow a method of compacting the huge library of system equates that Apple has created as part of the Macintosh development effort on the Lisa. These files take up a considerable amount of room. But with the ‘.dump’ directive, and a utility to pack the symbols file called PackSym, this overhead is greatly reduced.

LINKER

The linker is the heart of the development system. It is what actually puts together an application program for you. Macintosh files are composed of two sections called a “Resource Fork” and a “Data Fork”. The resource part of an application file includes icon and font definitions, window making instructions and the actual binary code of the program itself. The data part of the file is defined by the application program and normally is empty when the program starts up. The linker knows about resource files and how to pack the code into the application resource fork.

A number of important memory considerations involve the linker. Macintosh has a memory manager and segment loader that control where in memory different parts of an application program should be placed. Data structures defined in the source code by use of the “DC” directive are stored on the application heap along with the program code in segments. Variable storage defined by the “DS” directive are relocated by the linker and stored in an applcations globals area which is referenced to register A5, as shown in Fig. 2. The start of this applications globals area is set in the linker input instructions with the GLOBALS command.

The linker input file defines the segments in which the program code will be loaded by the linker. After the program is running, the segment manager can then dispose of unused segments. The manner in which the linker input file is put together determines which code files are associated with which segment. The linker also allows precompiled resource files and predefined data files to be linked to the application code as well. This allows all the files, both resource and data, that are required by a program to be linked together into the resource fork and data fork of the application. In this manner, the user sees only a single file on the disk; a file that in actuality is composed of several files. The system file is an example of a single file composed of many parts.

GETTING STARTED

In this month’s column, we get our feet wet with a program to create a window on the Macintosh. Since our program will call a number of toolbox routines, a few words about macros are in order. The Macintosh assembler uses both a Lisa type of macro and a style unique to the Mac. The Mac style is delimitated by the word MACRO followed by the macro name, and a vertical bar, ‘|’, that signifies the end of the macro. Be careful not to confuse this with a number one.

Our first step is to define the trap macros for the toolbox and operating system routines. This is done by setting the toolbox routine name equal to a trap word constant using the “DC.W” directive. All intructions found in memory that begin with $A are unimplemented instructions that cause the 68000 to “trap” to a special routine to handle the exception. Apple has taken advantage of this arrangement to extend the 68000 instruction set by a whole series of toolbox routines that are called as a result of this “1010” trap.

THE MAC DOES WINDOWS!

; EXAMPLE ASSEMBLY PROGRAM 
; WINDOWS2.5 (MacTech 1-1)
; VERSION 13 OCT 84
; (C) 1984 MacTec by David E. Smith

;    Macro subset for Toolbox stuff

MACRO _InitGraf =  DC.W $A86E|
MACRO _InitWind =DC.W $A912| 
MACRO _NewWindow = DC.W $A913|
MACRO _setport = DC.W $A873|
MACRO _InitFont =DC.W $A8FE|
MACRO _InitMenu =DC.W $A930|
MACRO _InitDialog =DC.W $A97B|
MACRO _TEInit   =DC.W $A9CC|
MACRO _Initpack =         DC.W $A9E5|
MACRO _FlushEvents = DC.W $A032|
MACRO _InitCursor =DC.W $A850|
MACRO _GetNextEvent =DC.W $A970|
MACRO _FrameRect = DC.W $A8A1|

;      DECLARE LABELS EXTERNAL

XDEF  START              ; required for linker 
 
;     LOCAL EQUATES
 
MouseDown equ  1
AllEvents equ  $0000FFFF
 
;     MAIN PROGRAM SEGMENT

 DC.B ‘MINE’;find start of program
 
; -- SAVE THE WORLD ------------
 
START:   MOVEM.L      D0-D7/A0-A6, -(SP)
  LEA SAVEREGS,A0
  MOVE.LA6,(A0)    ; local var
  MOVE.LA7,4(A0) ; stack ptr

Since a window is a grafport, our first task is to initialize quickdraw. To do this, we must supply a memory location where the quickdraw globals can be stored. In Fig. 3 we see how the quickdraw globals are stored relative to A5, between the application parameters, and the application globals.

; --INITIALIZE ALL MANAGERS--------

; SET UP QUICKDRAW GLOBALS
 
PEA -4(A5);push qd global ptr
_InitGraf ;init quickdraw global 

The quickdraw globals point to the current drawing port, as shown in Fig. 4. From A5, the application globals area is found. Then the pointer to the quickdraw globals, followed by the quickdraw globals themselves. Finally, the first entry in the quickdraw globals is a pointer to the current port defined by the current grafport data structure. The quickdraw globals are defined in Fig. 5. The grafport is described in “Inside Macintosh” and is too long to be included here.

Our next task is to define the remaining toolbox managers:

   
;---- SET UP REMAINING MANAGERS  --

_InitFont        ; init font manager
_InitWind   ; init window manager
_InitMenu   ; init menu manager
 
CLR.L -(SP) ; kill the restart 
_InitDialog ; init dialog manager
 
_TEInit ; init text edit (ROM) 
 
MOVE.W  #2,-(SP)   ;  set-up
_Initpack          ; init package mgr

There are two types of windows; those defined on the heap, as we are doing here, and those defined as resources, which we will cover next month. The window definition is defined on the heap by the _NewWindow routine, and is not relocatable. Hence, the heap can not be managed as effectively, but our program is small enough for this not to be a problem. Like all toolbox routines, we must imitate the Lisa Pascal calling sequence by pushing the necessary variables on the stack before actually calling the tooblox routine. The first item pushed is always space for any returned value, in this case, the pointer to the new window.

 
;-- SET UP NEW WINDOW ON HEAP ----
 
CLR.L -(SP)          ;return window ptr
CLR.L -(SP)          ;window record ptr.
PEA WBOUNDS              ;window rectangle 
PEA WINDTITLE            ; window title
MOVE.W #$100,-(SP)    ; true = visible 
MOVE.W #0,-(SP)           ; doc type window
MOVE.L #-1,-(SP)           ; window in front
MOVE.W #$100,-(SP)    ; true=closebox
MOVE.L #0, -(SP)         ; reference value 
_NewWindow                ; make new window
 
; --  ACTIVATE THIS NEW WINDOW ------

LEA WPOINTER,A0         ; copy window ptr 
MOVE.L (SP),(A0)        ; to stacksave
_setport                  ;current window

Now that we have a window on the desktop, we can draw into it. The remaining code sets up the event manager to detect a press of the mouse button. If the button has not been pressed, then we draw something (a box) in our window with a call to the subroutine QDSTUFF. Otherwise, when the button is pushed, we exit back to the finder. Note that the defined box we are drawing is set up as variables with the ‘DS’ assembly command. These values will be stored in the application globals area where they can be modified dynamically by our program if we wanted to animate the box.

; --EVENT LOOP ------------

MOVE.L  #AllEvents,D0     ;all events
_FlushEvents      ;flushed
 
_InitCursor   ; make cursor the arrow
 
GetEvent:

CLR-(SP);returned event 
MOVE  #AllEvents,-(SP)  ;mask all events
PEAEventRecord   ; event record block
_GetNextEvent    ;go check the mouse 
MOVE  (SP)+,D0 ;get event result 
CMP#0,D0;if 0 then no event 
BEQGetEvent ;loop until it happens

 ; JUMP TABLE OF EVENT PROCESSING
 
MOVE  What,D0    ;what to do!
CMP#MouseDown,D0      ; button down?
BEQEXIT ;yes so exit...
BSRQDSTUFF;no so draw box 
JMP    GetEvent  ;get next event

; ---------- END OF MAIN --------------

; ---------- QDSTUFF SUBROUTINE ----------
QDSTUFF:

LEAtop,A0
MOVE.W #10,   (A0) ;set up top
MOVE.W #30,  2(A0) ;left
MOVE.W #100, 4(A0) ;bottom
MOVE.W #200, 6(A0) ;right 

PEA top               ;window rectangle
_FrameRect;draw rectangle
RTS

; -- RESTORE THE WORLD --------

EXIT: LEA SAVEREGS,A0   ; get ‘em back
    MOVE.L   (A0),A6 ; local var
    MOVE.L 4(A0),A7;restore stack 
    MOVEM.L (SP)+,D0-D7/A0-A6 
 
; ---- RETURN TO FINDER --------

        RTS      ; return to finder

; ----LOCAL DATA AREA ----------

SAVEREGS: DCB.L   2,0     ;set  save area
WPOINTER: DC.L     0       ;store window pt
WBOUNDS:DC.W   40   ;rectangle 
 DC.W    2
 DC.W    335
 DC.W    508
WINDTITLE:       DC.B    12        ; title length
               DC.B    ‘DAVES WINDOW’,0      

EventRecord:
 
 What:  DC.W    0; what event
 Message: DC.L    0; ptr. to msg
 When:  DC.L    0
 Point:   DC.L    0
 Modify:  DC.W    0
 
EventTable:

 DC.L GetEvent ;null event
 DC.L Exit     ;mouse down event
 DC.L GetEvent ;mouse up
 DC.L GetEvent ;key down event
 DC.L GetEvent ;key up event
 DC.L GetEvent ;auto key
 DC.L GetEvent   ;update event
 DC.L GetEvent ;Disk Event
 DC.L GetEvent ;activate event
 
; --------------   APPLICATION GLOBALS  ----------
 
top:    DS.W1
left:   DS.W1
bottom: DS.W1
right:  DS.W1

; ------------   END OF PROGRAM ----------------

This completes our program. A typical Macintosh application follows this style of sitting in an event loop and waiting for something to happen. Join us next month for more from the Assembly Lab.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links... | Read more »
Price of Glory unleashes its 1.4 Alpha u...
As much as we all probably dislike Maths as a subject, we do have to hand it to geometry for giving us the good old Hexgrid, home of some of the best strategy games. One such example, Price of Glory, has dropped its 1.4 Alpha update, stocked full... | Read more »
The SLC 2025 kicks off this month to cro...
Ever since the Solo Leveling: Arise Championship 2025 was announced, I have been looking forward to it. The promotional clip they released a month or two back showed crowds going absolutely nuts for the previous competitions, so imagine the... | Read more »
Dive into some early Magicpunk fun as Cr...
Excellent news for fans of steampunk and magic; the Precursor Test for Magicpunk MMORPG Crystal of Atlan opens today. This rather fancy way of saying beta test will remain open until March 5th and is available for PC - boo - and Android devices -... | Read more »
Prepare to get your mind melted as Evang...
If you are a fan of sci-fi shooters and incredibly weird, mind-bending anime series, then you are in for a treat, as Goddess of Victory: Nikke is gearing up for its second collaboration with Evangelion. We were also treated to an upcoming... | Read more »
Square Enix gives with one hand and slap...
We have something of a mixed bag coming over from Square Enix HQ today. Two of their mobile games are revelling in life with new events keeping them alive, whilst another has been thrown onto the ever-growing discard pile Square is building. I... | Read more »
Let the world burn as you have some fest...
It is time to leave the world burning once again as you take a much-needed break from that whole “hero” lark and enjoy some celebrations in Genshin Impact. Version 5.4, Moonlight Amidst Dreams, will see you in Inazuma to attend the Mikawa Flower... | Read more »
Full Moon Over the Abyssal Sea lands on...
Aether Gazer has announced its latest major update, and it is one of the loveliest event names I have ever heard. Full Moon Over the Abyssal Sea is an amazing name, and it comes loaded with two side stories, a new S-grade Modifier, and some fancy... | Read more »
Open your own eatery for all the forest...
Very important question; when you read the title Zoo Restaurant, do you also immediately think of running a restaurant in which you cook Zoo animals as the course? I will just assume yes. Anyway, come June 23rd we will all be able to start up our... | Read more »
Crystal of Atlan opens registration for...
Nuverse was prominently featured in the last month for all the wrong reasons with the USA TikTok debacle, but now it is putting all that behind it and preparing for the Crystal of Atlan beta test. Taking place between February 18th and March 5th,... | Read more »

Price Scanner via MacPrices.net

AT&T is offering a 65% discount on the ne...
AT&T is offering the new iPhone 16e for up to 65% off their monthly finance fee with 36-months of service. No trade-in is required. Discount is applied via monthly bill credits over the 36 month... Read more
Use this code to get a free iPhone 13 at Visi...
For a limited time, use code SWEETDEAL to get a free 128GB iPhone 13 Visible, Verizon’s low-cost wireless cell service, Visible. Deal is valid when you purchase the Visible+ annual plan. Free... Read more
M4 Mac minis on sale for $50-$80 off MSRP at...
B&H Photo has M4 Mac minis in stock and on sale right now for $50 to $80 off Apple’s MSRP, each including free 1-2 day shipping to most US addresses: – M4 Mac mini (16GB/256GB): $549, $50 off... Read more
Buy an iPhone 16 at Boost Mobile and get one...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering one year of free Unlimited service with the purchase of any iPhone 16. Purchase the iPhone at standard MSRP, and then choose... Read more
Get an iPhone 15 for only $299 at Boost Mobil...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering the 128GB iPhone 15 for $299.99 including service with their Unlimited Premium plan (50GB of premium data, $60/month), or $20... Read more
Unreal Mobile is offering $100 off any new iP...
Unreal Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering a $100 discount on any new iPhone with service. This includes new iPhone 16 models as well as iPhone 15, 14, 13, and SE... Read more
Apple drops prices on clearance iPhone 14 mod...
With today’s introduction of the new iPhone 16e, Apple has discontinued the iPhone 14, 14 Pro, and SE. In response, Apple has dropped prices on unlocked, Certified Refurbished, iPhone 14 models to a... Read more
B&H has 16-inch M4 Max MacBook Pros on sa...
B&H Photo is offering a $360-$410 discount on new 16-inch MacBook Pros with M4 Max CPUs right now. B&H offers free 1-2 day shipping to most US addresses: – 16″ M4 Max MacBook Pro (36GB/1TB/... Read more
Amazon is offering a $100 discount on the M4...
Amazon has the M4 Pro Mac mini discounted $100 off MSRP right now. Shipping is free. Their price is the lowest currently available for this popular mini: – Mac mini M4 Pro (24GB/512GB): $1299, $100... Read more
B&H continues to offer $150-$220 discount...
B&H Photo has 14-inch M4 MacBook Pros on sale for $150-$220 off MSRP. B&H offers free 1-2 day shipping to most US addresses: – 14″ M4 MacBook Pro (16GB/512GB): $1449, $150 off MSRP – 14″ M4... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.